Linux Operating System — Study Notes

Unit 02

Navigation, File Management, Permissions, System, Text Processing, Process Control & Job Scheduling Commands

7 Questions 5 Marks Each Linux Commands
01
5 Marks Question

Explain Navigation Commands

Navigation commands in Linux are used to view, create, remove, list, and move between directories through the terminal. The main directory navigation commands are pwd, cd, mkdir, rmdir, ls, and tree.

01
pwd — Print Working Directory

The pwd command displays the complete path of the directory in which the user is currently working.

Syntax:
pwd

Example:
$ pwd
/home/user

Here, /home/user is the current working directory.

02
cd — Change Directory

The cd command is used to change the current working directory.

Syntax:
cd directory_name
cd /tmp       # Moves to /tmp directory
cd ..         # Moves one directory level up
cd ~          # Moves to the home directory
cd /          # Moves to the root directory

An absolute path specifies the complete path, such as cd /home/user/Pictures. A relative path specifies a directory relative to the current location, such as cd Downloads.

03
mkdir — Make Directory

The mkdir command is used to create a new directory.

Syntax:
mkdir directory_name

Example:
mkdir mydirectory

This creates mydirectory inside the current directory. Multiple directories can also be created at the same time.

04
rmdir — Remove Directory

The rmdir command is used to remove an empty directory.

Syntax:
rmdir directory_name

Example:
rmdir mydirectory

If a directory contains files or subdirectories, rm -r can be used instead:

rm -r directory_name
05
ls — List Files and Directories

The ls command is used to display files and directories present in the current directory.

Syntax:
ls [option]

Important Options:

ls -a    # Displays all files, including hidden files
ls -l    # Displays permissions, owner, group, size, date and filename
ls -R    # Displays files recursively, including subdirectories

Options can also be combined, such as ls -al.

06
tree — Display Directory Tree

The tree command is used to display files and directories in a tree-like hierarchical structure.

Syntax:
tree [option] [directory]

Example:
tree Documents

It displays the contents of the Documents directory along with its subdirectories in tree form.

Conclusion Linux provides navigation commands such as pwd to show the current directory, cd to change directories, mkdir to create directories, rmdir to remove empty directories, ls to list contents, and tree to display the directory hierarchy.
02
5 Marks Question

Explain File Management Commands

File management commands in Linux are used to create, view, copy, move, rename, and delete files and directories. The main file management commands are cat, rm, cp, mv, and touch.

01
cat — Create and View Files

The cat command is used to create, display, combine, and copy the contents of text files.

cat > filename          # Creates a new text file
cat filename            # Displays the contents of a file
cat file1 file2 > file3 # Combines file1 and file2 into file3
02
rm — Remove Files

The rm command is used to delete files from the system.

Syntax:
rm [option] filename

Example:
rm sample.txt

Important Options:

rm -r folder     # Recursively removes a directory and its contents
rm -f file.txt   # Forcefully removes a file
rm -rf folder    # Forcefully removes a directory and its contents
03
cp — Copy Files and Directories

The cp command is used to copy files or directories from one location to another.

Syntax:
cp [OPTION] source destination

Example:
cp original_file new_file

This copies the contents of original_file into new_file while keeping the original file. To copy a directory:

cp -r folder1 copyfolder1

Here, -r recursively copies the directory and its contents.

04
mv — Move and Rename Files

The mv command is used to move a file from one location to another or rename a file.

mv filename new_location
Example: mv sample.txt /home/user/Documents

mv filename newfilename
Example: mv test.txt newtest.txt

If superuser permission is required, sudo can be used: sudo mv sample.txt /home/user/Documents.

05
touch — Create an Empty File

The touch command is used to create a new empty file or update the timestamp of an existing file.

Syntax:
touch [OPTION] FILE

Example:
touch demo.txt

Creates an empty file named demo.txt. Multiple files can also be created with touch file1 file2. If the file already exists, touch updates its timestamp instead of creating a new file.

Quick Revision

CommandFunction
catCreate, view and combine text files
rmDelete files/directories
cpCopy files/directories
mvMove or rename files
touchCreate empty files/update timestamps
In short cat handles file contents, rm deletes files, cp copies files, mv moves or renames files, and touch creates empty files or updates their timestamps.
03
5 Marks Question

Explain File Permissions and Ownership Commands

Linux is a multi-user operating system, so file permissions and ownership are used to protect files and directories from unauthorized access. Linux provides two levels of authorization: Ownership and Permissions.

File Ownership

Every file and directory has three categories of users:

