There are plenty of precious command line stuff every admin should be aware on Linux. In this article I just decided to place some I use often and are interesting to know. Below commands are nothing special and probably many of experienced sys admins already know them. However I'm pretty sure novice admins and start-up Linux enthusiasts will find it useful. I know there much more to be said on the topic. So anyone is mostly welcome to share his used cmds.
1. Delete all files in directory except files with certain file extension
It is good trick to delete all files in directory except certain file formats, to do so:
root@linux:~# rm !(*.c|*.py|*.txt|*.mp3)
2. Write command output to multiple files (tee)
The normal way to write to file is by using redirect (to overwrite file) ">" or (to append to file) ">>";. However when you need to write output to multiple files there is a command called tee, i.e.:
root@linux:~# ps axuwwf | tee file1 file2 file3
3. Search for text in plain text file printing number of lines after match
Whether you need to print all number of lines after match of "search_text" use:
root@linux:~# grep -A 5 -i "search_text" text_file.txt
4. Show all files where text string is matched with GREP (Search for text recursively)
Searching for text match is extremely helpful for system administration. I use grep recursive (capability) almost on daily basis:
root@websrv:/etc/dovecot# grep -rli text *
conf.d/10-auth.conf
conf.d/10-mail.conf
dovecot.conf
5. Finding files and running command on each file type matched
In Linux with find command it is possible to search for files and run command on each file matched.
Lets say you we want to look in current directory for all files .swp (temporary) files produced so often by VIM and wipe them out:
root@linux:~# find . -iname '*.swp*' -exec rm -f {} \;
6. Convert DOS end of file (EOF) to UNIX with sed
If it happens you not have dos2unix command installed on Linux shell and you need to translate DOS end of file (\r\n – return carriage, new line) to UNIX's (\r – return carriage)), do it with sed:
root@linux:~# sed 's/.$//' filename
7. Remove file duplicate lines with awk:
cat test.txt
test
test
test duplicate
The brown fox jump over ...
Richard Stallman rox
root@linux:~# awk '!($0 in array) { array[$0]; print }' test.txt
test
test duplicate
The brown fox jump over ...
Richard Stallman rox
To remove duplicate text from all files in directory same can be easily scripped with bash for loop:
root@linux:~# for i in *; do
awk '!($0 in array) { array[$0]; print }' $i;
done
8. Print only selected columns from text file
To print text only in 1st and 7th column in plain text file with awk:
root@linux:~# awk '{print $1,$6;}' filename.txt ...
To print only all existing users on Linux with their respective set shell type:
root@linux:~# cat /etc/passwd|sed -e 's#:# #g'|awk '{print $1,$6;}'
9. Open file with VIM text editor starting from line
I use only vim for console text processing, and I often had to edit and fix file which fail to compile on certain line number. Thus use vim to open file for writing from necessary line num. To open file and set cursor to line 35
root@linux:~# vim +35 /home/hipo/current.c
10. Run last command with "!!" bash shorcut
Lets say last command you run is uname -a:
root@websrv:/home/student# uname -a
Linux websrv 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1 i686 GNU/Linux
To re-run it simply type "!!":
root@websrv:/home/student# !!
uname -a
Linux websrv 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1 i686 GNU/Linux
root@websrv:/home/student#