CLI Cheatsheet

  1. Print Working Directory:
    • pwd: Shows the current directory you’re in.
  2. Change Directory:
    • cd [directory]: This changes your current directory.
      • For example: cd Documents will change your current directory to the „Documents“ folder.
    • cd ..: Moves you up one directory.
    • cd: Without any arguments, this command takes you to your home directory.
    • cd ~: Also takes you to your home directory.
    • cd -: Takes you to the previous directory you were in.
  3. List Files and Directories:
    • ls: Lists files and directories in the current directory.
    • ls -l: Lists in long format, showing additional information like permissions, number of links, owner, group, size, and time of last modification.
    • ls -a: Lists all entries including hidden files (those starting with a dot).
    • ls -la: Combines both the above options.
    • ls [directory]: Lists files and directories in the specified directory.
  4. Absolute vs. Relative Paths:
    • Absolute Path: Refers to the exact path from the root directory (begins with a /). For example: /Users/yourusername/Documents
    • Relative Path: Refers to the path relative to the current directory. For example, if you’re in /Users/yourusername, you can just type cd Documents instead of the full absolute path.
  5. Creating Directories:
    • mkdir [directory-name]: Creates a new directory.
  6. Removing Directories:
    • rmdir [directory-name]: Removes an empty directory.
    • rm -r [directory-name]: Removes a directory and its contents. Use with caution!
  7. Path Autocompletion:
    • When typing out paths, you can use the Tab key for autocompletion. For instance, typing cd Doc and then pressing Tab will autocomplete to cd Documents/ (assuming „Documents“ is the only directory starting with „Doc“).

Creating Files

  1. touch:
    • The touch command is the easiest way to create new, empty files. It can also be used to change the timestamps on existing files.bashCopy codetouch filename.txt This will create an empty file named filename.txt in the current directory.
  2. echo:
    • You can use the echo command to put content into a file.bashCopy codeecho "Hello, World!" > hello.txt This will create a file named hello.txt with the content „Hello, World!“. If hello.txt already exists, this command will overwrite it.If you want to append to the file instead of overwriting, use >>:bashCopy codeecho "Another line of text" >> hello.txt
  3. printf:
    • Similar to echo, but offers more formatting options.bashCopy codeprintf "Hello, World!\n" > hello.txt
  4. cat with redirection:
    • The cat command can be used to display the content of files, but combined with the redirection operator, it can also create files.bashCopy codecat > filename.txt After executing this command, you can start typing content for the file directly into the terminal. Once done, press Ctrl + D (EOF) to save the content and exit.
  5. nano, vi, or vim:
    • These are popular command-line text editors. You can use them to create and edit files.bashCopy codenano filename.txt This will open the nano text editor. After adding content, you can save and exit by pressing Ctrl + O followed by Ctrl + X.For vi or vim:bashCopy codevi filename.txt OrbashCopy codevim filename.txt After making changes in vi or vim, press Esc, type :wq and press Enter to save and exit.
  6. Using a file descriptor:
    • You can create a file by opening a file descriptor and then closing it.bashCopy code> filename.txt This is somewhat less commonly used but is a neat trick. It creates an empty file named filename.txt.