Mastering Linux: Essential Commands for Every User

This guide covers key Linux commands across categories like file management, system monitoring, and networking. With examples and tips, it's a concise yet comprehensive resource for efficiently navigating and managing a Linux system. Perfect for beginners and pros alike.

user
Tilak Thapa

Tue, Jan 7, 2025

13 min read

thumbnail

1. File and Folder Management

Managing files and directories effectively is a crucial skill when working in Ubuntu or any Linux-based operating system. Below is a comprehensive guide to the most commonly used file and folder management commands, complete with examples, flags, and usage tips.

1. ls - List Directory Contents

The ls command lists files and directories in the current or specified directory.

1. Syntax:

ls [OPTIONS] [DIRECTORY]

2. Common Flags:

  • -l: Long format listing (displays permissions, ownership, size, etc.).
  • -a: Includes hidden files (those starting with .).
  • -h: Displays file sizes in human-readable format (e.g., KB, MB).
  • -R: Recursively lists all files in subdirectories.
  • -t: Sorts by modification time.

3. Examples:

bash
1.
ls -l
2.
ls -la /home/user
3.
ls -lh

2. pwd - Print Working Directory

The pwd command displays the full path of the current working directory.

1. Syntax:

pwd

2. Example:

pwd # Output: /home/user/documents

3. cd - Change Directory

The cd command changes the current directory to the specified one.

1. Syntax:

cd [DIRECTORY]

2. Usage Tips:

  • cd ~: Moves to the home directory.
  • cd ..: Moves up one directory.
  • cd -: Switches to the previous directory.

3. Examples:

bash
1.
cd /var/log
2.
cd ~/downloads
3.
cd ..

4. mkdir - Create Directories

The mkdir command creates new directories.

1. Syntax:

mkdir [OPTIONS] DIRECTORY_NAME

2. Common Flags:

  • -p: Creates parent directories as needed.
  • -v: Displays a message for each created directory.

3. Examples:

bash
1.
mkdir my_project
2.
mkdir -p projects/java/src

5. rmdir - Remove Empty Directories

The rmdir command removes empty directories.

1. Syntax:

rmdir [OPTIONS] DIRECTORY_NAME

2. Common Flags:

  • -v: Displays a message for each removed directory.

3. Example:

rmdir old_folder

6. rm - Remove Files or Directories

The rm command deletes files and directories.

1. Syntax:

rm [OPTIONS] FILE/DIRECTORY

2. Common Flags:

  • -r: Recursively deletes directories and their contents.
  • -f: Forces deletion without confirmation.
  • -i: Prompts for confirmation before deletion.

3. Examples:

bash
1.
rm file.txt
2.
rm -r my_folder
3.
rm -rf unwanted_folder

7. cp - Copy Files and Directories

The cp command copies files and directories.

1. Syntax:

cp [OPTIONS] SOURCE DESTINATION

2. Common Flags:

  • -r: Recursively copies directories.
  • -v: Displays the files being copied.
  • -u: Copies only when the source file is newer than the destination file.

3. Examples:

bash
1.
cp file.txt backup/
2.
cp -r project/ project_backup/

8. mv - Move or Rename Files and Directories

The mv command moves or renames files and directories.

1. Syntax:

mv [OPTIONS] SOURCE DESTINATION

2. Common Flags:

  • -v: Displays the files being moved.
  • -i: Prompts before overwriting files.

3. Examples:

bash
1.
mv file.txt /tmp/
2.
mv old_name.txt new_name.txt

9. touch - Create Empty Files or Update Timestamps

The touch command creates new, empty files or updates the timestamps of existing ones.

1. Syntax:

touch [OPTIONS] FILE_NAME

2. Common Flags:

  • -a: Updates the access time only.
  • -m: Updates the modification time only.

3. Examples:

bash
1.
touch new_file.txt
2.
touch -m file.txt

10. cat - Concatenate and Display File Content

The cat command displays the content of files or concatenates multiple files.

1. Syntax:

cat [OPTIONS] FILE

2. Common Flags:

  • -n: Numbers all output lines.
  • -E: Displays $ at the end of each line.

3. Examples:

bash
1.
cat file.txt
2.
cat -n file.txt
3.
cat file1.txt file2.txt > combined.txt # Concatenates files

11. less - View File Content Page by Page

The less command displays file content in a scrollable view.

1. Syntax:

less [OPTIONS] FILE

2. Common Flags:

  • /SEARCH_TERM: Searches for text within the file.
  • -N: Displays line numbers.

3. Example:

less file.txt

12. head - View the First Few Lines of a File

The head command displays the first 10 lines of a file by default.

1. Syntax:

head [OPTIONS] FILE

2. Common Flags:

  • -n N: Displays the first N lines.

3. Examples:

bash
1.
head file.txt
2.
head -n 5 file.txt

13. tail - View the Last Few Lines of a File

The tail command displays the last 10 lines of a file by default.

