Mac command cheat sheet

1. Basic Navigation

  • pwd
    Prints the current working directory (where you are currently located in the file system).
  • ls
    Lists all files and directories in the current directory.
  • cd [directory]
    Changes the directory to the specified one.
    Example:
    cd Documents
  • cd ..
    Moves up one directory level.
  • cd ~
    Changes to the home directory.

2. File Management

  • touch [filename]
    Creates a new empty file.
    Example:
    touch index.html
  • mkdir [directory_name]
    Creates a new directory.
    Example:
    mkdir my_project
  • rm [filename]
    Deletes a file.
    Example:
    rm index.html
  • rm -r [directory_name]
    Deletes a directory and its contents recursively.
    Example:
    rm -r my_project
  • cp [source] [destination]
    Copies a file or directory to a new location.
    Example:
    cp index.html backup_index.html
  • mv [source] [destination]
    Moves or renames a file or directory.
    Example:
    mv index.html new_index.html

3. Viewing and Editing Files

  • cat [filename]
    Displays the contents of a file.
    Example:
    cat index.html
  • nano [filename]
    Opens a file in the Nano text editor for editing.
    Example:
    nano index.html
  • head [filename]
    Displays the first 10 lines of a file.
    Example:
    head index.html
  • tail [filename]
    Displays the last 10 lines of a file.
    Example:
    tail index.html

4. Search and Find

  • grep "[search_term]" [filename]
    Searches for a specific term within a file.
    Example:
    grep "title" index.html
  • find [directory] -name "[filename]"
    Searches for a file or directory by name.
    Example:
    find . -name "index.html"

5. Permissions

  • chmod [permissions] [filename]
    Changes the file or directory permissions.
    Example:
    chmod 755 index.html
  • sudo [command]
    Runs a command with superuser (admin) privileges.
    Example:
    sudo nano /etc/hosts

6. Networking

  • ping [domain]
    Checks the network connection to a server.
    Example:
    ping google.com
  • curl [url]
    Retrieves the content of a URL.
    Example:
    curl http://example.com

7. Version Control (Git)

  • git clone [repository_url]
    Clones a remote Git repository to your local machine.
    Example:
    git clone https://github.com/username/repository.git
  • git status
    Shows the current status of your Git repository.
  • git add [filename]
    Adds a file to the staging area.
    Example:
    git add index.html
  • git commit -m "[message]"
    Commits the staged files with a message.
    Example:
    git commit -m "Initial commit"
  • git push
    Pushes your commits to the remote repository.

8. Miscellaneous

  • clear
    Clears the terminal screen.
  • open [filename/directory]
    Opens a file or directory in the default application.
    Example:
    open index.html
    or
    open .
    (opens current directory in Finder)
  • history
    Displays a list of recently used commands.
  • exit
    Closes the terminal session.