Assignment 1: Basic Unix Commands
1.echo
Purpose: The echo command outputs the text or strings and results of
computations to the terminal.
Example: tanwe@Tanweer:~$ echo "Hello"
Output: Hello
Echo command recognizes some escape characters like:
i. Backslash: ‘\\’
Purpose: Escapes the next character, treating it as literal or special.
Example: tanwe@Tanweer:~$ echo "This is a backslash: \\"
Output: This is a backslash: \
ii. Backspace: ‘\b’
Purpose: Represents a backspace in a string.
Example: tanwe@Tanweer:~$ echo -e "Hello\b World"
Output: Hell World
iii. Horizontal tab: ‘\t’
Purpose: Inserts a horizontal tab.
Example: tanwe@Tanweer:~$ echo -e "Hello\tWorld"
Output: Hello World
iv. New Line: ‘\n’
Purpose: Inserts a new line.
Example: tanwe@Tanweer:~$ echo -e "Hello\nWorld"
Output: Hello
World
v. Vertical tab: ‘\v’
Purpose: Inserts a vertical tab.
Example: tanwe@Tanweer:~$ echo -e "Hello\vWorld
Output: Hello
World
Page | 1
2.date
Purpose: The date command displays the current date and time.
Example: tanwe@Tanweer:~$ date
Output: Wed Nov 27 11:21:30 IST 2024
Specifiers like %m, %b, %y, etc., are used to customize formatting.
i. “date +%m”
Purpose: Displays the current month (numeric)
Example: tanwe@Tanweer:~$ date +%m
Output: 11
ii. “ date +%b ”
Purpose: Displays the current month (abbreviation)
Example: tanwe@Tanweer:~$ date +%b
Output: Nov
iii. “date +%y”
Purpose: Displays the last two digits of the current year
Example: tanwe@Tanweer:~$ echo -e date +%y
Output: 24
iv. “date +%I%P”
Purpose: Inserts a new line.
Example: tanwe@Tanweer:~$ echo -e date +%I%P
Output: 11am
v. ” date +%d ” "
Purpose: Insert a vertical tab.
Example: tanwe@Tanweer:~$ echo -e date +%d ”
Output: 27
vi. date '+%Y-%m-%d'
Example: tanwe@Tanweer:~$ date '+%Y-%m-%d'
Output: 2024-12-03
vii. date “+%T”
Example: tanwe@Tanweer:~$ date “+%T”
Output: 2024-12-03
Page | 2
3.cal
Purpose: Displays a calendar for the current month
Example: tanwe@Tanweer:~$ cal
Output:
November 2024
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
“cal 2024” displays the full calendar of the year 2024
4.who
Purpose: The who command displays the information of users currently logged
into the system. It returns the user’s ID/name, terminal, and the time at which he
or she logged in.
Example: tanwe@Tanweer:~$ who
Output: tanwe :0 2024-12-03 11:21 (: 0)
i. “who -u”
Purpose: Displays information about currently logged-in users along with their
idle time and process ID (PID).
Example: tanwe@Tanweer:~$ who -u
Output: tanwe :0 2024-12-03 08:30 00:25 5678 (: 0)
ii. “ whoami”
Purpose: Displays the name of the current user.
Example: tanwe@Tanweer:~$ whoami
Output: tanwe
5.uname
Purpose: Using this command, we can know the details of the system. Certain
options like r, v, m, and a can be used with this command.
Example: tanwe@Tanweer:~$ uname
Output: Linux
i. uname -r
Page | 3
Purpose: Displays the kernel release version.
Example: tanwe@Tanweer:~$ uname -r
Output: 5.10.16.3-microsoft-standard-WSL2
ii. uname -m
Purpose: Displays the machine hardware architecture.
Example: tanwe@Tanweer:~$ uname -m
Output: x86_64
iii. uname -v
Purpose: Displays the kernel version and build information.
Example: tanwe@Tanweer:~$ uname -v
Output: #1 SMP Fri Apr 2 22:23:49 UTC 2021
iv. uname -a
Purpose: Displays all system information in the following order:
Kernel name, network node hostname, kernel release date, kernel version,
machine hardware, hardware platform, operating system.
Example: tanwe@Tanweer:~$ uname -a
Output: Linux Tanwe 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2
22:23:49 UTC 2021 x86_64 GNU/Linux
6.cat
Purpose: Used to create small files and display the content of that file content.
i. cat > file_name.txt
Purpose: Creates the file.
Example: tanwe@Tanweer:~$ cat > test.txt
This is a text file created using 'cat'
^C
tanwe@Tanweer:~$
ii. cat test.txt
Purpose: Displays the content.
Example: tanwe@Tanweer:~$ cat test.txt
Output: This is a text file created using 'cat'
7.clear
Page | 4
Purpose: Clears all the text from the terminal screen.
Example: tanwe@Tanweer:~$ clear
Output: (Cleared terminal)
8.history
Purpose: It shows history if the commands used in the terminal.
Example: tanwe@Tanweer:~$ history
Output: 1 uname
2 uname -r
3 uname -m
4 uname -v
5 uname -a
6 cat > test.txt
7 cat test.txt
8 clear
9 pwd
10 history ….. and so on
9.pwd
Purpose: The ‘pwd’ command displays the absolute path of the current directory.
Example: tanwe@Tanweer:~$ pwd
Output: /home/tanwe
10. mkdir
Purpose: The ‘mkdir’ command is used to create new directory.
Example: tanwe@Tanweer:~$ mkdir OS
Output: (Creates a directory named ‘OS’)
11. ls
Purpose: Lists the files and directories in the current or specified directory.
Example: tanwe@Tanweer:~$ ls
Output: OS a.out childpid.c hello.sh hello2.c killchild.c pid.c test.sh
test2.sh ShellScripts child.c hello.c hello1.c hello3.c multiply.c processId.c
12. cd
Page | 5
Purpose: Change the current working directory to the specified one.
Example: tanwe@Tanweer:~$ cd OS
Output: tanwe@Tanweer:~/OS$
cd .. goes up one directory level
Example: tanwe@Tanweer:~/OS$ cd ..
Output: tanwe@Tanweer:~$
13. rmdir
Purpose: Removes a directory.
Example: tanwe@Tanweer:~$ rmdir OS
Output: (The directory ‘OS’ is removed)
14. ps
Purpose: Displays running processes for the current shell.
Example: tanwe@Tanweer:~$ ps
Output:
PID TTY TIME CMD
9 pts/0 00:00:00 bash
527 s/0 00:00:00 ps
15. ps -awx
Purpose: Displays running processes for the current shell.
Example: tanwe@Tanweer:~$ ps
Output:
PID TTY STAT TIME COMMAND
1? Sl 0:00 /init
7? Ss 0:00 /init
8? R 0:00 /init
9 pts/0 Ss 0:00 -bash
544 s/0 R+ 0:00 ps -awx
16. ls -l
Page | 6
Purpose: The ls -l command in Linux is used to list the files and directories in the
current directory with detailed information, including file permissions, ownership,
size, and timestamps.
Example: tanwe@Tanweer:~$ ls -l
Output:
total 88
drwxr-xr-x 2 tanwe tanwe 4096 Nov 23 19:35 OS
drwxr-xr-x 2 tanwe tanwe 4096 Nov 24 19:16 ShellScripts
drwxr-xr-x 2 tanwe tanwe 4096 Dec 3 20:17 Test
-rwxr-xr-x 1 tanwe tanwe 16176 Nov 24 17:50 a.out
-rw-r--r-- 1 tanwe tanwe 237 Nov 23 17:15 child7.c
-rw-r--r-- 1 tanwe tanwe 196 Nov 24 00:23 childpid.c
-rw-r--r-- 1 tanwe tanwe 116 Nov 23 16:49 hello.c
-rwxr-xr-x 1 tanwe tanwe 34 Nov 21 13:06 hello.sh
-rw-r--r-- 1 tanwe tanwe 115 Nov 23 13:24 hello1.c
-rw-r--r-- 1 tanwe tanwe 124 Nov 23 13:24 hello2.c
-rw-r--r-- 1 tanwe tanwe 133 Nov 23 13:27 hello3.c
-rw-r--r-- 1 tanwe tanwe 291 Nov 24 17:50 killchild.c
-rw-r--r-- 1 tanwe tanwe 310 Nov 23 16:57 multiply.c
-rw-r--r-- 1 tanwe tanwe 221 Nov 23 13:17 pid.c
-rw-r--r-- 1 tanwe tanwe 392 Nov 23 16:43 processId.c
-rwxrwxrwx 1 tanwe tanwe 111 Nov 24 18:03 test.sh
-rw-r--r-- 1 tanwe tanwe 40 Dec 3 19:53 test.txt
-rwxrwxrwx 1 tanwe tanwe 50 Nov 24 18:21 test2.sh
-rwxrwxrwx 1 tanwe tanwe 52 Nov 24 18:26 test3.sh
17. chmod
Purpose: The ‘chmod’ command is used to change the permissions of a file or
directory in Linux. Permissions determine who can read, write, or execute a file.
i. chmod 744 file_name
Purpose: Sets the permissions of the file so that:
The owner has read (r), write (w), and execute (x) permissions.
The group and others have read-only (r) permission.
Page | 7
Example: tanwe@Tanweer:~$ chmod 744 test.txt
(This sets the permissions for the file ‘test.txt’)
ii. chmod u+x file_name
Purpose: Adds execute (x) permission to the owner (u) of the file.
Example: tanwe@Tanweer:~$ chmod u+x test.txt
(Now the owner can execute the file.)
iii. chmod a-w file_name
Purpose: Removes write (w) permission for all users (owner, group, and
others).
Example: tanwe@Tanweer:~$ chmod a-w test.txt
(Now, no one can modify the file.)
iv. chmod g+w file_name
Purpose: Grants write (w) permission to the group (g).
Example: tanwe@Tanweer:~$ chmod g+w test.txt
(The group can now write to the file.)
Page | 8