Tips to be more efficient in a Linux Terminal

Linux Tips and Tricks

The life of Linux users became a lot easier with the advent of X Environments (Graphical User Interfaces). Many tasks can now be performed very quickly in these environments using simple clicks.

Does this mean that there is no need for a command-line interface anymore or to remember text commands and their parameters?

No, the terminal is the bread & butter of any Linux user worth his salt.

In this tutorial, you will learn a few tips and tricks to work efficiently with Linux commands.

Use Command-Line History

Instead of typing the same commands again and again, you can use the history command.

By scrolling with the [Up arrow] and [Down arrow] keys, you can find previously typed commands. By default, up to 500 commands can be stored in the bash command line history file.

The command line history is stored in a file called .bash_history in your login folder.

history

1 clear
2 ping google.com
3 ping yahoo.com
4 cd /var/log
5 cd /root/
6 clear
7 history
8 clear

Use Command-Line Completion

Another time-saving tool is the command completion feature.

If you type part of a file, command, or pathname and then press the [Tab] key, bash will either automatically complete the remaining portion of the file/path, or you will hear a beep sound.

The beep here represents that there are more than one files to display. In this case, just press the [Tab] key again. As a result, you will get a list of all those files or paths that match with what you have typed.  

How to use the [Tab] key in the bash terminal

While working with Linux commands, the [Tab] key is very handy. You can use the [Tab] key in the following four ways to get different outputs.

1. If the [Tab] key is preceded by a dollar sign ($), it will show all environment variables:

$[Tab]
$_                $GROUPS           $OSTYPE
$API_AUTH         $HISTCMD          $PATH
$API_URL          $HISTCONTROL      $PIPESTATUS
$BASH             $HISTFILE         $PPID
$BASH_ALIASES     $HISTFILESIZE     $PS1
$BASH_ARGC        $HISTSIZE         $PS2
$BASH_ARGV        $HOME             $PS4
$BASH_CMDS        $HOSTNAME         $PWD
$BASH_COMMAND     $HOSTTYPE         $RANDOM
$BASH_LINENO      $IFS              $SECONDS
$BASHOPTS         $LANG             $SHELL
$BASHPID          $LESSCLOSE        $SHELLOPTS
$BASH_SOURCE      $LESSOPEN         $SHLVL
$BASH_SUBSHELL    $LINENO           $SSH_CLIENT
$BASH_VERSINFO    $LINES            $SSH_CONNECTION
$BASH_VERSION     $LOGNAME          $SSH_TTY
$COLUMNS          $LS_COLORS        $TERM
$COMP_WORDBREAKS  $MACHTYPE         $UID
$DIRSTACK         $MAIL             $USER
$ENV_CATEGORY     $MAILCHECK        $XDG_RUNTIME_DIR
$ENV_DOMAIN       $OLDPWD           $XDG_SESSION_ID
$ENV_IP           $OPTERR
$EUID             $OPTIND

2. If it is preceded by a tilde sign ~, it will show all user names:

~[tab]
~_apt              ~mail/             ~sync/
~backup/           ~man/              ~sys/
~bin/              ~messagebus/       ~syslog
~bind/             ~news              ~systemd-network/
~daemon/           ~nobody            ~systemd-resolve/
~games/            ~proxy/            ~systemd-timesync/
~gnats             ~root/             ~uucp
~irc               ~sshd/             ~www-data/
~list              ~strongswan/
~lp                ~stunnel4/

3. If preceded by a letter, the [Tab] key will show all commands starting with that letter:

t[Tab]
tabs           tcpdump        tload          ts
tac            tee            toe            tset
tail           telinit        top            tsig-keygen
tar            tempfile       touch          tsort
tarcat         test           tpm_extendpcr  tty
tasksel        then           tput           tune2fs
taskset        tic            tr             tw_cli
tbl            time           trap           type
tc             timedatectl    troff          typeset
tcpd           timeout        true           tzconfig
tcpdchk        times          truncate       tzselect
tcpdmatch      tipc           try-from

4. If preceded by a @, it will show you the hostname:

@[Tab]
@::1                         @ip6-localhost
@ff02::1                     @ip6-loopback
@ff02::2                     @localhost
@ip6-allnodes                @ns3040947
@ip6-allrouters              @ns3040947.ip-37-187-102.eu

Execute Multiple Commands in Succession

Linux allows you to enter multiple commands at one time. To do this you can use a semicolon to separate the commands.

Let’s take an example.

Suppose you have downloaded a file called foobar-1.2-.2.i286.rpm, and you want to put it in a sub-folder named rpms within your home directory, but the sub-folder has not been created.

You can combine both the creation of the rpms folder and the moving of your downloaded file into the new folder in one line. For that you need to type the following at a shell prompt:

mkdir rpms/; mv foobar-1.2-2.i286.rpm rpms/; ls -l rpms/
total 0
-rw-r--r-- 1 root root 0 Oct 14 20:38 foobar-1.2-2.i286.rpm

Running the above combination of commands will first create the folder rpms, will move the file into this newly created folder, and will display the content of the newly created folder, in order to confirm that we have successfully moved the file.

Use Pipes and Pagers

In Linux, pipes connect the standard output of one command to the standard input of another command.

Consider the case when you issue the ls command and its output in the form of a long listing scroll by too quickly. To view the output one page (or screen) at a time, you can use the less command — a pager utility that allows you to view the output of any command page by page.

In other words, if you don’t want the output of the ls command to be displayed on the screen and scroll so quickly. Then, you can send this output to another command by using the pipe concept. For that, you have to use the vertical bar |, which represents the pipe between the commands.

