File Content

Updated at 2016-11-05 12:00

wc stands for word count. But it can also count other stuff like lines and bytes.

# number of lines in a file
wc -l my.log

# number of files in this directory
ls | wc -l

# lines of code in directory tree, including comments etc.
find . -name '*.cs' | xargs wc -l

# number of bytes in a file
wc -c my.log

# sorted numer of bytes of each file in directory tree
find /var/log -type f | xargs wc -c | sort

head / tail get X number of first / last lines. They can also be combined to get exact line.

# get file or directory that was most recently modified
ls -t | head -1

# show 10 last lines and keep it open, showing new lines as they come up
tail -f my.log

# get 13th line in a file
head -13 my.log | tail -1

cut splits entries by other delimiter than line. For example columns are separated by : in /etc/passwd.

# get only 1st and 6th colums in passwords
# effectively gets username and home directory
cut -d: -f1,6 /etc/passwd

# skip first line, take characters 2-12 and 52+ per line
# effectively gets file permissions and name
ls -l | tail -n +2 | cut -c2-12,52-

Other interesting command:

  • uniq returns only unique lines, -c also counts number of merged outputs.
  • paste merges two files by line-to-line basis
  • join merges two files by custom logic
  • split can act as a counter-operation to merges, splitting lines
  • tr transforms characters or words e.g. echo whatever mate | tr a-z A-Z


  • Linux Shell Handbook 7th Edition