1. Syntax:

tail [OPTIONS] FILE

2. Common Flags:

  • -n N: Displays the last N lines.
  • -f: Follows the file, displaying new lines as they are added.

3. Examples:

bash
1.
tail file.txt
2.
tail -n 5 file.txt
3.
tail -f /var/log/syslog

14. find - Search for Files and Directories

The find command searches for files and directories based on specified criteria.

1. Syntax:

find [PATH] [OPTIONS]

2. Common Flags:

  • -name PATTERN: Searches for files matching the pattern.
  • -type [f/d]: Searches for files (f) or directories (d).
  • -size [+/-]N: Searches for files of specific sizes.
  • -exec COMMAND {} \;: Executes a command on the found files.

3. Examples:

bash
1.
find . -name "*.txt"
2.
find /home/user -type d
3.
find /var/log -size +10M

That sounds like an excellent blog topic! Below is a comprehensive guide on the list of Ubuntu commands you provided. Each section includes the basic usage, flags, and examples of how these commands can be used.

15. chmod (Change File Mode Bits)

chmod is used to change the permissions of a file or directory. Permissions can be modified for the owner, group, and others.

1. Syntax:

chmod [options] mode file

2. Flags/Options:

  • -R - Apply changes recursively (for directories).
  • -v - Verbose mode, shows the changes made.
  • -c - Reports only when a change has been made.
  • + - Adds a permission.
  • - - Removes a permission.
  • = - Sets the permission and removes others.

3. Usage:

  • Change permissions of a file:
chmod 755 myfile.txt # Owner can read/write/execute, others can read/execute
  • Recursively change directory permissions:
chmod -R 755 /path/to/directory # Apply 755 to all files and subdirectories

16. chown (Change File Owner and Group)

chown is used to change the owner and group of a file or directory.

1. Syntax:

chown [options] user[:group] file

2. Flags/Options:

  • -R - Apply changes recursively.
  • -v - Verbose mode, show what is being changed.
  • --from=current_user:current_group - Change only if the file's current owner and group match the specified one.

3. Usage:

  • Change owner and group of a file:
chown john:admin myfile.txt # Changes owner to john and group to admin
  • Recursively change owner and group for a directory:
chown -R john:admin /path/to/directory

17. chgrp (Change Group Ownership)

chgrp is used to change the group ownership of a file or directory.

1. Syntax:

chgrp [options] group file

2. Flags/Options:

  • -R - Apply changes recursively.
  • -v - Verbose mode, shows what is being changed.

3. Usage:

  • Change group ownership:
chgrp admin myfile.txt # Changes group to admin
  • Recursively change group ownership for a directory:
chgrp -R admin /path/to/directory

18. unmask (Set Default File Creation Permissions)

umask sets the default file creation permissions.

1. Syntax:

umask [permissions]

2. Usage:

  • Set a umask value:
umask 022 # Default file creation permissions: rw-r--r--
  • Check the current umask value:
umask

19. df (Disk Free Space)

df shows the available disk space on mounted filesystems.

1. Syntax:

df [options]

2. Flags/Options:

  • -h - Human-readable format (KB, MB, GB).
  • -T - Shows filesystem type.
  • -a - Includes pseudo, duplicate, and inaccessible filesystems.

3. Usage:

  • Check disk space in human-readable format:
df -h
  • Check filesystem type:
df -T

20. stat (Display File or File System Status)

stat provides detailed information about a file or filesystem.

1. Syntax:

stat [options] file

2. Flags/Options:

  • -c - Format the output as specified by a format string.
  • --format=format - Use a custom format to display information.
  • -f - Display information about the filesystem.

3. Usage:

  • View detailed file status:
stat myfile.txt
  • View file status with custom formatting:
stat -c "%s %y" myfile.txt # Displays size and last modified date

21. file (Determine File Type)

file determines the type of a file by examining its contents.

1. Syntax:

file [options] file

2. Flags/Options:

  • -i - Output MIME type.
  • -z - Try to look inside compressed files.

3. Usage:

  • Check file type:
file myfile.txt
  • Check MIME type:
file -i myfile.txt

22. tar (Archive Files)

tar is used to compress and extract files from archives.

1. Syntax:

tar [options] archive_name.tar files

2. Flags/Options:

  • -c - Create a new archive.
  • -x - Extract files from an archive.
  • -v - Verbose mode, shows the files being archived/extracted.
  • -f - Specifies the filename of the archive.
  • -z - Compress with gzip.
  • -j - Compress with bzip2.
  • -J - Compress with xz.
  • -C - Extract to a specified directory.

3. Usage:

  • Create a tar archive:
tar -cvf archive.tar myfile.txt # Creates archive.tar containing myfile.txt
  • Extract a tar archive:
tar -xvf archive.tar
  • Create a compressed tar.gz archive:
tar -czvf archive.tar.gz myfolder
  • Extract tar.gz archive:
tar -xzvf archive.tar.gz

