Training/Linux - command line

From HPC
Jump to: navigation , search


This represents a series of teaching pages to allow you to learn more about Viper's Linux command-line interface

Command Line

Like other operating systems Linux does have a window’s type environment too called X and hopefully in the future its successor Wayland. (Linux refers to X and Wayland with the term Display Server). However, with VIPER the vast majority of work will be carried out on the command line.

The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style, there are a few different ones to choose from. However, the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

Below is an example of Viper's command-line interface using the terminal program MobaXterm.


Man pages

This will explain the use of man pages (also called manual pages) on Linux. Most Linux files and commands have pretty good man pages to explain their use. Type man followed by a command (for which you want help) and start reading. Press q to quit the man page. See below:

username@viper:~$ man whois  (shows manual page for the command whois)
username@viper:~$ man syslog.config  (shows the manual page for a configuration file)
username@viper:~$ man syslogd (show the manual for a daemon (background program))
username@viper:~$ man –k syslog  (an apropos which shows a list of available man pages with this string contained within it)

Working with directories

As with other operating systems such as Microsoft Windows the filesystem is based around files and directories. Linux is no exception to this and uses a number of commands for the user to navigate around its own filesystem.

This module is a brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux system.

This section will also discuss absolute and relative paths and path completion in the bash shell.


On the command line pwd (or print working directory) basically displays the current directory you are in. This would appear as:

username@viper:~$ pwd


On the command line cd (or change directory) changes your current directory to the one specified:

username@viper:~$ cd /var
username@viper:~$ pwd
username@viper:~$ cd /home/user
username@viper:~$ pwd

There is also a shortcut back to your home directory by typing the character ~ (tilda) which has the same effect as typing (for example) /home/user.

username@viper:~$ cd ~
username@viper:~$ pwd

To go to the directory above (or parent directory), we use the characters ..

username@viper:~$ pwd

username@viper:~$ cd ..
username@viper:~$ pwd

There is also the character. which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.

Absolute and relative paths

Icon pencil.png Although an important concept to understand the rule here is very simple. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point.

Here are two examples, firstly of the absolute path:

username@viper:~$ pwd

user@viper:~$ cd /var
user@viper:~$ pwd

And relative path

username@viper:~$ pwd

username@viper:~$ cd myfiles
username@viper:~$ pwd

The command line will help you in typing a path without errors. If you type a partial command such as:

username@viper:~$ cd /home/user/my

Pressing the TAB key will fill in the rest of the directory name, if that directory exists and it is unique). So the following would appear

username@viper:~$ cd /home/user/myfiles


This command lists the contents of a directory:

username@viper:~$ ls
myfile1.txt myfile2.txt mydirectory1

ls has a number of useful different options

username@viper:~$ ls –l  (show a long listing with more information)
username@viper:~$ ls –a  (show all files including those that are hidden)
username@viper:~$ ls –la  (combines both of the options above)


This commands makes a directory from the specified directory:

username@viper:~$ mkdir mydirectory1
username@viper:~$ cd mydirectory1
username@viper:~$ pwd


This command removes the specified directory, note the directory must be empty and must not be the directory you are currently in:

username@viper:~$ rmdir mydirectory1

Working with files

In this section we learn how to recognise, create, remove, copy and move files using commands like file, touch, rm, cp, mv and rename.

Just before we start looking at these commands we need to cover two important areas:

  • Files on Linux are case sensitive. This means that FILE1 is different from file1, and /etc/hosts are different from /etc/Hosts (the latter one does not exist on a typical Linux computer).
  • Everything on Linux is a file. A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1), and any process are all represented somewhere in the file system as a file.


This command determines the file type. Unlike Windows, Linux does not determine the file type from the extension but from examining the file header/contents itself.

username@viper:~$ file mypicture.png
pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced

username@viper:~$ file parallel.c
parallel.c: ASCII C program text


This creates an empty file, which can be useful for various uses.

username@viper:~$ touch newfile.c
username@viper:~$ ls


