You can be very productive using the shell, however there are some fantastic tricks to make you faster and more efficient.
Command line Completion
If you had to type in cd /var/www/vhosts/mydomain.com/httpdocs/wp-content/themes/mylongthemename/ you run the risk of making a mistake or taking forever to get it right. Using the tab key to auto-complete directory names can save you a lot of time. Here we will show you how to use it just in the / directory, for the sake of simplicity. Type in cd / then type in cd v and hit the tab key. Now type in cd /h and hit the tab.
As you can see, the tab key will try to complete the directory name for you. If you have two directories that have the same beginning, it will complete as far as it can, then ask you to give one more letter, after which you can hit tab again. For example, if you have two directories documents and documentation, you would type cd d tab and it would give you documentation and sound the bell, at which point you could hit a and tab again and it would give the rest of documents. If it dings halfway through a directory name and you don’t know what your options are hit tab again and it will list them for you. This double tabtab can be very useful.
This can also be used for commands as well. Try typing in di tab it will ding once, hit the tab again and it will list your options.
Did you know that you can go back through previous commands that you have used by using the up arrow?
Searching your history
If you used the find command mentioned in Part 2 of this series and want to use it again today, but don't want to retype it, try using the CTRL + r command. This lets you search backward through your past commands for the one you used previously. In this case, hit CTRL + r , then type find , then keep hitting CTRL + r until your find command shows up. Then hit enter.
Reuse the last command parameters in new command
You have just entered ls -l /var/www/vhosts/domain.com/httpdocs/wp-content/themes and now realize you want to cd into that directory. Type cd then hit esc followed by .
Clearing to the beginning of the line
If you have just typed in a particularly long path for a command and realized you have the wrong command, go to the end of the command using your left and right cursor keys. Then hit CTRL + u.
The shopt command is short for shell options. If you run the shopt command on its own, it will list all the options and their current settings.
Setting long path names as a variable
Run this command shopt -s cdable_vars. Then you can create aliases for long directories.
For example: export Dmydomain.com="/var/www/vhosts/mydomain.com/httpdocs/". You can then go straight to your domains main directory by using cd Dmydomain.com
Use the leading capital D so you know it is a Directory expansion.
Separating commands using ;
You can put two commands on one line by using the ;. On its own, this is not of great use, but it does combine with other features of bash to prove quite useful.
Temporarily changing working directory to execute a command
In bash, you can group commands together using (). A useful trick is to combine a cd operation with another command, but return to your original directory.
For instance, if you are in /root and working there, but want to temporarily go to /var/www/domain.com and execute a command, you could do:
cd /var/www/domain.com command cd -
Or you could do:
There are lots of tricks and secrets in the bash shell and any downtime you have spent searching them out can be repaid in time saved later. We hope the few we have explained here have given you some speedier ways of doing things.