1
Commands: Commands are divided into six different categories as given
below:
1. Directory Related Commands: pwd, cd, mkdir, rmdir
2. File related commands: cat, cp, rm, mv, ls, chmod, wc, diff, cmp
3. General purpose: cal, date, echo, passwd, who, man
4. Filters: head, tail, cut, paste, sort, tr, grep
5. Process related: ps, time, kill
6. Others: expr, tee, set
Directory Related Commands : pwd, cd, mkdir, rmdir
1) pwd
pwd stands for Print Working Directory.
Linux pwd (print working directory) command displays your location
currently you are working on. It will give the whole path starting from
the root ending to the directory.
Syntax: pwd
Usage: Print working directory name
2) cd
The "cd" stands for 'change directory' and this command is used to
change the current directory i.e; the directory in which the user is
currently working.
Syntax:
cd <dirname>
Example: cd student
Usage: change working directory
2
Option Description
cd filename Change current directory to new directory
cd ~ Brings you to your home directory.
cd .. Brings you to the parent directory of current directory.
cd / It takes you to the entire system's root directory.
3) mkdir - make directory
The mkdir stands for 'make directory'. With the help of mkdir command,
you can create a new directory wherever you want in your system.
Syntax: mkdir <dir name>
Usage: creates new directory
Example: mkdir test
Which creates a new directory named test
- If you want to create a directory in a different location other than
'Home directory', you could use the following command ,
Example: mkdir /tmp/Music
It will create a directory 'Music' under '/tmp' directory
- You can also create more than one directory at a time.
Example: mkdir r1 r2 r3
3
4) rmdir – remove directory
rmdir command is used remove empty directories from the file system
in Linux.
The rmdir command removes each and every directory specified in the
command line only if these directories are empty.
So if the specified directory has some directories or files in it then this
cannot be removed by rmdir command.
Syntax: rmdir directoryname
Example: rmdir test
It will delete the directory test
File related commands: cat, cp, rm, mv, ls, chmod, wc, diff, cmp
1) cat
Cat(concatenate) command is very frequently used in Linux. It reads data
from the file and gives their content as output.
- To view a single file
Syntax: cat filename
Output: It will show content of given filename
- To view multiple files
Syntax: cat file1 file2
Output: This will show the content of file1 and file2.
- To view contents of a file preceding with line numbers.
Syntax: cat -n filename
Output: It will show content with line number
example:- cat-n m.txt
1) hello
2) how are you
4
- Create a file
Syntax: cat >newfile
Output: Will create and a file named newfile
- Cat command can append the contents of one file to the end of
another file.
Syntax: cat file1 >> file2
Output: Will append the contents of one file to the end of another file
2) cp
cp means copy. 'cp' command is used to copy a file or a directory. To
copy a file into the same directory syntax will be,
Syntax:
cp <existing file name> <new file name>
Usage: copies a file or group of files
3) rm
The 'rm' means remove. This command is used to remove a file. The
command line doesn't have a recycle bin or trash unlike other GUI's to
recover the files. Hence, be very much careful while using this
command. Once you have deleted a file, it is removed permanently.
Syntax:
rm <filename>
Usage: Remove files
Example:
rm myfile1
In above , we have removed file myfile1 permanently with the help of
'rm' command
5
4) mv
Linux mv command is used to move existing file or directory from one
location to another. It is also used to rename a file or directory. If you
want to rename a single directory or file then 'mv' option will be
better to use.
Syntax: mv filename new_file_location
Usage: Renames a file or moves a file or group of files.
Example: Suppose we want to move the file "sample2" to location
/home/stu/Documents. Executing the command
mv sample2 /home/stu/Documents
5) ls
If you want to see the list of files on your UNIX or Linux system, use
the 'ls' command.
It shows the files /directories in your current directory.
Syntax: ls
Usage: list out contents or list of a directory
6) chmod
In Unix-like operating systems, the chmod command is used to change
the access mode of a file.
Syntax:
chmod [reference][operator][mode] file...
The references are used to distinguish the users to whom the
permissions apply i.e. they are list of letters that specifies whom to
give permissions. The references are represented by one or more of
the following letters:
6
Reference Class Description
U Owner file’s owner
G group Users who are members of the file’s group
O others Users who are neither the file’s owner nor
members of the file’s group
A All All three of the above
The operator is used to specify how the modes of a file should be
adjusted. The following operators are accepted:
+ (add) Add the specified modes to the specified classes
- (delete) Removes the specified modes from the specified classes
= (set) the modes specified are to be made the exact modes for the
specified classes
The modes indicate which permissions are to be granted or removed from
the specified classes. There are three basic modes which correspond to
the basic permissions:
r(read) Permission to read the file.
w(write/delete) Permission to write (or delete) the file.
x(execute) Permission to execute the file.
7) wc
The 'wc' command helps in counting the lines, words and characters in a
file.
Syntax:
wc <fileName> (Counts words, lines and characters)
wc -l <fileName> (Counts only lines)
wc -w <fileName> (Counts only words)
wc -c <fileName> (Counts only characters)
7
Usage: Counts number of characters, words and lines in given input file.
If file is not given, it takes input from standard input.
Example:
wc exm.txt
wc -l exm.txt
wc -w exm.txt
wc -c exm.txt
8) diff
diff stands for difference. This command is used to display the
differences in the files by comparing the files line by line.
Syntax: diff file1 file2
Usage: Compares two files, gives location of mismatch as well as
suggests changes to make two files identical.
9) cmp
Compare two files, and if they differ, tells the first byte and line number
where they differ.
Syntax: cmp file1 file2
Usage: compares two files and gives location of first mismatch.
General purpose: cal, date, echo, passwd, who, man
1) cal
Calendar
If a user wants a quick view of calendar in Linux terminal, cal is the
command for you. By default, cal command shows current month
calendar as output.
cal command is a calendar command in Linux which is used to see the
calendar of a specific month or a whole year.
8
Syntax:
cal displays calendar of the current month
cal year# displays calendar of the particular year
cal month# year# displays calendar of the particular
month of a particular year
Usage: Display calendar.
2) date
date command is used to display the system date and time. date
command is also used to set date and time of the system. By default the
date command displays the date in the time zone on which linux
operating system is configured. You must be the super-user (root) to
change the date and time.
Syntax: date
Usage: Displays current date and time.
3) echo
echo command in linux is used to display line of text/string that are
passed as an argument . This is a built in command that is mostly used in
shell scripts and batch files to output status text to the screen or a file.
Syntax: echo message
Usage: Displays message as well as evaluates variables.
Example:
echo “hello world..!” //Displays message
echo $count //evaluates variable count
9
4) passwd
passwd command in Linux is used to change the user account
passwords. The root user reserves the privilege to change the password
for any user on the system, while a normal user can only change the
account password for his or her own account.
Syntax: passwd
Usage: changes password of an user
5) who
who command show who is logged in.
Syntax:
who //displays all users
who am i //displays user of the current shell only
Usage: Displays information about all users who have logged in to the
system currently.
Information includes username, device name, date and time of logged
in, and machine name from where user has logged-in.
6) man
man command in Linux is used to display the user manual of any
command that we can run on the terminal. It provides a detailed view of
the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS,
EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES,
AUTHORS and SEE ALSO.
10
Syntax: man command_name
Usage: Print entries from the on-line reference manuals, find manual
entries by keyword
In short it displays on-line help about title, title can be a command name,
system call or function name.
Example: man ls
It will display man page for ls command
Filters: head, tail, cut, paste, sort, tr, grep
1) head
The head command, as the name implies, print the top N number of
data of the given input. By default, it prints the first 10 lines of the
specified files. If more than one file name is provided then data from
each file is preceded by its file name.
Syntax: head [-n] filename
Usage: Displays top of the file. By default, it displays first 10 lines of a
file, but if used with –n option display first n lines of a file.
Where, n indicates number of lines to be display from top of a file.
Example:
head test.txt by default displays first 10 lines of a file test.txt
head -20 test.txt displays first 20 lines of a file test.txt
2) tail
It is the complementary of head command. The tail command, as the
name implies, print the last N number of data of the given input. By
default it prints the last 10 lines of the specified files. If more than one
11
file name is provided then data from each file is precedes by its file
name.
Syntax: tail [-n] filename
Usage: Displays end of the file. By default, it displays last 10 lines of a
file, but if used with –n option, displays last n lines of a file.
Where, n indicates number of lines to be displayed from end of a file.
Example:
tail test.txt by default displays last 10 lines of a file test.txt
tail -20 test.txt displays last 20 lines of a file test.txt
3) sort
The 'sort' command sorts the file content in an alphabetical order. By
default, the sort command sorts file assuming the contents are ASCII.
Syntax: sort <fileName>
Usage: sorts data in a file. Data will be sorted line by line.
Example: sort weeks.txt
4) grep
The grep filter searches a file for a particular pattern of characters, and
displays all lines that contain that pattern. The pattern that is searched
in the file is referred to as the regular expression (grep stands for global
regular expression print)
12
Syntax:
grep <searchword/pattern> <file name>
Usage: Displays lines from files that match the given pattern.
Example:
grep “hello” test.txt
5) cut
The cut command in UNIX is a command for cutting out the sections
from each line of files and writing the result to standard output. It can be
used to cut parts of a line by byte position, character and field. Basically
the cut command slices a line and extracts the text. It is necessary to
specify option with command otherwise it gives error.
Syntax:
cut –c list filename // to cut columns
cut –f list filename //to cut fields
Where, c : extract specific columns as given in list.
f: extract specific fields as given in list or delimited by char.
Usage: cuts a file vertically. Extracts specific columns or fields from an
input file.
Example:
state.txt
Andhra Pradesh
Bihar
Chhattisgarh
13
List without ranges
$ cut -c 1,2,3 state.txt
And
Bih
Chh
List with ranges
$ cut -c 1-3,5-7 state.txt
Andra
Bihr
Chhtti
6) paste
The paste command merges the lines from multiple files.
Syntax: paste [-d char] file1 file2
Usage: pastes files. It combines two or more files vertically. Here.
Vertically means data will not be appended to end of other files but
merged on the same lines.
Examples:
> cat file1
Unix
Linux
Windows
> cat file2
Dedicated server
Virtual server
14
1. Merging files in parallel
By default, the paste command merges the files in parallel. The paste
command writes corresponding lines from the files as a tab delimited on the
terminal.
> paste file1 file2
Unix Dedicated server
Linux Virtual server
Windows
> paste file2 file1
Dedicated server Unix
Virtual server Linux
Windows
2. Specifying the delimiter
Paste command uses the tab delimiter by default for merging the files. You can
change the delimiter to any other character by using the -d option.
> paste -d"|" file1 file2
Unix|Dedicated server
Linux|Virtual server
Windows|
7) tr
15
The tr command in UNIX is a command line utility for translating or deleting
characters. It supports a range of transformations including uppercase to
lowercase, squeezing repeating characters, deleting specific characters and
basic find and replace.
Syntax: tr [-cds] [string1] [string2] input
Where, c: translate character not in string1
d: delete character in string1 from input
s: truncate repeated characters given in string1 from input.
Usage: Translate character in a file. It is used to replace characters, delete
characters, changing case of a text as well as compressing multiple
consecutive characters.
Examples:
tr ‘:’ ‘|’ < test.txt
it will replace all ‘:’ from test.txt file with ‘|’
tr ‘[a-z]’ ‘[A-Z]’ < test.txt
it will translates lower case to upper case
tr –d ‘/’ < bdate.txt
it will delete ‘/’ from bdate.txt file
tr –s ‘ ‘ < test.txt
it will compresses multiple consecutive spaces.
Process related: ps, time, kill
16
1) ps
ps is one of the basic Unix commands that helps you access information
about processes running on your system.
Syntax: ps [options]
Usage: displays status of current running processes. By default it shows
process ID, terminal with which process is associated, time consumed by
the process since it has been started and process name.
Options:
f: full listing showing the PPID (parent process ID) of each process
e or A : all processes including user and system processes
u usr: processes of user usr only
l : long lisiting showing memory related information
t term: processes running on terminal term.
2) time
time command in Linux is used to execute a command and prints a
summary of real-time, user CPU time and system CPU time spent by
executing a command when it terminates. ‘real‘ time is the time elapsed
wall clock time taken by a command to get executed, while ‘user‘ and ‘sys‘
time are the number of CPU seconds that command uses in user and kernel
mode respectively.
Syntax: time command/program
17
Usage: executes command or program and displays time usage on the
terminal.
Here, time usage is displayed in form of three different time values:
I) Real time: total time from the invocation of the command till its
termination.
II) User time: actual time spent by program in execution of itself.
iii)System time: time spent by kernel in doing work behalf of the user
process, means providing services to requests such as reading data from
a file stored on disk.
3) kill
kill command in Linux (located in /bin/kill), is a built-in command which is
used to terminate processes manually. kill command sends a signal to a
process which terminates the process. If the user doesn’t specify any signal
which is to be sent along with kill command then default TERM signal is
sent that terminates the process.
Syntax: kill [-signal] pid
Usage: sends a signal to a process. By default, kill other process having
process ID pid. Various signals which can be sent to other process, can be
found from man help.
Examples:
kill 125
It will terminates process having ID 125.
kill -9 127
it will sends signal-9 to process 127
18
Others: expr
1) expr
The expr command in linux evaluates a given expression and displays its
corresponding output. It is used for:
- Basic operations like addition, subtraction, multiplication, division, and
modulus on integers.
- Evaluating regular expressions, string operations like substring, length of
strings etc.
Syntax: expr expression
Usage: evaluates the given mathematical expression.
Options:
- Expression can be of the form: expr1 operator expr2
- Here, operator can be +(plus), -(minus), \*(multiplication), /(division), or
%(modulo).
- Also operator must be enclosed by white space on either side of it.
Examples:
expr 5 + 3
it will add 5 with 3 and displays 8 as answer.
expr $x \* $y
it will multiplies values stored in variables x and y.