Easily remember linux commands

Posted on Sun 19 February 2012 in Tech

I use an absolute ton of awesome tools my various linux distros. The problem is I tend to forget a lot of the time how to use the tools, so I got a list of frequently used command with and a few examples of how to use them from here.

So here's the file I use.

— Create a new tar archive $ tar cvf archive_name.tar dirname/ — Extract from an existing tar archive. $ tar xvf archive_name.tar — Grep for a given string in a file (case in-sensitive search). $ grep -i "the" demo_file — Print the grep line, along with the 3 lines after it. $ grep -A 3 -i "example" demo_text — Grep for a given string in all files recursively $ grep -r "ramesh" * — Grep for a string that starts with $ char and contains what's in $1 grep — color=auto -B 1 -E '^$.b'$1'b' cmd_examples.txt — Find files using file-name ( case in-sensitve find) $ find -iname "MyCProgram.c" — Execute commands on files found by the find command $ find -iname "MyCProgram.c" -exec md5sum {} ; — Find all empty files in home directory $ find ~ -empty — Converts the DOS file format to Unix file format using sed command. $ sed 's/.$//' filename — Print file content in reverse order $ sed -n '1!G;h;$p' thegeekstuff.txt — Add line number for all non-empty-lines in a file $ sed '/./=' thegeekstuff.txt | sed 'N; s/n/ /' — Remove duplicate lines using awk $ awk '!($0 in array) { array[$0]; print }' temp — Print all lines from /etc/passwd that has the same uid and gid $ awk -F ':' '$3==$4' passwd.txt — Print only specific field from a file. $ awk '{print $2,$5;}' employee.txt — Diff ignore white space while comparing. $ diff -w name_list.txt name_list_new.txt — Sort a file in ascending order $ sort names.txt — Sort a file in descending order $ sort -r names.txt — Sort passwd file by 3rd field. — Xarg:Copy all images to external hard-drive $ ls .jpg | xargs -n1 -i cp {} /external-hard-drive/directory — Xarg:Search all jpg images in the system and archive it. $ find / -name .jpg -type f -print | xargs tar -cvzf images.tar.gz — XargLDownload all the URLs mentioned in the url-list.txt file $ cat url-list.txt | xargs wget –c — LS:Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr $ ls -ltr — LS:Visual Classification of Files With Special Characters Using ls -F $ ls -F — Gzip:Display compression ratio of the compressed file using gzip -l $ gzip -l .gz $ bzip2 test.txt $ bzip2 -d test.txt.bz2 — UNZip:View the contents of .zip file (Without unzipping it): $ unzip -l jasper.zip — Shutdown the system after 10 minutes. $ shutdown -h +10 — Force the filesystem check during reboot. # shutdown -Fr now — FTP:Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following. $ ftp IP/hostname — FTP:To download a file $ ftp> mget .html — To view the file names located on the remote server before downloading, mls ftp command as shown below. $ ftp> mls .html - — View crontab entry for a specific user $ crontab -u john -l — Schedule a cron job every 10 minutes. $ /10 * * * * /home/ramesh/check-disk-cron $ service ssh status — Status:Check the status of all the services. $ service — status-all — Restart a service. # service ssh restart — To view current running processes. $ ps -ef | more — To view current running processes in a tree structure. H option stands for process hierarchy. $ ps -efH | more $ df -T $ rm -i filename.txt $ rm -i file — Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it. $ cp -i file1 file2 — While displaying the file, following cat -n command will prepend the line number to each line of the output. $ cat -n /etc/logrotate.conf — Give full access to user and group (i.e read, write and execute ) on a specific file. $ chmod ug+rwx file.txt — Revoke all access for the group (i.e read, write and execute ) on a specific file. $ chmod g-rwx file.txt — Apply the file permissions recursively to all the files in the sub-directories. $ chmod -R ug+rwx file.txt — To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time. $ chown oracle:dba dbora.sh — Create nested directories using one mkdir command. $ mkdir -p dir1/dir2/dir3/dir4/ — Sample uname output from a Ubuntu laptop is shown below. $ uname -a — When you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command. $ whereis ls — This searches for the executable lsmk in the /tmp directory, and displays it, if it is available. $ whereis -u -B /tmp -f lsmk — Whatis command displays a single line description about a command. $ whatis ls — The example below shows all files in the system that contains the word crontab in it. $ locate crontab — Print the last 10 lines of a file by default. $ tail filename.txt — Print N number of lines from the file named filename.txt $ tail -n N filename.txt — View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C. $ tail -f log-file — less is very efficient while viewing huge log files, as it doesn't need to load the full file while opening. $ less huge-log-file.log — Ping a remote host by sending only 5 packets. $ ping -c 5 gmail.com — Download and store it with a different name. $ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701

Then I use this command to search for the specific command and get back the examples with an explanation. Please note, to add your own examples, you need to save the same format of the command starting with a $ and the line above providing the explanation. When you do a search you must search for the command.

grep — color=auto -B 1 -E '^$.bpingb' ~/cmd_examples.txt

And if you wish to use it in a bash alias, you'll need to create a function

function exam() { grep — color=auto -B 1 -E '^$.b'"$*"'b' ~/cmd_examples.txt ;}

Calling

$ exam ping

should return

— Ping a remote host by sending only 5 packets.

$ ping -c 5 gmail.com