Skip to main content

Bash Cheatsheet

Bash is a shell scripting language that is used to automate repetitive tasks. The following are commonly used bash commands.

SSH Commands

  • ssh {user}@{host} - SSH into a remote host using a password.
  • ssh -i {keyfile} {user}@{host} - SSH into a remote host using a private key.

Filesystem Commands

  • pwd - Prints the current working directory.
  • ls - Lists the contents of the current directory.
  • cd - Changes the current working directory.
  • mkdir - Creates a directory.
  • touch - Creates a file.
  • rm - Removes a file or directory.
  • cp - Copies a file or directory.
  • mv - Moves a file or directory.
  • cat - Displays the contents of a file.
  • grep - Searches for a string in a file.
  • head - Displays the first few lines of a file.
  • tail - Displays the last few lines of a file.
  • less - Displays the contents of a file in a pager.
  • more - Displays the contents of a file in a pager.

Text Processing Commands

  • nano - A text editor.
  • vim - A text editor.
  • vi - A text editor.

Permissions

  • chmod - Changes the permissions of a file or directory.
  • chown - Changes the owner of a file or directory.
  • sudo - Runs a command as the super user.
  • su - Runs a command as another user.

Resource Management

  • top - Shows the processes running on the node.
  • htop - Interactive version of top.
  • kill {pid} - Kills a process by its PID.
  • kill -KILL {pid} - Forcefully kill a process.
  • killall -9 {name} - Kills all processes by name.
  • skill -KILL -u {user} - Kills all processes owned by a user and logs them out.
  • pkill -KILL -u {user} {process} - Kills all processes owned by a user.

User Management

  • w or who - Shows users logged in to the host.
  • whoami - Shows the current user.
  • ps -fu {user} - See running processes for a user.
  • passwd {user} - Set the password for a user.
  • cat /etc/passwd - List users on the system.
  • sudo usermod -l {new_login_name} {old_login_name} - Change the user's login name.
  • usermod -d {new_home_directory_path} username - Change the user's home directory.
  • userdel -r {username} - Delete a user.

Networking

  • curl - Makes a HTTP request.

Miscellaneous

  • man {topic} - Opens the manual for a topic.