Working on the command line - part 2

A good introduction on how to work on the command line is available at the software carpentries website. The tutorial on using the command line can be found at: http://swcarpentry.github.io/shell-novice/

Since it is not useful to repeat all of this here, we will just give a very brief overview of some basic commands. Note that you can also try working with these commands in the exercises we have set up for our basic course: Exercises basic course

The command ls gives an overview of files in the current directory. E.g.:

username@habrok:~ ls
adfinput                 java.log.31087  ondemand
ado                      java.log.37373  openstack

With the option -l you can obtain more information on the files:

username@habrok:~ ls -l 
total 719852
-rwx------  1 username group      1708 17 sep  2018 adfinput
drwxr-xr-x  3 username group      4096 25 jan  2018 ado
drwx------  2 username group      4096 18 jul  2019 alma
drwx------  2 username group      4096 18 sep  2018 boumantsje
drwx------  3 username group      4096 17 mrt 11:35 build
drwxrwxr-x  8 username group      4096 19 dec 16:31 coursera

The information shown is listing permissions, ownership, file size and modification date.

Extended explanation of the output

Extended explanation of the output

For the curious people, the other things in the listing are described below. We won't go into more details, as those can be found online.

  1. total: number of 1024 byte blocks in the file system for the listed items.
  2. -rw-r–r–: file type, followed by permissions. The first '-' means it is a normal file. 'd' would be for directories and 'l' for links.
    This is followed by three groups of 'r', 'w' or 'x', each of which may appear or not ('-'). The 'r' means read permissions, the 'w' means write permissions. The 'x' means execute permissions for files or “change into” permissions for a directory.
    There are three groups of these. The first gives the permission for the owner, the second for the group, the last for anybody else on the system. It is possible to adjust these permissions.
  3. The next number is the number of hard links to a file.
  4. The first (user)name after that is the owner of the file, which is a user on the system.
  5. The second (group)name after that is for the group of the file. Since each user has a private group, with only that user in it, most files will have the same name appearing for both owner and group. If you are a member of a different group, you may see another group there, which has more than one user. It is possible for the owner of a file to change the group of a file.
  6. Next is the file size in bytes.
  7. Modification date and time are shown.
  8. File- or directoryname.

Changing into a directory can be done using the command cd:

username@habrok:~ cd ondemand
username@habrok:ondemand ls

This command will change into the directory ondemand. As you can see the current location is shown in the prompt. The ~ is a shortcut for the home directory. After the cd command it has changed into ondemand.

A new directory can be created using the command mkdir:

username@habrok:ondemand mkdir enzymes
username@habrok:ondemand 

As you can see the command does not return any output, unless there was an error.

Files can be copies using the command cp. The command takes source(s) and destination as arguments. The simplest version is:

username@habrok:enzymes cp pepsin.dat pepsin_copy.dat
username@habrok:enzymes 

Here source and destination are a file. The destination can also be a directory. If no filename is specified the file is copied into this directory, using the same filename as before. In this case we copy the file into the directory input:

username@habrok:enzymes mkdir input
username@habrok:enzymes cp pepsin.dat input
username@habrok:enzymes ls input
pepsin.dat
username@habrok:enzymes 

For the source multiple files can be specified as an argument. If multiple files are to be copied the destination has to be a directory, otherwise you will get an error. E.g:

username@habrok:enzymes cp pepsin.dat pepsin_copy.dat input
username@habrok:enzymes 

Directories can also be copied using cp. But then the option -r has to be specified, which stands for recursive:

username@habrok:enzymes cp input input.copy
cp: omitting directory ‘input’
username@habrok:enzymes cp -r input input.copy
username@habrok:enzymes ls input.copy/
pepsin_copy.dat  pepsin.dat
username@habrok:enzymes 

Empty directories can be removed using rmdir. E.g.:

username@habrok:enzymes mkdir amylase
username@habrok:enzymes rmdir amylase
username@habrok:enzymes 

If a directory is not empty, the command will fail:

username@habrok:enzymes rmdir input
rmdir: failed to remove ‘input’: Directory not empty

Files and/or directories can be removed using rm:

username@habrok:enzymes rm pepsin.dat
username@habrok:enzymes 

Multiple files or directories can be given as an argument to rm:

username@habrok:enzymes rm pepsin.dat pepsin_copy.dat 
username@habrok:enzymes 

In cases like these wildcards can be useful. The wildcards are ? and *.

  • ? is a wildcard matching a single character. For the pepsin.dat this could be used like pepsi?.dat, where all files starting with pepsi and then a single character, followed by .dat will match. So: pepsin.dat would match, but also pepsic.dat.
  • * will match any series of characters, including nothing. For example pepsin*.dat would match both pepsin.dat and pepsin_copy.dat. The first match is for no characers at all, while the second matches with _copy.

When trying to remove directories the option -r for recursive has to be used, which will recurse through the directory and remove anything it finds:

username@habrok:enzymes rm input
rm: cannot remove ‘input’: Is a directory
username@habrok:enzymes rm -r input
username@habrok:enzymes 

Files can be renamed or moved (which is the same operation) using mv. It works in a similar fashion as cp, with respect to sources and destination.

username@habrok:enzymes mv  pepsin.dat pepsin2.dat
username@habrok:enzymes mv pepsin2.dat pepsin_copy.dat input
username@habrok:enzymes mv pepsin* input

For a more extensive and useful introduction into the command line interface we would like to refer you to: http://swcarpentry.github.io/shell-novice/

Next section: Viewing and editing files