Day 1 Linux: Basic Commands with Practical Examples

CommandDescriptionWhy We Use It?Best Practice
lsLists files and directories.To see available files in a directory.Use ls -lah for human-readable sizes and hidden files.
ls -lDetailed file listing (permissions, owner, size, date).To check file details and permissions.Combine with grep to filter specific files (e.g., `ls -l
ls -ltrLists files sorted by modification time (oldest first).To find recently modified files quickly.Use `ls -ltr
cdChanges the current directory.To navigate through directories.Use cd - to go back to the previous directory.
pwdPrints the current working directory.To confirm which directory you’re in.Use echo $PWD in scripts for debugging paths.
mkdirCreates a new directory.To organize files into folders.Use mkdir -p parent/child to create nested directories in one command.
rmDeletes files.To remove unwanted files.Always use rm -i for confirmation before deletion.
rmdirDeletes an empty directory.To remove unused directories.Use rmdir -p parent/child to remove parent directories if empty.
catDisplays file content.To quickly view a file’s content.Use less for large files instead of cat to avoid scrolling issues.
zcatDisplays the content of a compressed file.To read .gz files without extracting them.Combine with grep (`zcat file.gz
touchCreates 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.
headDisplays 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.
tailDisplays the last 10 lines of a file.To see recent logs or file endings.Combine with grep (`tail -n 50 file.log
tail -fContinuously monitors a file’s changes in real time.To track live logs (e.g., server logs).Use `tail -f file.log
less, moreOpens 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.
cpCopies files or directories.To duplicate files or backup data.Use cp -r for directories and cp -i to avoid overwriting files.
mvMoves or renames files and directories.To organize or rename files.Use mv -i to prompt before overwriting an existing file.
wcCounts 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.
viOpens 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.
cutExtracts 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.
teeWrites 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.
sortSorts file content alphabetically or numerically.To arrange and organize data.Use sort -u to remove duplicates while sorting.
clearClears the terminal screen.To remove clutter from the console.Use Ctrl + L as a shortcut to clear the terminal screen.
diffCompares 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 TypeSymbolOctal ValueDescription
Readr4Allows viewing file content or listing directory contents
Writew2Allows modifying or deleting file content
Executex1Allows executing a script or accessing a directory
No Permission-0No access

File Permission Representation

Owner (User)GroupOthersSymbolic RepresentationOctal Representation
Read, Write, ExecuteRead, WriteReadrwxrw-r--764
Read, WriteReadReadrw-r--r--644
Read, ExecuteRead, ExecuteReadr-xr-xr--554
Read, Write, ExecuteRead, ExecuteExecuterwxr-x--x751
Read, ExecuteReadNo Permissionr-xr-- ---540

Commands to Manage File Permissions

CommandDescriptionExample
ls -lLists files with permissionsls -l myfile.txt
chmod 755 filenameSets rwx for owner, rx for group and otherschmod 755 script.sh
chmod u+x filenameAdds execute permission for owner (user)chmod u+x script.sh
chmod g-w filenameRemoves write permission for groupchmod g-w document.txt
chmod o+r filenameAdds read permission for otherschmod o+r report.txt
chmod 644 filenameSets rw for owner, r for group and otherschmod 644 file.txt
chmod -R 700 dirnameSets full access for owner, no access for others recursivelychmod -R 700 myfolder/
chown user:group filenameChanges file ownership to a user and groupchown devops:developers project.txt
chown -R user:group directoryRecursively changes ownership of a directorychown -R cloudadmin:devops data/

Special File Permissions (chmod)

Special PermissionSymbolOctalDescription
Set User ID (SUID)s4Allows execution as the file owner
Set Group ID (SGID)s2Newly created files inherit the group
Sticky Bitt1Only the file owner can delete files

Commands for Special Permissions

CommandDescriptionExample
chmod u+s filenameAdds SUID permissionchmod u+s script.sh
chmod g+s directoryAdds SGID permissionchmod g+s shared_folder/
chmod +t directoryAdds 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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top