The Linux shell offers lots of commands, but there are a few used every day. For a standard user, the shell isn’t really important. We can do (almost) whatever using GUIs, but there are some actions that are to be done with a shell. It’s powerful, faster, and easier. How do you put in a text file all permissions of all files in a given directory? How do you modify a line matching a pattern all system wide? We shall have a look at the frequently used commands:
You can use more for paging through text one screenful at a time. There is another command, less, that does the same but faster. more came first, but while it only used to allow moving forward through the text, the less command let the user go backwards. Nowadays, we can use both commands as synonyms.
$ ls -l /etc/ | more
$ ls -l /etc/ | less
$ less file.text
$ more file.text
Warning: type q to quit less or more.
It’s kind of more. It outputs the contents of a file.
$ cat text.file
Another filter that sorts lines of text files. The manual is quite large, so let’s see a few quick examples:
$ sort /etc/htpasswd
Shows on stdout (the screen, by default), the /etc/htpasswd sorted alphabetically by lines.
$ sort -t: -k3 -n /etc/passwd
Performs a numeric sort (-n) by the 3rd field (-k3). Two fields are separated by ‘:’ (-t:).
$ ls -l /etc | sort -k5 -n -r | less
Orders the output of ls -l /etc by the 5th field (the file’s size, as man ls says) descending (-r). We use the field separator by default (one or more white spaces).
Selects parts of lines of the input file.
$ cut -d: -f3,5 /etc/passwd
Shows the 3rd and 5th field of all lines of /etc/passwd, having ‘:‘ as a field separator (-d:). Notice that with the sort command, we used -t to choose the separator.
$ ls -l /etc/passwd | cut -c2-10
Only outputs the file permissions, as they are on columns 2 to 10 in ls -l /etc/passwd.
Selects the first lines or characters in a file. We can use it after a pipe ( | ) or as a usual command:
$ cat /etc/passwd/ | head -2
Outputs the 2 first lines of /etc/passwd
$ head -2 /etc/passwd
You have learnt the cut usage, haven’t you? Then mix your knowledge:
$ sort /etc/passwd | head -2 | cut -d: -f1
Shows the 2 first user id of the system, having the file sorted alphabetically.
Opposite of head. Outputs the last lines or characters of files.
$ ls -l /etc | tail -2
shows the two last lines of the ls -l /etc output. Is it useful? Not really, yet. Use sort:
$ ls -l /etc | sort -k5 -n | tail -2
Shows the 2 biggest files in /etc
This is one of the most useful commands. It just translates characters. e.g: you want to replace all ‘a’ chars in a file for ‘z’, or e.g: you wish to delete all ‘b’ in a file (replace ‘b’ with ‘BLANKSPACE’):
$ tr aeiou AEIOU < /etc/passwd
the < character means: «redirect /etc/passwd to the standard input of the tr command». This line replaces all lower-case vowels to upper-case. a->A, e->E and so on. The first letter in «aeiou» corresponds to the first letter in «AEIOU». Thus, if you want to replace a,e,i,o,u with ‘V’, do:
$ tr aeiou VVVVV < dummyfile
$ tr aeiou V < dummyfile
This line doesn’t modify the /etc/passwd or the dummyfile. If you wish so, you have to redirect the output to the file you want to save the changes in, like that:
$ tr aeiou VVVVV < dummyfile > dummyout.
DON’T DO IT WITH /etc/passwd
As simple as «word-count», but powerful: it can count lines, words and characters of the input. Example:
$ wc -c /etc/passwd
How many characters does /etc/passwd contain?
$ ls -l /etc | wc -l
how many files and directories are in /etc ?
reports or ommits repeated lines in a list. «compacts» them. e.g: We have a file containing:
edu@debian:~$ cat dummyfile
This is a script of Family Guy.
Have a good day,
Have a good day,
Have a good day,
Its got repeated lines. I like counting things and I wish to know how many repeated lines are there.
edu@debian:~$ cat dummyfile | uniq -c
1 This is a script of Family Guy.
3 Have a good day,
1 Eduard Gamonal
Funny, but not really useful. What if instead of a dummy file we had a list of processes running on our system?
$ ps -e | cut -c25- | sort | uniq -c
For each running command , outputs the processes that are executing it. ps -e shows the command name from the 25th column on (notice the last hyphen in -c25- ).
It may seem complex sometimes, and the user my spend half an our to type a correct command, but after that we have the output we wanted and we can use it, since it’s text in a shell, not in a window.