ls -al /etc | less
-rw-r--r-- 1 root root 10368 Oct 2 2015 sensors3.conf
drwxr-xr-x 2 root root 4096 Sep 3 2018 sensors.d
-rw-r--r-- 1 root root 19183 Dec 26 2016 services
drwxr-xr-x 2 root root 4096 Sep 3 2018 sgml
-rw-r----- 1 root shadow 884 Sep 3 2018 shadow
-rw-r----- 1 root shadow 884 Sep 3 2018 shadow-
-rw-r--r-- 1 root root 89 Sep 3 2018 shells
drwxr-xr-x 2 root root 4096 Sep 3 2018 skel
-rw-r--r-- 1 root root 7056 Feb 6 2016 smartd.conf
drwxr-xr-x 4 root root 4096 May 16 2018 smartmontools
drwxr-xr-x 2 root root 4096 Sep 3 2018 ssh
drwxr-xr-x 4 root root 4096 Sep 3 2018 ssl
-rw-r--r-- 1 root root 453 Sep 3 2018 strongswan.conf
-rw-r--r-- 1 root root 281 Mar 20 2018 strongswan.conf.dpkg-dist
drwxr-xr-x 3 root root 4096 Sep 3 2018 strongswan.d
drwxr-xr-x 2 root root 4096 Sep 3 2018 stunnel
-rw-r--r-- 1 root root 0 Feb 28 2018 subgid
-rw-r--r-- 1 root root 0 Feb 28 2018 subuid
-r--r----- 1 root root 755 Jul 4 2017 sudoers
drwxr-xr-x 2 root root 4096 Sep 3 2018 sudoers.d
-rw-r--r-- 1 root root 2894 Sep 3 2018 sysctl.conf
-rw-r--r-- 1 root root 2295 Sep 3 2018 sysctl.conf.dpkg-old
drwxr-xr-x 2 root root 4096 Sep 3 2018 sysctl.d
drwxr-xr-x 5 root root 4096 Sep 3 2018 systemd
drwxr-xr-x 2 root root 4096 Sep 3 2018 terminfo
-rw-r--r-- 1 root root 13 Sep 3 2018 timezone
drwxr-xr-x 2 root root 4096 Sep 3 2018 tmpfiles.d
-rw-r--r-- 1 root root 1260 Mar 16 2016 ucf.conf
drwxr-xr-x 4 root root 4096 Sep 3 2018 udev
drwxr-xr-x 3 root root 4096 May 16 2018 ufw
drwxr-xr-x 3 root root 4096 Sep 3 2018 update-manager
drwxr-xr-x 2 root root 4096 Sep 3 2018 update-motd.d
drwxr-xr-x 2 root root 4096 Mar 2 2018 update-notifier
-rw-r--r-- 1 root root 10 Sep 3 2018 vconsole.conf
drwxr-xr-x 2 root root 4096 Sep 3 2018 vim
lrwxrwxrwx 1 root root 23 May 16 2018 vtrgb -> /etc/alternatives/vtrgb
-rw-r--r-- 1 root root 4942 May 8 2018 wgetrc
drwxr-xr-x 4 root root 4096 Sep 3 2018 X11
drwxr-xr-x 4 root root 4096 Sep 3 2018 xdg
drwxr-xr-x 2 root root 4096 Sep 3 2018 xml
(END)

By issuing the above command, you can view the contents of /etc one screen at a time. The following keys are used with the less command.

  • To move forward a screen, press [Space];
  • to move back a screen, press [B];
  • to quit, press [Q].

Alternatively, you can use the arrow keys to navigate with the less command.

To search the output of a text file using the less command, press / and then type the keyword you want to search for within the file. For example, to jump at the first occurrence of timezone:

/timezone

-rw-r--r--   1 root root      13 Sep  3  2018 timezone
drwxr-xr-x   2 root root    4096 Sep  3  2018 tmpfiles.d
-rw-r--r--   1 root root    1260 Mar 16  2016 ucf.conf
drwxr-xr-x   4 root root    4096 Sep  3  2018 udev
drwxr-xr-x   3 root root    4096 May 16  2018 ufw
drwxr-xr-x   3 root root    4096 Sep  3  2018 update-manager
drwxr-xr-x   2 root root    4096 Sep  3  2018 update-motd.d
drwxr-xr-x   2 root root    4096 Mar  2  2018 update-notifier
-rw-r--r--   1 root root      10 Sep  3  2018 vconsole.conf
drwxr-xr-x   2 root root    4096 Sep  3  2018 vim
lrwxrwxrwx   1 root root      23 May 16  2018 vtrgb -> /etc/alternatives/vtrgb
-rw-r--r--   1 root root    4942 May  8  2018 wgetrc
drwxr-xr-x   4 root root    4096 Sep  3  2018 X11
drwxr-xr-x   4 root root    4096 Sep  3  2018 xdg
drwxr-xr-x   2 root root    4096 Sep  3  2018 xml
~
~
~
~
~
~
~
~
/timezone

Let’s take another example where Pipe can be used. Suppose you need to get the PID of the MySQL process.

ps aux | grep mysql | awk '{print $2}'
22174

ps aux – lists all the processes running, this means multiple lines of output that will be sent as input to the next command.

grep mysql – using | it receives the output from the first command as input and discards everything except only the lines containing the string mysql

awk {print $2}  –  it receives the output from the grep command as input, prints the second string from that input.

The result of these 3 piped commands is the process id of our MySQL service.

For more examples of piping, you can study our detailed tutorial.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Compare Strings
Read More

Bash Compare Strings

Similar to other programming languages, strings in bash is the datatype that holds a sequence of characters. In…