Tips & Tricks
Access the complete command list with descriptions
Here is a command that enables you to see the list of commands with their descriptions (based on the packages installed in your system).
apropos -r “[a-z]” > List_of_Commands.txt
– Vijay Kumar, vijjav@gmail.com
Displaying coloured output with the ‘tail -f’ command when a pattern match succeeds
GNU/Linux provides a very powerful set of commands. ‘tail’ is just one of them. It shows the last part of the files. Additionally, the ‘-f’ option keeps the file in an open state and continuously displays the data as the file grows. But we often want to look at only certain lines and are not interested in the whole log file. We can achieve this very efficiently by combining the ‘tail -f’ command with the ‘awk’ command, which will show certain lines in a different colour when pattern match succeeds.
For instance, the simple command shown below will display a line in red colour if the pattern is present in the current line; otherwise, it will display the line in the regular fashion. Because of the different colour combinations, it becomes very easy to take a look at the required logs. By combining these two utilities, one can improve productivity greatly. Let us try this out with a simple example.
First, let us define a string to be searched:
[bash]$ export SEARCH_STRING=”jumps”
Now search for the pattern ‘jumps’ in a current line and if it is present, display the line in red colour; if it is not present, display the line in the regular way.
[bash]$ tail -f output.log | awk -W interactive ‘{if($0 ~ “’$SEARCH_STRING’”) {print “33[0;31m”$0”33[0m”} else {print}}’ the quick brown fox jumps over ## NOTE: Only this line will be shown in red color the lazy dog
In the above example ‘-W interactive’ option sets unbuffered writes to ‘stdout’.
– Narendra Kangralkar, narendrakangralkar@gmail.com
Group commands and execution in the subshell
To group commands and execution in the subshell and then store the execution of the subshell in /tmp/all.out, run the following command in the terminal. Each command will execute in a subshell and finally get stored in /tmp/all.out. If one of the commands fails, then the subsequent command will not be executed.
$ (pwd; ls; cd ../elsewhere; pwd; ls) > /tmp/all.out
– Gururaj Rao, raogrr@gmail.com
Single line execution to remove files and directories with filtered contents
We often need to remove all files and directories which are not ‘LINUX’. Here is a command that will help you do this. We will skip all ‘.svn’ and ‘.’
find . -mindepth 0 -maxdepth 1 \( -type f -o -type d \) \( ! -name “LINUX” -a ! -name “.svn” -a ! -name “.” \)|xargs rm -rf {}
Note: Please use this carefully as it uses a command to delete files and folders.
– Gururaj Rao, raogrr@gmail.com
Open any application directly from the terminal with its default application
The ‘xdg-open’ command opens the default application associated with the file type directly from the terminal.
For example, the following command…
xdg-open http://opensourceforu.com
…opens opensourcefor.com in the default browser on your system. Similarly,
xdg-open test.py
…opens test.py, a Python script in the default text editor set for Python.
– Sricharan Chiruvolu, sricharanized@gmail.com
Tracking an IP address
To track an IP address, we need to check the established connection using the command netstat of sshd daemon. The following command checks the same:
#sudo netstat -tnpa | grep ESTABLISHED.*sshd
A small script that will help you find out the IP address is given below:
#!/bin/bash clear echo -e “\n\n” a=`sudo netstat -tnpa | grep ESTABLISHED.*sshd | grep -Po “\d+\.\d+\.\d+\d+\.\d+” | sort | uniq -c > /tmp/ip` echo -e “\t\t\tNumber_of_times_ssh \tIP Address” awk ‘{printf “\t\t\t\t”$1”11””11”$2}’ /tmp/ip echo -e “\n”
– Rupin Puthukudi, rupinmp@gmail.com
Taking screenshots in Linux and displaying an image using the CLI
Do you want to take a screenshot and that too from the terminal? You can use the command ‘import’ at the prompt, followed by the name of the file and format in which you want to save the screenshot, as follows:
$import screenshot.png
After executing the command, the mouse pointer changes to ‘X’ (cross). Now you can click on the window that you want to take the screenshot of. This command is part of the ImageMagick package, which is used for image manipulation.
Now, to display the screenshot using the terminal, you can use the command ‘display’ followed by the file name you want to be displayed. For example, if you want to display the file by the name ‘file1.png’, then give the following command:
$display file1.png
– Sathyanarayanan S, sathyanarayanan_s@yahoo.com
Dumping utmp and wtmp logs
Like pacct, you can also dump the contents of the utmp and wtmp files. Both these files provide login records for the host. This information may be critical, especially if applications rely on the proper output of these files to function.
Being able to analyse the records gives you the power to examine your systems in and out. Furthermore, it may help you diagnose problems with logins, for example, via VNC or SSH, non-console and console login attempts, and more.
You can dump the logs using the dump-utmp utility. There is no dump-wtmp utility; the former works for both.
You can also use the following command:
dump-utmp /var/log/wtmp
This will print a utmp file in human-readable format for you to analyse.
– Somya Jain, somya1124@yahoo.co.in
A beginner’s guide to Sed
The Sed (Stream Editor) command is used for changes in files automatically and also to replace or substitute a string. For example, if we have a file a.txt with content like:
hello how are you
…and we want to replace the word ‘hello’ with ‘welcome’, then we use the following command:
sed ‘/s/hello/welcome/’ a.txt
Option ‘-s’ is used to search and replace. Similarly, if we want to replace more than one word like ‘hello’ and ‘how’ with ‘welcome’ and ‘where’, use the following command:
sed -e ‘s/hello/welcome/’ -e ‘s/how/where/’ a.txt
Option ‘-e’ is for multiple strings. There are many other options that you can refer to in the manual of Sed.
– Ajay Trivedi, ajay.trivedi67@gmail.com