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.
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.
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.
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.
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
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.
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.
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.
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.
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
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
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.
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.
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
| Command | Function |
|---|---|
| cat | Create, view and combine text files |
| rm | Delete files/directories |
| cp | Copy files/directories |
| mv | Move or rename files |
| touch | Create empty files/update timestamps |
cat handles file contents, rm deletes files, cp copies files, mv moves or renames files, and touch creates empty files or updates their timestamps.
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:
File Permissions
Linux provides three basic permissions:
| Permission | Symbol | Value | Meaning |
|---|---|---|---|
| Read | r | 4 | Allows reading the file |
| Write | w | 2 | Allows modifying the file |
| Execute | x | 1 | Allows executing the file |
For example, -rwxrw-r-- means the owner has rwx, the group has rw-, and others have r--.
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.
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
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.
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
| Command | Purpose |
|---|---|
| chmod | Changes file/directory permissions |
| chown | Changes file/directory owner |
| chgrp | Changes group ownership |
| umask | Controls default permissions for new files/directories |
chmod, chown, chgrp, and umask are used to manage these permissions and ownership settings.
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.
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
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.
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.
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.
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.
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
| Command | Purpose |
|---|---|
| who | Shows currently logged-in users |
| whoami | Shows the current user's name |
| man | Displays the manual/help for commands |
| echo | Displays text or command results |
| date | Displays or sets date and time |
| clear | Clears the terminal screen |
who, whoami), get command help (man), display output (echo), manage date/time (date), and clean the terminal (clear).
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.
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
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
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
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
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
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.
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.
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
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
| Command | Function |
|---|---|
| head | Displays beginning of a file |
| tail | Displays end of a file |
| cut | Extracts selected fields/characters |
| sort | Sorts lines of text |
| cmp | Compares two files |
| tr | Translates/deletes characters |
| uniq | Removes adjacent duplicate lines |
| wc | Counts lines, words, etc. |
| tee | Displays and saves output |
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.
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
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 &.
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.
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.
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.
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
| Command | Function |
|---|---|
| ps | Displays process status |
| bg | Resumes a process in the background |
| fg | Brings a process to the foreground |
| top | Displays running processes in real time |
| kill | Terminates a process |
| nice | Sets the priority of a process |
ps and top, control foreground/background execution using fg and bg, terminate processes using kill, and control process priority using nice.
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.
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.
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.
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.
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
| Command | Purpose |
|---|---|
| at | Schedules a one-time job at a specified time |
| batch | Runs a job when system load is low |
| cron | Service for recurring/periodic jobs |
| crontab | Creates and manages cron schedules |
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.