Commands - File Content
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
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
# 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:
uniqreturns only unique lines,
-calso counts number of merged outputs.
pastemerges two files by line-to-line basis
joinmerges two files by custom logic
splitcan act as a counter-operation to merges, splitting lines
trtransforms characters or words e.g.
echo whatever mate | tr a-z A-Z
- Linux Shell Handbook 7th Edition