Linux Commands: I. Directory Oriented Commands
Linux Commands: I. Directory Oriented Commands
LINUX COMMANDS
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:
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 2
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
afzal@Afzal:~$ ls p*
pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh pgm6.sh prgm.sh
pgm:
pgms:
pgrms:
programs:
afzal@Afzal:~$ ls *s
pgms:
pgrms:
programs:
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
afzal@Afzal:~$ ls pgm[1-5].sh
pgm1.sh pgm2.sh pgm3.sh pgm4.sh pgm5.sh
3. CREATING 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:~$ ls firstsem
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
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
cd <directory_name>
afzal@Afzal:~$ cd bcomca
afzal@Afzal:~/bcomca$ cd ..
afzal@Afzal:~$
afzal@Afzal:~$ pwd
/home/afzal
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>
1. CATENATED/CONCATENATE COMMAND
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 5
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2. COPY COMMAND
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 6
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
rm [-options] <filename>
afzal@Afzal:~$ rm a.txt
mv <soure> <destination>
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
wc [-options] <filename>
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 7
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
6. FILE COMMAND
file <filename>
7. COMAPARE COMMAND
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 (–)
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
Symbolic notation:
afzal@Afzal:~$ ls -l a.txt
-rw-rw-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:~$ ls -l a.txt
-rwxrw-rw- 1 afzal afzal 0 Dec 22 13:33 a.txt
afzal@Afzal:~$ ls -l a.txt
-rwxrwxrwx 1 afzal afzal 0 Dec 22 13:33 a.txt
Octal notation:
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:~$ ls -l c.txt
-rwxr----- 1 afzal afzal 27 Dec 22 13:15 c.txt
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 9
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
tail ±n <filename>
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 10
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
1. PROCESS COMMAND
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 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
1. & (AMPERSAND)
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
afzal@Afzal:~$ ps
PID TTY TIME CMD
194 tty1 00:00:00 bash
206 tty1 00:00:00 ps
3. AT COMMAND
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
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
1. WRITE COMMAND
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]
2. MAIL COMMAND
mail <username>
<message>
[Ctrl+d]
3. WALL COMMAND
wall
<message>
[Ctrl+d]
afzal@Afzal:~$ wall
hello
1. DATE COMMAND
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
02
2. WHO COMMAND
3. WHO AM I COMMAND
4. MAN COMMAND
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
6. TEE COMMAND
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
7. SCRIPT COMMAND
script <filename>
afzal@Afzal:~$ exit
exit
Script done, file is aa.txt
8. TPUT COMMAND
tput clear
9. SPLIT COMMAND
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 16
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
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
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
afzal@Afzal:~$ bc
10 + 20
30
1. PIPE
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
3. FILTERS
1. SORT COMMAND
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 18
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
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
4. PASTE COMMAND
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 19
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.
Linux Commands
2
3
5. SED COMMAND
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.
6. TR COMMAND
EFGH
Prepared by: Afzal, Dept. of Computer Science, FMKMC College, Madikeri. Page 21
Contact: afzal285@outlook.com, s.afzlahmd@gmail.com.