0% found this document useful (0 votes)
107 views

100 Useful Unix Commands

Uploaded by

Vedant Kakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

100 Useful Unix Commands

Uploaded by

Vedant Kakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

150 unix commands

Ming Tommy Tang

2023-06-23

150 useful unix commands

1. awk '{for(i=NF;i>=1;i--) printf("%s ", $i); print ""}' file: Reverse the order of fields in
each line.

2. awk '!a[$1,$2]++' file: print out unique rows based on the first and second column
3. awk '{print $0}': Print each line as is.
4. awk '{print $NF}': Print the last field of each line.
5. awk '{print NF}': Print the number of fields in each line.

6. awk '{print NR,$0}' file: Number lines in a file with their line numbers.
7. awk '{sum+=$1}END{print sum}' file: Calculate the sum of numbers in a file using AWK.
8. awk '{sum+=$NF}END{print sum}' file: Calculate the sum of values in the last field of a file using
AWK.

9. cal -y: Display a yearly calendar starting Sunday as the first day of the week.
10. cat -A: Display non-printing characters with visible representations.
11. cat -E: Display $ at the end of each line.

12. cat -T: Display tabs as “ˆI” in output.


13. chmod -R +x directory: Add execute permission to all files and directories recursively.
14. chmod -R a-x directory: Remove execute permission for all users from files and directories recur-
sively.

15. chmod -R u=rwX,g=rX,o=rX directory: Set permissions recursively to allow read and write access
for the owner and read access for group and others.
16. chmod a+x script.sh: Add execute permission to a script for all users.
17. chmod u+x script.sh: Add execute permission for the owner to a script.

18. comm -12 file1 file2: Display lines common to both files. Note, file1 and file2 need to be sorted
first.
19. comm -13 file1 file2: Display lines unique to file1 and file2.
20. comm -23 file1 file2: Display lines unique to file1.

21. comm -3 file1 file2: Display lines unique to file1 and file2, excluding common lines.

1
22. cut -c N-M: Extract characters from the Nth to the Mth position of each line.
23. cut -c N-: Extract characters from the Nth position to the end of each line.
24. cut -d ',' -f N-M file: Extract fields N to M from each line (using ‘,’ as the delimiter).
25. cut -d ':' -f N file1 file2: Extract the Nth field from multiple files (using ‘:’ as the delimiter).

26. cut -d' ' -f N- file: Extract fields N and beyond from each line (using space delimiter).
27. df -T -h: Display disk space usage along with filesystem type information in a human-readable format.
28. df -T: Display disk space usage along with filesystem type information.

29. df -h: Display disk space usage in a human-readable format.


30. df -i: Display the number of inodes used and available.
31. diff -r dir1 dir2 | grep "Only in" | cut -d':' -f2-: List files that exist only in dir1 or dir2
without the “Only in” prefix.

32. diff -rq dir1 dir2: Recursively compare directories.


33. diff -u --color=always file1 file2 | less -R: Show differences between files in unified context
format with colored output, preserving colors in the less pager.
34. diff -y file1 file2: Show differences between files side by side.

35. diff -u <(sort file1) <(sort file2): Compare sorted files line by line.
36. diff -u <(sort file1) <(sort file2) | grep -v "ˆ[-+] ": Compare sorted files line by line,
excluding lines with only “+” or “-” symbols.
37. diff -uN file1 file2 > diff.patch: Save differences between files in unified context format to a
patch file.
38. diff -y --suppress-common-lines file1 file2 > diff.txt: Save differences between files in
side-by-side format to a file.
39. diff -y --suppress-common-lines file1 file2 | grep "[<>\\-]": Show differences between
files in side-by-side format, displaying only change lines.

40. diff -y --suppress-common-lines file1 file2 | grep -v "|": Show differences between files in
side-by-side format, excluding common lines.
41. du -ch | sort -h: Display the sizes of all files and directories in a human-readable format, sorted by
size.

42. du -ch: Display the total size of a directory along with a summary.
43. du -sh * | sort -h: Display the sizes of all directories and files in the current directory, sorted by
size.
44. du -sh * | sort -h: Display the sizes of all files and directories in a human-readable format, sorted
by size.
45. du -sh: Display the total size of a directory.
46. find . -name "*.txt" -exec mv {} destination/ \\;: Move files with a .txt extension to a des-
tination folder.

47. find . -name "*.txt": Find all files with a .txt extension.

2
48. find . -type f -empty: Find empty files.
49. find . -type d -empty -delete: Delete empty directories recursively.
50. find . -type f -exec chmod 644 {} +: Set file permissions to 644 recursively.
51. find . -type f -exec grep -H -c "pattern" {} \\; | grep -v :0$ | cut -d':' -f1- |
xargs -d'\\n' tar -cvf archive.tar: Find files containing a pattern with nonzero occurrences,
then create a tar archive of those files.
52. find . -type f -exec grep -H -c "pattern" {} \\; | grep -v :0$: Find files containing a
pattern with nonzero occurrences.

