Basic Commands
1. pwd (Print Working Directory)
• Usage: Displays the current working directory.
• Syntax:
pwd
2. ls (List Directory Contents)
• Usage: Lists files and directories in the current or specified directory.
• Syntax:
ls [options] [directory]
• Example:
ls -l # Lists files in long format
3. cd (Change Directory)
• Usage: Changes the current working directory.
• Syntax:
cd [directory]
• Example:
cd /home/user # Moves to /home/user directory
4. echo (Print Text to Terminal)
• Usage: Displays text or variable values.
• Syntax:
echo [text]
• Example:
echo "Hello, World!"
5. man (Manual Pages)
• Usage: Displays the manual for a command.
• Syntax:
man [command]
• Example:
man ls
Folder/ Directory manipulation Commands
1. mkdir (Make Directory)
• Usage: Creates a new directory.
• Syntax:
mkdir [directory_name]
• Example:
mkdir my_folder
2. rmdir (Remove Directory)
• Usage: Removes an empty directory.
• Syntax:
rmdir [directory_name]
• Example:
rmdir my_folder
File manipulation Commands
1. rm (Remove Files or Directories)
• Usage: Deletes files or directories.
• Syntax:
rm [options] [file/directory]
• Example:
rm file.txt # Removes file.txt
rm -r my_folder # Removes a directory and its contents
2. mv (Move or Rename Files)
• Usage: Moves or renames files/directories.
• Syntax:
mv [source] [destination]
• Example:
mv file1.txt file2.txt # Renames file1.txt to file2.txt
mv file.txt /home/user/ # Moves file.txt to /home/user/
3. cp (Copy Files or Directories)
• Usage: Copies files or directories.
• Syntax:
cp [source] [destination]
• Example:
cp file.txt /home/user/ # Copies file.txt to /home/user/
cp -r folder1 folder2 # Copies folder1 to folder2
4. cat (Concatenate and Display File Contents)
• Usage: Displays file contents.
• Syntax:
cat [file]
• Example:
cat file.txt
5. cmp (Compare Two Files Byte by Byte)
• Usage: Compares two files and reports differences.
• Syntax:
cmp [file1] [file2]
• Example:
cmp file1.txt file2.txt
6. wc (Word Count)
• Usage: Counts lines, words, and characters in a file.
• Syntax:
wc [options] [file]
• Example:
wc file.txt # Displays line, word, and byte count
wc -l file.txt # Displays only line count
7. comm (Compare Two Sorted Files Line by Line)
• Usage: Compares two sorted files and outputs unique and common lines.
• Syntax:
comm [file1] [file2]
• Example:
comm file1.txt file2.txt
8. diff (Compare Two Files Line by Line)
• Usage: Shows line-by-line differences between two files.
• Syntax:
diff [file1] [file2]
• Example:
diff file1.txt file2.txt
9. touch (Create or Update File Timestamp)
• Usage: Creates an empty file or updates the timestamp of an existing file.
• Syntax:
touch [file]
• Example:
touch newfile.txt
10. chmod (Change File Permissions)
• Usage: Modifies file permissions.
• Syntax:
chmod [mode] [file]
• Example:
chmod 755 script.sh # Sets read, write, execute for owner, read-execute for others
chmod u+x script.sh # Adds execute permission to the user
Text Processing Commands
1. sort – Sort lines of text files
The sort command is used to arrange the lines of a file in alphabetical or numerical order.
Syntax:
sort [options] filename
Examples:
Sort a file alphabetically:
sort file.txt
Sort numerically:
sort -n numbers.txt
Sort in reverse order:
sort -r file.txt
2. uniq – Remove duplicate lines
The uniq command filters out adjacent duplicate lines in a file.
Syntax:
uniq [options] filename
Examples:
Remove duplicates:
uniq file.txt
Count occurrences of each unique line:
uniq -c file.txt
Ignore case while comparing:
uniq -i file.txt
Note: uniq only removes consecutive duplicates, so use sort before uniq if necessary.
3. head – Display the first N lines of a file
The head command prints the first 10 lines of a file by default.
Syntax:
head -n N filename
Examples:
Display the first 10 lines (default):
head file.txt
Display the first 5 lines:
head -n 5 file.txt
4. tail – Display the last N lines of a file
The tail command prints the last 10 lines of a file by default.
Syntax:
tail -n N filename
Examples:
Display the last 10 lines (default):
tail file.txt
Display the last 5 lines:
tail -n 5 file.txt
Continuously monitor a file for new lines (useful for logs):
tail -f logfile.txt
5. paste – Merge lines of files
The paste command merges corresponding lines from multiple files.
Syntax:
paste file1 file2
Examples:
Merge two files line by line:
paste file1.txt file2.txt
Use a custom delimiter (e.g., ,):
paste -d "," file1.txt file2.txt
6. cut – Remove sections from each line
The cut command extracts specific columns or fields from a file.
Syntax:
cut [options] filename
Examples:
Extract the first 5 characters of each line:
cut -c 1-5 file.txt
Extract the second field from a comma-separated file:
cut -d "," -f 2 file.csv