Here's a detailed guide for the additional commands you mentioned. These commands are essential for compression, file transfer, mounting/unmounting, and text processing in a Linux environment.

23. gzip / gunzip (Compression/Decompression)

gzip is used to compress files, and gunzip is used to decompress them.

1. Syntax:

bash
1.
gzip [options] file
2.
gunzip [options] file.gz

2. Flags/Options:

  • -d - Decompress (for gzip only).
  • -v - Verbose mode, shows the compression ratio.
  • -c - Compress to standard output.
  • -k - Keep the original file after compression.
  • -l - List the contents of a compressed file.

3. Usage:

  • Compress a file:
gzip myfile.txt # Compresses myfile.txt into myfile.txt.gz
  • Decompress a file:
gunzip myfile.txt.gz # Decompresses myfile.txt.gz into myfile.txt
  • Keep the original file after compression:
gzip -k myfile.txt # Compress and keep the original file

24. zip / unzip (Archive/Extract Files)

zip is used to create compressed archives, and unzip is used to extract files from them.

1. Syntax:

bash
1.
zip [options] archive.zip file
2.
unzip [options] archive.zip

2. Flags/Options:

  • -r - Recursively zip files and directories.
  • -d - Extract to a specific directory.
  • -l - List the contents of a zip file.
  • -x - Exclude files from the zip archive.
  • -o - Overwrite existing files without prompting.

3. Usage:

  • Create a zip archive:
zip archive.zip myfile.txt # Creates archive.zip containing myfile.txt
  • Create a zip archive of a directory:
zip -r archive.zip /path/to/directory
  • Extract a zip archive:
unzip archive.zip # Extracts all files from archive.zip

3. rsync (Remote Synchronization)

rsync is a powerful tool used to synchronize files and directories between two locations, either on the same machine or across remote machines.

4. Syntax:

rsync [options] source destination

5. Flags/Options:

  • -r - Recursively copy directories.
  • -a - Archive mode, preserves symlinks, permissions, and timestamps.
  • -v - Verbose output.
  • -z - Compress file data during the transfer.
  • -e - Specify a remote shell program (e.g., SSH).
  • --delete - Delete extraneous files from the destination.

6. Usage:

  • Synchronize files from local to remote:
rsync -avz myfile.txt user@remote:/path/to/destination
  • Synchronize directories:
rsync -avz /local/dir user@remote:/remote/dir

25. scp (Secure Copy)

scp is used to securely transfer files and directories between hosts over SSH.

1. Syntax:

scp [options] source destination

2. Flags/Options:

  • -r - Copy directories recursively.
  • -P - Specify a port (if SSH runs on a different port).
  • -v - Verbose mode.
  • -q - Quiet mode (no output).
  • -C - Enable compression during transfer.

3. Usage:

  • Copy a file to a remote machine:
scp myfile.txt user@remote:/path/to/destination
  • Copy a directory to a remote machine:
scp -r mydirectory user@remote:/path/to/destination

26. mount / umount (Mount/Unmount Filesystems)

mount is used to attach a filesystem to a directory, and umount is used to detach it.

1. Syntax:

bash
1.
mount [options] device mount_point
2.
umount [options] mount_point

2. Flags/Options:

  • -t - Specify the filesystem type.
  • -o - Mount with specific options (e.g., ro for read-only).
  • -v - Verbose mode.

3. Usage:

  • Mount a filesystem:
mount /dev/sda1 /mnt # Mounts /dev/sda1 to /mnt
  • Unmount a filesystem:
umount /mnt # Unmounts the /mnt directory

27. alias (Create Aliases for Commands)

alias is used to create shortcuts for commands.

1. Syntax:

bash
1.
alias name='command'
2.
unalias name

2. Flags/Options:

  • -p - Display all currently defined aliases.
  • -g - Create a global alias.

3. Usage:

  • Create an alias:
alias ll='ls -la' # Creates an alias 'll' for 'ls -la'
  • Remove an alias:
unalias ll # Removes the alias 'll'

28. diff (Compare Files Line by Line)

diff is used to compare files and show the differences between them.

1. Syntax:

diff [options] file1 file2

2. Flags/Options:

  • -u - Unified format, provides context around the differences.
  • -r - Recursively compare directories.
  • -q - Show only whether the files differ, not the actual differences.

3. Usage:

  • Compare two files:
diff file1.txt file2.txt
  • Compare two directories:
diff -r dir1 dir2

29. wc (Word, Line, Character, Byte Count)

wc counts the number of lines, words, characters, and bytes in a file.

1. Syntax:

wc [options] file

2. Flags/Options:

  • -l - Count lines.
  • -w - Count words.
  • -c - Count characters.
  • -m - Count characters (multibyte-safe).
  • -L - Print the length of the longest line.

3. Usage:

  • Count lines, words, and characters:
wc myfile.txt
  • Count only lines:
wc -l myfile.txt

Will be continued...