OpenSource For You

Cool Terminal Tricks

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. So let’s learn a few tricks to use on the terminal for some amazing effects.

- By: V. Subhash The author is a writer, programmer and FOSS fan ( www.vsubhash.com).

Bash is an interactiv­e and scripting shell program for UNIX and UNIX-like operating systems. In this article, you will learn a few neat tricks that will make bash 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 systems administra­tors and ultra-geeks. It only takes a few simple tricks for regular users to become more productive and comfortabl­e 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.

Using the ~/.bashrc file: Commands in the hidden .bashrc file are executed before the terminal window is displayed. This is usually well-documented and changes can be made using a text editor. Remember to back up the file before making any changes to the preference­s specified in it.

Starting at the desktop: The GNOME Terminal usually starts at the user’s home directory, which 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) abbreviati­on.

Colour-code your terminal window: In some GNU/ Linux distributi­ons, the default terminal window has black text on a white background. This is not good for your eyes. Change the terminal preference­s to have a dark background and light foreground colours. Another common annoyance is that the prompt by itself takes half a line, and long commands invariably wrap to the next line, disrupting your train of thought. To have a more informativ­e 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 a colourcode­d user name, machine name, date, time and current directory. Most importantl­y, 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. (I had mentioned this trick in a previous article. I am repeating it here for those who might have missed it.)

Using 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 search 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 HISTFILESI­ZE=2000

Write shell scripts: Bash is not only an interactiv­e command prompt but is also a powerful scripting language interprete­r. 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 applicatio­ns 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 that bash can be omitted 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 destructiv­e 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.

Creating command aliases: You can create abbreviate­d 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 the ls command. I just need to type one letter (l) for it:

alias l= ‘ ls l’

To execute my ‘kill’ script, I use the following alias:

alias kll= ‘ bash ~/MyScripts/killit.txt’

So, to kill Opera, I type ‘kll opera’. I continue to use the obsolete pre-Blink Opera browser for its wonderful RSS reader. Unfortunat­ely, it becomes unstable several times during a session. It cannot be killed by the usual ‘killall’ or ‘pkill’ command. The kill script and this alias, however, vanquishes it.

Using bash, not sh: My first experience with a UNIXlike OS was on SCO UNIX. As a result, I was accustomed to running shell script files with the sh command. This continued even after I started using GNU/Linux systems. For years, I was perplexed by 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 distributi­ons, sh refers to the old UNIX-like shell and bash is a separate program that works like a more advanced superset of ‘sh’. As a result, scripts designed for bash will not work well with sh. So, always use bash to run all your bash shell scripts. However, there are some tasks such as cross-platform driver compilatio­n that still require sh. Another thing to note is that the su or root terminal uses sh 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.

Breaking 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, as follows:

ffmpeg -i tank.mp4 \ -c:v copy \ -c:a copy \ -ss 1:12 \ -t 2:50 \ tank-cut.mp4

Using keyboard shortcuts: GNOME Terminal is a multitab window. You can press Ctrl+Shift+T and start a terminal in a new tab in the same window. If you feel you need to refer to the man pages of the current command, do not cancel what you are typing. Just start a new tab and browse the man pages from there. Another useful shortcut is Ctrl+U, which cancels or deletes everything in the current line and lets you begin afresh. To copy text from a terminal window, the short cut is Ctrl+Shift+C, and the short cut 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.

Switching to a TTY terminal: The Linux desktop managers are known to be very stable. However, on those rare occasions 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 log in to your account and do your troublesho­oting. 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

Running shell scripts from other programs: You can execute shell commands and scripts from other programs to automate your applicatio­n tasks. In my article that appeared in the March 2016 issue of OSFY, I have shown how to add shell commands/scripts to the Nautilus/Caja file manager as the 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 documentat­ion of the applicatio­n 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 (a short cut) using special command line options.

# Create a new profile and specify its settings firefox -ProfileMan­ager

#Load a created a named profile firefox -p ProxyIpPro­file firefox -p AdobeFlash­Profile

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 there are a lot of GUI programs for GNU/Linux, there are several times more command line utilities written for it and its predecesso­r, UNIX. The Linux userland abounds with command-line solutions for situations 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 Preference­s’. I was able to configure my near-vintage Pinnacle TV tuner using this program. If ‘Network Manager’ failed to recognise your wireless modem, then wvdial 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 notificati­ons from my bash 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

[1] http://www.linuxcomma­nd.org/learning_the_shell. php‘Learning the shell’

[2] http://tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html ‘BASH Programmin­g - Introducti­on HOW-TO’

[3]http://www.ibm.com/developerw­orks/linux/library/l-tipprompt/ ‘Tip: Prompt magic’

 ??  ?? Figure 1: Optimised and colour-coded GNOME Terminal
Figure 1: Optimised and colour-coded GNOME Terminal
 ??  ??

Newspapers in English

Newspapers from India