User (u)
The owner of the file. By default, the person who creates the file becomes its owner.
Group (g)
A group can contain multiple users who share the same permissions.
Other (o)
All other users who are neither the owner nor members of the file's group.

File Permissions

Linux provides three basic permissions:

PermissionSymbolValueMeaning
Readr4Allows reading the file
Writew2Allows modifying the file
Executex1Allows executing the file

For example, -rwxrw-r-- means the owner has rwx, the group has rw-, and others have r--.

01
chmod — Change Permissions

The chmod (change mode) command is used to change the read, write, and execute permissions of files and directories.

Syntax:
chmod permissions filename

There are two ways to use chmod:

Numeric / Absolute Mode

Permissions are represented using numbers:

0 = No permission          5 = Read + Execute
1 = Execute                 6 = Read + Write
2 = Write                   7 = Read + Write + Execute
4 = Read
Example:
chmod 764 sample.txt

764 means:
7 → Owner  = read + write + execute
6 → Group  = read + write
4 → Others = read only

Symbolic Mode

Symbols are used to add or remove permissions:

chmod u+x file.txt    # Add execute permission to user
chmod g-w file.txt    # Remove write permission from group
chmod o+r file.txt    # Add read permission to others

Here, + adds permission, - removes permission, and = sets/overrides permission.

02
chown — Change Owner

The chown command is used to change the owner of a file or directory.

Syntax:
chown user filename
Example: chown faizan sample.txt

To change both the user and group:
chown user:group filename
Example: chown faizan:students sample.txt
03
chgrp — Change Group

The chgrp (change group) command changes the group ownership of a file or directory.

Syntax:
chgrp group_name filename

Example:
chgrp students sample.txt

This changes the group owner of sample.txt to students.

04
umask — Set Default Permissions

The umask command controls the default permissions assigned to newly created files and directories.

Before applying umask, the default file permission is 666 (rw-rw-rw-) and the default directory permission is 777 (rwxrwxrwx). The permissions are calculated as:

Permissions = Default Permissions - umask
umask         # Displays the current umask in numeric form
umask -s      # Displays the umask in symbolic form
umask 0022    # Changes the umask value to 0022

Quick Revision

CommandPurpose
chmodChanges file/directory permissions
chownChanges file/directory owner
chgrpChanges group ownership
umaskControls default permissions for new files/directories
In short Linux uses read (r), write (w), and execute (x) permissions for the user, group, and others. The commands chmod, chown, chgrp, and umask are used to manage these permissions and ownership settings.
04
5 Marks Question

Explain Common System Commands

Common system commands in Linux are used to perform basic system operations such as checking logged-in users, displaying information, getting help, viewing date and time, and clearing the terminal. The important common system commands are who, whoami, man, echo, date, and clear.

01
who — Display Logged-in Users

The who command displays information about the users currently logged into the system, such as login name, terminal name, and login time.

Syntax:
who [options] [filename]
who -a     # Displays all available information
who -b     # Displays the time of last system boot
who -q     # Displays logged-in usernames and their count
02
whoami — Display Current User

The whoami command displays the username of the currently logged-in/effective user.

Syntax:
whoami [option]

Example:
$ whoami
faizan

Options: whoami --help displays help information and whoami --version displays version information.

03
man — Manual Command

The man command is the built-in manual/help system of Linux. It provides detailed information about commands, including their syntax, description, options, and examples.

Syntax:
man [options] [section_number] command_name

Example:
man ls

This displays the manual page for the ls command. A manual page generally contains sections such as NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXAMPLES, FILES, and SEE ALSO.

04
echo — Display Text

The echo command displays a given string or command result on the standard output.

Syntax:
echo [option] [string]

Example:
echo "Hello, World!"
echo -n "Hello"     # Does not add a new line after output
echo -e "A\nB"      # Enables escape characters
echo -E "A\nB"      # Disables interpretation of escape characters

With -e, escape characters such as \n for a new line and \t for a horizontal tab can be used.

05
date — Display or Set Date and Time

The date command is used to display or set the system date and time. It can also display the date and time in customized formats.

Syntax:
date [option] [+format]

Example:
date
date -d "2 days ago"    # Displays a specified date
date -u                 # Displays date and time in UTC
date -s "..."           # Sets the system date/time

date +%Y     # Year
date +%m     # Month
date +%d     # Day

For example, date +%d/%m/%Y displays the date in day/month/year format.

06
clear — Clear Terminal

The clear command is used to clear all previous commands and output from the terminal screen, providing a clean terminal window.

