Basic Linux Commands

Basic linux commands are introduced for non Linux users. Commands are the most staightforward way for a user to interact with the Linux operating system (OS). Linux OS gives the user application access to computer resources (CPU, memory, disks, devices, etc) through a  file-system. A Linux Shell is a commonly used interface that enables users to access the OS services. Shells are usually available through a terminal, SSH (secure shell) connection, or else. After opening a terminal, or after establishing a SSH connection, the user can enter commands into the shell prompt line. This quick help provides a list of usage examples of some basic commands. In the following example code, lines starting with the dollar symbol $ (also called shell prompt) indicate commands to be entered.

Topics and commands introduced in this tutorial:

Get Help Manage files & directories Manage processes Check system resources usage
-h, --help, -help pwd, cd ps top, htop
man ls, ls -a, ls -l, ls -lh top, htop free -m
info cp, mv, rm, rm -r, rmdir kill, kill -9 df, du
  cat, more, less, head, tail    
  nano, vi, emacs    

1. Get Help

Commands are case-sensitive words made of symbols and alphabetical letters (examples: hmac256, ssh-copy-id) that allow the user to run programs or to perform shell operations. Commands may have parameters and arguments that control the operations to be made. A command or a combination of  commands with or without parameters is called a command line. There are different methods to obtain information about a given command. Three common methods are

1.1 From the command itself

Depending of the command, usage information may be obtained by using one of the -h, --help, or -help parameters, or by entering the command without parameters. For example help information from the command bash

 $ bash --help
1.2 From the manual command man,

Example to show the manual page for the command find

 $ man find
1.3 From the info command info
 $ info

The methods above can be used to obtain more details and examples about the usage of the commands that will appear in the remaining of the tutorial.

2. Manage files and directories

After logging into the cluster or after opening a terminal the user is located into the working directory. Operations like listing, modification, editing, execution, and so forth can be performed to any file in the filesystem that the user has the permissions to access.

2.1 Navigating into directories

The command pwd may be used to obtain the value of the working directory, For example

 $ pwd
 /home/s/someuser

The output of the pwd command is a directory full pathname. Any file or directory in the filesystem has a full pathname which starts with the backslash symbol /. The command cd is used to change into a different existing directory. For example

 $ cd /apps
 $ pwd
 /apps
2.2 List files and directories

The command ls is used to print the list of the files and directories in the working directory. Without any parameters the command print a multicolumn list

 $ ls

Files or directories names can be added to the command to print list of files and list the contents of directories. For example as following where the contents of the  system root  directory (/) is listed

 $ ls /
 apps    cgroup  gridware  lib64       mnt   root     srv   usr
 bin     dev     home      lost+found  net   sbin     sys   var
 boot    etc     imports   media       opt   scratch  tmp   work
 bucket  genefs  lib       misc        proc  selinux  unit

Parameters may be added to produce different printing output with more ore less information. For example, the parameter -F allows to differentiate between files types and directories in a mutlicolumn listing

 $ ls -F

the parameter -a shows hidden files (hidden files start with a dot symbol, like .bashrc)

 $ ls -a

the parameter -l print one file per line with detailed file information (creation date, permissions, file size, etc)

 $ ls -l

Parameter can be concatenated to combine their actions. For example adding the parameter -h to the parameter -l makes the command ls outputs the size of the files using k, M, and G suffixes, for kilobytes, megabytes and gigabytes respectively

 $ ls -lh
2.3 Copy, delete, rename and move

A given file, say source file,  can be copied to another file, say destination file using the command cp. The operation is a duplication if the destination file does not exist, or a overwriting if the destination file exist. For example source file, fileS, is copied to destination file, fileD, by doing

 $ cp fileS fileD

Similarly a file can be renamed (if the destination file does not exist) or moved (if the destination file is a directory name) by using the command mv. For example

 $ mv fileS fileD

or

 $ mv fileS /tmp

The command rm deletes a given file

 $ rm fileS 

