Blog Paldi

Bash cheatsheet

Sure! Here’s a comprehensive Bash cheatsheet covering essential commands and features:


Bash Cheatsheet

Basic Commands

  • pwd – Print Working Directory
  • ls – List directory contents
  • ls -l – Long format
  • ls -a – Include hidden files
  • ls -lh – Human-readable sizes
  • cd – Change Directory
  • cd .. – Move up one directory
  • cd ~ – Go to home directory
  • echo – Display a line of text
  • echo "Hello, World!"

File Operations

  • touch filename – Create an empty file
  • cat filename – View file content
  • cp source destination – Copy file
  • mv source destination – Move/Rename file
  • rm filename – Remove file
  • rm -r directory – Remove directory and its contents
  • mkdir directory – Create a directory
  • rmdir directory – Remove an empty directory

Viewing and Editing Files

  • cat filename – Display file content
  • less filename – View file content page by page
  • more filename – View file content page by page
  • head filename – View the first few lines of a file
  • head -n 5 filename – View the first 5 lines
  • tail filename – View the last few lines of a file
  • tail -n 5 filename – View the last 5 lines
  • nano filename – Edit file using Nano editor
  • vim filename – Edit file using Vim editor

File Permissions

  • chmod permissions filename – Change file permissions
  • chmod 755 filename – Set read, write, execute for owner; read, execute for group and others
  • chown user:group filename – Change file owner and group

Searching

  • grep 'pattern' filename – Search for a pattern in a file
  • grep -r 'pattern' directory – Recursively search in a directory
  • grep -i 'pattern' filename – Case-insensitive search
  • grep -n 'pattern' filename – Show line numbers with matches
  • find /path -name filename – Find files by name
  • find /path -type d -name dirname – Find directories by name
  • find /path -mtime -1 – Find files modified in the last day

Process Management

  • ps – Display current processes
  • ps aux – Detailed view of all processes
  • top – Real-time process monitoring
  • kill PID – Terminate process with given PID
  • kill -9 PID – Force terminate process

Networking

  • ping hostname – Test network connectivity
  • ifconfig – Display network configuration
  • wget URL – Download files from the internet
  • curl URL – Transfer data from/to a server

Disk Usage

  • df – Display disk space usage
  • df -h – Human-readable format
  • du – Estimate file space usage
  • du -sh directory – Summary of directory size

Package Management (Debian/Ubuntu)

  • sudo apt-get update – Update package lists
  • sudo apt-get upgrade – Upgrade installed packages
  • sudo apt-get install package – Install a package
  • sudo apt-get remove package – Remove a package

Scripting Basics

  • Create a script file:
  #!/bin/bash
  echo "Hello, World!"
  • Make the script executable:
  chmod +x script.sh
  • Run the script:
  ./script.sh

Environment Variables

  • export VAR=value – Set an environment variable
  • echo $VAR – Access an environment variable
  • env – Display all environment variables

Miscellaneous

  • alias name='command' – Create an alias
  • alias ll='ls -lah'
  • history – Show command history
  • !! – Repeat last command
  • !n – Run nth command from history
  • clear – Clear the terminal screen

This should cover most of the common Bash commands and operations you might need. Feel free to ask if you need more detailed information on any specific command or topic!


Posted

in

by

Tags: