Cool Terminal Tricks
This article was originally published in the Open Source For You magazine. It had also inspired me to write a book titled Linux Command-Line Tips & Tricks.
bash
is an interactive and scripting shell program for Unix and Unix-like operating systems. In this article, you will learn a few neat tricks that will makebash
a more convenient and useful tool to manage your GNU/Linux system.
While a lot of effort has gone into creating attractive desktop managers such as KDE, Gnome and Xfce, the humble terminal remains the desktop of compulsion/choice for system administrators and ultra-geeks. It only takes a few simple tricks for regular users to be more productive and comfortable at the terminal. While there are several terminal programs, this article relies on Gnome Terminal. A working knowledge of shell linux commands such as ls, mkdir, cd, cp, mv, chmod, head, tail, echo
and sudo
and basic scripting is needed for this article.
-
Use the
~/.bashrc
file: Commands in the hidden.bashrc
file are executed before the terminal window is displayed. It is usually well-documented and changes can be made to it using a text editor. Remember to backup the file before making any changes to the preferences specified in it. -
Start at the Desktop: The Gnome Terminal usually starts at the user's home directory. This directory is not easily accessible. Put the command "
cd ~/Desktop
" at the end of the.bashrc
file so that any files created at the terminal can be easily found on the desktop. The home directory can be referred in your commands and scripts using the "~" (tilde) abbreviation. -
Colour-code your terminal window: In some GNU/Linux distributions, the default terminal window has black text on white background. This is not good for your eyes. Change the terminal preferences to have dark background and light foreground colours. Another common annoyance is that the prompt by itself takes half of a line and long commands invariably wrap to the next line, disrupting your train of thought. To have a more informative and colourful prompt, add the following line at the end of your
.bashrc
file.PS1="\a\n\n\e[31;1m\u@\h on \d at \@\n\e[33;1m\w\e[0m\n$ "
This setting ensures that the prompt contains colour-coded username, machine name, date, time and current directory. Most importantly, you get an entire line to type your command. If you want to tinker with the prompt even further, visit the IBM Developer Works Linux library page for Prompt Magic.
-
Use command history: The commands that you type are stored in a hidden file named
.bash_history
. You can browse the history using the Up and Down arrow keys at the prompt. You can also do a search on this history using the keyboard shortcut "Ctrl+R". You can increase the length of the command history by changing the following variables in the.bashrc
file. They limit the number of lines of typed commands that can be stored in memory and in the.bash_history
file.HISTSIZE=1000 HISTFILESIZE=2000
-
Write shell scripts:
bash
is not only an interactive command prompt, it is also a powerful scripting language interpreter. As many terminal tasks require more than one shell command, you can store them in a shell script text file and pass the variable data as command-line arguments. I use the following script to kill applications by their name.for sID in `pgrep $1` do ps $sID kill -STOP $sID done for sID in `pgrep $1` do ps $sID kill -KILL $sID done
To kill all instances of the Opera browser from the command-line, I can type "
bash ~/MyScripts/kill.txt opera
". Even though an "execution bit" can be added to the file so thatbash
can be ommitted from the command, I never do this. As a long-time Windows user, I am paranoid about malware finding executable scripts and adding their own destructive commands. In Microsoft Windows operating systems, I usually change the default action of .vbs, .js, .wsf and .reg file types to "Edit", rather than leave them at "Open" or "Merge". I follow the same precaution in GNU/Linux. -
Create command aliases: You can create abbreviated forms for your commands in the
.bashrc
file. One of the most common commands that you have to type may be "ls -l
". Hence, I have the following alias for thels
command. I just need to type one letter for it - l.alias l='ls -l'
To execute my "kill" script, I use another alias:
alias kll='bash ~/MyScripts/killit.txt $1'
So, to kill Opera, I type
kll opera
. (I continue to use the obsolete pre-Blink Opera browser (pre-Blink) for its wonderful RSS reader. Unfortunately, it becomes unstable several times during a session. It cannot be killed by the usualkillall
orpkill
command. The kill script and this alias however vanquishes it.) -
Use
bash
, notsh
: My first experience with a Unix-like OS was on SCO Unix. As a result, I was accustomed to runing shell script files withsh
command. This continued even after I started using GNU/Linux systems. For years, I was perplexed why many of the scripts were not working well but they did fine after I set the execution bit (chmod +x
) and ran them directly without the shell command. Apparently, in many GNU/Linux distributions,sh
refers to the old Unix-like shell andbash
is a separate program and works like a more advanced superset ofsh
. As a result, scripts designed forbash
will not work well withsh
. So, always usebash
to run all yourbash
shell scripts. However, there are some tasks such as cross-platform driver compilation that still requiresh
. Another thing to note is that the su or root terminal usessh
by default. This is why the Up/Down arrow keys will not let you browse the history. So, be aware of which shell program you are currently using. -
Break-up long commands: When you have to type a long command, you can type a "slash" to easily wrap the command to the next line and continue from there.
ffmpeg -i tank.mp4 \ -c:v copy \ -c:a copy \ -ss 1:12 \ -t 2:50 \ tank-cut.mp4
-
Use keyboard shortcuts:
Gnome Terminal
is a multi-tab window. You can press "Ctrl+Shift+T" and start a terminal in a new tab in the same window. If you feel like you need to refer the manpages of the current command, do not cancel what you are typing. Just start a new tab and browsemanpages
from there. Another useful shortcut is "Ctrl+U", which cancels/deletes everything in the current line and lets you begin afresh. To copy text from a terminal window, the shortcut is "Ctrl+Shift+C" and the shortcut to paste text is "Ctrl+Shift+V". "Ctrl+C" is for killing the current command and should not be used to copy text in the terminal. -
Switch to a TTY Terminal: The Linux desktop managers are known to be very stable. However, in the rare occurrence when the desktop hangs, you can hold down "Ctrl+Alt" and press any of the function keys to switch to a "TTY terminal". These terminals are created by the OS before loading the GUI (desktop manager). From one of these terminals, you can login to your account and do your troubleshooting. One of the following commands can restart the desktop manager.
sudo service gdm restart # for Gnome 2 sudo service mdm restart # for Mate sudo service lightdm restart # for Upstart sudo service gdm3 restart # for Gnome 3
-
Run shell scripts from other programs: You can execute shell commands and scripts from other programs to automate your application tasks. In my March article, I have shown how to add shell commands/scripts to Nautilus/Caja file manager as context menu options. Many other programs provide shell support. In Pluma/Gedit (the default text editor), I have an "external tool command" to convert Markdown files to formatted HTML.
perl ~/MyScripts/markdown.pl --html4tags \ "$GEDIT_CURRENT_DOCUMENT_NAME" > \ "${GEDIT_CURRENT_DOCUMENT_NAME%.*}.htm"
You will have to look up the help docs of the application to find out how to run shell commands.
I use isolated Firefox profiles to load web pages with Adobe Flash or run the browser behind a US-based proxy IP. These profiles can be quickly loaded from other programs or from a launcher (shortcut) using special command-line options.
# Create a new profile and specify its settings firefox -ProfileManager #Load a created a named profile firefox -p ProxyIpProfile firefox -p AdobeFlashProfile
You will be surprised how many popular GUI programs have an elaborate command-line interface.
cvlc
is the command-line version of the media player VLC. I use it to play audio files from the terminal.While a lot of GUI programs exist for GNU/Linux, there are several times even more command-line utilities written for it and its predecessor Unix. The Linux userland abounds with command-line solutions where the GUI option is deficient or does not exist. For example, there is a wonderful program called
alsamixer
which provides a command-line version of Sound Preferences. I was able to configure my near-vintage Pinnacle TV tuner using this program. If Network Manager failed to recognize your wireless modem, thenwvdial
will still be able to configure and use it. Espeak can convert text arguments to voiced audio, which can then be captured as wave files. I use the wave files to provide audio notifications from mybash
scripts and to annotate my YouTube videos. Well... one can go on forever like this. The terminal can become your best friend and saviour. All that you need to do is a bit of exploring and some inevitable trial-and-error to get the best out of your system.
References
- Learning the shell; http://www.linuxcommand.org/lc3_learning_the_shell.php
- BASH Programming - Introduction HOW-TO; http://tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html
- Tip: Prompt magic; https://www.ibm.com/developerworks/linux/library/l-tip-prompt/