Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux cut, sort, uniq, tr, paste and diff Commands Explained with Examples

Raw text is messy. You need to pick one column, sort it, remove duplicates, count them, change characters, put files side by side and see what changed. cut, sort, uniq, tr, paste and diff do these jobs. Together they answer questions like 'top 10 IPs hitting my server' in one line.

In this chapter

  • Pick columns and characters with cut
  • Sort text, numbers, sizes and by column
  • Remove and count duplicates with uniq
  • Change or delete characters with tr
  • Join files side by side with paste, join and comm
  • Compare files with diff and make patches

1. An everyday example: a class result sheet

Mr. Kulkarni has a result list: roll number, name, city and marks. He wants only the names (pick a column), toppers first (sort by marks), how many students from each city (count duplicates), all names in capitals for the certificate (change characters), the old list next to the new list, and what changed after re-checking (compare).

Pick a column = cut. Toppers first = sort -k4 -nr. Count per city = sort | uniq -c. Capitals = tr a-z A-Z. Side by side = paste. What changed = diff.

The classic count pipelineRaw log linescut picks the field you needsort groups equal values togetheruniq -c counts each groupsort -nr puts the biggest firsthead shows the top N
uniq counts neighbouring identical lines. Sort first when you want to count all copies regardless of their original positions.

2. cut: pick columns or characters

CSV files, /etc/passwd and many logs have fields split by a character. Often you need just one field.

cut -d',' -f2 picks field 2 split by commas. -f1,3 picks fields 1 and 3, -f2- from 2 to the end. -c1-5 picks characters 1 to 5. --complement gives everything except the chosen fields. The default delimiter is TAB. cut cannot treat many spaces as one; for space-aligned output use awk or tr -s ' ' first.

  1. Usernames from /etc/passwd.
cut -d: -f1 /etc/passwd | head -3

Output:

root
bin
daemon

Fields are split by colons.

  1. User and shell.
cut -d: -f1,7 /etc/passwd | tail -2

Output:

ravi:/bin/bash
nginx:/sbin/nologin

Output keeps the same delimiter.

  1. City column from a CSV.
cut -d, -f2 sales.csv | tail -n +2 | head -3

Output:

Pune
Nashik
Pune

tail -n +2 drops the header.

  1. First 10 characters (the date) of each log line.
cut -c1-10 app.log | head -2

Output:

2026-10-25
2026-10-25

-c works by character position.

  1. Everything except the password column.
cut -d, -f3 --complement users.csv | head -2

Output:

id,name,city
1,Aditi,Pune

--complement inverts the pick (GNU cut).

  1. Space-aligned output: squeeze first.
ps aux | tr -s ' ' | cut -d' ' -f2,11 | head -2

Output:

PID COMMAND
1 /sbin/init

tr -s turns many spaces into one so cut can split.

3. sort: put lines in order

Sorted data is easy to read, and tools like uniq, comm and join need sorted input.

sort orders lines as text by default. -n numeric, -r reverse, -h human sizes (2K, 5M, 1G), -k2 by field 2, -t, sets the field separator, -u unique, -f ignore case, -V version order (v1.2 < v1.10), -o file writes output (safe even onto the same file), -c checks if sorted. -k2,2 means only field 2; -k2 means from field 2 to the end of the line.

OptionUse
-nNumbers: 9 before 10
-hSizes like 512K 2G
-rReverse
-t, -k3,3Sort by field 3 of a CSV
-uRemove duplicates
-VVersions
-o fWrite to file f
  1. Text sort vs number sort.
printf '10\n9\n100\n' | sort; printf '10\n9\n100\n' | sort -n

Output:

10
100
9
9
10
100

Without -n, 100 comes before 9 because '1' < '9'.

  1. Students by marks, highest first.
sort -t, -k4,4nr marks.csv | head -3

Output:

7,Sneha,Pune,98
3,Imran,Mumbai,95
9,Anna,Goa,91

Field 4, numeric, reverse.

  1. Biggest folders.