53. find . -type f -exec grep -l "pattern" {} + | xargs sed -i 's/foo/bar/g': Find files con-
taining a pattern, then replace “foo” with “bar” in those files.
54. find . -type f -name "*.txt" -delete: Delete all .txt files recursively.
55. find . -type f -name "*.txt" -exec grep -H "pattern" {} +: Search for a pattern in all .txt
files, displaying file names.

56. find . -type f -name "*.txt" -exec grep -l "pattern" {} + | xargs sed -i 's/foo/bar/g':
Find files containing a pattern, then replace “foo” with “bar” in those files.
57. find . -type f -name "*.txt" -print0 | xargs -0 grep "pattern": Search for a pattern in
.txt files with null-separated output.

58. grep -A N "pattern" file: Print N lines after matching a pattern.


59. grep -B N -A M "pattern" file: Print N lines before and M lines after matching a pattern.
60. grep -C N "pattern" file: Display N lines of context around a pattern match.

61. grep -R "pattern" directory --include="*.txt": Search for a pattern recursively in a directory,
limiting to .txt files.
62. grep -Rl "pattern" directory: Find files containing a pattern recursively in a directory and display
their names only.
63. grep -o: Only print the matched part of the line.

64. grep -r "pattern" directory --include="*.txt" --exclude-dir="subdir": Search for a pat-


tern recursively in a directory, limiting to .txt files and excluding a subdirectory.
65. grep -r "pattern" directory: Search for a pattern recursively in a directory.
66. grep -r -i "pattern" directory: Search for a case-insensitive pattern recursively in a directory.

67. grep -rL "pattern" directory: Find files not containing a pattern recursively in a directory.
68. grep -v: Invert the match (exclude matching lines).
69. grep -w "word" file: Match whole words only.

70. grep -w -f patterns.txt file: Match whole words from a file of patterns in another file.
71. head -c N file: Display the first N bytes from a file.
72. head -c N: Display the first N bytes of a file.
73. head -n N: Display the first N lines of a file.

74. head -q -n N file1 file2: Display the first N lines from multiple files quietly.

3
75. join -t',' -1 2 -2 1 file1 file2: Join two files based on the second field of the first file and the
first field of the second file (using ‘,’ as the delimiter).
76. join -t',' -j2 -o 1.1,2.2 file1 file2: Join two files based on the second field and display spe-
cific fields from both files.
77. join -t',' -j2 file1 file2: Join two files based on the second field.
78. join -t',' file1 file2: Join two files based on a common field (using ‘,’ as the delimiter). The
files need to be sorted first.
79. less +G: Open a file at the end.
80. less -J: Display file contents with status columns separated by “|” characters.
81. less -i: Ignore case when searching.
82. ls -R: Recursively list all files and directories.
83. ls -l --sort=extension: List files sorted by extension.
84. ls -l --sort=size: List files in long format sorted by size.
85. ls -l --sort=time: List files in long format sorted by modification time.
86. ls -ltr: List files sorted by modification time in reverse order.
87. ls -m: Display the names as a comma-separated list.
88. man -a keyword: Display all manual pages related to a keyword.
89. man -f keyword: Display the short description of a keyword from the manual pages.
90. man -k keyword: Search manual page descriptions for a keyword.
91. man -l filename: Display a specific file as a formatted man page.
92. nl -nln -w3 -s'. ' file: Number lines in a file, three digits wide, without leading zeros, with a
dot-space separator.
93. nl -nrz -w3 file: Number lines in a file, right-aligned, zero-filled, and three digits wide.
94. nl -w3 -s'. ' file: Number lines in a file, three digits wide, with a dot-space separator.
95. nl file: Number lines in a file.
96. paste -d' ' - -: Merge every two lines of a file.
97. paste -d',' file1 file2: Merge two files with a comma delimiter.
98. paste -d'|' file1 file2: Merge two files with a pipe delimiter.
99. paste -s -d',' file: Merge all lines of a file into a single comma-separated line.
100. paste -s: Merge consecutive lines of files into a single line.
101. sed '/ˆ$/d' file: Delete empty lines from a file.
102. sed '/pattern/!d' file: Delete lines not containing a pattern.
103. sed '/pattern/d' file: Delete lines containing a pattern.
104. sed 's/foo/bar/2': Replace the second occurrence of “foo” with “bar” on each line.
105. sed 's/foo/bar/g': Replace all occurrences of “foo” with “bar”.

