Skip to content

Bash Commands Cheat Sheet

Bash Commands - Cheat Sheet - cover image

Bash (Bourne Again Shell) is a widely used command-line shell and scripting language for Linux, macOS, and other Unix-based systems. Use this guide as a handy cheat sheet of common Bash commands to help you navigate and manage your system efficiently.

File and Directory Operations

  • pwd: Displays the current working directory.
  • ls: Lists files and directories in the current directory.
  • cd: Changes the current directory.
  cd path/to/directory
  • mkdir: Creates a new directory.
  mkdir new_directory
  • rm: Deletes a file or directory (-r for directories, -f to force).
  rm file_name
rm -rf directory_name
  • cp: Copies files or directories.
  cp source_file destination
cp -r source_directory destination_directory
  • mv: Moves or renames files or directories.
  mv old_name new_name

File Viewing and Editing

  • cat: Displays the content of a file.
  cat file.txt
  • less: Views file content one page at a time.
  less file.txt
  • nano: Opens the nano text editor to edit files.
  nano file.txt
  • touch: Creates a new empty file or updates the timestamp of an existing file.
  touch newfile.txt

System Information

  • uname -a: Shows detailed system information.
  • top: Displays active processes and system usage in real-time.
  • df -h: Shows available disk space in human-readable format.
  • free -h: Displays available and used memory.

Permissions and Ownership

  • chmod: Changes file or directory permissions.
  chmod 755 file.txt
  • chown: Changes file ownership.
  chown user:group file.txt
  • sudo: Runs commands with superuser privileges.
  sudo command

Process Management

  • ps: Lists running processes.
  ps aux
  • kill: Terminates a process by its PID (Process ID).
  kill PID
  • bg: Resumes a suspended process in the background.
  • fg: Brings a background process to the foreground.

Networking

  • ping: Tests connectivity to a network host.
  ping google.com
  • ifconfig: Displays or configures network interfaces.
  • curl: Transfers data from or to a server using different protocols.
  curl http://example.com

File Search

  • find: Searches for files and directories within a directory hierarchy.
  find /path -name file.txt
  • grep: Searches for a pattern within files.
  grep 'pattern' file.txt

Archiving and Compression

  • tar: Archives and compresses files.
  tar -czvf archive.tar.gz file_or_directory
  • unzip: Extracts compressed .zip files.
  unzip file.zip

Shell Scripting

  • echo: Prints text or variables to the terminal.
  echo "Hello, World!"
  • alias: Creates a shortcut for a command.
  alias ll='ls -la'
  • export: Sets environment variables.
  export PATH=$PATH:/new/path

Package Management (Ubuntu/Debian)

  • apt-get update: Updates the list of available packages.
  • apt-get install: Installs a package.
  sudo apt-get install package_name

Tips

  • Use tab for auto-completion of file and directory names.
  • Use Ctrl+C to stop a running process in the terminal.
  • Use Ctrl+Z to suspend a process and bg to resume it in the background.

Keep this cheat sheet handy to improve your efficiency when working with the Bash terminal!

Bash in AI

Bash is an essential skill for AI experts, as it plays a crucial role in several aspects of their work. It allows users to handle, process, and manipulate large datasets with ease. Automation is another key benefit; many AI workflows involve repetitive tasks like data preprocessing or model training, and Bash scripting can significantly streamline these processes, saving time and reducing the potential for errors.

Bash knowledge is also important for setting up and managing development environments, including installing dependencies and configuring virtual environments, which are often critical for AI projects. Many popular AI tools and frameworks, such as TensorFlow and PyTorch, can be invoked and managed through the command line, making proficiency in Bash highly beneficial.

Version control is integral to AI development, and Bash commands are commonly used alongside Git to track changes and collaborate effectively. Overall, while Bash may not be the core focus of AI, it enhances productivity and efficiency in managing the tasks and environments essential for successful AI development.

For more insights on learning AI, check out our article: How to Learn AI For Free: 2024 Guide From the AI Experts.

FAQs

What is Bash and why is it important?

Bash (Bourne Again Shell) is a command-line interface and scripting language widely used in Unix-based systems like Linux and macOS. It allows users to interact with the operating system, automate tasks, manage files, and run scripts, making it essential for system administrators and developers.

How can I view hidden files in a directory using Bash?

To view hidden files (which start with a dot), use ls -a. This will display all files, including hidden ones, in the directory.

What is the difference between > and >> in Bash?

The > operator overwrites a file with new content, while >> appends the new content to the end of the file.

For example:
echo "Hello" > file.txt # Overwrites file
echo "World" >> file.txt # Appends to file