Learn how to navigate, manage, and automate with Bash
by Your Name
$ user@hostname:~/directory$
user — your usernamehostname — the machine name~/directory — current working directory
pwd # Print current directory
ls # List files
cd /path # Change directory
cd .. # Go up one directory
cd ~ # Go to home directory
touch file.txt # Create file
mkdir folder # Create directory
cp file1 file2 # Copy file
mv old new # Move or rename
rm file.txt # Remove file
rm -r folder # Remove directory recursively
cat file.txt # Print file contents
less file.txt # Scroll through file
head file.txt # Show first 10 lines
tail file.txt # Show last 10 lines
grep "text" file.txt # Search for text
ls -l # Show permissions
chmod 755 file.sh # Change permissions
chown user:group file # Change owner and group
Example permission format: -rwxr-xr--
ps # Show processes
top # Interactive process viewer
kill PID # Terminate process
kill -9 PID # Force kill process
jobs # List background jobs
fg / bg # Bring job to foreground/background
command > file # Redirect output to file
command >> file # Append output to file
command < file # Read input from file
command1 | command2 # Pipe output of one into another
Example: cat file.txt | grep "error"
echo $HOME
echo $PATH
export MYVAR="Hello"
echo $MYVAR
unset MYVAR
#!/bin/bash
# My first script
echo "Hello, $USER!"
pwd
ls
Save as script.sh, make executable: chmod +x script.sh
Run: ./script.sh
if [ -f file.txt ]; then
echo "File exists"
else
echo "File not found"
fi
for file in *.txt; do
echo $file
done
history # View command history
!45 # Run command #45 from history
alias ll='ls -lh' # Create alias
unalias ll # Remove alias
Ctrl + C – Cancel commandCtrl + L – Clear screenCtrl + R – Reverse search historyTab – Autocomplete!! – Repeat last commandman command for help&& or ;sudo for admin taskstmux or screen for multitasking“The command line is where power users live.”