Useful bash commands

These are few of the bash commands I often find useful:

z: Z allows jumping (cd) into frecently (i.e. frequently and recently) used directories. Instead of doing cd /home/pratyush/websites/project_name each time, I can now simply do z project_name. Z supports tab completion too. Link to Z library.

ps -u <username> -o pid,rss,command | awk '{print $0}{sum+=$2} END {print "Total", sum/1024, "MB"}': When accessing remote servers using shell, I often need to check the list of scripts running and the memory they are consuming. This command does exactly that. It is a sort of minimal task-manager. I found this command here.

php -S localhost:8888 -t .: Though I am not a PHP fan (anymore), this command serves the current directory using a live php server. This comes handy to tryout a local wordpress installation by just extracting the package.

python -m SimpleHTTPServer 8080 is a Python alternative for starting a live server from current directory to serve the files.

netstat -plunt: To see which ports are open and which application is on each one.

du file/path -chs: du is the disk-utility command. This command shows the total size of any directory. -c is for total, -h is for showing humanize file sizes and -s for showing only the summary. I often use this command on web-servers to find the size of file-system caches.

ssh-copy-id This appends the public key to the remote-host for password-less ssh logins.

mysqldump -u username -p --all-databases > alldbs.sql: For creating a backup of all the mysql databases.

mysql -u username -p < alldbs.sql: For restoring all the databases from the dump.

Both of the these mysql commands are high on performance. These come handy for creating a backup snapshots of databases.

I found these two commands here.

ssh -D 31500 This turns the SSH client into a SOCKS proxy server. It provides me a VPN on the fly. So if a website refuses to open, or is restricted to a particular country, I run this command and then update the proxy settings in Firefox as below:

1. Enable proxy in firefox.
2. Enter "" for "SOCKS Host"
3. Enter "31500" (or whatever port we chose) for Port.

Full documentation for this trick is available here.

howdoi: HowDoI provides answers to programming questions from command line. Thus instead of opening a browser and getting distracted in web, I can now simply type in something like howdoi convert csv to namedtuple to get the leads.

I usually pass the -ac arguments: -a provides the full text of the answer, -c enables colorized output.

ab -n 100 -c 10 AB is a poor man's website performance benchmarking tool. -c specifies the number of concurrent requests and -n specifies the total number of requests to be sent to a webpage. This comes handy while migrating websites to new servers or when making significant frontend changes.

wget "url" -c: WGET is for downloading files from command-line. I often find the download speeds significantly different in browser's built-in download managers and wget. -c enables the resume support.

There are various other must-know bash commands such as awk, uniq, head which are super-useful in daily work. Akshay has covered them in a brief tutorial here.

Pratyush Mittal @pratyush