Syntax:
clear

Example:
$ clear

After execution, the terminal screen is cleared.

Quick Revision

CommandPurpose
whoShows currently logged-in users
whoamiShows the current user's name
manDisplays the manual/help for commands
echoDisplays text or command results
dateDisplays or sets date and time
clearClears the terminal screen
In short These common system commands help users monitor users (who, whoami), get command help (man), display output (echo), manage date/time (date), and clean the terminal (clear).
05
5 Marks Question

Explain Text Processing Commands

Text processing commands in Linux are used to view, extract, sort, compare, transform, filter, count, and redirect text data stored in files. The important text processing commands are head, tail, cut, sort, cmp, tr, uniq, wc, and tee.

01
head — Display Beginning of a File

The head command displays the first lines of a file. By default, it displays the first 10 lines.

Syntax:
head [option] filename

head -n 5 sample.txt    # Displays first 5 lines
head -c 20 sample.txt   # Displays first 20 bytes
head -q file1 file2     # Does not display file-name headers
02
tail — Display End of a File

The tail command displays data from the end of a file. By default, it displays the last 10 lines. It is also useful for monitoring log files.

Syntax:
tail [options] [file]

tail -n 5 sample.txt    # Displays last 5 lines
tail -f sample.txt      # Continuously displays newly added data
tail -q file1 file2     # Removes file-name headers
03
cut — Extract Parts of Text

The cut command is used to extract selected characters, fields, or columns from each line of a file.

Syntax:
cut [option] filename

cut -c 1-5 file.txt      # Extracts characters 1 to 5
cut -f 1 file.txt        # Extracts the first field
cut -d "," -f 2 file.txt # Uses comma as delimiter and extracts field 2
04
sort — Sort Text

The sort command is used to arrange lines of text in a particular order, such as alphabetical or numerical order.

Syntax:
sort [option] filename

sort -r file.txt       # Sorts in reverse order
sort -n file.txt       # Sorts numerically
sort -u file.txt       # Sorts and removes duplicate lines
05
cmp — Compare Two Files

The cmp command compares two files byte by byte. It can be used to determine whether two files are identical and locate their first difference.

Syntax:
cmp [OPTION] FILE1 FILE2

cmp -s file1 file2     # Silent comparison
cmp -l file1 file2     # Displays all differing bytes
cmp -n 10 file1 file2  # Compares only first 10 bytes
06
tr — Translate Characters

The tr command is used to translate, replace, or delete characters from text.

Syntax:
tr [option] SET1 [SET2]

tr 'a-z' 'A-Z'    # Converts lowercase to uppercase
tr -d '0-9'       # Deletes digits
tr -s ' '         # Squeezes repeated spaces
tr -c '0-9'       # Complements the specified set

The -d option deletes characters, -s squeezes repeated characters, and -c complements the specified set.

07
uniq — Remove Duplicate Lines

The uniq command is used to detect and remove adjacent duplicate lines from a file.

Syntax:
uniq [OPTIONS] [INPUT_FILE [OUTPUT_FILE]]

uniq -c file.txt       # Displays occurrence count
uniq -d file.txt       # Displays only duplicate lines
uniq -u file.txt       # Displays only unique lines

uniq normally detects duplicates only when the duplicate lines are adjacent to each other.

08
wc — Word Count

The wc command is used to count the number of lines, words, characters, or bytes in a file.

Syntax:
wc [option] filename

wc -l file.txt       # Counts lines
wc -w file.txt       # Counts words
wc -c file.txt       # Counts bytes
09
tee — Display and Store Output

The tee command reads data from standard input and displays it on the screen while also writing it to a file.

Syntax:
command | tee [option] filename

Example:
ls | tee output.txt

This displays the output of ls on the terminal and also saves it in output.txt.

tee -a file.txt      # Appends instead of overwriting
tee -i file.txt      # Ignores interrupt signals
tee --help           # Displays help information

Quick Revision

CommandFunction
headDisplays beginning of a file
tailDisplays end of a file
cutExtracts selected fields/characters
sortSorts lines of text
cmpCompares two files
trTranslates/deletes characters
uniqRemoves adjacent duplicate lines
wcCounts lines, words, etc.
teeDisplays and saves output
In short These commands provide powerful tools for viewing, extracting, sorting, comparing, modifying, filtering, and counting text directly from the Linux terminal.
06
5 Marks Question

Explain Process Control Commands

A process is an instance of a program currently running in the system. Process control commands are used to view, control, move, and terminate running processes in Linux. The main process control commands are ps, bg, fg, top, kill, and nice.

