{ ILoveJS }

Bash Commands Cheatsheet

bash

Essential bash commands for developers.

10 sections · 59 items

File Operations

cp
cp [-riv] source destination

Copy files or directories

bash
cp file.txt backup.txt
cp -r src/ dest/      # recursive copy
cp -i *.js ./backup/  # interactive mode
mv
mv [-iv] source destination

Move or rename files and directories

bash
mv old.txt new.txt    # rename
mv *.log /var/logs/   # move multiple files
rm
rm [-rfi] file...

Remove files or directories

bash
rm file.txt
rm -rf node_modules/  # force recursive delete
rm -i *.tmp           # interactive delete
mkdir
mkdir [-p] directory...

Create directories

bash
mkdir new_folder
mkdir -p path/to/nested/dir  # create parents
touch
touch file...

Create empty file or update timestamp

bash
touch newfile.txt
touch file1.js file2.js
ln
ln [-s] target link_name

Create hard or symbolic links

bash
ln -s /path/to/file symlink   # symbolic link
ln original.txt hardlink.txt  # hard link

Viewing & Comparing Files

cat
cat [-n] file...

Display file contents

bash
cat file.txt
cat -n script.sh    # with line numbers
cat file1 file2 > combined.txt
less
less [file]

View file with pagination (q to quit)

bash
less largefile.log
# Use /pattern to search, n for next match
head
head [-n lines] file

Display first lines of file

bash
head -n 20 file.txt   # first 20 lines
head -c 100 file.txt  # first 100 bytes
tail
tail [-n lines] [-f] file

Display last lines of file

bash
tail -n 50 app.log    # last 50 lines
tail -f app.log       # follow new lines
wc
wc [-lwc] file...

Count lines, words, and characters

bash
wc -l file.txt    # line count only
wc -w essay.txt   # word count only
diff
diff [-u] file1 file2

Compare files line by line

bash
diff old.txt new.txt
diff -u v1.js v2.js > changes.patch

Search & Transform

find
find [path] [options] [expression]

Search for files in directory hierarchy

bash
find . -name "*.js"
find /var -type f -mtime -7
find . -size +10M -delete
grep
grep [-rinE] pattern [file...]

Search text using patterns

bash
grep "error" app.log
grep -r "TODO" ./src/
grep -E "^[0-9]+" data.txt  # regex
sed
sed [-i] 's/pattern/replacement/g' file

Stream editor for text transformation

bash
sed 's/old/new/g' file.txt
sed -i 's/http/https/g' urls.txt
sed -n '10,20p' file.txt  # print lines 10-20
awk
awk 'pattern {action}' file

Pattern scanning and text processing

bash
awk '{print $1}' file.txt       # first column
awk -F: '{print $1}' /etc/passwd
awk 'NR>1 {sum+=$2} END {print sum}' data.csv

Permissions & Ownership

ls -la
ls -la [path]

List files with permissions and ownership

bash
ls -la
# -rw-r--r-- 1 user group 1024 Jan 1 file.txt
chmod
chmod [-R] mode file...

Change file permissions

bash
chmod 755 script.sh
chmod +x run.sh       # add execute
chmod -R 644 ./docs/  # recursive
chown
chown [-R] user[:group] file...

Change file owner and group

bash
chown user:group file.txt
chown -R www-data:www-data /var/www/

Process Management

ps
ps [aux] [-ef]

Display running processes

bash
ps aux               # all processes
ps aux | grep node   # filter by name
kill
kill [-signal] pid...

Send signal to process

bash
kill 1234            # SIGTERM (graceful)
kill -9 1234         # SIGKILL (force)
kill -HUP 1234       # reload config
top
top [-u user]

Interactive process viewer

bash
top
top -u www-data   # filter by user
htop
htop

Enhanced interactive process viewer

bash
htop    # requires installation
bg
bg [job_id]

Resume suspended job in background

bash
bg %1    # resume job 1 in background
fg
fg [job_id]

Bring background job to foreground

bash
fg %1    # bring job 1 to foreground
jobs
jobs [-l]

List background jobs

bash
jobs -l   # with process IDs
nohup
nohup command &

Run command immune to hangups

bash
nohup ./server.sh &
nohup node app.js > output.log 2>&1 &

Network