4
106. sed -i '10,20d' file: Delete lines 10 to 20 from a file in-place.
107. sed -i '10,20s/foo/bar/g' file: Replace “foo” with “bar” in lines 10 to 20 of a file in-place.
108. sed -i '10d' file: Delete the 10th line from a file in-place.
109. sed -i 's/foo/bar/2g' file: Replace all occurrences of “foo” with “bar” on each line, starting from
the second occurrence.
110. sed -i 's/foo/bar/g' file: Replace “foo” with “bar” in-place in a file.
111. sed -n '/pattern/!p' file: Print lines not containing a pattern.
112. sed -n '/pattern/{n;p;}' file: Print the line after a pattern match.
113. sed -n '10,20p' file: Print lines 10 to 20 from a file.
114. sed -n '10~2p' file: Print every second line starting from line 10.
115. sort -R file1 file2: Randomly sort lines in multiple files.
116. sort -R file: Randomly sort lines in a file.
117. sort -m file1 file2: Merge and sort multiple files.
118. sort -n file: Sort a file numerically.
119. sort -t' ' -k1,1 -k2n file: Sort a file based on the first field (using space delimiter) and then
numerically on the second field.
120. sort -t',' -k1,1 -k2,2 file: Sort a file based on multiple fields (using ‘,’ as the delimiter) in
lexicographical order.
121. sort -t',' -k2,2 -rn file: Sort a file based on the second field (using ‘,’ as the delimiter) in reverse
numerical order.
122. sort -t',' -k2,2n file: Sort a file based on the second field (using ‘,’ as the delimiter) numerically.
123. sort -t':' -k1,1 -k2,2n -k3,3nr file: Sort a file based on multiple fields (using ‘:’ as the delim-
iter) in lexicographical and numerical order.
124. sort -t':' -k2,2 -n file: Sort a file based on the second field (using ‘:’ as the delimiter) numeri-
cally.
125. sort -u: Sort and remove duplicate lines.
126. tail -F -n +N file: Output appended data as the file grows, starting from line N.
127. tail -F file: Output appended data as the file grows, including rotated files.
128. tail -c N: Display the last N bytes of a file.
129. tail -f file: Output appended data as the file grows.
130. tail -n +N -f file: Output appended data as the file grows, starting from line N.
131. tail -n +N file: Display lines starting from line N to the end of a file.
132. tail -n N: Display the last N lines of a file.
133. tail -q -n N file1 file2: Display the last N lines from multiple files quietly.
134. tar -cvf archive.tar --exclude=pattern file: Create a tar archive excluding files that match a
pattern.

5
135. tar -cvf archive.tar files: Create a tar archive of specific files.
136. tar -cvzf archive.tar.gz directory: Create a gzipped tar archive of a directory.
137. tar -czvf archive.tar.gz --exclude-from=exclude.txt directory: Create a gzipped tar
archive of a directory excluding files listed in exclude.txt.
138. tar -czvf archive.tar.gz --exclude="*.log" directory: Create a gzipped tar archive of a di-
rectory excluding .log files.
139. tar -czvf archive.tar.gz directory: Create a gzipped tar archive of a directory with verbose
output.
140. tar -xvf archive.tar --strip-components N: Extract a tar archive while stripping N leading com-
ponents from file paths.
141. tar -xvf archive.tar --wildcards "*.txt": Extract only .txt files from a tar archive.
142. tar -xvf archive.tar.gz: Extract a tar archive with verbose output.
143. tar -xvzf archive.tar.gz: Extract a gzipped tar archive.
144. tr '[:lower:]' '[:upper:]': Convert text to uppercase.
145. tr '[:upper:]' '[:lower:]': Convert text to lowercase.
146. tr -cd '[:alnum:]' < inputfile: Remove all non-alphanumeric characters from a file.
147. tr -d '[:space:]' < inputfile: Remove all whitespace characters from a file.
148. tr -d '\\n' < inputfile > outputfile: Remove all newline characters from a file.
149. tr -d '\\r' < inputfile > outputfile: Remove carriage return characters from a file.
150. tr -s ' ' < inputfile > outputfile: Squeeze multiple spaces into a single space in a file.
151. tr -s '\\n' < inputfile > outputfile: Squeeze multiple consecutive newlines into a single new-
line in a file.
152. uniq -c -d: Count and display only duplicate lines.
153. uniq -d: Only print duplicate lines.
154. uniq -u: Only print unique lines.
155. wc -L file: Find the length of the longest line in a file. (this ignores the newline)
156. wc -l: Count the number of lines in a file.
157. wc -m: Count the number of characters in a file.
158. wc -w -l file: Count the number of words and lines in a file.
159. wc -w file1 file2: Count the number of words in multiple files.
160. wc -w: Count the number of words in a file.
161. xargs -I {} command {} arg1: Execute a command with a specific argument.
162. xargs -P N command: Run N instances of a command in parallel.
163. xargs -n N command: Run a command with N arguments at a time from standard input.
164. xargs -n1 command: Execute a command with one argument at a time.

You might also like