2. File and Directory Handling
The pwd command shows the path to the
A folder can
be created by typing in mkdir dirName where
dirName is the name of the
The ls command lists all the files in the
home directory, except for the ones whose names start with a dot (.). These are
known as hidden files and usually contain important program configuration
ls is a command that can take options, which slightly
change the way a command executes. For example, the hidden files can be listed
by typing ls -a.
The cd command, followed by a pathname, is
used to access a different directory. The pathname is the route taken along the
branches of the tree to get to the desired directory. The pathname can be
specified in two different ways: relative pathnames or absolute pathnames.
pathnames start at the root of the directory and follows every branch of the
tree until it reaches its destination, while the relative pathnames start from
the current directory. To do this, it uses a couple of special notation to
represent the location in the tree: “.”
(the current directory) and “..”
(the parent of the current directory). If a pathname is not specified, the
working directory will be assumed.
provides special characters to make it easier and faster to specify and work
with groups of filenames. These special characters are named wildcards and they
allow the user to select filenames based on patterns of characters. Wildcards
can be used with any command that accepts filename arguments.
ls cyg???????? will list all the filenames starting
with “cyg” and followed by any other eight characters.
ls cyg*.exe will list all the filenames that
begin with “cyg” and end in “.exe”
ls ab* will list any filename that begins
with a or b followed by any other characters.
ls *!*.exe will list any filenames that don’t
end with “.exe”
The cp file1 file2 command makes a copy of
the file1 and renames it file2. If file2 already exists, it will be silently
overwritten. This can be avoided by typing cp
-i file1 file2 which changes the command’s behaviour to prompt the user
before it overwrites with contents from file1. To copy the contents of a
directory into another the user needs to type in cp -R dir1 dir2. If dir2 does not exist, it is created. Otherwise,
it creates a directory named dir1 inside the directory dir2.
cp *10* Project command will copy all the filenames
that contain the number 10 inside the Project directory.
The mv command can move or rename a file or
directory, depending on how it is used.
mv filename1 filename2 will change the name of filename1 to
mv filename1 filename2 filename3 dir1 will move the filenames in the dir1
mv * .. will move all the files from the
current directory into the parent directory.
mv Dir1 *dll Dir2 will move the Dir1 directory and all
the filenames ending in “dll” from the current folder into the Dir2 directory.
The rm command deletes files and
such as ls print their output on the
display. However, by using some special notation, the commands output can be
redirected to files, devices and to the input of other commands. Most command
line programs display the results to a tool called standard output. By default,
the standard output prints its content on the display. To redirect it, the user
needs to use the “>” character.
The ls > example.txt command
executes the ls command and instead of displaying the result in the console, it
saves everything in the example.txt file. If the example.txt file does not
exist, it will be created.
Each time this command is called, the text file is
overwritten with the new information. The “>>”
character can be used to avoid this and instead of overwriting the text file,
it adds the new results to the end of the file.
commands can accept input from a tool called standard input. By default,
standard input gets its contents from the keyboard, but it can be redirected
just like the standard output. To do this, the user must use the “<" character. The sort -r < example.txt command takes all the information from the example.txt file and sorts it in reverse alphabetical order then outputs it on the display, leaving the text file intact. By adding a standard output to the command, the results can be saved in a new file. One of the most useful and powerful thing that can be done with I/O redirection is to connect multiple commands with pipelines. By using pipelines, the standard output of a command is fed to the standard input of another. The du | sort -nr command displays a list of directories and how much space they consume, sorted from the largest to the smallest. The command ls | grep a >
text.txt finds all the files and directories in the current directory that
contain the letter “a” and stores the output in the text.txt file.
The ls -l |less command outputs the content
of the current folder in a long format.
On a Unix system, files and
directories are allocated access right for the owner, the other users in the group,
and everyone else. These rights can be to read, write or execute a file. The
permission’s structure consist of a character specifying the file type,
proceeded by three sets of three characters each that indicate the permission to
read, write and execute for the owner, group members and other users.
In order to change a file’s
permissions, the user can use the chmod
command, followed by the specifications of the desired modifcations and the
The Unix terminal contains a couple of
commands for handling processes. These commands can display, kill or change the
priority of the running processes. There are two types of processes: Foreground
Processes, which run on the screen and need user input (Text editor, web
browser) and Background Processes, which run in the background and normally do
not need input from the user (Antivirus, Virtual Drives).
To run a process in the background, the
commad requires a & symbol at
the end. Without the & symbol,
the process is ran in the foreground and the user can do nothing but wait until
the command propt is returned. Background processes are useful for jobs which
take a long time to complete.
The currently running processes can be
displayed by using the ps (process
A process can be ended in several ways. If
it is running in the foreground mode, from a console, the CTRL+C keys pressed simultanously will exit the command. However,
if it is running in the background mode, the user will need to enter the kill command followed by the process ID
or job number. The kill command’s
real purpose is to send a signal towards the process. Programs wait for signals
from the operating system and respond to them, usually to allow the process to
terminate in a smooth manner.
If a process is running in the foreground
and it needs to be sent in the background, it can be suspended by holding down
CTRL+Z then sent back using the bg
command. However, it does not work for Cygwin on Windows.
To bring a process from the background in
the foreground the user must use the fg
The jobs command lists suspended and
%jobnumber command is used to restart a suspended process.
4. File system (disk) management and
system is a logical collection of files on a partition or disk. Unix uses a
hierarchical file system structure, like an upside-down tree with the /root at
the base of the file system and the other files and directories spreading from
The df (disk free) command displays
the disk space usage. Adding the -h
extension makes the output easier to understand.
A file system must be mounted in order to be used by the system. The
currently mounted file systems can be listed by using the mount command.
5. CLIs and Scripting
script is a list of lines in an executable file that contain shell commands.
They are useful because by using them, the user can execute multiple commands
by only calling one.
arguments in a script:
Simple for loop:
commands inside a script:
William Shotts, J.
(2018). LinuxCommand.org: Learn the Linux command line. online
Linuxcommand.org. Available at: http://linuxcommand.org/ Accessed 20 Jan.
Shotts, W. (2016). The Linux
command line. 3rd ed. San Francisco: No Starch Press.
(2018). UNIX / Linux Tutorial for Beginners. online Available at:
http://www.ee.surrey.ac.uk/Teaching/Unix/ Accessed 21 Jan. 2018.