Unit 2 : Basic Linux Commands

Unit 2 : Basic Linux Commands
2.1 Directory Navigation Commands (pwd, cd, mkdir, rmdir, ls, tree)
2.2 File Management Commands (cat, rm, cp, mv, touch)
2.3 File Permissions and Ownership (chmod, chgrp, chown, umask)
2.4 Common System Commands (who, whoami, man, echo, date, clear)
2.5 Text Processing Commands (head, tail, cut, sort, cmp, tr, uniq, wc, tee)
2.6 Introduction to Process
2.7 Process Control commands : ps, fg, bg, kill, sleep
2.8 Job Scheduling commands : at, batch, crontab.
NOTES
2.1 Directory Navigation Commands
In the Linux operating system, files and directories are organized in a hierarchical structure. Navigating through this structure is an essential skill for working efficiently in the terminal. The following are basic commands used for directory navigation:
🔹 1. pwd
– Print Working Directory
Purpose:
Displays the current location (directory) in which the user is working.
Usage:

Explanation:
When you open a terminal, you are in a specific directory. The pwd
command tells you the full path of your present directory from the root (/
). This helps you know where you are in the system.
Example:

🔹 2. cd
– Change Directory
Purpose:
Allows you to move from one directory to another.
Usage:

Common Options:
cd ..
– Go one level up (to the parent directory)cd /
– Go to the root directorycd ~
– Go to the home directorycd -
– Go to the previous directory
Example:

🔹 3. mkdir
– Make Directory
Purpose:
Creates a new directory.
Usage:

Options:
mkdir -p path/dir1/dir2
– Creates nested directories at once
Example:

This will create a new folder named “Projects” in your current directory.
🔹 4. rmdir
– Remove Directory
Purpose:
Deletes an empty directory.
Usage:

Note:
It will not remove directories that contain files or subdirectories.
Example:

🔹 5. ls
– List Directory Contents
Purpose:
Displays the list of files and directories in the current directory.
Usage:

Common Options:
ls -l
– Detailed list (permissions, owner, size, date)ls -a
– Show hidden files (those starting with.
)ls -lh
– Human-readable file sizesls /path
– List contents of a specific directory
Example:

🔹 6. tree
– Display Directory Structure
Purpose:
Shows a tree-like view of directory and file structure.
Usage:


This command shows the nested structure of files and directories within “Documents”.
Note:
The tree
command may need to be installed separately using:

✅ Summary Table
Command | Full Form | Purpose |
---|---|---|
pwd |
Print Working Directory | Shows current directory path |
cd |
Change Directory | Moves to another directory |
mkdir |
Make Directory | Creates a new directory |
rmdir |
Remove Directory | Deletes an empty directory |
ls |
List | Lists files and folders |
tree |
Tree Structure Display | Displays nested file structure |
Commands: cat, rm, cp, mv, touch
Managing files is a key part of using the Linux terminal. These commands help you create, view, copy, move, and delete files. Let’s explore each one in detail.
🔹 1. cat
– Concatenate and Display File Contents
Purpose:
The cat
command is used to display the contents of a file on the terminal. It can also combine multiple files and create new ones.
Usage:

Other Uses:
Create a new file:

Combine multiple files:

Example:

🔹 2. rm
– Remove Files or Directories
Purpose:
Deletes files or directories permanently.
Usage:

Common Options:
rm -r foldername
– Remove a directory and its contentsrm -f file.txt
– Force delete without confirmationrm -rf *
– Delete all files and folders in the current directory (use with caution)
Example:

🔹 3. cp
– Copy Files and Directories
Purpose:
Copies files or folders from one location to another.
Usage:

Common Options:
cp file1.txt /home/user/Docs/
– Copy file to another foldercp -r dir1/ dir2/
– Copy an entire folder recursively
Example:

🔹 4. mv
– Move or Rename Files
Purpose:
Used to move files to another location or rename them.
Usage:

Examples:
Rename a file:

Move a file to another directory:

🔹 5. touch
– Create Empty Files
Purpose:
Creates a new empty file. It can also be used to update the timestamp of an existing file.
Usage:

Example:

✅ Summary Table
Command | Purpose | Common Use Example |
---|---|---|
cat |
View, combine, or create files | cat file.txt |
rm |
Delete files or directories | rm -r foldername/ |
cp |
Copy files or folders | cp file1.txt backup/ |
mv |
Move or rename files | mv old.txt new.txt |
touch |
Create empty files or update time | touch newfile.txt |
Unit 2.3: File Permissions and Ownership
In Linux, every file and directory has permissions and ownership attributes that control who can read, write, or execute them. Managing these permissions is important for system security and file access control.
Linux assigns three types of users to each file:
Owner: The user who created the file.
Group: A group of users who can share file access.
Others: Everyone else on the system.
And three types of permissions:
Read (r) – View file content or list directory.
Write (w) – Modify file or directory.
Execute (x) – Run the file (if it’s a script/program) or access directory.
🔹 1. chmod
– Change File Permissions
Purpose:
Used to modify file or directory permissions for the owner, group, or others.
Syntax:

Two Modes of chmod:
✅ Symbolic Mode:

u
= user (owner),g
= group,o
= others,a
= all+
adds permission,-
removes,=
sets exact permission
✅ Numeric (Octal) Mode:

r=4
,w=2
,x=1
→ 7 = rwx, 5 = r-x755
means: owner=rwx, group=r-x, others=r-x

2. chgrp
– Change Group Ownership
Purpose:
Used to change the group ownership of a file or directory.
Syntax:

Example:

3. chown
– Change Owner and Group
Purpose:
Used to change the owner and/or group of a file or directory.
Syntax:

Examples:

4. umask
– Set Default File Permissions
Purpose:
Sets the default permission values for new files and directories.
How It Works:
Default permission for a file is 666 (rw-rw-rw-)
Default for a directory is 777 (rwxrwxrwx)
The
umask
value subtracts from these defaults
Syntax:

Example:

Means:
File: 666 – 022 = 644 (rw-r–r–)
Directory: 777 – 022 = 755 (rwxr-xr-x)
To view current umask:

✅ Summary Table
Command | Purpose | Example Usage |
---|---|---|
chmod |
Change permissions | chmod 755 file.sh |
chgrp |
Change group ownership | chgrp staff notes.txt |
chown |
Change file owner and group | chown user:group report.pdf |
umask |
Set default permissions for new files | umask 022 |
Unit 2.4: Common System Commands
Linux offers a wide range of system commands to help users manage system activities, view information, and perform simple yet essential tasks. In this section, we explore six commonly used Linux commands:
1. who
– Show Logged-in Users
Purpose: Displays information about all users currently logged into the system.
Syntax:

Explanation:
This command lists all users currently using the system. It provides details like username, terminal name, login time, and sometimes the IP address or hostname if accessed remotely.
Example:

OUTPUT

2. whoami
– Display Current Username
Purpose: Displays the username of the current user.
Syntax:

Explanation:
This command is especially useful in multi-user environments. It helps verify the identity of the currently logged-in user, especially when working with different user accounts.
Example:

OutputÂ
