Bash Commands Cheatsheet
bashEssential bash commands for developers.
File Operations
cp [-riv] source destinationCopy files or directories
cp file.txt backup.txt
cp -r src/ dest/ # recursive copy
cp -i *.js ./backup/ # interactive modemv [-iv] source destinationMove or rename files and directories
mv old.txt new.txt # rename
mv *.log /var/logs/ # move multiple filesrm [-rfi] file...Remove files or directories
rm file.txt
rm -rf node_modules/ # force recursive delete
rm -i *.tmp # interactive deletemkdir [-p] directory...Create directories
mkdir new_folder
mkdir -p path/to/nested/dir # create parentstouch file...Create empty file or update timestamp
touch newfile.txt
touch file1.js file2.jsln [-s] target link_nameCreate hard or symbolic links
ln -s /path/to/file symlink # symbolic link
ln original.txt hardlink.txt # hard linkViewing & Comparing Files
cat [-n] file...Display file contents
cat file.txt
cat -n script.sh # with line numbers
cat file1 file2 > combined.txtless [file]View file with pagination (q to quit)
less largefile.log
# Use /pattern to search, n for next matchhead [-n lines] fileDisplay first lines of file
head -n 20 file.txt # first 20 lines
head -c 100 file.txt # first 100 bytestail [-n lines] [-f] fileDisplay last lines of file
tail -n 50 app.log # last 50 lines
tail -f app.log # follow new lineswc [-lwc] file...Count lines, words, and characters
wc -l file.txt # line count only
wc -w essay.txt # word count onlydiff [-u] file1 file2Compare files line by line
diff old.txt new.txt
diff -u v1.js v2.js > changes.patchSearch & Transform
find [path] [options] [expression]Search for files in directory hierarchy
find . -name "*.js"
find /var -type f -mtime -7
find . -size +10M -deletegrep [-rinE] pattern [file...]Search text using patterns
grep "error" app.log
grep -r "TODO" ./src/
grep -E "^[0-9]+" data.txt # regexsed [-i] 's/pattern/replacement/g' fileStream editor for text transformation
sed 's/old/new/g' file.txt
sed -i 's/http/https/g' urls.txt
sed -n '10,20p' file.txt # print lines 10-20awk 'pattern {action}' filePattern scanning and text processing
awk '{print $1}' file.txt # first column
awk -F: '{print $1}' /etc/passwd
awk 'NR>1 {sum+=$2} END {print sum}' data.csvPermissions & Ownership
ls -la [path]List files with permissions and ownership
ls -la
# -rw-r--r-- 1 user group 1024 Jan 1 file.txtchmod [-R] mode file...Change file permissions
chmod 755 script.sh
chmod +x run.sh # add execute
chmod -R 644 ./docs/ # recursivechown [-R] user[:group] file...Change file owner and group
chown user:group file.txt
chown -R www-data:www-data /var/www/Process Management
ps [aux] [-ef]Display running processes
ps aux # all processes
ps aux | grep node # filter by namekill [-signal] pid...Send signal to process
kill 1234 # SIGTERM (graceful)
kill -9 1234 # SIGKILL (force)
kill -HUP 1234 # reload configtop [-u user]Interactive process viewer
top
top -u www-data # filter by userhtopEnhanced interactive process viewer
htop # requires installationbg [job_id]Resume suspended job in background
bg %1 # resume job 1 in backgroundfg [job_id]Bring background job to foreground
fg %1 # bring job 1 to foregroundjobs [-l]List background jobs
jobs -l # with process IDsnohup command &Run command immune to hangups
nohup ./server.sh &
nohup node app.js > output.log 2>&1 &Network
curl [-XOHI] [options] urlTransfer data from or to server
curl https://api.example.com/data
curl -X POST -d '{"key":"val"}' -H "Content-Type: application/json" url
curl -O https://example.com/file.zipwget [-qOc] urlDownload files from web
wget https://example.com/file.tar.gz
wget -c url # resume download
wget -q -O - url | bash # pipe to bashssh [-i key] [-p port] user@hostSecure shell remote login
ssh user@192.168.1.100
ssh -i ~/.ssh/key.pem ec2-user@host
ssh -p 2222 user@hostscp [-rP] source destinationSecure copy files between hosts
scp file.txt user@host:/path/
scp -r ./folder/ user@host:~/
scp user@host:/remote/file.txt ./local/nc [-lvz] host portNetcat for reading/writing network connections
nc -zv host 80 # port scan
nc -l 8080 # listen on port
echo "msg" | nc host 1234netstat [-tulpn]Display network connections and ports
netstat -tulpn # listening ports
netstat -an | grep ESTABLISHEDlsof [-i] [-p pid]List open files and network connections
lsof -i :3000 # what's using port 3000
lsof -p 1234 # files opened by PIDArchives & Compression
tar -czvf archive.tar.gz files...Create compressed tar archive
tar -czvf backup.tar.gz ./project/
tar -cvf archive.tar file1 file2tar -xzvf archive.tar.gz [-C dir]Extract tar archive
tar -xzvf backup.tar.gz
tar -xzvf backup.tar.gz -C /dest/gzip [-dk] fileCompress or decompress files
gzip large.log # creates large.log.gz
gzip -d file.gz # decompress
gzip -k file.txt # keep originalzip [-r] archive.zip files...Create zip archive
zip archive.zip file1.txt file2.txt
zip -r project.zip ./src/unzip [-d dir] archive.zipExtract zip archive
unzip archive.zip
unzip archive.zip -d ./extracted/Variables & Scripting
export VAR=valueSet environment variable for child processes
export PATH="$PATH:/new/path"
export NODE_ENV=productionecho [-en] stringPrint text to stdout
echo "Hello World"
echo -n "no newline"
echo -e "line1\nline2"$VAR or ${VAR}Access variable value
name="John"
echo "Hello $name"
echo "Path: ${HOME}/docs"if [ condition ]; then ... fiConditional execution
if [ -f "file.txt" ]; then
echo "exists"
else
echo "not found"
fifor var in list; do ... doneIterate over list of items
for f in *.js; do
echo "Processing $f"
donewhile [ condition ]; do ... doneLoop while condition is true
while read line; do
echo "$line"
done < file.txtfunction name() { ... }Define reusable function
greet() {
echo "Hello, $1!"
}
greet "World"cmd1 && cmd2Execute cmd2 only if cmd1 succeeds
npm test && npm run buildcmd1 || cmd2Execute cmd2 only if cmd1 fails
mkdir dir || echo "mkdir failed"cmd1 | cmd2Pass stdout of cmd1 to stdin of cmd2
cat file.txt | grep "error" | wc -l
ps aux | grep node | awk '{print $2}'Keyboard Shortcuts
Ctrl+CInterrupt and terminate current process
# Stop a running commandCtrl+ZSuspend current process (use bg/fg to resume)
# Pause process, then: bg or fgCtrl+DEnd of input / logout from shell
# Exit shell or end stdin inputCtrl+RReverse search command history
# Type to search, Enter to execute!!Repeat last command
sudo !! # run last command with sudo!$Use last argument of previous command
mkdir new_folder
cd !$ # cd new_folder