File Management
Creates a temporary file with a random name. Guarantees that the new file doesn’t exist:
mktemp
Creates both new_file.txt and new_file_2.txt:
touch new_file.txt new_file_2.txt
touch {new_file, new_file_2}.txt
Creates file_name_a, file_name_b, and file_name_c:
touch file_name_{a..c}
Creates file_name_1, file_name_2, and file_name_3:
touch file_name_{1..3}
Copies file:
cp file_name.txt file_name_copy.txt
Moves/renames file:
mv file_name.txt new_file_copy.txt
Print file contents:
cat file_name.txt
Print first 10 lines:
head file_name.txt
Print last 10 lines:
tail file_name.txt
Remove file:
rm file_name.txt
rm -f file_name.txt # ignore if missing
Compression and Decompression
Archive files:
zip archive_name.zip file1 file2
tar -cvf archive_name.tar file1 file2
tar -zcf archive_name.tar.gz file1 file2
Archive directory:
zip -r archive_name.zip directory
tar -cvf archive_name.tar directory
tar -zcf archive_name.tar.gz directory
Extract archive:
unzip archive_name.zip
tar -xvf archive_name.tar
tar -zxvf archive_name.tar.gz
Replacing in Files
Replace text in place:
sed -i 's/search/replace/' file.txt
Replace globally and save to new file:
sed 's/search/replace/g' file.txt > new_file.txt
Search in Files
Search for text:
grep 'pattern' file.txt
Recursive search:
grep 'pattern' directory -r
Case insensitive:
grep -i 'pattern' directory
Show line numbers:
grep -n 'pattern' directory
Show matching filenames only:
grep -l 'pattern' directory
Invert match:
grep -v 'pattern' directory
Directories
Navigate:
cd directory
cd - # previous dir
cd ~ # home dir
cd .. # up one level
List contents:
ls
ls -a # include hidden
ls -l # long format
ls -lh # human readable
ls -t # sort by modification time
Show tree:
tree
tree -d # directories only
tree -a # include hidden
Create directories:
mkdir new_dir
mkdir -p parent/child/nested
Move/copy directories:
mv old_dir new_dir
cp -r directory directory_copy
Remove directories:
rmdir directory
rm -r directory
Symbolic Links
Create symlink:
ln -s path link
Overwrite symlink:
ln -sf path link
Permissions
Octal to symbolic mapping:
0 ---none1 --xexecute2 -w-write3 -wxwrite+exec4 r--read5 r-xread+exec6 rw-read+write7 rwxfull
Examples:
chmod 777 file # all perms (insecure)
chmod 755 file # owner rwx, others r-x
chmod 644 file # owner rw, others r
Change ownership:
chown user file
chown :group file
chown user:group file
Arrays
Indexed array:
arr=(one two three)
Associative array:
declare -A assoc=([key1]=val1 [key2]=val2)
Add element:
arr+=(new)
assoc+=( [key3]=val3 )
Print:
echo ${arr[@]}
echo ${!assoc[@]} # keys
echo ${assoc[@]} # values
Delete element:
unset arr[2]
unset assoc[key1]
Resource Usage and Processes
Show processes:
top
ps all
pidof name
Change priority:
nice -n 10 process
renice 10 pid
Kill process:
kill pid
killall name
Jobs/background:
jobs
jobs -p
System usage:
free # memory
du # directory sizes
df # disk usage
lsof # open files
Shutdown and Reboot
shutdown # 1 min\shutdown now # immediate
shutdown +10 # 10 min
shutdown -r # reboot
shutdown -c # cancel
reboot # reboot
reboot -f # force reboot
Scheduled Tasks (cron)
Format: minute hour day month weekday command
- Minute: 0–59
- Hour: 0–23
- Day: 1–31
- Month: 1–12
- Weekday: 0–6 (Sun=0 or 7)
Examples:
* * * * * cmd # every minute
@daily cmd # daily
@monthly cmd # monthly
@reboot cmd # on reboot
0 12,15,17 * * * cmd # specific hours
Edit/view/remove crontab:
crontab -e
crontab -l
crontab -r
Tmux Basics
tmux # start
<prefix> + % # split horizontal
<prefix> + " # split vertical
<prefix> + arrow # move panes
<prefix> + c # new window
<prefix> + n # next window
<prefix> + p # previous window
<prefix> + d # detach
HTTP Requests
curl https://example.com
curl -i https://example.com # include headers
curl -o file.txt https://example.com
curl -H "User-Agent: X" https://example.com
Network and DNS
ip addr # show IPs
ip route show # routes
ping host # ping
ping -c 15 -i 3 host # 15 pings every 3s
netstat -l # open ports
netstat -i # interface stats
traceroute host
nmap 0.0.0.0 # common ports
nmap 0.0.0.0 -p1-65535 # all ports
host example.net # DNS lookup
dig example.net # full DNS
dig example.net +short # short
SSH
ssh hostname # default user, port 22
ssh user@host # with username
ssh user@host -p 2222 # custom port