Command | Description | Why We Use It? | Best Practice |
---|---|---|---|
ls | Lists files and directories. | To see available files in a directory. | Use ls -lah for human-readable sizes and hidden files. |
ls -l | Detailed file listing (permissions, owner, size, date). | To check file details and permissions. | Combine with grep to filter specific files (e.g., `ls -l |
ls -ltr | Lists files sorted by modification time (oldest first). | To find recently modified files quickly. | Use `ls -ltr |
cd | Changes the current directory. | To navigate through directories. | Use cd - to go back to the previous directory. |
pwd | Prints the current working directory. | To confirm which directory you’re in. | Use echo $PWD in scripts for debugging paths. |
mkdir | Creates a new directory. | To organize files into folders. | Use mkdir -p parent/child to create nested directories in one command. |
rm | Deletes files. | To remove unwanted files. | Always use rm -i for confirmation before deletion. |
rmdir | Deletes an empty directory. | To remove unused directories. | Use rmdir -p parent/child to remove parent directories if empty. |
cat | Displays file content. | To quickly view a file’s content. | Use less for large files instead of cat to avoid scrolling issues. |
zcat | Displays the content of a compressed file. | To read .gz files without extracting them. | Combine with grep (`zcat file.gz |
touch | Creates an empty file or updates a file’s timestamp. | To create new files for scripts or logs. | Use touch -d 'yesterday' file.txt to create files with a past timestamp. |
head | Displays the first 10 lines of a file. | To preview the start of a file. | Use head -n 20 file.txt to customize the number of lines displayed. |
tail | Displays the last 10 lines of a file. | To see recent logs or file endings. | Combine with grep (`tail -n 50 file.log |
tail -f | Continuously monitors a file’s changes in real time. | To track live logs (e.g., server logs). | Use `tail -f file.log |
less, more | Opens large files for scrolling and viewing. | To read large files efficiently. | Use less +F file.log to view a file in real-time mode like tail -f . |
cp | Copies files or directories. | To duplicate files or backup data. | Use cp -r for directories and cp -i to avoid overwriting files. |
mv | Moves or renames files and directories. | To organize or rename files. | Use mv -i to prompt before overwriting an existing file. |
wc | Counts lines, words, and characters in a file. | To analyze file content size. | Use wc -l to count lines and wc -w to count words separately. |
vi | Opens a file in the vi text editor. | To edit files directly in the terminal. | Use vim instead for modern features, and learn shortcuts like :q! to exit without saving. |
ln (hard link, soft link) | Creates file links (hard or symbolic). | To create shortcuts or reference files. | Use ln -s target_file link_name for soft links to prevent file duplication. |
cut | Extracts specific sections from a file. | To process and filter data. | Use cut -d',' -f2 file.csv to extract the second column of a CSV file. |
tee | Writes command output to a file while displaying it. | To save and view results simultaneously. | Use tee -a to append instead of overwriting an existing file. |
sort | Sorts file content alphabetically or numerically. | To arrange and organize data. | Use sort -u to remove duplicates while sorting. |
clear | Clears the terminal screen. | To remove clutter from the console. | Use Ctrl + L as a shortcut to clear the terminal screen. |
diff | Compares differences between two files. | To track changes or debug scripts. | Use diff -u file1.txt file2.txt for a clearer side-by-side comparison. |
1. ls
– List Directory Contents
The ls
command is used to list files and directories.
Usage:
ls # List all files in the current directory
ls -l # List with detailed information (permissions, size, owner, etc.)
ls -a # Show hidden files (files starting with `.`)
ls -lh # List with human-readable file sizes
ls -lt # List files sorted by modification time
ls -ltr # List in reverse order of modification time
Example:
mkdir test_folder # Create a test folder
cd test_folder # Navigate inside the folder
touch file1.txt file2.txt # Create two files
ls -l # Check file details
2. cd
– Change Directory
The cd
command allows navigation between directories.
Usage:
cd /home/user # Change to the specified directory
cd Documents # Change to Documents folder (relative path)
cd .. # Move up one directory level
cd ../.. # Move up two levels
cd ~ # Change to home directory
cd - # Switch to the last visited directory
Example:
cd ~/Downloads # Go to the Downloads directory
cd - # Switch back to the last directory
3. cat
– Display File Contents
The cat
command is used to view, create, or concatenate files.
Usage:
cat filename.txt # Display file contents
cat file1.txt file2.txt # Concatenate and display multiple files
cat > newfile.txt # Create a new file and add text (Ctrl+D to save)
cat >> existing.txt # Append text to an existing file
Example:
echo "Hello World" > sample.txt # Create a file with content
cat sample.txt # Display the content
4. cp
– Copy Files and Directories
The cp
command is used to copy files or directories.
Usage:
cp file1.txt file2.txt # Copy file1.txt to file2.txt
cp file.txt /home/user/ # Copy file to a specific directory
cp -r folder1 folder2 # Copy directories recursively
Example:
mkdir backup # Create a backup directory
cp sample.txt backup/ # Copy file into backup
ls backup/ # Verify the copy
5. mv
– Move/Rename Files and Directories
The mv
command moves or renames files.
Usage:
mv oldname.txt newname.txt # Rename a file
mv file.txt /home/user/ # Move file to another directory
mv folder1 folder2/ # Move folder1 inside folder2
Example:
mv sample.txt document.txt # Rename sample.txt to document.txt
mv document.txt backup/ # Move document.txt to the backup folder
ls backup/ # Verify the move
6. rm
– Remove Files and Directories
The rm
command deletes files or directories.
Usage:
rm file.txt # Delete a file
rm -r folder/ # Delete a directory and its contents
rm -i file.txt # Prompt before deleting
rm -rf /directory # Force delete directory (use with caution)
Example:
rm document.txt # Delete a file
rm -r backup/ # Delete a directory with its contents
7. chmod
– Change File Permissions
The chmod
command modifies file permissions.
Linux File Permissions Table
Permission Type | Symbol | Octal Value | Description |
---|---|---|---|
Read | r | 4 | Allows viewing file content or listing directory contents |
Write | w | 2 | Allows modifying or deleting file content |
Execute | x | 1 | Allows executing a script or accessing a directory |
No Permission | - | 0 | No access |
File Permission Representation
Owner (User) | Group | Others | Symbolic Representation | Octal Representation |
---|---|---|---|---|
Read, Write, Execute | Read, Write | Read | rwxrw-r-- | 764 |
Read, Write | Read | Read | rw-r--r-- | 644 |
Read, Execute | Read, Execute | Read | r-xr-xr-- | 554 |
Read, Write, Execute | Read, Execute | Execute | rwxr-x--x | 751 |
Read, Execute | Read | No Permission | r-xr-- --- | 540 |
Commands to Manage File Permissions
Command | Description | Example |
---|---|---|
ls -l | Lists files with permissions | ls -l myfile.txt |
chmod 755 filename | Sets rwx for owner, rx for group and others | chmod 755 script.sh |
chmod u+x filename | Adds execute permission for owner (user) | chmod u+x script.sh |
chmod g-w filename | Removes write permission for group | chmod g-w document.txt |
chmod o+r filename | Adds read permission for others | chmod o+r report.txt |
chmod 644 filename | Sets rw for owner, r for group and others | chmod 644 file.txt |
chmod -R 700 dirname | Sets full access for owner, no access for others recursively | chmod -R 700 myfolder/ |
chown user:group filename | Changes file ownership to a user and group | chown devops:developers project.txt |
chown -R user:group directory | Recursively changes ownership of a directory | chown -R cloudadmin:devops data/ |
Special File Permissions (chmod
)
Special Permission | Symbol | Octal | Description |
---|---|---|---|
Set User ID (SUID) | s | 4 | Allows execution as the file owner |
Set Group ID (SGID) | s | 2 | Newly created files inherit the group |
Sticky Bit | t | 1 | Only the file owner can delete files |
Commands for Special Permissions
Command | Description | Example |
---|---|---|
chmod u+s filename | Adds SUID permission | chmod u+s script.sh |
chmod g+s directory | Adds SGID permission | chmod g+s shared_folder/ |
chmod +t directory | Adds Sticky Bit (prevents others from deleting files) | chmod +t /var/tmp/ |
Verify Permissions
To check file permissions:
ls -l filename
Example Output:
-rwxr-xr-- 1 user group 1234 Jan 1 12:34 script.sh
Owner (user
): rwx
(read, write, execute)
Group (group
): r-x
(read, execute)
Others: r--
(read)
Usage:
chmod 755 file.sh # Give execute permission to owner, read & execute to others
chmod u+x file.sh # Add execute permission for the owner
chmod g-w file.txt # Remove write permission from group
chmod o+r file.txt # Give read permission to others
Example:
touch script.sh # Create a script file
chmod +x script.sh # Make it executable
ls -l script.sh # Check permissions
8. chown
– Change File Ownership
The chown
command changes file or directory ownership.
Usage:
chown user:group file.txt # Change owner and group
chown user file.txt # Change only the owner
chown -R user:group folder/ # Change ownership recursively
Example:
sudo chown username file.txt # Change ownership to the current user
ls -l file.txt # Verify ownership change
8. pwd
– Print Working Directory
Displays the current directory path.
Usage:
pwd
Output:
/home/user/Documents
9. mkdir
– Create Directories
Used to create new directories.
Usage
mkdir project # Create a single directory
mkdir -p work/code # Create nested directories
10. rmdir
– Remove Empty Directories
Used to delete empty directories.
Usage
rmdir empty_folder
11. zcat
– View Compressed Files
Used to display compressed file content without unzipping.
Usage
zcat file.gz
12. touch
– Create Empty Files
Creates an empty file or updates the timestamp of an existing file.
Usage
touch newfile.txt
13. head
– View First 10 Lines of a File
Displays the first lines of a file.
Usage
head file.txt
head -20 file.txt # Display first 20 lines
14. tail
– View Last 10 Lines of a File
Displays the last lines of a file.
Usage
tail file.txt
tail -n 20 file.txt # Show last 20 lines
15. tail -f
– Monitor Log Files in Real-Time
Continuously displays new lines added to a file (useful for logs).
Usage
tail -f /var/log/syslog
16. less
& more
– View Large Files
Used for navigating large files efficiently.
Usage
less file.txt
more file.txt
less
allows backward and forward scrolling.more
only moves forward.
17. wc
– Word Count in a File
Counts lines, words, and characters in a file.
Usage
wc file.txt # Show line, word, and character count
wc -l file.txt # Show only line count
wc -w file.txt # Show only word count
18. vi
– Text Editor
Opens a file in the vi
text editor.
Usage
vi file.txt
- Press
i
to insert text. - Press
ESC
, type:wq
to save and exit.
19. cut
– Extract Specific Columns from a File
Used to extract fields from a text file.
Usage
cut -d',' -f1,3 file.csv # Extract 1st and 3rd columns
20. tee
– Save and Display Output
Redirects output to a file and displays it.
Usage
ls -l | tee output.txt
21. sort
– Sort File Contents
Sorts file contents alphabetically or numerically.
Usage
sort file.txt
sort -r file.txt # Reverse sort
22. clear
– Clear Terminal Screen
Clears the terminal window.
Usage
clear
23. diff
– Compare Two Files
Compares differences between two files.
Usage
diff file1.txt file2.txt