Advance Command Line 1: Pre Laboratory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER

SYSTEM ENGINEERING - Linux Lab


EXPERIMENTE #3 Advance command Line 1

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.

To write an absolute path-name:


 Start at the root directory (/) and work down.
 Write a slash (/) after every directory name (last one is optional).

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 /.

Dr. Allam Abumwais


ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...

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...

Part 2: Piping and Redirection:

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.

 The standard input (stdin) device is the keyboard.


 The standard output (stdout) device is the screen.

With redirection, the above standard input/output can be changed with the < and > operators
respectively.

Output Redirection:

The '>' symbol is used for output (STDOUT) 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:

The '<' symbol is used for input (STDIN) 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’

Part 3: Bash History Commands.


The history is stored in the ~/. bash_history file by default. You could also run 'cat ~/.
bash_history' which is similar but does not include the line numbers or formatting. As follows:

Location of the history file:

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...

 Repeat specific command from the bottom.

 Clear history file with the -c command.

Part 4: View the content of the file.


7|Page
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...

Commands:

 cat command: This is the simplest command to view a file in Linux.

cat simply prints the content of the file.

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.

After pressing enter:

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.

By default, tail command displays the last 10 lines of a file.

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.

 Part 4: Search in Unix/Linux.


Commands:
o Search for commands :
whatis command: is used to get a one-line manual page descriptions.
whereis command: is used to find the location of source/binary file of
a command .

11 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...

o Search for char/words:


grep is a Linux /Unix command-line tool used to search for a string of characters
in a specified file.

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.

o Search for files/directories:


find command: searches through files in the file system in real time. The search
can be by filename, size, type or permission.

find [where to start searching from] [-options] [what to find]

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

To find a file by filename in the path (/):

13 | P a g e
ARAB AMERICAN UNIVERSITY – FACULTY OF ENGINEERING & INFORMATION TECHNOLOGY – COMPUTER
SYSTEM ENGINEERING - Linux Lab
EXPERIMENTE #4 Linux Command line Cont...

To find the symbolic files in (/):

To find files using their permission in (/):

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).

The database must be updated before using the locate command.

15 | P a g e

You might also like