Remove a file, as always be very careful with this command and without a backup, this file will be lost forever.

username@viper:~$ ls
newfile.c backup.c
username@viper:~$ rm newfile.c
username@viper:~$ ls

username@viper:~$ rm –i backup.c   (this is interactive delete and will ask you to delete the file before it occurs)
username@viper:~$ rm –r mydirectory   (works recursively down the specified directory but not removing any non-empty directories)
username@viper:~$ rm –rf mydirectory   (works recursively down the specified directory however the –f option means force. This option is very powerful but also needs to be used with extreme care!)

As with many Linux commands there are a few options with can be used with rm (these can be view by typing man rm).


Copy files or directories from a source to a destination:

username@viper:~$ cp parallel.c mybackup.c  (copies parallel.c to mybackup.c)
username@viper:~$ cp parallel.c mydirectory1   (copies parallel.c to mydirectory1)
username@viper:~$ cp *c backupdirectory/  (copies all *.c files to backupdirectory)
username@viper:~$ cp –r mydirectory1 mydirectory2  (copies one directory to another, note the option –r for recursive copying)

As with many Linux commands there are a few options with can be used with cp (these can be view by typing man cp).


Move files from a source to a destination. A versatile command that can rename a file too:

username@viper:~$ mv file1.c testfile.c  (rename file1.c to testfile.c)
username@viper:~$ mv directory1 directory2  (rename directory)
username@viper:~$ mv file1.c /home/user/myrepo  (mv file1.c to /home/user/myrepo/file1.c)


Although preferably to use the mv command, this command does exist to rename files

username@viper:~$ touch file1.c file2.c file3.c
username@viper:~$ ls
file1.c file2.c file3.c

username@viper:~$ rename .c .backup *.c
username@viper:~$ ls
file1.backup file2.backup file3.backup

Working with file contents

This section will look at working with file contents themselves, such commands are head, tail, cat, tac, more and less.


By default, the head command will show the first ten lines of a file.

username@viper:~$ head /etc/passwd



Similar to head but this time it will show the last 10 lines of the file by default.

username@viper:~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp 		# binkp fidonet protocol
asp 27374/tcp 		# Address Search Protocol
asp 27374/udp
csync2 30865/tcp 		# cluster synchronization tool
dircproxy 57000/tcp 	# Detachable IRC Proxy
tfido 60177/tcp 		# fidonet EMSI over telnet
fido 60179/tcp 		# fidonet EMSI over TCP


The cat command (short for concatenate) one of the most universal tools, yet all it does is copy standard input to standard output. In combination with the shell, this can be very powerful and diverse. Some examples will give a glimpse into the possibilities.

username@viper:~$ cat /etc/resolv.conf


username@viper:~$ cat file1.c file2.c >file3.all  (concatenate c files into file3.all)


Works the same as cat but will show you the file backwards:

username@viper:~$ cat numbers

username@viper:~$ tac numbers


The more command is useful for displaying files that take up more than one screen. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit. Some people prefer the less command to more.


Very similar to more but with some additional features

Basic Linux Tools

This chapter introduces commands to find or locate files and to compress files, together with other common tools that were not discussed before.


This command is very useful to find files, more options are provided on the command line by typing man find. Here are some useful examples below:

username@viper:~$ find /etc   (find all files in the /etc directory)
username@viper:~$ find . –name “*.conf”   (find all files that end in .conf from the current directory)
username@viper:~$ find . –newer file1.c   (find all files newer than file1.c)
username@viper:~$ find /etc >etcfiles.txt   (find all files but this time put them in (pipe) to the file etcfiles.txt)


The locate command is very different from find in that it uses an index to locate files. This is a lot faster than traversing all the directories, but it also means that it may be outdated too.


The date command can display the date, time, time zone, and more.

username@viper:~$ date
Sat Feb 23 12:44:30 BST 2017


The cal command displays the current month, with the current day highlighted.

username@viper:~$ cal
     April 2016
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


The sleep command is sometimes used in scripts to wait a number of seconds. This example shows a five-second sleep.

