Working on the command line - part 2
The first commands
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
Listing files: ls
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.
Changing directory: cd
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
.
Make a directory: mkdir
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.
Copying: cp
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
Removing a directory: rmdir
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
Removing files and/or directories: rm
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 thepepsin.dat
this could be used likepepsi?.dat
, where all files starting withpepsi
and then a single character, followed by.dat
will match. So:pepsin.dat
would match, but alsopepsic.dat
.*
will match any series of characters, including nothing. For examplepepsin*.dat
would match bothpepsin.dat
andpepsin_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
Moving/renaming files or directories: mv
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
Software carpentries
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