curl
curl [-XOHI] [options] url

Transfer data from or to server

bash
curl https://api.example.com/data
curl -X POST -d '{"key":"val"}' -H "Content-Type: application/json" url
curl -O https://example.com/file.zip
wget
wget [-qOc] url

Download files from web

bash
wget https://example.com/file.tar.gz
wget -c url    # resume download
wget -q -O - url | bash  # pipe to bash
ssh
ssh [-i key] [-p port] user@host

Secure shell remote login

bash
ssh user@192.168.1.100
ssh -i ~/.ssh/key.pem ec2-user@host
ssh -p 2222 user@host
scp
scp [-rP] source destination

Secure copy files between hosts

bash
scp file.txt user@host:/path/
scp -r ./folder/ user@host:~/
scp user@host:/remote/file.txt ./local/
nc
nc [-lvz] host port

Netcat for reading/writing network connections

bash
nc -zv host 80       # port scan
nc -l 8080           # listen on port
echo "msg" | nc host 1234
netstat
netstat [-tulpn]

Display network connections and ports

bash
netstat -tulpn       # listening ports
netstat -an | grep ESTABLISHED
lsof
lsof [-i] [-p pid]

List open files and network connections

bash
lsof -i :3000        # what's using port 3000
lsof -p 1234         # files opened by PID

Archives & Compression

tar create
tar -czvf archive.tar.gz files...

Create compressed tar archive

bash
tar -czvf backup.tar.gz ./project/
tar -cvf archive.tar file1 file2
tar extract
tar -xzvf archive.tar.gz [-C dir]

Extract tar archive

bash
tar -xzvf backup.tar.gz
tar -xzvf backup.tar.gz -C /dest/
gzip
gzip [-dk] file

Compress or decompress files

bash
gzip large.log        # creates large.log.gz
gzip -d file.gz       # decompress
gzip -k file.txt      # keep original
zip
zip [-r] archive.zip files...

Create zip archive

bash
zip archive.zip file1.txt file2.txt
zip -r project.zip ./src/
unzip
unzip [-d dir] archive.zip

Extract zip archive

bash
unzip archive.zip
unzip archive.zip -d ./extracted/

Variables & Scripting

export
export VAR=value

Set environment variable for child processes

bash
export PATH="$PATH:/new/path"
export NODE_ENV=production
echo
echo [-en] string

Print text to stdout

bash
echo "Hello World"
echo -n "no newline"
echo -e "line1\nline2"
Variable usage
$VAR or ${VAR}

Access variable value

bash
name="John"
echo "Hello $name"
echo "Path: ${HOME}/docs"
if statement
if [ condition ]; then ... fi

Conditional execution

bash
if [ -f "file.txt" ]; then
  echo "exists"
else
  echo "not found"
fi
for loop
for var in list; do ... done

Iterate over list of items

bash
for f in *.js; do
  echo "Processing $f"
done
while loop
while [ condition ]; do ... done

Loop while condition is true

bash
while read line; do
  echo "$line"
done < file.txt
function
function name() { ... }

Define reusable function

bash
greet() {
  echo "Hello, $1!"
}
greet "World"
&& operator
cmd1 && cmd2

Execute cmd2 only if cmd1 succeeds

bash
npm test && npm run build
|| operator
cmd1 || cmd2

Execute cmd2 only if cmd1 fails

bash
mkdir dir || echo "mkdir failed"
pipe
cmd1 | cmd2

Pass stdout of cmd1 to stdin of cmd2

bash
cat file.txt | grep "error" | wc -l
ps aux | grep node | awk '{print $2}'

Keyboard Shortcuts

Ctrl+C
Ctrl+C

Interrupt and terminate current process

bash
# Stop a running command
Ctrl+Z
Ctrl+Z

Suspend current process (use bg/fg to resume)

bash
# Pause process, then: bg or fg
Ctrl+D
Ctrl+D

End of input / logout from shell

bash
# Exit shell or end stdin input
Ctrl+R
Ctrl+R

Reverse search command history

bash
# Type to search, Enter to execute
!!
!!

Repeat last command

bash
sudo !!    # run last command with sudo
!$
!$

Use last argument of previous command

bash
mkdir new_folder
cd !$    # cd new_folder

Related Content