Linux tool tips

  • Remove duplicate lines: awk ‘!a[$0]++’
  • The sed command r inserts a file. For example, insert foo.h after ‘INCLUDE’: sed ‘/INCLUDE/ r foo.h’
  • Show line endings in a file: cat -E
  • The option -i or –ignore-case tells diff to do a case-insensitive comparison
  • List files containing a match for a regular expression: grep -l
  • The substitute command in sed can take a ‘w’ option to specify an output file. e.g. sed s/foo/bar/w myfile
  • Awk script to count the number of words in a file: awk ‘{ total = total + NF }; END { print total+0 }’
  • wc -L shows the length of the longest line in a file.
  • Recursively search all C files (Gnu grep): grep -r –include “*.c” “pattern”
  • wc returns the number of lines, words, and bytes in a file. Can use -l, -w, -c to get just one of these.
  • One way to practice sed commands is to pipe echo out. Example: echo ‘hello world’ | sed s/world/orb/
  • Delete files, asking for confirmation: rm -i
  • The awk variable NF contains the number of fields in the current record.
  • Change the third ‘apple’ in each record to ‘orange’: sed ‘s/apple/orange/3’ somefile
  • Sort a file numerically: sort -n
  • Show tabs in a file: cat -T
  • In bash, !$ expands to last word of previous command.
  • Gnu sed allows addresses of the form start~step. For example, 1~2 matches odd-numbered lines.
  • delete from line 3 up to and including first blank line: sed 3,/^$/d filename
  • Search man pages for a given string: man -k string
  • sed script to delete blank lines /^$/d
  • Emacs search, ignoring punctuation or spaces between words: M-s w
  • sed script to delete last line of a file:
  • List files in descending order of size: ls -S
  • Show which lines are repeated in a sorted file: uniq -d
  • The sed command p prints. For example, print lines 3 through 7 of a file: sed -n ‘3,7p’ somefile
  • Sort a file in case-insensitive order: sort -f. ‘f’ for ‘fold,’ i.e. fold upper and lower case together
  • Output a file, displaying non-printing characters: cat -v
  • The name ‘awk’ comes from the initials of its creators: Aho, Weinberger, and Kernighan
  • Split a text file into files with 50 lines each: split –lines=50 foo.txt
  • Change ‘lead’ to ‘gold’ everywhere: sed ‘s/lead/gold/g’ somefile
  • sed and awk both use -f <filename> to specify a file of commands.
  • awk uses -F to specify a field separator. For example, -F: would say to use a colon as the field separator.
  • Hexadecimal dump of a file: od -x <file>
  • Current UTC time: date -u
  • diff -e produces an ed program. ed is the ancestor of sed.
  • sort a file based on the 3rd field in each line: sort -k 3. Fields separated by white space.

Thanks @UnixToolTip For your wonderful tricks.

Leave a comment