10 useful Linux command line tricks

When working on Linux on a day to day basis, there are times when you need to perform some tasks from command line. You can run combination of commands to get the desired result. Following are 10 of many useful Linux Command Line tricks I use almost everyday.

1. print column 2 of all tsv files in a directory and pipe it to a file:

for i in `ls`; do tail $i | tr "\t" "~" | cut -d"~" -f2 >> titles; done

2. Find unique string from a file:

sort -u filename

3. Calculate number of lines in a file:

wc -l

4. Send email from command line with attachment:

mutt -s 'my subject' -a /var/log/httpd/error_log -- youremail@domain.com < /dev/null

6. Read a file, print UNIQUE value in a specific column (column 5 in this case)

cat re | tr "\t" "~" | cut -d"~" -f5 | sort -u

7. Generate alphanumeric password

echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10`

8. Allow MySQL access in IPTable for a specific IP

-A INPUT -p tcp -s FROM_IP (IP1) --sport 1024:65535 -d TO_IP (IP2) --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT

-A OUTPUT -p tcp -s FROM_IP (IP2) --sport 3306 -d TO_IP (IP1) --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

9. While files are being copied/moved to a directory, run the following command to count number of files in that directory (in loop) and print it – acts like a timer.

printed=0; while [ `ls /some/directory/ | wc -l` -gt 0 ]; do output=`ls /some/directory/ | wc -l`; if [ $output -gt $printed ]; then clear; echo $output; elif [ $output == $printed ]; then echo $printed; printed=$output; fi; done

10. Print date in specific timezone

TZ=EST date

Profession: Web Application Developer Employment: Technical Lead, Interactive Division, GANZ Location: Toronto, Canada

You Might Also Like