The same command may be used with the option -r to deletes all the files in a directory, directories, sub-directories and files in the sub-directories. For example

 $ rm -r dirA

An empty directory can be deleted using the command rmdir, like

 $ rmdir dirA
2.4 Show contents of a file

The whole content of a text file can be output to the terminal screen with the command cat. The user needs to use the mouse to scroll into the text. Apply this command with a binary file may have unpredictable results

 $ cat .bashrc

The commands, more, less, head, and tail help to make more or less sense of the text file content. more will output the file content into the terminal screen and will allow you to scroll forward and backward using the space key and key b, respectively, until the end of the file is reached. For example

 $ more /usr/share/X11/rgb.txt

more allows for example, to make string search using the backslash /. The command less is similar to more, but it has many more features. On of these features is being faster than more for scrolling into big text files. Example

 $ less /usr/share/X11/rgb.txt

The command head displays the first line of a text file or list of text files. Without parameters head outputs only the first 10 lines of the file,

 $ head /usr/share/X11/rgb.txt

a parameters can be used to print more or less than 10 lines of the file

 $ head -5 /proc/meminfo
 MemTotal:       49409336 kB
 MemFree:        14880700 kB
 Buffers:          379124 kB
 Cached:         30606776 kB
 SwapCached:         8500 kB

The command tail is similar to the command head but displays the last lines of a file

 $ tail /usr/share/X11/rgb.txt
 $ tail -24 /usr/share/X11/rgb.txt
2.5 Edit a file

There exist many text file editors for terminal that can be more or less easy to learn and to use. An easy to use and to learn simple text file editor is nano

 $ nano /etc/nanorc

Other more sophisticated but very popular text file editor is vi and its modern improvment vim

 $ vi /etc/vimrc
 $ vim /etc/vimrc
2.6 Special directories names

The symbol / has two meanings, the system root directory when it is at the beginning of a full pathname, or directory name separator in a directory or file pathname. Beside the symbol / there are the symbols ~, ., and .. that represent the user home directory, working directory, and the parent of the working directory, respectively.
List the current working directory

 $ ls .

List the parent directory

 $ ls ..

List the user home directory

 $ ls ~

List the parent directory of the user directory

 $ ls ~/..

3. Manage processes

A process is a running program or application that performs operations using the system resources. Each process is identified by a unique process ID (PID).

3.1 Show running processes

The command top allows to monitor the running processes. Press the key q to quit the monitoring. To have help about how to control the monitoring parameters and output, press the key h.

 $ top

 Tasks: 601 total,   2 running, 573 sleeping,  26 stopped,   0 zombie
 Cpu(s):  0.1%us,  0.2%sy,  0.0%ni, 99.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
 Mem:  49409336k total, 40975628k used,  8433708k free,   370372k buffers
 Swap:  4194296k total,   106720k used,  4087576k free, 38039904k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
 14470 etaillef  20   0 26328 1860 1144 R  1.0  0.0   0:00.08 top                
  1874 root      20   0     0    0    0 S  0.3  0.0   0:49.98 flush-253:0        
     1 root      20   0 21436 1056  844 S  0.0  0.0   6:44.42 init               
     2 root      20   0     0    0    0 S  0.0  0.0   0:22.93 kthreadd           
     3 root      RT   0     0    0    0 S  0.0  0.0   0:39.45 migration/0        
  .....

The command ps shows the processes running on a given Linux operating system. For example, to show all the running process owned by the current user:

 $ ps wux

 USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
 1370       776  0.0  0.0 124368  7052 pts/43   Ss+  Sep09   0:00 -bash
 1370     17167 10.0  0.0 121140  1376 pts/33   R+   17:59   0:00 ps wux
 1370     29903  0.0  0.0 114048  4960 ?        S    Sep09   0:32 sshd:  etaillefer@pts/41,pts/43,pts/33
 1370     29904  0.0  0.0 124368  7032 pts/41   Ss   Sep09   0:00 -bash
 1370     31016  0.0  0.0 124372  7072 pts/33   Ss   Sep10   0:01 -bash

