Advance Command Line 1: Pre Laboratory
Advance Command Line 1: Pre Laboratory
Advance Command Line 1: Pre Laboratory
Pre Laboratory:
1. What is the difference between absolute and relative path?
2. What is the main function of piping?
3. What is the main function of redirection?
4. For searching files and directories, we use find and locate command? Which is the best in
linux?
5. What is the different between more and less command?
Objective:
1. To be familiar with the types of paths of files and directories in Linux and distinguish
between them.
2. Understand the concept of piping and how to perform it.
3. Learn some basic commands in Unix/Linux including Bash History Commands.
4. Learn how to view the content of the files.
5. Learn how to search for commands and files/directories in Unix/Linux.
6. Learn how to search for words or text in files.
Part 1: Paths.
A path is a unique location to a file or a folder in a file system of an OS.A path to a file
is a combination of / and alpha-numeric characters.
Absolute Path:
An absolute path is defined as the specifying the location of a file or directory from the
root directory (/).In other words, we can say that an absolute path is a complete path from
start of actual file system from / directory.
Relative path:
Relative path is defined as the path related to the present working directly (pwd). It starts
at your current directory and never starts with a /.
Example:
Linux noorkmail
Desktop Desktop
folder1
2|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
Redirection is a feature in Linux such that when executing a command, you can change the
standard input/output devices. The basic workflow of any Linux command is that it takes an input
and give an output.
With redirection, the above standard input/output can be changed with the < and > operators
respectively.
Output Redirection:
Example:
The output of command ls -al is re-directed to file "fileX” instead of your screen.
Cat command is used to display the contents of the file.
Use the correct file name while redirecting command output to a file. If there is an
existing file with the same name, the redirected command will delete the contents of
that file and then it may be overwritten.
If you do not want a file to be overwritten but want to add more content to an existing file, then
you should use '>>' operator.
Example:
3|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
Input redirection:
Example:
SORT command is used to sort a file, arranging the records in a particular order. By
default, the sort command sorts file assuming the contents are ASCII. Using options
in sort command, it can also be used to sort numerically.
A pipe is a form of redirection (transfer of standard output to some other destination) that is used
in Linux and other Unix-like operating systems to send the output of one
command/program/process to another command/program/process for further processing. The
Unix/Linux systems allow standard output of a command to be connected to standard input of
another command. You can make it do so by using the pipe character ‘|’.
4|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
Example:
Pipe the output of command ‘cd Desktop’ to the input of command ‘ls -l’
Commands:
Print History using history command which shows a list of the commands entered since
you started the session. Commands are numbered, with older commands at the top and
newer commands at the bottom.
5|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
Print n Lines, while the default is to print all history lines, you can specify a number after
the history command to output this amount of the most recent lines.
Repeat most recent command, the most recent command can be executed by entering
‘!!’.
Repeat specific command, the bash history command displays line numbers. It is
possible to repeat a command by specifying its line number.
6|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
Commands:
The problem with cat command is that it displays the text on the screen. Imagine if you
use cat command with a file that has 2000 lines. Your entire screen will be flooded with
the 200 lines and that’s not the ideal situation.
You can use cat command with options, use man or --help commands to see these options.
less command: views the file one page at a time. The best thing is that you exit less (by
pressing q), there are no lines displayed on the screen. Your terminal remains clean.
8|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
More command: is used to view the text files in the command prompt, displaying one
screen at a time in case the file is large.
9|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
head command: is another way of viewing text file but with a slight difference. The head
command displays the first 10 lines of a text file by default.
You can change this behavior by using options with head command.
head -2 /var/log/messages displays the first two lines in the file /var/log/messages.
tail command: is similar and yet opposite to the head command. While head command
displays file from the beginning, the tail command displays file from the end.
10 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
You can change this behavior by using options with tail command.
tail -3 /var/log/messages displays the last 3 lines from the file /var/log/messages.
11 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
The grep command consists of three parts in its most basic form. The first part
starts with grep, followed by the pattern that you are searching for. After the string
comes the file name that the grep searches through.
12 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
Option Description
-name filename Base of file name.
-size –n/+n/n File uses less than, more than or exactly n units of space,
rounding up. The following suffixes can be used:
`b' for 512-byte blocks (this is the default if no suffix is
used)
`c' for bytes
`w' for two-byte words
`k' for kibibytes
`M' for mebibytes
`G' for gibibytes
-perm mode File's permission bits are exactly mode (octal or
symbolic).
-type c File is of type c:
b:block (buffered) special
c:character (unbuffered) special
d:directory
f:regular file
l:symbolic link
13 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
locate command: is used to find files by their filename. The locate command is
lightning fast because there is a background process that runs on your system that
continuously finds new files and stores them in a database. When you use
14 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...
the locate command, it then searches that database for the filename instead of
searching your filesystem while you wait (which is what the find command does).
15 | P a g e