01
ps — Process Status

The ps command displays information about the currently running processes. It is similar to the Task Manager in Windows.

Syntax:
ps [options]

ps -e        # Displays all running processes
ps -f        # Displays full information about processes
ps -u user   # Displays processes of a particular user
ps ux        # Detailed info about processes running under the user
ps 1234      # Checks a particular process using its PID
02
bg — Background Process

The bg command is used to resume a stopped process in the background. This allows the user to continue using the terminal while the process runs.

Syntax:
bg [job]

Example:
bg %1

This resumes job number 1 in the background. A command can also be directly started in the background using &: sleep 100 &.

03
fg — Foreground Process

The fg command brings a stopped or background process back to the foreground.

Syntax:
fg jobname

Example:
fg %1

This brings job number 1 to the foreground. A foreground process can be stopped using Ctrl + Z and then resumed using fg or bg.

04
top — Display Running Processes

The top command displays information about all running processes in real time.

Syntax:
top

It displays information such as PID (Process ID), USER (process owner), PR (priority), NI (nice value), %CPU, %MEM, and COMMAND. Press q to exit the top command.

05
kill — Terminate a Process

The kill command is used to terminate a running process. The PID of the process is required.

Syntax:
kill [option] PID

Example:
kill 1234
kill -15 1234     # Terminates the process normally
kill -9 1234      # Forcefully terminates the process
kill -1 1234      # Sends hangup/reload signal

The PID of a process can be found using pidof process_name.

06
nice — Set Process Priority

The nice command is used to start a process with a particular priority. The niceness value ranges from -20 to 19: a lower nice value means higher priority, and a higher nice value means lower priority. The default nice value is 0.

Syntax:
nice -n value process_name

Example:
nice -n 10 firefox

This starts firefox with a nice value of 10.

Quick Revision

CommandFunction
psDisplays process status
bgResumes a process in the background
fgBrings a process to the foreground
topDisplays running processes in real time
killTerminates a process
niceSets the priority of a process
In short Process control commands allow Linux users to monitor processes using ps and top, control foreground/background execution using fg and bg, terminate processes using kill, and control process priority using nice.
07
5 Marks Question

Explain Job Scheduling Commands

Job scheduling in Linux is used to automatically execute commands or tasks at a specified time or under specified conditions. The important job scheduling commands are at, batch, cron, and crontab.

01
at — Schedule a One-Time Job

The at command is used to schedule a command or job that will execute once at a specified time.

Syntax:
at [option] time

Example:
at 10:30 PM
at> echo "Backup Started" > backup.txt
at> Ctrl+D

This schedules the command to execute at 10:30 PM. Jobs can also be scheduled using relative time: at now + 1 hour.

at -l             # Lists scheduled jobs
at -c 7           # Displays contents of job number 7
at -m 10:30 PM    # Sends mail after job completion

The -M option can be used to avoid sending email notifications.

02
batch — Schedule Based on System Load

The batch command schedules a job to execute when the system load becomes low, rather than at an exact specified time.

Syntax:
batch

Example:
batch
at> echo "Job Executed" >> output.txt
at> Ctrl+D

A command can also be passed directly to batch: echo "touch example.txt" | batch. After entering the commands, Ctrl+D is pressed to finish scheduling the job.

03
cron — Schedule Recurring Jobs

Cron is a Linux service used to execute commands or scripts automatically at regular intervals. It is useful for repetitive tasks such as taking backups every day, running scripts every hour, or performing maintenance every week.

Cron jobs are configured using a crontab (cron table). A cron schedule contains five time fields:

Minute  Hour  Day  Month  Weekday  Command
  *      *     *     *       *      command

Example:
30 10 * * * /home/user/backup.sh

This executes backup.sh every day at 10:30 AM.

04
crontab — Manage Cron Jobs

The crontab command is used to create, view, edit, and remove recurring cron jobs.

Syntax:
crontab [option]

crontab -e     # Edits/adds cron jobs
crontab -l     # Lists existing cron jobs
crontab -r     # Removes all cron jobs
Example:
crontab -e

Then a recurring job can be added:
0 8 * * * /home/user/script.sh

This executes script.sh every day at 8:00 AM.

Quick Revision

CommandPurpose
atSchedules a one-time job at a specified time
batchRuns a job when system load is low
cronService for recurring/periodic jobs
crontabCreates and manages cron schedules
In short Use at for one-time tasks, batch for tasks based on system load, and cron/crontab for tasks that need to run repeatedly at scheduled intervals.