du -sh /var/log/* | sort -rh | head -3

Output:

1.2G /var/log/journal
300M /var/log/nginx
12M /var/log/dnf.log

-h understands K, M and G.

  1. Version order.
printf 'v1.10\nv1.2\nv1.9\n' | sort -V

Output:

v1.2
v1.9
v1.10

-V sorts like humans read versions.

  1. Unique sorted cities.
cut -d, -f3 marks.csv | tail -n +2 | sort -u

Output:

Goa
Mumbai
Pune

Same as sort | uniq.

  1. Sort a file in place safely.
sort -o names.txt names.txt

Output:

(no output)

sort names.txt > names.txt would empty the file; -o is safe.

4. uniq: remove and count duplicates

uniq looks for repeated neighboring lines. Imagine a file with rows tea, coffee, and tea. Running uniq -c alone counts three separate runs. Sorting first moves matching lines next to each other so the resulting counts show one coffee and two tea lines. Try predicting the counts before running both variants.

The same rule applies even when cut supplies a column. Choose the field, sort the extracted values, then count adjacent duplicates. Expect different values to stay separate if they differ in capitalization or whitespace under the chosen comparison rules. If an expected group split into two, print those inputs in a way that makes the actual characters visible before changing the command.

Who logged in most? Which error repeats? Counting repeats is the heart of log analysis.

uniq joins neighbouring equal lines. So you almost always sort first. -c adds a count, -d shows only repeated lines, -u only lines that appear once, -i ignores case, -f1 skips the first field. The famous pipeline is sort | uniq -c | sort -nr | head.

  1. Why sort first.
printf 'a\nb\na\n' | uniq; printf 'a\nb\na\n' | sort | uniq

Output:

a
b
a
a
b

Without sort, the two a's are not neighbours.

  1. Count students per city.
cut -d, -f3 marks.csv | tail -n +2 | sort | uniq -c

Output:

      2 Goa
      4 Mumbai
      6 Pune

The number is the count.

  1. Top 3 IPs in an access log.
cut -d' ' -f1 access.log | sort | uniq -c | sort -nr | head -3

Output:

    812 10.0.0.7
    430 10.0.0.2
     95 49.36.12.4

10.0.0.7 sent 812 requests.

  1. Only duplicate emails.
sort emails.txt | uniq -d

Output:

aditi@example.com

These appear more than once.

  1. Only unique lines.
sort emails.txt | uniq -u | wc -l

Output:

148

148 emails appear only once.

  1. Count error types.
grep -o 'ERROR [A-Za-z]*' app.log | sort | uniq -c | sort -nr

Output:

     41 ERROR timeout
      9 ERROR auth

grep -o prints only the match (next chapter).

5. tr: translate, squeeze and delete characters

You need uppercase names, to remove Windows line ends, turn commas into new lines or squeeze many spaces.

tr reads only stdin (no file name argument) and works on single characters. tr 'a-z' 'A-Z' changes case, tr -d '\r' deletes characters, tr -s ' ' squeezes repeats into one, tr ',' '\n' changes commas to new lines, tr -cd '[:digit:]' keeps only digits (-c = complement). Classes: [:upper:] [:lower:] [:digit:] [:space:] [:alnum:] [:punct:].

  1. Uppercase.
echo 'pune city' | tr 'a-z' 'A-Z'

Output:

PUNE CITY

Each letter maps to its capital.

  1. Remove Windows line ends.
tr -d '\r' < win.sh > unix.sh && cat -A unix.sh | head -1

Output:

#!/bin/bash$

No more ^M. Same job as dos2unix.

  1. Squeeze spaces.
echo 'a    b   c' | tr -s ' '

Output:

a b c

Many spaces become one.

  1. Split a comma list into lines.
echo 'web1,web2,db1' | tr ',' '\n'

Output:

web1
web2
db1

Now you can sort or loop over it.

  1. Keep only digits from a phone number.
echo '+91 (98220) 12-345' | tr -cd '[:digit:]'

Output:

919822012345

-c means 'not these', -d deletes them.

  1. Show PATH one per line.
echo "$PATH" | tr ':' '\n'

Output:

/usr/local/bin
/usr/bin
/bin

Easy to read and check.

6. paste: join files side by side

Names in one file and emails in another must become one list.

paste joins line 1 with line 1, line 2 with line 2, using a TAB. -d, uses a comma. -s joins all lines of one file into one line. paste -sd+ builds a sum for bc.

  1. Names and emails side by side.
paste -d, names.txt emails.txt

Output:

Aditi,aditi@ex.com
John,john@ex.com

Line 1 with line 1, line 2 with line 2.

  1. All lines into one comma list.
paste -sd, servers.txt

Output:

web1,web2,db1

Handy for building arguments.

  1. Add numbers with paste and bc.
paste -sd+ amounts.txt | bc

Output:

12750

Makes 4500+3200+5050 and bc adds it.

7. join: merge two files on a key

ids with names in one file and ids with cities in another need one combined table, like a database join.

join FILE1 FILE2 joins on field 1 by default. -t, sets the separator, -1 2 -2 3 picks key fields, -a1 keeps unmatched lines from file 1 (left join). Both files must be sorted on the key.

  1. Join two files on id.
join -t, <(sort ids_names.csv) <(sort ids_city.csv)

Output:

1,Aditi,Pune
2,John,London

Both files must be sorted on the key.

  1. Left join: keep unmatched.
join -t, -a1 <(sort ids_names.csv) <(sort ids_city.csv)

Output:

1,Aditi,Pune
2,John,London
3,Mei

Mei has no city but is kept.

8. comm: compare two sorted lists

Old server list vs new list: what was added, removed, or kept?

comm prints three columns: only in file 1, only in file 2, in both. -1 -2 -3 hide columns: -12 common, -23 only in file 1, -13 only in file 2. Inputs must be sorted.

  1. Servers in both lists.
comm -12 <(sort old.txt) <(sort new.txt)

Output:

web1
web2

Common to both.

  1. Servers removed.
comm -23 <(sort old.txt) <(sort new.txt)

Output:

db-old

Only in the old list.

  1. Servers added.
comm -13 <(sort old.txt) <(sort new.txt)

Output:

web3

Only in the new list.

9. diff: see exactly what changed

A config change broke the site. You must find which line changed between the backup and the live file.

diff a b shows the lines to change to turn a into b. -u gives unified format (the one Git uses): lines with - are removed, + are added, @@ shows line numbers. -y side by side, -q only says if they differ, -r compares folders, -i ignores case, -w ignores spaces. Exit code: 0 same, 1 different, 2 trouble. patch applies a diff -u file. cmp compares binary files byte by byte; vimdiff shows colours.

  1. Unified diff of two configs.
diff -u nginx.conf.bak nginx.conf

Output:

--- nginx.conf.bak
+++ nginx.conf
@@ -12,3 +12,3 @@
     listen 80;
-    root /var/www/shop;
+    root /var/www/shopp;
     index index.html;

Someone typed shopp. That broke the site.

  1. Just ask: are they different?
diff -q a.txt b.txt; echo $?

Output:

Files a.txt and b.txt differ
1

Exit code 1 means different. Good in scripts.

  1. Side by side.
diff -y --width=50 old.txt new.txt

Output:

web1          web1
web2        | web3
db1           db1

| marks a changed line.

  1. Compare two folders.
diff -rq /etc/nginx /backup/nginx

Output:

Files /etc/nginx/nginx.conf and /backup/nginx/nginx.conf differ
Only in /etc/nginx/conf.d: new.conf

Shows changed and extra files.

  1. Make and apply a patch.
diff -u app.conf.orig app.conf > fix.patch && patch app.conf.orig < fix.patch

Output:

patching file app.conf.orig

Now the orig file has the same change.

  1. Compare output of two servers.
diff <(ssh web1 rpm -qa | sort) <(ssh web2 rpm -qa | sort)

Output:

< openssl-3.0.7-1
> openssl-3.0.8-1

Process substitution <( ) lets diff compare commands.

10. cmp: compare files byte by byte

For binaries, images or archives, line diffs make no sense. You just need to know if they are identical and where they differ.

cmp a b prints the first differing byte and line. -s is silent (exit code only: 0 same, 1 different). -l lists every differing byte. For checking downloads, sha256sum is often better.

  1. Compare two binaries.
cmp app-v1.bin app-v2.bin

Output:

app-v1.bin app-v2.bin differ: byte 1025, line 3

First difference at byte 1025.

  1. Silent check in a script.
cmp -s backup.tar live.tar && echo identical || echo changed

Output:

identical

Uses only the exit code.

11. Real examples from many industries

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

Each row is a made-up team. It shows who uses this and how.

Team and cityHow they use itCommand
Rohit, security, HyderabadFinds top attacking IPs from access logscut -d' ' -f1 access.log | sort | uniq -c | sort -nr | head
Emma, retail BI, ParisTotals daily sales from a CSV columncut -d, -f4 sales.csv | tail -n +2 | paste -sd+ | bc
Kenji, infra, TokyoCompares package lists between two serversdiff <(rpm -qa|sort) <(ssh b rpm -qa|sort)
Pooja, HR tech, ChennaiFinds duplicate employee emailssort emails.txt | uniq -d
Diego, logistics, MadridCleans Windows CSV exports before importtr -d '\r' < export.csv > clean.csv
Zainab, cloud FinOps, LagosFinds instances in billing but not in inventorycomm -23 billing.txt inventory.txt

12. Common errors and how to fix them

Everyone hits these errors. Knowing the cause saves hours.

Read the exact message first. Then match it to a row below.

Error you seeWhy it happensHow to fix
uniq shows duplicates still thereInput was not sortedSort before uniq.
sort file | uniq -c
sort puts 100 before 9Text sort, not numericAdd -n (or -h for sizes).
sort -n nums.txt
File became empty after sort file > file> empties the file before sort reads itUse sort -o.
sort -o file file
cut: the delimiter must be a single characterYou gave -d with two charactersUse one character or use awk -F.
awk -F'::' '{print $2}' f
cut prints the whole lineThe delimiter is not in that line (default is TAB)Set -d correctly; use -s to skip such lines.
cut -d, -f2 -s data.csv
tr: extra operand 'file.txt'tr does not take file namesRedirect the file into tr.
tr a-z A-Z < file.txt
join: file 1 is not in sorted orderjoin needs both inputs sorted on the keySort both on the same field.
join <(sort a) <(sort b)
comm shows wrong resultsInputs not sorted, or sorted with different localeSort both with the same LC_ALL.
LC_ALL=C sort a > a.s
diff shows every line changedOne file has Windows line ends (\r)Use --strip-trailing-cr or tr -d '\r'.
diff --strip-trailing-cr a b
sort -k2 sorts wrongly-k2 means field 2 to end of line; separator not setUse -t and -k2,2.
sort -t, -k2,2 f.csv

13. Troubleshooting step by step

When a text pipeline gives a wrong answer, run it one step at a time and look at each output.

Go top to bottom. Stop at the first step that shows the problem.

  1. Look at the raw input and its hidden characters.
head -3 data.csv | cat -A
  1. Check the delimiter and field you cut.
head -3 data.csv | cut -d, -f2
  1. Check sorting is numeric where needed.
cut -d, -f4 data.csv | sort -n | tail -3
  1. Check uniq got sorted input.
cut -d, -f3 data.csv | sort | uniq -c
  1. Fix locale surprises.
LC_ALL=C sort file | head
  1. Check that diff is not just line ends.
diff --strip-trailing-cr -q a b

14. Lab 1: Top visitors report from a web log

Build the classic count pipeline step by step.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Make a sample log.
mkdir -p ~/lab8 && cd ~/lab8 && for i in $(seq 1 300); do echo "10.0.0.$((i%4)) - GET /p$((i%5)) $((i%7==0?500:200))"; done > access.log; head -2 access.log

Output:

10.0.0.1 - GET /p1 200
10.0.0.2 - GET /p2 200
  1. Pick the IP field.
cut -d' ' -f1 access.log | head -2

Output:

10.0.0.1
10.0.0.2
  1. Count each IP.
cut -d' ' -f1 access.log | sort | uniq -c

Output:

     75 10.0.0.0
     75 10.0.0.1
     75 10.0.0.2
     75 10.0.0.3
  1. Top pages with 500 errors.
grep ' 500$' access.log | cut -d' ' -f4 | sort | uniq -c | sort -nr | head -3

Output:

     9 /p0
     9 /p1
     8 /p4
  1. Save the report.
cut -d' ' -f1 access.log | sort | uniq -c | sort -nr > top-ips.txt && wc -l top-ips.txt

Output:

4 top-ips.txt

15. Lab 2: Find a broken config change with diff

Make a backup, change a line, find it and undo it with a patch.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Create a config and a backup.
cd ~/lab8 && printf 'port=8080\nroot=/srv/shop\nmode=prod\n' > app.conf && cp app.conf app.conf.bak

Output:

(no output)
  1. Make a mistake.
sed -i 's#/srv/shop#/srv/shopp#' app.conf

Output:

(no output)
  1. Find it.
diff -u app.conf.bak app.conf

Output:

-root=/srv/shop
+root=/srv/shopp
  1. Make a reverse patch and apply it.
diff -u app.conf app.conf.bak > undo.patch; patch app.conf < undo.patch

Output:

patching file app.conf
  1. Confirm they match.
diff -q app.conf app.conf.bak && echo same

Output:

same

Ravindra Bagale's Tip

Remember the golden pipeline: cut | sort | uniq -c | sort -nr | head. It answers 'what is the most common X?' for IPs, URLs, users, errors and status codes in seconds.

16. Frequently asked questions

Q. Why must I sort before uniq?

uniq only compares a line with the line just before it. Sort brings equal lines together.

Q. cut or awk?

cut is simple and fast for one fixed delimiter. awk handles many spaces, conditions and maths.

Q. What does -k2,2 mean in sort?

Start and end the key at field 2. Without ,2 the key runs to the end of the line.

Q. Why does sort order differ on two servers?

Locale. en_US.UTF-8 and C sort differently. Use LC_ALL=C for byte order.

Q. Can tr replace words?

No, only single characters. Use sed for words.

Q. What is the exit code of diff?

0 same, 1 different, 2 error. Scripts use it to decide.

Q. comm vs diff?

comm needs sorted input and shows set style columns (only A, only B, both). diff shows edits in order.

Q. How do I count unique values?

sort -u file | wc -l.

17. Interview questions and answers

Beginner level

Q. How do you print the first field of /etc/passwd?

cut -d: -f1 /etc/passwd.

Q. How do you sort numbers correctly?

sort -n (add -r for highest first).

Q. How do you change lowercase to uppercase?

tr 'a-z' 'A-Z' or tr '[:lower:]' '[:upper:]'.

Q. How do you see differences between two files?

diff -u old new.

Mid level level

Q. Find the top 5 IPs in an access log.

cut -d' ' -f1 access.log | sort | uniq -c | sort -nr | head -5 (or awk '{print $1}').

Q. Sort a CSV by the 3rd column numerically.

sort -t, -k3,3n file.csv.

Q. Find lines present in file A but not B.

comm -23 <(sort A) <(sort B), or grep -Fxv -f B A.

Q. Remove Windows CRLF from a script.

tr -d '\r' < in > out, sed -i 's/\r$//' or dos2unix.

Senior level

Q. Sorting a 50 GB file fails with No space left. Fix?

sort uses temp files in /tmp. Use -T /bigdisk, --buffer-size, --parallel, and LC_ALL=C for speed; or split and sort -m to merge.

Q. Why can uniq -c counts be wrong after sort on a server with a UTF-8 locale?

Locale collation can treat some characters as equal or order them oddly. Use LC_ALL=C on both sort and uniq for exact byte comparison.

Q. How would you detect config drift across 50 servers?

Collect a checksum or the file from each (ssh/ansible), then diff against a golden copy or compare md5sum lists with sort | uniq -c.

Q. A diff in CI shows every line changed but the content looks the same. Cause?

Line endings (CRLF vs LF), trailing spaces or encoding/BOM. Check with cat -A or file; use --strip-trailing-cr, -w, or a .gitattributes rule.

Try at home

  1. Write 10 friends with their city in a file like 'name,city'. Find how many friends live in each city with cut, sort and uniq -c.
  2. Copy a config file, change one line, and use diff -u to see the change.

Practise this chapter

Interview questions and answers

When is sort followed by uniq useful?

I would use that pipeline to count repeated values pulled from a chosen column. Sorting groups equal inputs so uniq can count consecutive runs as one group. Before interpreting surprising totals I would check the actual values and comparison settings, especially whitespace and case.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. For tea, coffee, tea, what does uniq -c alone count?

Answer and explanation

A. Two separate runs for tea and one for coffee

The two tea lines are not adjacent, so uniq handles them as separate runs.

2. What does sorting first give uniq?

Answer and explanation

B. Adjacent copies of equal values under the chosen comparison rules

Grouping repeated values makes adjacent counting appropriate for that dataset.

3. Two values look alike but get different counts. What should you inspect?

Answer and explanation

B. Actual characters, whitespace and comparison settings

Differences such as a stray space or case can create distinct values.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Vidarbha Cotton Mills is a textiles business in Nagpur. Ganesh Wankhede looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy Usernames from passwd

Audits need a user list.

Print only the seventh field (the login shell) of /etc/passwd (fields split by colon).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Sort numbers

Plain sort puts 100 before 9.

Sort amounts.txt numerically.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Sort and remove duplicates

Clean an email list.

Sort emails.txt and keep each line only once, in one command.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Uppercase text

Codes must be uppercase.

Convert codes.txt to uppercase and print it, reading the file with input redirection.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Squeeze spaces

Many spaces break cut.

Squeeze repeated spaces in report.txt into one, reading with <.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Unified diff

Review config changes.

Show the differences between nginx.conf.orig and nginx.conf in unified format.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Two CSV columns

Send only names and cities.

Print columns 2 and 3 of customers.csv (comma separated).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Sort CSV by a column

Top scorers first.

Sort scores.csv (comma separated) by column 3, numbers, highest first.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Sort human sizes

Find the biggest folders.

Sort the output of du -sh /var/* with biggest first using human-number sort.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Count repeats

Which IP hits most?

Count how many times each line appears in ips.txt (sort first, then uniq).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Only duplicates

Find double registrations.

Show only lines that appear more than once in sorted.txt (already sorted).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Remove Windows CR

^M breaks bash scripts.

Delete all \r characters from setup-crlf.sh and save the result as setup-lf.sh.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Join files side by side

Names and emails in two files.

Join ids.txt and phones.txt side by side with a comma.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Compare two folders

Did the backup match?

Briefly list which files differ between folders /etc/haproxy and /backup/haproxy, recursively.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Common lines of two lists

Servers in both old and new.

old.txt and new.txt are sorted. Print only lines in both.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Silent binary compare

Scripts need only yes or no.

Compare a.bin and b.bin byte by byte silently (exit code only).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. cut -d -f picks fields; -c picks characters.
  2. sort -n -h -r -k -t -u -V -o.
  3. Sort before uniq -c to count identical lines across the whole input.
  4. tr changes, squeezes (-s) or deletes (-d) characters from stdin.
  5. paste joins side by side; join on a key; comm compares sorted sets.
  6. diff -u shows changes; exit 0 same, 1 different.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Text toolbox. cut splits on a delimiter. Use a CSV parser when a quoted field may contain that delimiter. uniq -c counts neighbouring equal lines. Sort first only to count matching lines across the full input. Log IP counts: extract the IP field from your log format, sort it, then run uniq -c. Sort the counts numerically to find the highest totals.
Revision card: Text toolbox · Download PNG · Open card page
Compare files. Know exactly what changed. diff lines, comm lists, cmp bytes. -u shows a readable patch. Review a config change.
Revision card: Compare files · Download PNG · Open card page
sort vs uniq. Orders lines. / Removes neighbour repeats.
Comparison: sort vs uniq · Download PNG · Open card page
diff vs comm. Any two files. / Two sorted lists.
Comparison: diff vs comm · Download PNG · Open card page
Why uniq misses duplicates?. uniq only checks neighbours. Sort first, then uniq. Count repeats.
Interview card: Why uniq misses duplicates? · Download PNG · Open card page
Why 100 sorts before 9?. Plain sort compares text. -n numbers, -h sizes. Biggest folders.
Interview card: Why 100 sorts before 9? · Download PNG · Open card page
Script fails with ^M?. Windows line ends (CRLF). Delete \r with tr. Fix the script.
Interview card: Script fails with ^M? · Download PNG · Open card page
cut breaks on many spaces?. cut counts each space. Squeeze first, or use awk. Column 2 of ps.
Interview card: cut breaks on many spaces? · Download PNG · Open card page

More cards: Ravi Sir's Cards.