Linux Commands
LINUX COMMANDS
I. DIRECTORY ORIENTED COMMANDS
1. LIST DIRECTORY COMMANDS
To list directories and files.
afzal@Afzal:~$ ls
aaaa pgm pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh pgms
pgrms prgm.sh programs
To list files and directories along with hidden files.
afzal@Afzal:~$ ls -a
. .bash_history .bashrc .sudo_as_admin_successful aaaa pgm1.sh
pgm3.sh pgm5.sh pgms prgm.sh
.. .bash_logout .profile .viminfo pgm pgm2.sh
pgm4.sh pgm6.sh pgrms programs
To list files and directories in long format.
afzal@Afzal:~$ ls -l
total 12
drwxrwxrwx 1 afzal afzal 4096 Dec 19 18:46 aaaa
drwxrwxrwx 1 afzal afzal 4096 Aug 8 18:40 pgm
-rw-rw-rw- 1 afzal afzal 516 Aug 6 14:20 pgm1.sh
-rw-rw-rw- 1 afzal afzal 775 Aug 7 17:52 pgm2.sh
-rw-rw-rw- 1 afzal afzal 540 Aug 7 18:11 pgm3.sh
-rw-rw-rw- 1 afzal afzal 576 Aug 7 18:34 pgm4.sh
-rw-rw-rw- 1 afzal afzal 368 Aug 7 18:42 pgm5.sh
-rw-rw-rw- 1 afzal afzal 910 Aug 8 18:40 pgm6.sh
drwxrwxrwx 1 afzal afzal 4096 Aug 8 18:40 pgms
drwxrwxrwx 1 afzal afzal 4096 Aug 8 18:40 pgrms
-rw-rw-rw- 1 afzal afzal 775 Aug 7 17:35 prgm.sh
drwxrwxrwx 1 afzal afzal 4096 Aug 8 18:40 programs
To list files and directories in reverse order.
afzal@Afzal:~$ ls -r
programs prgm.sh pgrms pgms pgm6.sh pgm5.sh pgm4.sh pgm3.sh
pgm2.sh pgm1.sh pgm aaaa
To list files and directories according to their last modification
time.
afzal@Afzal:~$ ls -t
aaaa pgm6.sh programs pgrms pgms pgm pgm5.sh pgm4.sh pgm3.sh
pgm2.sh prgm.sh pgm1.sh
To recursively list files and sub-directories as well as the files in
the sub-directories.
afzal@Afzal:~$ ls -R
.:
aaaa pgm pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh pgms
pgrms prgm.sh programs
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 1
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
./aaaa:
college office
./aaaa/college:
bcom bsc
./aaaa/college/bcom:
b.txt
./aaaa/college/bsc:
./aaaa/office:
non-teaching teaching
./aaaa/office/non-teaching:
a.txt d.txt
./aaaa/office/teaching:
c.txt
./pgm:
./pgms:
./pgrms:
./programs:
To put a slash after each directory.
afzal@Afzal:~$ ls -p
aaaa/ pgm/ pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh
pgms/ pgrms/ prgm.sh programs/
To display the number of storage blocks used by a file/directory.
afzal@Afzal:~$ ls -s
total 12
0 aaaa 0 pgm 0 pgm1.sh 4 pgm2.sh 0 pgm3.sh 0 pgm4.sh 0 pgm5.sh
4 pgm6.sh 0 pgms 0 pgrms 4 prgm.sh 0 programs
To list contents by lines instead of by columns in sorted order.
afzal@Afzal:~$ ls -x
aaaa pgm pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh pgms
pgrms prgm.sh programs
To mark executable files with * and directories with /.
afzal@Afzal:~$ ls -F
aaaa/ pgm/ pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh
pgms/ pgrms/ prgm.sh programs/
To list files and directories in long format according to their last
modification time.
afzal@Afzal:~$ ls -lt
total 12
drwxrwxrwx 1 afzal afzal 4096 Dec 19 18:46 aaaa
-rw-rw-rw- 1 afzal afzal 910 Aug 8 18:40 pgm6.sh
drwxrwxrwx 1 afzal afzal 4096 Aug 8 18:40 programs
-rw-rw-rw- 1 afzal afzal 368 Aug 7 18:42 pgm5.sh
-rw-rw-rw- 1 afzal afzal 576 Aug 7 18:34 pgm4.sh
-rw-rw-rw- 1 afzal afzal 540 Aug 7 18:11 pgm3.sh
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 2
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2. WILD CARD CHARACTERS
1. * (ASTERISK) – REPRESENTS ANY NUMBER OF CHARACTERS.
Example 1: To Match all the file/directory names beginning with
the letter p.
afzal@Afzal:~$ ls p*
pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh prgm.sh
pgm:
pgms:
pgrms:
programs:
Example 2: To Match all the file/directory names ending with the
letter s.
afzal@Afzal:~$ ls *s
pgms:
pgrms:
programs:
2. ? – REPRESENTS A SINGLE CHARACTER.
Example 1: To Match all the file names ending with the pattern
‘gm1.sh’.
afzal@Afzal:~$ ls ?gm1.sh
pgm1.sh
Example 2: To Match all the file names that matches the pattern
‘pgm?.sh’. (? holds place for a single character)
afzal@Afzal:~$ ls pgm?.sh
pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh
3. [] – REPRESENTS A SUBSET OF RELATED FILENAMES.
Example: To Match all the file names pgm1.sh through pgm5.sh.
afzal@Afzal:~$ ls pgm[1-5].sh
pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh
3. CREATING DIRECTORIES
mkdir [-p] <directory_name1> <directory_name2>
-p -> To create consequences of directories.
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 3
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
afzal@Afzal:~$ mkdir BCOM
afzal@Afzal:~$ mkdir firstsem firstsem/OS
afzal@Afzal:~$ ls firstsem
OS
afzal@Afzal:~$ mkdir -p bcomca/first/os
afzal@Afzal:~$ ls bcomca
first
afzal@Afzal:~$ ls bcomca/first
os
afzal@Afzal:~$ ls -R bcomca
bcomca:
first
bcomca/first:
os
bcomca/first/os:
4. REMOVING/DELETING DIRECTORIES
rmdir [-p] <directory_name1> <directory_name2>
-p -> To remove consequences of directories.
afzal@Afzal:~$ ls
D1 D2 D3 aaaa bcomca firstsem pgm pgm1.sh pgm2.sh pgm3.sh
pgm4.sh pgm5.sh pgm6.sh pgms pgrms prgm.sh programs
afzal@Afzal:~$ rmdir -p D1 D2 D3
afzal@Afzal:~$ ls
aaaa bcomca firstsem pgm pgm1.sh pgm2.sh pgm3.sh pgm4.sh
pgm5.sh pgm6.sh pgms pgrms prgm.sh programs
5. CHANGE DIRECTORY COMMAND
cd <directory_name>
afzal@Afzal:~$ cd bcomca
afzal@Afzal:~/bcomca$ cd ..
afzal@Afzal:~$
6. PRINT CURRENT WORKING DIRECTORY
afzal@Afzal:~$ pwd
/home/afzal
7. FIND FILE COMMAND
find <path_list> <selection_criteria> <action>
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 4
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
Selection Criteria:
-name <file_name>
-type d
-mtime {n/+n/-n}
-mmin {n/+n/-n}
-atime {n/+n/-n}
-amin {n/+n/-n}
Action:
-print
-exec <command>
afzal@Afzal:~$ find -name "*.sh" -print
./pgm1.sh
./pgm2.sh
./pgm3.sh
./pgm4.sh
./pgm5.sh
II. FILE ORIENTED COMMANDS
1. CATENATED/CONCATENATE COMMAND
cat [-options] <file_name1> [<filename2> ..]
> -> To create a file.
afzal@Afzal:~$ cat > a.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
To display the contents of a file.
afzal@Afzal:~$ cat a.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
To number non-blank output lines.
afzal@Afzal:~$ cat -b a.txt
1 Operating System Lab
2 First Semester B.Com - CA
3 FMKMC College, Madikeri
To number all output lines.
afzal@Afzal:~$ cat -n a.txt
1 Operating System Lab
2
3 First Semester B.Com - CA
4
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 5
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
5 FMKMC College, Madikeri
To display the contents of multiple files using a single command.
afzal@Afzal:~$ cat a.txt b.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
ABCDEFGHIJKLMNOPQRSTUVWXYZ
2. COPY COMMAND
To copy the contents of one file to another.
cp [-options] <source-file> <destination-file>
afzal@Afzal:~$ cat a.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
afzal@Afzal:~$ cat c.txt
afzal@Afzal:~$ cp a.txt c.txt
afzal@Afzal:~$ cat c.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
afzal@Afzal:~$ cat a.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
-i -> To prompt before overwriting destination files.
afzal@Afzal:~$ cp -i a.txt c.txt
cp: overwrite 'c.txt'? yes
afzal@Afzal:~$ cat c.txt
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
afzal@Afzal:~$ cp a.txt c.txt
afzal@Afzal:~$ cat c.txt
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 6
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
Operating System Lab
First Semester B.Com - CA
FMKMC College, Madikeri
3. REMOVE/DELETE FILE COMMAND
rm [-options] <filename>
afzal@Afzal:~$ rm a.txt
To recursively delete the contents of the specified directory.
afzal@Afzal:~$ rm -r c.txt
To prompt before deleting.
afzal@Afzal:~$ rm -i b.txt
rm: remove regular file 'b.txt'? no
To remove write-protected files also, without prompting.
afzal@Afzal:~$ rm -f a.txt
4. MOVE/RENAME FILE COMMAND
mv <soure> <destination>
To rename a specified file or directory.
afzal@Afzal:~$ ls
aaaa b.txt bcomca firstsem pgm pgm1.sh pgm2.sh pgm3.sh
pgm4.sh pgm5.sh pgm6.sh pgms pgrms prgm.sh programs
afzal@Afzal:~$ mv b.txt c.txt
afzal@Afzal:~$ ls
aaaa bcomca c.txt firstsem pgm pgm1.sh pgm2.sh pgm3.sh
pgm4.sh pgm5.sh pgm6.sh pgms pgrms prgm.sh programs
5. WORD/LINE/CHARACTER COUNT COMMAND
wc [-options] <filename>
To display the number of words, lines, and characters present in the
specified file.
afzal@Afzal:~$ wc c.txt
1 27 c.txt
To display the number of words present in the specified file.
afzal@Afzal:~$ wc -w c.txt
c.txt
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 7
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
To display the number of lines present in the specified file.
afzal@Afzal:~$ wc -l c.txt
c.txt
To display the number of characters present in the specified file.
afzal@Afzal:~$ wc -c c.txt
27 c.tx
6. FILE COMMAND
To display the general classification of a specifies file.
file <filename>
afzal@Afzal:~$ file c.txt
c.txt: ASCII text
afzal@Afzal:~$ file pgm1.sh
pgm1.sh: Bourne-Again shell script, ASCII text executable
afzal@Afzal:~$ file aaaa
aaaa: directory
7. COMAPARE COMMAND
cmp <filename1> <filename2>
afzal@Afzal:~$ cat > abc.txt
good morning
afzal@Afzal:~$ cat > xyz.txt
good evening
afzal@Afzal:~$ cmp abc.txt xyz.txt
abc.txt xyz.txt differ: byte 6, line 1
8. FILE ACCESS PERMISSIONS
Types of file:
1. Ordinary file
2. Directory file
3. Device file (Special file)
Access modes:
1. Read mode (r)
2. Write mode (w)
3. Execute mode (x)
4. Denial of permission (–)
User symbols/Categories of users:
1. User (u)
2. User group (g)
3. Others (o)
Set/deny symbols:
1. + Assign permission
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 8
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2. – Remove permission
3. = Assign absolute permission
afzal@Afzal:~$ ls -l a.txt
-rw-rw-rw- 1 afzal afzal 0 Dec 22 13:33 a.txt
9. CHANGE MODE COMMAND
Symbolic notation:
chmod user_symbols set/deny_symbol access_symbols <filename(s)>
afzal@Afzal:~$ ls -l a.txt
-rw-rw-rw- 1 afzal afzal 0 Dec 22 13:33 a.txt
afzal@Afzal:~$ chmod u+x a.txt
afzal@Afzal:~$ ls -l a.txt
-rwxrw-rw- 1 afzal afzal 0 Dec 22 13:33 a.txt
afzal@Afzal:~$ ls -l a.txt
-rwxrw-rw- 1 afzal afzal 0 Dec 22 13:33 a.txt
afzal@Afzal:~$ chmod ugo=rwx a.txt
afzal@Afzal:~$ ls -l a.txt
-rwxrwxrwx 1 afzal afzal 0 Dec 22 13:33 a.txt
Octal notation:
chmod three-digit-number <filename(s)>
Digits and their meaning:
1. 0 No permission
2. 4 Read
3. 2 Write
4. 1 Execute
Mixing of permissions:
1. 3 Write and Execute (2+1)
2. 5 Read and Execute (4+1)
3. 6 Read and Write (4+2)
4. 7 Read, Write and Execute (4+2+1)
afzal@Afzal:~$ ls -l c.txt
-rw-rw-rw- 1 afzal afzal 27 Dec 22 13:15 c.txt
afzal@Afzal:~$ chmod 740 c.txt
afzal@Afzal:~$ ls -l c.txt
-rwxr----- 1 afzal afzal 27 Dec 22 13:15 c.txt
10. TAIL COMMAND
To display the end of the specified file.
+n -> To display nth line to the end.
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 9
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
-n -> To display the last ‘n’ lines.
If ±n is not used, then the last 10 lines are displayed.
tail ±n <filename>
afzal@Afzal:~$ tail +20 s.txt
t
u
v
w
x
y
z
afzal@Afzal:~$ tail -2 s.txt
y
z
afzal@Afzal:~$ tail s.txt
q
r
s
t
u
v
w
x
y
z
11. HEAD COMMAND
To display the top of the specified file.
-n -> To display the first ‘n’ lines.
If -n is not used, then the first 10 lines are displayed.
head [-n] <filename>
afzal@Afzal:~$ head s.txt
a
b
c
d
e
f
g
h
i
j
afzal@Afzal:~$ head -3 s.txt
a
b
c
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 10
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
III. PROCESS ORIENTED COMMANDS
1. PROCESS COMMAND
To display the running processes.
ps
afzal@Afzal:~$ ps
PID TTY TIME CMD
7 tty1 00:00:01 bash
185 tty1 00:00:00 ps
To display the processes of all the users who are logged on to the
system.
afzal@Afzal:~$ ps -a
PID TTY TIME CMD
7 tty1 00:00:01 bash
186 tty1 00:00:00 ps
To display the processes which are running on the specified terminal.
afzal@Afzal:~$ ps –t <terminal_name>
PID TTY STAT TIME COMMAND
6 tty1 Ss 0:00 /init
7 tty1 S 0:01 -bash
187 tty1 R 0:00 ps -t
To display the processes which are running for the specified user.
afzal@Afzal:~$ ps –u <username>
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME
COMMAND
afzal 7 0.0 0.0 16796 3420 tty1 S 12:07 0:01 -
bash
afzal 188 0.0 0.0 17380 1916 tty1 R 14:08 0:00 ps-u
To display the system processes.
afzal@Afzal:~$ ps -x
PID TTY STAT TIME COMMAND
7 tty1 S 0:01 -bash
189 tty1 R 0:00 ps –x
IV. BACKGROUND PROCESSING COMMANDS
1. & (AMPERSAND)
To send the command for background processing.
afzal@Afzal:~$ sort a.txt&
[1] 190
afzal@Afzal:~$ ls -l a.txt
-rwxrwxrwx 1 afzal afzal 0 Dec 22 13:33 a.txt
[1]+ Done sort a.txt
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 11
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2. KILL/TERMINATE PROCESS COMMAND
To terminate a process prematurely.
kill [-SignalNumber] <PID>
afzal@Afzal:~$ ps
PID TTY TIME CMD
194 tty1 00:00:00 bash
206 tty1 00:00:00 ps
afzal@Afzal:~$ kill -9 194
3. AT COMMAND
To execute the specified command at future time.
at <time>
<command(s)>
[Ctrl+d]
afzal@Afzal:~$ at 14:35
warning: commands will be executed using /bin/sh
at> echo "hi"
at> <EOT>
job 1 at Sun Dec 22 14:35:00 2019
hi
4. BATCH COMMAND
To execute the specified commands when the system load permits.
batch
<commands>
[Ctrl+d]
afzal@Afzal:~$ batch
warning: commands will be executed using /bin/sh
at> echo "hi"
at> cat a.txt
at> ls -l c.txt
at> <EOT>
job 3 at Sun Dec 22 14:37:00 2019
hi
-rwxr----- 1 afzal afzal 27 Dec 22 13:15 c.txt
V. COMMUNICATION ORIENTED COMMANDS
1. WRITE COMMAND
To write messages on another user’s terminal.
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 12
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
write <RecipienntLoginName>
<message>
[Ctrl+d]
afzal@Afzal:~$ write afzal
Hi
[Ctrl+d]
2. MAIL COMMAND
To perform offline communication.
mail <username>
<message>
[Ctrl+d]
3. WALL COMMAND
To send a message to all the logged-in users.
wall
<message>
[Ctrl+d]
afzal@Afzal:~$ wall
hello
VI. GENERAL PURPOSE COMMANDS
1. DATE COMMAND
To display the system’s date and time.
date +<format>
Format:
%H Hour (00 to 23)
%I Hour (00 to 12)
%M Minute (00 to 59)
%S Second (00 to 59)
%D Date (MM/DD/YY)
%T Time (HH:MM:SS)
%w Day of the week
%r Time in AM/PM
%y Last two digits of the year
afzal@Afzal:~$ date
Sun Dec 22 14:49:07 IST 2019
afzal@Afzal:~$ date +%H
14
afzal@Afzal:~$ date +%I
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 13
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
02
afzal@Afzal:~$ date +%M
49
afzal@Afzal:~$ date +%S
39
afzal@Afzal:~$ date +%D
12/22/19
afzal@Afzal:~$ date +%T
14:49:46
afzal@Afzal:~$ date +%w
0
afzal@Afzal:~$ date +%r
02:49:58 PM
afzal@Afzal:~$ date +%y
19
2. WHO COMMAND
To display the logged-in users.
who
3. WHO AM I COMMAND
To display the current terminal user details.
who am i
4. MAN COMMAND
To display the syntax and detailed usage of the Linux commands.
man <command>
afzal@Afzal:~$ man who
WHO(1) User Commands
WHO(1)
NAME
who - show who is logged on
SYNOPSIS
who [OPTION]... [ FILE | ARG1 ARG2 ]
DESCRIPTION
Print information about users who are currently logged in.
5. CALENDAR COMMAND
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 14
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
To display calendar for the specified month and year.
cal [<month>] <year>
afzal@Afzal:~$ cal 2019
2019
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
afzal@Afzal:~$ cal 12 2019
December 2019
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
6. TEE COMMAND
To send the output of a command into standard output as well as to a
file.
command | tee <filename>
afzal@Afzal:~$ ls
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 15
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
a.txt abc.txt c.txt pgm pgm2.sh pgm4.sh pgm6.sh pgrms
programs xyz.txt
aaaa bcomca firstsem pgm1.sh pgm3.sh pgm5.sh pgms prgm.sh
s.txt
afzal@Afzal:~$ ls | tee list.txt
pgm1.sh
pgm2.sh
pgm3.sh
pgm4.sh
pgm5.sh
afzal@Afzal:~$ cat list.txt
pgm1.sh
pgm2.sh
pgm3.sh
pgm4.sh
pgm5.sh
7. SCRIPT COMMAND
To store the login session into a specified file.
script <filename>
afzal@Afzal:~$ script aa.txt
Script started, file is aa.txt
afzal@Afzal:~$ cat c.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
afzal@Afzal:~$ cat b.txt
cat: b.txt: No such file or directory
afzal@Afzal:~$ exit
exit
Script done, file is aa.txt
afzal@Afzal:~$ cat aa.txt
Script started on 2019-12-22 15:01:35+0530
afzal@Afzal:~$ cat c.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
afzal@Afzal:~$ cat b.txt
cat: b.txt: No such file or directory
afzal@Afzal:~$ exit
exit
Script done on 2019-12-22 15:01:54+0530
8. TPUT COMMAND
To clear the screen content.
tput clear
9. SPLIT COMMAND
To split the file into several small files.
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 16
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
split -<number> <filename>
afzal@Afzal:~$ split -5 s.txt
afzal@Afzal:~$ ls
a.txt aaaa bcomca firstsem pgm pgm2.sh pgm4.sh pgm6.sh
pgrms programs xaa xac xae xyz.txt
aa.txt abc.txt c.txt list.txt pgm1.sh pgm3.sh pgm5.sh pgms
prgm.sh s.txt xab xad xaf
10. EXPR COMMAND
To perform arithmetic operations on integers.
afzal@Afzal:~$ x=5
afzal@Afzal:~$ y=2
afzal@Afzal:~$ expr $x + $y
7
afzal@Afzal:~$ expr $x - $y
3
afzal@Afzal:~$ expr $x \* $y
10
afzal@Afzal:~$ expr $x / $y
2
afzal@Afzal:~$ expr $x % $y
1
afzal@Afzal:~$ expr $x + 10
15
11. BC COMMAND
To perform arithmetic operations on integers as well as on floats.
afzal@Afzal:~$ bc
10 + 20
30
VII. PIPE AND FILTERS
1. PIPE
To redirect the output of a command as an input to another command.
command1 | command2
afzal@Afzal:~$ ls | wc -w
27
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 17
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2. REDIRECTION
1. Standard input (<) – 0
2. Standard output (>) – 1
3. Standard error (2>) – 2
afzal@Afzal:~$ cat < c.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
afzal@Afzal:~$ cat z.txt 2> abc.txt
afzal@Afzal:~$ cat abc.txt
cat: z.txt: No such file or directory
3. FILTERS
1. SORT COMMAND
To sort the contents of a specified file based on the ASCII value
of characters.
sort [-options] <filename>
afzal@Afzal:~$ cat m.txt
pgm1.sh
pgm2.sh
pgm3.sh
pgm4.sh
pgm5.sh
pgm6.sh
To sort the contents in reverse order.
afzal@Afzal:~$ sort -r m.txt
pgm6.sh
pgm5.sh
pgm4.sh
pgm3.sh
pgm2.sh
pgm1.sh
2. GREP (GLOBAL SEARCH FOR REGULAR EXPRESSION) COMMAND
To search for a specified pattern from a specified file and display
those lines containing the pattern.
grep [-options] pattern <filename>
afzal@Afzal:~$ cat emp.dat
101 abc bca
102 def bcom
103 ghi ba
104 jkl bba
105 mno bsc
106 pqr bca
107 stu bcom
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 18
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
afzal@Afzal:~$ grep "bc" emp.dat
101 abc bca
102 def bcom
106 pqr bca
107 stu bcom
To display the resultant lines along with their line numbers.
afzal@Afzal:~$ grep -n "bc" emp.dat
1:101 abc bca
2:102 def bcom
6:106 pqr bca
7:107 stu bcom
To display only the lines that do not match the specified pattern.
afzal@Afzal:~$ grep -v "bc" emp.dat
103 ghi ba
104 jkl bba
105 mno bsc
To ignore case distinction for matching.
afzal@Afzal:~$ grep -i "bc" emp.dat
101 abc bca
102 def bcom
106 pqr bca
107 stu bcom
3. CUT COMMAND -> To cut the columns/fields of a specified file.
cut [-options] <filename>
afzal@Afzal:~$ cut -f 2 emp.dat
abc
def
ghi
jkl
mno
afzal@Afzal:~$ cut -f 2-3 emp.dat
abc bca
def bcom
ghi ba
jkl bba
mno bsc
4. PASTE COMMAND
To concatenate the contents of the specified file into a single
file vertically.
paste <filename1> <filename2>
afzal@Afzal:~$ cat > e1.dat
1
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 19
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2
3
afzal@Afzal:~$ cat > e2.dat
a
b
c
afzal@Afzal:~$ paste e1.dat e2.dat
1 a
2 b
3 c
5. SED COMMAND
To perform line editing.
sed ‘EditCommands’ <filename>
Edit Commands:
q – To quit.
p – To print lines.
d – To delete lines.
c – To change lines.
a – To append after line.
i – To insert before line.
afzal@Afzal:~$ sed '2q' emp.dat
101 abc bca
102 def bcom
afzal@Afzal:~$ sed '2d' emp.dat
101 abc bca
103 ghi ba
104 jkl bba
105 mno bsc
106 pqr bca
107 stu bcom
afzal@Afzal:~$ sed '/bc/d' emp.dat
103 ghi ba
104 jkl bba
105 mno bsc
6. TR COMMAND
tr <CharacterSet1> <CharacterSet2> <Standard-Input>
To translate/change the case of alphabets.
afzal@Afzal:~$ cat aa.txt
ABCD
abcd
EFGH
afzal@Afzal:~$ tr "A,D" "a,d" < aa.txt
aBCd
abcd
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 20
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
EFGH
afzal@Afzal:~$ tr "E,F" "e,f" < aa.txt
ABCD
abcd
efGH
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 21
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.