Master the Linux Terminal: 18 Essential Commands You Need to Know

Essential Linux Commands: Your Cheat Sheet to Mastering the Terminal

·

8 min read

Table of contents

1. ls: Listing Directory Contents

  • Purpose: Lists the contents of a directory, providing information about files and subdirectories.

  • Syntax:ls [options] [directory_path]

  • Options:

    • -l: Displays a detailed listing, including file permissions, owner, group, size, and modification time.

    • -F: Adds file type indicators at the end of filenames (e.g., * for executable, / for directory).

    • -a: Shows all files, including hidden ones (starting with a dot).

    • -h: Displays human-readable file sizes (e.g., KB, MB, GB).

  • Examples:

    • ls: Lists the contents of the current working directory.

    • ls -l: Provides a detailed listing of the current directory.

    • ls -la /etc: Shows a detailed listing of all files (including hidden) in the /etc directory.

    • ls -hR: Lists all files (including hidden) recursively in the current directory and its subdirectories, displaying sizes in human-readable format.

2. cp: Copying Files

  • Purpose: Creates a copy of a file or directory.

  • Syntax:cp source_file destination_file/directory

  • Examples:

    • cp file1 file2: Copies file1 to file2 within the same directory.

    • cp file1 dir/: Copies file1 to a directory named dir.

    • cp -r dir1 dir2: Copies the entire directory dir1 and its contents to dir2. (Use the -r flag for recursive copying.)

  • Key takeaway: The destination can be a file or a directory. When copying directories, use -r to include subdirectories.

3. mv: Renaming and Moving Files

  • Purpose: Renames a file or moves it to a different location.

  • Syntax:mv source_file/directory destination_file/directory

  • Examples:

    • mv file1 new_name: Renames file1 to new_name within the same directory.

    • mv file1 dir/: Moves file1 to a directory named dir.

    • mv dir1/file2 dir3/: Moves the file file2 from dir1 to dir3.

  • Key takeaway:mv can be used for both renaming and moving. Use caution, as overwriting existing files can lead to data loss.

4. touch: Creating and Modifying Files

  • Purpose: Creates an empty file or updates the modification timestamp of an existing file.

  • Syntax:touch [options] file(s)

  • Options:

    • -a: Updates the access timestamp along with the modification time.

    • -c: Creates the file only if it doesn't exist.

  • Examples:

    • touch new_file: Creates an empty file named new_file.

    • touch -a existing_file: Updates the access and modification timestamps of existing_file.

    • touch -c file1 file2: Creates file1 and file2 only if they don't exist.

  • Key takeaway: Use touch to create empty files or update timestamps for existing files.

5. rm: Deleting Files (Use with Caution!)

  • Purpose: Removes files or directories. Caution: Use with extreme care, as deleted files are generally unrecoverable.

  • Syntax:rm [options] file(s)/directory

  • Options:

    • -f: Forces deletion without prompting for confirmation. (Use with caution!)

    • -r: Recursively removes directories and their contents.

  • Examples:

    • rm file1: Deletes the file file1.

    • rm -rf empty_dir: Caution! This permanently removes the directory empty_dir and all its contents.

  • Key takeaway: Use rm cautiously and only for files or directories you are sure you want to delete. Consider making backups before using rm.

6. echo: Displaying Text

  • Purpose: Prints text to the standard output (terminal).

  • Syntax:echo [options] string(s)

  • Options:

    • -e: Enables interpreting backslash escape sequences for special formatting (e.g., \n for newline, \t for tab).
  • Examples:

    • echo Hello, world!: Prints the message "Hello, world!" to the terminal.

    • echo -e "Line 1\nLine 2": Prints "Line 1" on a new line, followed by "Line 2" (using \n for newline).

    • echo $HOME: Prints the value of the environment variable HOME, which typically points to your home directory.

  • Key takeaway:echo is versatile for printing messages, testing commands, and displaying variable values. Use -e for basic formatting.

Navigating Directories

  • / - root directory

  • / is used as directory separator.

  • . is used to represent current working directory

  • .. is used to represent parent directory.

  • if you’re in /usr/lib, the path . is still /usr/lib, and ./X11 is /usr/lib/X11.

  • multiple sub-directories under root directory.

7. cd: Changing Directory

  • Purpose: Navigates to a different directory.

  • Syntax:cd [directory_path]

  • Examples:

    • cd ..: Moves to the parent directory.

    • cd /etc: Changes to the /etc directory.

    • cd ~: Goes to your home directory (shortcut for cd $HOME).

  • Key takeaway: Use cd to move between directories. Remember the directory structure and use absolute or relative paths accordingly.

8. mkdir: Creating Directories

  • Purpose: Creates a new directory.

  • Syntax:mkdir [options] directory_name

  • Options:

    • -p: Creates all necessary parent directories if they don't exist.
  • Examples:

    • mkdir new_dir: Creates a directory named new_dir in the current directory.

    • mkdir -p documents/subfolder1/subfolder2: Creates the directory structure documents/subfolder1/subfolder2 if it doesn't exist.

  • Key takeaway: Use mkdir to create new directories. The -p option ensures the entire directory path is created if needed.

9. rmdir: Removing Directories

  • Purpose: Removes an empty directory. Use with caution, as deleted directories cannot be recovered.

  • Syntax:rmdir [directory]

  • Examples:

    • rmdir empty_dir: Removes the directory named empty_dir.
  • Key takeaway:rmdir only works for empty directories. Use rm -rf cautiously for deleting directories with contents, but be aware of the permanent data loss risk.

