Linux Command Line Cheatsheet

Quick Reference Guide for Command Line Operations

Author

Sky (Kehan) Sheng

1 πŸ“ Basic Navigation & File Inspection

  1. ~ β€” Home directory
  2. pwd β€” Print current working directory
  3. ls β€” List directory contents
    • -l β€” Long format (e.g., ls -l)
    • -lt β€” Long format, sorted by time (newest first)
    • -ltr β€” Long format, sorted by time (oldest first)
    • -ltrh β€” Long format, oldest first, human-readable sizes
    • -a β€” Show all files including hidden ones
    • -1 - Show 1 file per line
  4. cd folder_name β€” Change directory
    • cd .. β€” Go to parent directory
    • cd . β€” Stay in current directory
  5. find β€” find files
    • e.g., find . -name "*fish*": find all the files whose name contains fish

2 πŸ“ Viewing File Content

  1. cat file.txt β€” Print entire content
  2. head file.txt β€” Print first 10 lines
  3. tail file.txt β€” Print last 10 lines

3 πŸ“ Editing Files

  1. nano file.txt β€” Open file in Nano editor
    • Ctrl+O to save, Ctrl+X to exit, Ctrl+K to delete a line
  2. vi file.txt β€” Open file in Vi editor
    • Press i to insert
    • Press Esc, then type :wq and hit Enter to save and quit
    • Type :q! and hit Enter to quit without saving

4 πŸ“ Creating and Modifying Files

  1. touch file.txt β€” Create an empty file or update timestamp if the file already exist
  2. echo "text" > file.txt β€” Write text to file (overwrite)
  3. echo "text" >> file.txt β€” Append text to file
  4. cat file1.txt file2.txt > file3.txt β€” Combine files

5 πŸ“ File Movement, Copying, and Deletion

  1. mv oldname.txt newname.txt β€” Rename a file
  2. mv file.txt folder/ β€” Move file to folder
  3. cp file.txt copy.txt β€” Copy file
  4. cp -r folder1 folder2 β€” Copy folder recursively
  5. rm file.txt β€” Delete a file
  6. rm -r folder/ β€” Recursively delete folder
  7. rm -f file.txt β€” Force delete file (no warning)
  8. rm -rf folder/ β€” Forcefully delete folder and contents

6 πŸ“ Directory Management

  1. mkdir new_folder β€” Create a new folder
  2. mkdir -p parent/child β€” Create nested folders including parent folders if parent folders do not exist yet
  3. rmdir empty_folder β€” Remove empty folder

7 πŸ“ Permissions & Ownership

  1. chmod 400 key.pem β€” Change file permission
    • first digit = privillage of owner of the file; second digit = other people in the same group; third digit = everyone else in the world
    • 4 = read, 2 = write, 1 = execute
    • 7 = 4 + 2 + 1 = read & write & execute
    • Examples:
      • 400 = read-only by owner
      • 600 = read/write by owner
      • 756 = full access for owner, read & execute for others in the same group, read & write for everyone else in the world
  2. chown user:group folder/ β€” Change ownership of the folder
  3. sudo β€” Run command with superuser privileges, β€œsuper user do”

8 πŸ“ Wildcards & Patterns

  1. * β€” Wildcard: matches anything
    • Example: mv fish* folder/
  2. ? β€” Matches a single unknown character

10 πŸ“ System & File Info

  1. history β€” Show command history
  2. df -h β€” Show disk space in human-readable format
  3. clear β€” Clear the screen
  4. wc file.txt β€” Count number of words in this file
    • -l β€” Count number of lines in this file
  5. sort file.txt β€” Sort lines alphabetically

11 πŸ“ Compression

  1. zip file.zip file.txt β€” Compress file
  2. unzip file.zip β€” Decompress file

12 πŸ“ Process Management

  1. Ctrl+C β€” Stop running command
  2. exit β€” exit the SSH connection to a server, or exit a mode of being a user
  3. COMMAND FILE1 | COMMAND β€” Connect output of commands
    • for instance, cat FILE1 | sort