and to show all processes running on the system

 $ ps waux
 USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
 root         1  0.0  0.0  21436  1056 ?        Ss   Jun22   6:45 /sbin/init
 root         2  0.0  0.0      0     0 ?        S    Jun22   0:23 [kthreadd]
  .....
 fujie    30391  0.0  0.0 138152  3996 pts/6    Ss+  Sep08   0:00 -zsh
 4538     30418  0.0  0.0 119592  2376 pts/13   Ss+  09:20   0:01 -bash
 nrpe     30742  0.0  0.0  43400   912 ?        Ss   Jun25   7:13  /usr/sbin/nrpe -c /etc/nagios/nrpe.cfg -d
 1370     31016  0.0  0.0 124372  7072 pts/33   Ss   Sep10   0:01 -bash
 1053     31272  0.0  0.0 100940   604 pts/4    T    Aug29   0:00 tail -f DCN.joboutputs
 satoshi  31495  0.0  0.0  69720  3736 pts/31   S+   Sep02   0:02 ssh -Y tombo-login1.oist.jp
  .....
3.2 Stop a process

A process can be stopped  by using the kill command with a list of process PID:

 $ kill 29903 29904

A SIGKILL signal (-9) can also be sent to processes that refuse to stop:

 $ kill -9 29903

4. Check system resources usage

4.1 CPU usage

The CPU usage can be monitored using the column %CPU from the top command. htop is another CPU usage monitor with nice printing.

4.2 Memory usage

The amount of free and used memory is obtained with free. Usage amount can be output in bytes, kilobytes, megabytes or gigabytes using the parameters-b, -k, -m, and -g, respectively:

 $ free -m
              total       used       free     shared    buffers     cached
 Mem:       1033944     554314     479630          0        365     541146
 -/+ buffers/cache:      12802    1021142
 Swap:         4095          0       4095

The columns VIRT, RES, and %MEM in the top command can also be used to monitor the memory usage of a given process.

4.3 Disk usage

A given file or given directory is located into a filesystem which resides into a partition. Partition can be a part of a segregated storage device, the aggregation of several storage devices, or a remote storage system provided by some network protocol. A given partition is attached to a directory full pathname, this pathname is called mounted point. The command df prints the usage information about all the mounted partitions living in the system. Example

 $ df -h
 Filesystem                           Size  Used Avail Use% Mounted on
 /dev/mapper/vg_system-slash           30G  7.3G   21G  26% /
 tmpfs                                 24G   84K   24G   1% /dev/shm
 /dev/sda1                            2.0G   98M  1.8G   6% /boot
 /dev/mapper/vg_system-scratch        220G  317M  208G   1% /scratch
 /dev/mapper/vg_system-var_log         20G  278M   19G   2% /var/log
 ddnsfa10ke-4:/genefs                 599T  492T  107T  83% /genefs
 homes.oist.jp:/vol/home              8.0T  4.0T  4.1T  50% /imports/home
 tombo-mds1@tcp:tombo-mds2@tcp:/work  393T  328T   62T  85% /work
 kago.oist.jp:/vol/hpc_software       1.6T  1.4T  197G  88% /imports/hpc_software
 bucket.oist.jp:/mabuya/bucket        1.2P  236T  940T  21% /imports/bucket
 web-home.oist.jp:/vol/web_home       820G   97G  723G  12% /imports/web_home

One can also inspect the usage information of a partition to which a given directory belongs to. For example to obtain the usage information of the partition holding the directory /home/e

 $ df -h /home/e
 Filesystem               Size  Used Avail Use% Mounted on
 homes.oist.jp:/vol/home  8.0T  4.0T  4.1T  50% /imports/home
4.4 File/directory space usage

The space taken by directories and files can be obtain using the command du. For example, to obtain the amount of space used by the directory /apps/src

 $ du -shc /apps/src
 5.5G    /apps/src
 5.5G    total