Why Typing Speed
When taking any hands-on exam, your typing speed can make a difference.
So, you need to know some tips to go quicker.
Here, the purpose is not to give a comprehensive list but to list all the main tips.
Bash Completion
Before anything else, check that bash completion is enabled (it’s not the case with the minimal install):
# yum install bash-completion
From now on, the tab key will provide you some relevant options.
Some useful Bash shortcuts
Searching for a command in the history.
- When you want to search quickly in your command history, type Ctr-R followed by the command that you want to reuse.
Recalling the last argument from the previous command.
- I regularly type:
# more /var/log/messages
when I really meant:
# less /var/log/messages
In this case, you can type less followed by Alt-. which copies the last argument from the previous command.
Reexecuting the previous command.
- You want to execute a root privilege command with only user privilege:
$ cat /etc/passwd cat: /etc/passwd: Permission denied $ sudo !!
!! immediately rexecutes the last command (source).
The mkdir Command
Creating nested directories and browsing them can take some time.
- The normal way to create the /projects/myproject/data tree would be:
# mkdir projects # cd projects # mkdir myproject # cd myproject # mkdir data
# cd data - Going much quicker is possible:
# mkdir -p projects/myproject/data
# cd !$
Creating a directory and setting its permissions normally takes two steps.
- Creation of the directory and permission assignment:
# mkdir /projects # chmod 770 /projects
- Both steps can be combined into only one (Note: only numbers accepted):
# mkdir -m 770 /projects
The systemctl Command
Writing the complete name of a service is not necessary:
- Normally you should write:
# systemctl start ntpd.service
- However it is much quicker to only type:
# systemctl start ntpd
Starting and activating services at boot take two lines:
- Start the service and activate it at boot:
# systemctl start ntpd # systemctl enable ntpd
- Both lines can be combined into only one (Note: the second command is only executed if the first one didn’t return an error):
# systemctl start ntpd && systemctl enable ntpd
With the RHEL 7.2 release, you can now enable and start (in this order) one or several services with only one command (this works with the disable and mask options too):
# systemctl enable --now ntpd httpd
When you need to start several services at the same time, it’s painful:
- Start the two services:
# systemctl start ntpd # systemctl start httpd
- Both lines can be combined into only one:
# systemctl start ntpd httpd
Note: For people who used to deal with RHEL 6 and type service httpd start, who now have to type systemctl start httpd with RHEL 7, there is a nice trick: ESC+t swaps the last two arguments on the command line (source).
Shell File Management
When you need to execute the same operation on several files, the shell provides an interesting feature:
- Create three empty files:
# touch /opt/file1 /opt/file2 /opt/file3
- This operation can be simplified:
# touch /opt/{file1,file2,file3}
Note: This only works with commands that accept several arguments at a time.
This is especially useful when dealing with Firewalld.
Let’s assume that you need to add 3 different services to your firewall at the same time: http, https, and dns:
- You can run four different commands with 3 almost identical ones:
# firewall-cmd --permanent --add-service=http # firewall-cmd --permanent --add-service=https # firewall-cmd --permanent --add-service=dns # firewall-cmd --reload
- Or you can run only two commands:
# firewall-cmd --permanent --add-service={http,https,dns} # firewall-cmd --reload
The Shell History
With the arrow keys up and down you can navigate in the shell history. But with the ! character you can search for a pattern in the shell history to execute a previously typed command. This can save you some typing.
The shell history can be very helpful when dealing with Firewalld:
- You can use two commands to allow the http protocol through the firewall:
# firewall-cmd --permanent --add-service=http # firewall-cmd --reload
- Or you can type:
# firewall-cmd --add-service=http # firewall-cmd --add-service=http --permanent
According to Bert Van Vreckem the second option is much quicker with the help of the shell history (see details here).
The virsh Command
If you need to start a KVM guest and then connect to it through the console, you’ve got two commands to type:
# virsh start vm.example.com # virsh console vm.example.com
These two steps can be combined into only one:
# virsh start vm.example.com --console
Note: Furthermore, you will not miss any messages displayed on the console like in the two-step solution!
The passwd Command
Szymon Niedziela in the Linkedin’s Red Hat Linux Professional Users group provides a nice tip about the passwd command.
Instead of typing twice your root password with the risk of typo, type:
# echo mypass|passwd --stdin root
Note1: You can’t remove the root argument in this case.
Note2: It’s quick and clean at the exam but not very clever after the exam: your password will be stored in the command history list.
Bash Aliases
If you plan to type several times the same complicated command, you can decide to create an alias.
For example, edit the /root/.bashrc file and paste the following line into it:
alias APACHE-RESTART="systemctl restart httpd"
Then, make the alias active (or exit and reconnect):
# source /root/.bashrc
Now, you only need to type A (uppercase) and Tab to restart your Apache server.
Note1: Use uppercase characters in your alias to make the call to your alias easy.
Note2: Use “ or ‘ but not `, otherwise the command in the alias will be executed each time at the connection.
Thanks to Ernesto for this tip.
Additional Resources
You can also read this interesting Redpill Linpro article about job control and other useful tips.
Maxim Burgerhout from RedHat provides some useful Bash tips.
Don’t hesitate to provide some other useful tips through your comments.
Excellent topic, It will definitely help me during exam!
I’m glad to hear it.
Hi CertDepot, excellent job. Thanks for this website….
I generally use an alias to save time when it is needed to restart a service more than 3 times. For instance, Apache service probably must restart 4 or 5 times, so with: alias APACHE-RESTART=”systemctl restart httpd”, you can save time just typing A (uppercase) and TAB.
It is an interesting idea, thanks.
Hi CertDepot. Thanks again for all your support. About “Bash Aliases” In the example above, I think that in the /root/.bashrc file lacks the command “alias”. Am I in the right?
Yes, you are absolutely right. I was certainly thinking about something else!
Thank you very much for bringing this mistake to my attention.
On the passwd command tip, a character at the start of the command will keep it out of the shell history log.
Which character? Could you be more precise?
I think a space (‘ ‘) character included before the command leaves it out of the bash history. However, the environment variable $HISTCONTROL is to be set.
export HISTCONTROL=ignorespace
Reference:
http://stackoverflow.com/questions/8473121/execute-command-without-keeping-it-in-history
Interesting. Thanks.
CertDepot,
There are a few bash shortcuts that can be useful. I use the “Ctrl -r” pattern.
This searches for reverse history. Handy for repeating some commands.
Reference (there are a few of them):
http://www.tldp.org/LDP/abs/html/special-chars.html
under title Control Characters
This is very interesting. Thank you.
This is a good one. Another one that’s extremelly useful is Alt + . – use the last word of the previous command.
Thank you! This blog is informative and helpful.
It has an excellent resource for a newbie to learn.
Thanks.
Hi CertDepot,
Thank you very much for your very useful tips. I already use few of them and shall adopt the others too.
One more thing regarding “# touch /opt/{file1,file2,file3}”
You can also use a quicker way.
If you want to create 5 directories and 9 files in each directory.
# mkdir dir{1..5}
# touch dir{1..5}/file{1..9}
Thanks.
There is a way to append to the current command line the last strings of the commands saved in history:
Alt+.
If you repeat this sequence, last command strings are appearing.
Also, another one:
!! – used more with sudo !!
It takes the last command typed on command line, normally which needs privileges in this example, to be executed with sudo.
I hope helps and be understandable.
Interesting. Thanks.
I have noticed that the Alt one was on a comment XD.
Another one, when recharging the file “.bashrc”, if your current directory is the root home the only thing it is needed to type is:
. .bashrc