10. Wildcards: Matching File and String Patterns

  • Purpose: Represent patterns to match multiple filenames or strings in commands.

  • Common wildcards:

    • *: Matches zero or more characters.

    • ?: Matches any single character.

    • []: Matches a set of characters (e.g., [a-z] for lowercase letters, [0-9] for digits).

  • Examples:

    • ls *.txt: Lists all files ending with .txt in the current directory.

    • cp image[0-9]*.jpg photos/: Copies all JPEG image files starting with "image" and followed by one or more digits to the "photos" directory.

  • Key takeaway: Wildcards are powerful for working with groups of files that share similar names. Use them carefully to avoid unintended matches.

11. grep: Searching Text Files

  • Purpose: Searches for lines in a file that match a specified pattern.

  • Syntax:grep [options] pattern file(s)

  • Options:

    • -i: Ignores case sensitivity during the search.

    • -r: Searches recursively within directories.

    • -w: Matches whole words only, not parts of words.

    • -v: Inverts the search, displaying lines that don't match the pattern.

  • Examples:

    • grep root /etc/passwd: Searches for the word "root" in the /etc/passwd file.

    • grep -i error *.log: Searches for lines containing "error" (case-insensitive) in all .log files in the current directory.

    • grep -r "system startup" /etc: Searches recursively within the /etc directory for lines containing "system startup".

    • grep -w root /etc/shadow: Searches for the complete word "root" (not just occurrences within other words) in the /etc/shadow file.

  • Key takeaway:grep is a powerful tool for finding specific text within files. Regular expressions can be used for more complex pattern matching.

12. less: Viewing Large Files

  • Purpose: Displays the contents of a file one screen at a time, allowing navigation through large files without overwhelming the terminal.

  • Syntax:less file

  • Navigation keys:

    • space: Moves down one page.

    • b: Moves up one page.

    • /pattern: Searches for the next occurrence of a pattern within the file.

    • q: Exits the less viewer.

  • Examples:

    • less /etc/hosts: Displays the /etc/hosts file contents page by page.

    • /root: Searches for the next line containing "root" within the file while using less.

  • Key takeaway:less is useful for browsing large files efficiently. Use the navigation keys to move around the file and search for specific content.

13. pwd: Displaying the Working Directory

  • Purpose: Shows the full path of the current working directory.

  • Syntax:pwd

  • Example:

    • pwd: Prints the current directory path (e.g., /home/your_username).
  • Key takeaway: Use pwd to confirm your location within the directory structure and ensure you are working in the intended directory.

14. diff: Comparing Files

  • Purpose: Compares the differences between two files line by line.

  • Syntax:diff file1 file2

  • Examples:

    • diff file1.txt file2.txt: Shows the line-by-line differences between file1.txt and file2.txt.
  • Key takeaway:diff helps identify changes made between two versions of a file. This is useful for tracking revisions, detecting errors, and managing code changes.

15. file: Identifying File Types

  • Purpose: Examines a file and attempts to determine its type based on its content.

  • Syntax:file file_name

  • Examples:

    • file mysterious_file: Reveals the type of the file named mysterious_file (e.g., "PNG image" or "text file").
  • Key takeaway: Use file to identify unknown file types, especially when dealing with downloaded files or files without extensions.

16. find:

  • Syntax:find [options] [path] [expression]

  • Options:

    • -name pattern: Searches for files by name (e.g., find . -name "*.txt" finds all files ending with ".txt" in the current directory and its subdirectories).

    • -type type: Searches for files based on type (e.g., find . -type f finds all regular files, find . -type d finds all directories).

    • -size +|- n: Searches for files based on size (e.g., find . -size +10M finds files larger than 10 megabytes).

    • -perm permission: Searches for files based on permission (e.g., find . -perm 755 finds files with read, write, and execute permissions for the owner).

17. locate:

  • Syntax:locate pattern

    💡
    locate relies on a pre-built database that needs to be updated periodically (usually with the updatedb command). This may not reflect the latest file system changes.

Examples:

  • find:

    • find /home/user -name "myfile.txt": Searches for the file "myfile.txt" in the user's home directory.

    • find . -type f -mtime -1: Finds all files modified in the last 24 hours within the current directory and its subdirectories (using -mtime -1 for modification time within 1 day).

  • locate:

    • locate *.pdf: Finds all files with the ".pdf" extension in the system (based on the updated database).

Key takeaway:

  • find is more powerful and flexible for complex searches.

  • locate is faster for simple searches based on filenames but relies on an updated database.

17. head and tail: Viewing File Head and Tail

  • Purpose:

    • head: Displays the beginning of a file.

    • tail: Displays the end of a file.

  • Syntax:

    • head [options] number file

    • tail [options] number file

  • Options:

    • -n number: Specifies the number of lines to display (default: 10).

Examples:

  • head -n 5 myreport.txt: Shows the first 5 lines of "myreport.txt".

  • tail -n 3 system.log: Displays the last 3 lines of "system.log".

Key takeaway:

  • Use head to preview the start of a file or check its content quickly.

  • Use tail to view the latest additions to a file, such as log entries, without scrolling through the entire content.

18. sort: Arranging Lines in Files

  • Purpose: Sorts the lines of a file in a specific order.

  • Syntax:sort [options] file

  • Options:

    • -n: Sorts numerically.

    • -r: Sorts in reverse order (descending).

Examples:

  • sort names.txt: Sorts the lines in "names.txt" alphabetically.

  • sort -nr system.log: Sorts the lines in "system.log" numerically in reverse order (largest to smallest).

Key takeaway:

  • sort is useful for organizing and analyzing data stored in text files.

  • Use options like -n and -r to control the sorting behavior.