username@viper:~$ sleep 5  (five seconds later)


The command sort will sort lines of text files. By default, the output is to the screen but this can be piped to a file or another program.

username@viper:~$ sort myfile.txt


The time command can display how long it takes to execute a command. The date command takes only a little time.

username@viper:~$ time date
Sat Feb 23 13:08:27 BST 2016
real 0m0.014s
user 0m0.008s
sys 0m0.006s

gzip – gunzip

This is a useful compression program (like zip which also exists in Linux). The gzip command can make files take up less space.

username@viper:~$ gzip myfile.c   (will create myfile.c.gz)
username@viper:~$ gunzip myfile.c.gz   (will create myfile.c again)

bzip2 – bunzip2

Files can also be compressed with bzip2 which takes a little more time than gzip, but compresses better.

username@viper:~$ bzip2 myfile.c  (will create myfile.c.bz2)
username@viper:~$ bunzip2 myfile.c.bz2  (will create myfile.c again)

zip – unzip

A compression program which is compatible with other zip programs found in MS Windows and other OSes.

username@viper:~$ zip myfile.c   (will create
username@viper:~$ unzip   (will create myfile.c again)


The grep filter is famous among Linux (and UNIX) users. The most common use of grep is to filter lines of text containing (or not containing) a certain string.

username@viper:~$ grep “Darren” /etc/passwd 
user:x:1000:1000:Darren Bird,,,:/home/user:/bin/bash

As with most Linux commands, there are also a large number of useful options that will go with each command and grep is certainly no exception here

username@viper:~$ grep –i “Darren” /etc/passwd  (search in a case insensitive way)
username@viper:~$ grep –r “Darren” /etc/passwd  (search recursively down any directories too)
username@viper:~$ grep –v “Darren” /etc/passwd  (search for everything not containing “Darren”)


Counting words, lines, and characters are easy with wc.

username@viper:~$ wc myfile.c  (show number of words, lines, and characters)
5 10 100 tennis.txt

File Permissions


Similar to many other operating systems Linux uses a method of access rights on files and directories. These can be view by using the ls command

username@viper:~$ ls –l   (option l is for long listing)

-rw-r--r--  1 dbird admin    12211 May 11  2016 template.countries
-rw-r--r--  1 dbird admin     3097 May 11  2016 template.languages    (1)
drwxr-x---  2 dbird admin     4096 Dec  1 10:54 Templates       (2)
-rw-r--r--  1 dbird admin     8087 Feb 12 14:05 temp.txt
-rwx------  1 dbird admin     1110 Mar  8 10:53    (3)

Each file and directory has access rights that are associated with each one. When we look at the 10 symbol string above on the left-hand side (e.g. drwxr-xr-x).

  • The first letter present whether the file is a directory or not.
  • The next three represent the file permission for the user that owns that file (i.e. dbird in this example).
  • The next three represent the file permission of the group to whom that user belongs (i.e. group admin).
  • The last three represent the file permissions for everyone else (i.e. all users).

For each of the permission parts the letters mean the following in their groups:

  • r indicates read permission to read and copy the file, its absence indicates this is not available.
  • w indicates write permission to write the file, its absence indicates this is not available.
  • x indicates execution permission to allow the file to be executed, its absence indicates this is not available.

Using the example above would mean:

  • Example (1) has read/write access for user dbird and read access only for everyone else.
  • Example (2) is a directory with full access for user dbird and read access for only users in the admin group.
  • Example (3) is an application which is only accessible by the user dbird, note not only is it read and write but it also has its ‘execution’ permission set for that user also.

Changing access rights

This command allows the user to change file (and directory) permissions.

  • u User
  • g Group
  • o Other
  • a All
  • r Read
  • w Write (and erase)
  • x Execution (and access directory
  • + Add permission
  • - Remove permission

username@viper:~$ chmod go-rwx myfile.c   (remove read, write and execute permissions removed for group and other)
username@viper:~$ chmod u+x   (make the program executable to the user (i.e. the owner of the file))

Further Information