- Calendar
- Change Directory
- Make Directory
- Remove empty directories
- Touch command
- Cat command
- ls command
- Pwd command
- Echo command
- Copy command
- Move command
- chmod command
- Word count
- rm command
- Filters (more, head, tail, tr, tee, grep)
- Date
- Others (man, who, uname, tty)
- Shell Scripting
cal : Shows current month calendar
shashi@Mac-Shashi:/unix$ cal
December 2020
Su Mo Tu We Th Fr Sa
1 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
cal -3 : Shows n months including, the curent, previous and upcoimng months.
shashi@Mac-Shashi:/unix$ cal -3
November 2020 December 2020 January 2021
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 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 29 30
31
cal -m 7 : Show a particular month.
shashi@Mac-Shashi:/unix$ cal -m 7
July 2020
Su Mo Tu We Th Fr Sa
1 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
cal -y 2020 : Shows the whole calendar of the year
shashi@Mac-Shashi:/unix$ cal -y 2020
cal -m 3 2010 : Show a particular month of a particular year
shashi@Mac-Shashi:/unix$ cal -m 3 2010
March 2010
Su Mo Tu We Th Fr Sa
1 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
cal -A 1 : Shows the current month and the next n months. (A - After)
cal -B 1 : Shows the current month and the previous n months. (B - Before)
shashi@Mac-Shashi:/unix$ cal -A 1
December 2020 January 2021
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 29 30
31
shashi@Mac-Shashi:/unix$ cal -B 1
November 2020 December 2020
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 5
8 9 10 11 12 13 14 6 7 8 9 10 11 12
15 16 17 18 19 20 21 13 14 15 16 17 18 19
22 23 24 25 26 27 28 20 21 22 23 24 25 26
29 30 27 28 29 30 31
cd : Used to change current working directory.
cd dir_name : Move inside a subdirectory
shashi@Mac-Shashi:/unix$ cd newfolder
shashi@Mac-Shashi:/unix/newfolder$
cd .. : Move to the parent of current directory
shashi@Mac-Shashi:/folder/subfolder$ cd ..
shashi@Mac-Shashi:/folder$
cd : Change current directory to home directory
shashi@Mac-Shashi:/folder$ cd
shashi@Mac-Shashi:~$
mkdir : Used to create directories
mkdir dir_name : Create new directory
shashi@Mac-Shashi:/unix$ mkdir newfolder
mkdir dir1 dir2 dir3 : create multiple directories
shashi@Mac-Shashi:/unix$ mkdir folder1 folder2 folder3
mkdir -p parent_dir/child_dir : create multiple directories and subdirectories
shashi@Mac-Shashi:/unix$ mkdir -p parentFolder/childFolder
-v : Print a message for each created directory
shashi@Mac-Shashi:/unix$ mkdir -p -v parentFolder/childFolder
mkdir: created directory 'parentFolder'
mkdir: created directory 'parentFolder/childFolder'
shashi@Mac-Shashi:/unix$ mkdir -p -v test1/subject1 test2/subject1
mkdir: created directory 'test1'
mkdir: created directory 'test1/subject1'
mkdir: created directory 'test2'
mkdir: created directory 'test2/subject1'
rmdir - Used to remove empty directories
Syntax:
rmdir dir_name : Remove directory
Example:
shashi@Mac-Shashi:/unix$ rmdir test1
-v : Print a message for each deleted directory
shashi@Mac-Shashi:/unix$ rmdir -v test1
rmdir: removing directory, 'test1'
rmdir dir1 dir2: Delete multiple directories
shashi@Mac-Shashi:/unix$ rmdir -v test1 test2
rmdir: removing directory, 'test1'
rmdir: removing directory, 'test2'
rmdir -p parent_dir/child_dir: Delete parent and child directories and subdirectories at once
shashi@Mac-Shashi:/unix$ rmdir -p -v test2/subject1
rmdir: removing directory, 'test2/subject1'
rmdir: removing directory, 'test2'
touch : Used to change, modify timestamps of a file
touch fileName : Create new file
shashi@Mac-Shashi:/unix$ touch file1
Create multiple files at once
shashi@Mac-Shashi:/unix$ touch file1 file2
cat : Used to read data from the file and gives the content as output
cat fileName : Display content of a file
(Same as: cat < fileName )
shashi@Mac-Shashi:/unix$ cat file1
Heading1--
This is file 1
End--
cat file1 file2 : Display content of multiple files
shashi@Mac-Shashi:/unix$ cat file3.txt file1
Heading3--
This is file 3
End--
Heading1--
This is file 1
End--
cat > fileName : Create a new file
shashi@Mac-Shashi:/unix$ cat > file3.txt
Heading3--
This is file 3
End--
cat >> fileName : Append an existing file
shashi@Mac-Shashi:/unix$ cat >> file3.txt
End of FILE3
^Z
[3]+ Stopped cat >> file3.txt
shashi@Mac-Shashi:/unix$ cat file3.txt
Heading3--
This is file 3
End--
End of FILE3
cat file1 > file2 : Copy contents of one file to another file
shashi@Mac-Shashi:/unix$ touch file2
shashi@Mac-Shashi:/unix$ cat file1 > file2
shashi@Mac-Shashi:/unix$ cat file2
Heading1--
This is file 1
End--
cat file1 >> file2 : Append contents of one file to another file
shashi@Mac-Shashi:/unix$ cat file1 >> file2
shashi@Mac-Shashi:/unix$ cat file2
Heading--
This is file 1
End
Heading--
This is file 1
End
ls - Used to list directory contents
ls : list all files of current directory
shashi@Mac-Shashi:/unix$ ls
README.md file1.txt file2 scripting test1 test2
ls -l : display all information about files and subdirectories
shashi@Mac-Shashi:/unix$ ls -l
total 8
-rwxrwxrwx 1 shashi shashi 7024 Dec 1 19:12 README.md
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:30 file1.txt
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:29 file2
drwxrwxrwx 1 shashi shashi 4096 Dec 1 11:28 scripting
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 test1
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 test2
ls -t : Order files based on last modified time
shashi@Mac-Shashi:/unix$ ls -t -l
total 8
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:30 file1.txt
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:29 file2
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 test2
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 test1
-rwxrwxrwx 1 shashi shashi 7024 Dec 1 19:12 README.md
drwxrwxrwx 1 shashi shashi 4096 Dec 1 11:28 scripting
-r : Reverse the order of files
shashi@Mac-Shashi:/unix$ ls -t -r -l
total 8
drwxrwxrwx 1 shashi shashi 4096 Dec 1 11:28 scripting
-rwxrwxrwx 1 shashi shashi 7024 Dec 1 19:12 README.md
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 test1
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 test2
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:29 file2
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:30 file1.txt
ls -S : Order files based on size
shashi@Mac-Shashi:/unix$ ls -S -l
total 12
-rwxrwxrwx 1 shashi shashi 9432 Dec 1 19:53 README.md
drwxrwxrwx 1 shashi shashi 4096 Dec 1 11:28 scripting
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 test1
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 test2
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:30 file1.txt
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:29 file2
-R : Display files recursively
shashi@Mac-Shashi:/unix$ ls -l -R
.:
total 12
-rwxrwxrwx 1 shashi shashi 8696 Dec 1 19:50 README.md
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:30 file1.txt
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:29 file2
drwxrwxrwx 1 shashi shashi 4096 Dec 1 11:28 scripting
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 test1
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 test2
./scripting:
total 0
-rwxrwxrwx 1 shashi shashi 416 Dec 1 12:05 variables.sh
./test1:
total 0
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 subject1
./test1/subject1:
total 0
./test2:
total 0
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 subject1
./test2/subject1:
total 0
ls -a : Display hidden files
shashi@Mac-Shashi:/unix$ ls -a -l
total 8
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:30 .
drwxrwxrwx 1 shashi shashi 4096 Nov 30 23:23 ..
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:12 .git
-rwxrwxrwx 1 shashi shashi 7024 Dec 1 19:12 README.md
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:30 file1.txt
-rwxrwxrwx 1 shashi shashi 0 Dec 1 19:29 file2
drwxrwxrwx 1 shashi shashi 4096 Dec 1 11:28 scripting
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:28 test1
drwxrwxrwx 1 shashi shashi 4096 Dec 1 19:29 test2
pwd - Print working directory
pwd : Used to print absolute path of current directory
shashi@Mac-Shashi:~$ pwd
/home/shashi
shashi@Mac-Shashi:~$ cd unix
shashi@Mac-Shashi:/unix$ pwd
/unix
echo - Used to display messages on the terminal
We use it in two ways:
- To display a message (echo shashi)
- To evaluate shell variables (echo $SHELL)
echo "string" : Display text
shashi@Mac-Shashi:/unix$ echo "Hello"
Hello
\n : Create new line
-e : Enable interpretation of backslash escape
\t : Tab Space
shashi@Mac-Shashi:/unix$ echo -e "Hi, I'm Shashi.\nAndroid Developer."
Hi, I'm Shashi.
Android Developer.
\r : carriage return with backspace interpretor
shashi@Mac-Shashi:/unix$ echo -e "Hi \rI'm Shashi"
I'm Shashi
echo * : Prints all files/directory (similar to ls command)
shashi@Mac-Shashi:/unix$ echo *
README.md file1.txt file2 scripting test1 test2
cp : (Copy) Used to copy files
Syntax
cp [OPTION] Source Directory NewName
Copy file: Test1Subject1.txt into Test1/Subject1
shashi@Mac-Shashi:/unix$ cp test1subject1.txt /unix/test1/subject1/test1subj1.txt
-i (Interactive): -i option asks the user for confirmation before copying a file that would overwrite an existing file [y : Yes , n : No]
shashi@Mac-Shashi:/unix$ cp -i test1subj1.txt /unix/test1/subject1
cp: overwrite 'unix/test1/subject1/test1subj1.txt'? y
Rename a file (Copy the file with a different name)
shashi@Mac-Shashi:/unix$ cp test1subject1.txt test1copied.txt
mv : (Move) Used to move files from one place to another
Syntax
mv [Option] source destination
Move file: Test1Subject2.txt into Test1/Subject2
shashi@Mac-Shashi:/unix$ mv test1subject2.txt /unix/test1/subject2
-i (Interactive): -i option asks the user for confirmation before moving a file that would overwrite an existing file [y : Yes , n : No]
shashi@Mac-Shashi:/unix$ mv -i test1subj2.txt /unix/test2/subject2
mv: overwrite '/unix/test2/subject2'? y
chmod : (Change mode) To change access permissions
Syntax
chmod [reference][operator][mode] file_name
Understanding file permissions
When we hit the ls -l commands, we see this string of letters, drwxrwxr-x. This represents the permissions that are set for this folder/file.
d-rw-r--r--
First character specifies the type, where
'd' : directory
'-' : file
Next 3 characters : Owner/User permissions
Next 3 characters : Owner/User's group permissions
Next 3 characters : Other group permissions
Reference:
'u' : user
'g' : group
'o' : other group
'a' : all
Operator:
'+' : add a permission
'-' : remove a permission
'=' : remove previous permissions and save current permission
Mode:
r : read
w : write
x : execute
[shashi@ubuntu ~]$ls -l
total 4
drwxrwxr-x. 2 shashi shashi 18 Sep 1 05:58 dir1
drwxrwxr-x. 3 shashi shashi 17 Aug 24 17:51 dir2
-rw-rw-r--. 1 shashi shashi 0 Dec 2 07:32 file1
-rw-rw-r--. 1 shashi shashi 0 Dec 2 07:32 file2
-rw-rw-r--. 1 shashi shashi 17 Dec 2 07:31 t2s1.txt
drwxrwxr-x. 3 shashi shashi 21 Dec 2 07:30 test1
drwxrwxr-x. 3 shashi shashi 21 Dec 2 07:30 test2
Add execute permission to File1
[shashi@ubuntu ~]$chmod u+x file1
We can see the difference in permission, when we enter ls -l command
Before
-rw-rw-r--. 1 shashi shashi 0 Dec 2 07:32 file1
After
-rwxrw-r--. 1 shashi shashi 0 Dec 2 07:32 file1
Give multiple permissions
In this example: we will give write and execute permission to other group for File1
[shashi@ubuntu ~]$chmod o+wx file1
We can see the difference in permission, when we enter ls -l command
Before
-rw-rw-r--. 1 shashi shashi 0 Dec 2 07:32 file1
After
-rwxrw-rwx. 1 shashi shashi 0 Dec 2 07:32 file1
Remove permission
Remove read permission of File1 from others
[shashi@ubuntu ~]$chmod o-r file1
We can see the difference in permission, when we enter ls -l command
Before
-rwxrw-rwx. 1 shashi shashi 0 Dec 2 07:32 file1
After
-rwxrw--wx. 1 shashi shashi 0 Dec 2 07:32 file1
wc : (Word count) Used to count words, characters and lines in file(s)
Syntax
wc [Option] fileName
Eg: wc file1.txt : Displays lines, words and no of characters in file1
shashi@Mac-Shashi:/unix$ wc file1.txt
3 13 63 file1.txt
Using tags: [l = lines, c = characters, w = words]
shashi@Mac-Shashi:/unix$ wc -l file1.txt
3 file1.txt
shashi@Mac-Shashi:/unix$ wc -c file1.txt
63 file1.txt
shashi@Mac-Shashi:/unix$ wc -w file1.txt
13 file1.txt
rm : Remove files or directories
Syntax
rm [Option] fileName
Delete file1
shashi@Mac-Shashi:/unix$ rm file1.txt
-i (Interactive Deletion): Ask the user for confirmation before removing each file, [ y = Yes, n = No ]
shashi@Mac-Shashi:/unix$ rm -i file2.txt
rm: remove regular file 'file2.txt'? y
Delete directory
-r : Remove directories and their contents
shashi@Mac-Shashi:/unix$ rm -r test1
[ -f (Force deletion): -f option removes the file forcefully ]
more : used to view the text files in the command prompt, displaying one screen at a time in case the file is large
shashi@Mac-Shashi:/unix$ more file1.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
head : prints the top N number of lines of the given input
shashi@Mac-Shashi:/unix$ head -5 file1.txt
line1
line2
line3
line4
line5
tail : prints the last N number of lines of the given input
shashi@Mac-Shashi:/unix$ tail -5 file1.txt
line6
line7
line8
line9
line10
Pipe "|" : Used to combine two or more commands, and in this, the output of one command acts as input to another command
[ Note : Data flows from left to right through the pipeline ]
Example: To print only 5th and 6th line of file1
- Use head command to get first 6 lines
- Use tail command to get last 2 lines
shashi@Mac-Shashi:/unix$ head -6 file1.txt | tail -2
line5
line6
tr command
tr : (translate) Used to translate or delete characters
Syntax :
$ tr [OPTION] SET1 [SET2]
Say, we have a file - file1.txt
shashi@Mac-Shashi:/unix$ cat file1.txt
this is file1
every text is written in lower case initially.
end of file1
Change f to F in file1.txt
shashi@Mac-Shashi:/unix$ tr 'f' 'F' < file1.txt
this is File1
every text is written in lower case initially.
end oF File1
Change file1.txt to uppercase
shashi@Mac-Shashi:/unix$ tr 'a-z' 'A-Z' < file1.txt
THIS IS FILE1
EVERY TEXT IS WRITTEN IN LOWER CASE INITIALLY.
END OF FILE1
-d : Used to delete character(s)
shashi@Mac-Shashi:/unix$ tr -d '0-9' < file3.txt
this is File
every text is written in lower case initially.
end oF File
tee command
tee : reads the standard input and writes it to both the standard output and one or more files
Syntax
tee [OPTION]... [FILE]...
Say we have this file
shashi@Mac-Shashi:/unix$ cat file1.txt
this is file1
every text is written in lower case initially.
end of file1
To save first 2 lines of file1.txt as newFile1
shashi@Mac-Shashi:/unix$ head -2 file1.txt | tee newFile1
this is file1
every text is written in lower case initially.
shashi@Mac-Shashi:/unix$ cat newFile1
this is file1
every text is written in lower case initially.
grep command
grep : filter searches a file for a pattern of characters and displays all lines that contain that pattern
(grep : globally search for regular expression and print out)
Syntax:
grep [options] pattern filename
Say we have this file
shashi@Mac-Shashi:/unix$ cat courses.txt
java
Java
c
C
c++
C++
kotlin
Kotlin
unix
Unix
bell
Search for "java"
shashi@Mac-Shashi:/unix$ grep java courses.txt
java
-i : Ignore case distinctions
shashi@Mac-Shashi:/unix$ grep -i java courses.txt
java
Java
-c : Count
shashi@Mac-Shashi:/unix$ grep -i -c java courses.txt
2
-n : Prefix each line of output with the line number within its input file.
shashi@Mac-Shashi:/unix$ grep -i -n java courses.txt
1:java
2:Java
Using wildcards to find patterns:
^ (caret)
^a : Find all lines and files starting with a
$ (dollar sign)
a$ : Find all lines and files ending with a
Find lines starting with k
shashi@Mac-Shashi:/unix$ grep -i "^k" courses.txt
kotlin
Kotlin
Note : When 2 caret are used, the result becomes invert.
Here, we get find all line starting with lower case ( [^a-z] ) and then we again write a caret ^ ( ^[^a-z] ) which reverts the output and finds lines that start with upper case instead.
shashi@Mac-Shashi:/unix$ grep "^[^a-z]" courses.txt
Java
C
C++
Kotlin
Unix
Find lines ending with n
shashi@Mac-Shashi:/unix$ grep "n$" courses.txt
kotlin
Kotlin
date : Used to display the system date and time
Syntax:
date [option]
Display the current date and time
shashi@Mac-Shashi:~$ date
Thu Dec 3 16:29:24 IST 2020
Display the month
shashi@Mac-Shashi:~$ date +%m
12
Display the month name
shashi@Mac-Shashi:~$ date +%h
Dec
Combine multiple commands
shashi@Mac-Shashi:~$ date +"%h %m"
Dec 12
D - The date in the format mm/dd/yy
H, M and S - The hour, minute and second, respectively
shashi@Mac-Shashi:~$ date +%D
12/03/20
man : used to display the user manual of any command (man command_name)
who : used to get information about currently logged in user on to system
uname : displays the information about the system (-a : For all info)
tty: prints the file name of the TERMINAL connected to standard input