Tips & Tricks

OpenSource For You - - CONTENTS -

Ac­cess the com­plete com­mand list with de­scrip­tions

Here is a com­mand that en­ables you to see the list of com­mands with their de­scrip­tions (based on the pack­ages in­stalled in your sys­tem).

apro­pos -r “[a-z]” > List_of_Com­mands.txt

– Vi­jay Ku­mar, vi­j­[email protected]

Dis­play­ing coloured out­put with the ‘tail -f’ com­mand when a pat­tern match suc­ceeds

GNU/Linux pro­vides a very pow­er­ful set of com­mands. ‘tail’ is just one of them. It shows the last part of the files. Ad­di­tion­ally, the ‘-f’ op­tion keeps the file in an open state and con­tin­u­ously dis­plays the data as the file grows. But we of­ten want to look at only cer­tain lines and are not in­ter­ested in the whole log file. We can achieve this very ef­fi­ciently by com­bin­ing the ‘tail -f’ com­mand with the ‘awk’ com­mand, which will show cer­tain lines in a dif­fer­ent colour when pat­tern match suc­ceeds.

For in­stance, the sim­ple com­mand shown be­low will dis­play a line in red colour if the pat­tern is present in the cur­rent line; oth­er­wise, it will dis­play the line in the reg­u­lar fashion. Be­cause of the dif­fer­ent colour com­bi­na­tions, it be­comes very easy to take a look at the re­quired logs. By com­bin­ing these two util­i­ties, one can im­prove pro­duc­tiv­ity greatly. Let us try this out with a sim­ple ex­am­ple.

First, let us de­fine a string to be searched:

[bash]$ ex­port SEARCH_STRING=”jumps”

Now search for the pat­tern ‘jumps’ in a cur­rent line and if it is present, dis­play the line in red colour; if it is not present, dis­play the line in the reg­u­lar way.

[bash]$ tail -f out­put.log | awk -W in­ter­ac­tive ‘{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 ex­am­ple ‘-W in­ter­ac­tive’ op­tion sets un­buffered writes to ‘std­out’.

– Naren­dra Kan­gralkar, naren­drakan­[email protected]

Group com­mands and ex­e­cu­tion in the sub­shell

To group com­mands and ex­e­cu­tion in the sub­shell and then store the ex­e­cu­tion of the sub­shell in /tmp/all.out, run the fol­low­ing com­mand in the ter­mi­nal. Each com­mand will ex­e­cute in a sub­shell and fi­nally get stored in /tmp/all.out. If one of the com­mands fails, then the sub­se­quent com­mand will not be ex­e­cuted.

$ (pwd; ls; cd ../else­where; pwd; ls) > /tmp/all.out

– Gu­ru­raj Rao, rao­[email protected]

Sin­gle line ex­e­cu­tion to re­move files and di­rec­to­ries with fil­tered con­tents

We of­ten need to re­move all files and di­rec­to­ries which are not ‘LINUX’. Here is a com­mand that will help you do this. We will skip all ‘.svn’ and ‘.’

find . -min­depth 0 -maxdepth 1 \( -type f -o -type d \) \( ! -name “LINUX” -a ! -name “.svn” -a ! -name “.” \)|xargs rm -rf {}

Note: Please use this care­fully as it uses a com­mand to delete files and fold­ers.

– Gu­ru­raj Rao, rao­[email protected]

Open any ap­pli­ca­tion di­rectly from the ter­mi­nal with its de­fault ap­pli­ca­tion

The ‘xdg-open’ com­mand opens the de­fault ap­pli­ca­tion as­so­ci­ated with the file type di­rectly from the ter­mi­nal.

For ex­am­ple, the fol­low­ing com­mand…

xdg-open http://open­source­foru.com

…opens open­source­for.com in the de­fault browser on your sys­tem. Sim­i­larly,

xdg-open test.py

…opens test.py, a Python script in the de­fault text edi­tor set for Python.

– Sricha­ran Chiru­volu, sricha­[email protected]

Track­ing an IP ad­dress

To track an IP ad­dress, we need to check the es­tab­lished con­nec­tion us­ing the com­mand net­stat of sshd dae­mon. The fol­low­ing com­mand checks the same:

#sudo net­stat -tnpa | grep ES­TAB­LISHED.*sshd

A small script that will help you find out the IP ad­dress is given be­low:

#!/bin/bash clear echo -e “\n\n” a=`sudo net­stat -tnpa | grep ES­TAB­LISHED.*sshd | grep -Po “\d+\.\d+\.\d+\d+\.\d+” | sort | uniq -c > /tmp/ip` echo -e “\t\t\tNum­ber_of_­times_ssh \tIP Ad­dress” awk ‘{printf “\t\t\t\t”$1”11””11”$2}’ /tmp/ip echo -e “\n”

– Rupin Puthukudi, [email protected]

Tak­ing screen­shots in Linux and dis­play­ing an im­age us­ing the CLI

Do you want to take a screen­shot and that too from the ter­mi­nal? You can use the com­mand ‘im­port’ at the prompt, fol­lowed by the name of the file and for­mat in which you want to save the screen­shot, as fol­lows:

$im­port screen­shot.png

Af­ter ex­e­cut­ing the com­mand, the mouse pointer changes to ‘X’ (cross). Now you can click on the win­dow that you want to take the screen­shot of. This com­mand is part of the ImageMag­ick pack­age, which is used for im­age ma­nip­u­la­tion.

Now, to dis­play the screen­shot us­ing the ter­mi­nal, you can use the com­mand ‘dis­play’ fol­lowed by the file name you want to be dis­played. For ex­am­ple, if you want to dis­play the file by the name ‘file1.png’, then give the fol­low­ing com­mand:

$dis­play file1.png

– Sathya­narayanan S, sathya­[email protected]­hoo.com

Dump­ing utmp and wtmp logs

Like pacct, you can also dump the con­tents of the utmp and wtmp files. Both these files pro­vide lo­gin records for the host. This in­for­ma­tion may be crit­i­cal, es­pe­cially if ap­pli­ca­tions rely on the proper out­put of these files to func­tion.

Be­ing able to an­a­lyse the records gives you the power to ex­am­ine your sys­tems in and out. Fur­ther­more, it may help you di­ag­nose prob­lems with lo­gins, for ex­am­ple, via VNC or SSH, non-con­sole and con­sole lo­gin at­tempts, and more.

You can dump the logs us­ing the dump-utmp util­ity. There is no dump-wtmp util­ity; the for­mer works for both.

You can also use the fol­low­ing com­mand:

dump-utmp /var/log/wtmp

This will print a utmp file in hu­man-read­able for­mat for you to an­a­lyse.

– Somya Jain, [email protected]­hoo.co.in

A begin­ner’s guide to Sed

The Sed (Stream Edi­tor) com­mand is used for changes in files au­to­mat­i­cally and also to re­place or sub­sti­tute a string. For ex­am­ple, if we have a file a.txt with con­tent like:

hello how are you

…and we want to re­place the word ‘hello’ with ‘wel­come’, then we use the fol­low­ing com­mand:

sed ‘/s/hello/wel­come/’ a.txt

Op­tion ‘-s’ is used to search and re­place. Sim­i­larly, if we want to re­place more than one word like ‘hello’ and ‘how’ with ‘wel­come’ and ‘where’, use the fol­low­ing com­mand:

sed -e ‘s/hello/wel­come/’ -e ‘s/how/where/’ a.txt

Op­tion ‘-e’ is for mul­ti­ple strings. There are many other op­tions that you can re­fer to in the man­ual of Sed.

– Ajay Trivedi, [email protected]

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.