File System & Navigation
File System & Navigation Navigation & File Operations # Navigation pwd # print working directory cd /var/log # absolute path cd ~/projects # home directory cd -…
File System & Navigation
Navigation & File Operations
# Navigation
pwd # print working directory
cd /var/log # absolute path
cd ~/projects # home directory
cd - # previous directory
cd .. # parent directory
# List files
ls # basic listing
ls -la # long format + hidden files
ls -lh # human-readable sizes
ls -lt # sorted by modification time
ls -lS # sorted by size
# File operations
cp file.txt backup.txt # copy file
cp -r dir/ newdir/ # copy directory recursively
mv old.txt new.txt # move/rename
rm file.txt # delete file
rm -rf dir/ # delete directory (DANGER: no undo)
mkdir -p a/b/c # create nested directories
touch file.txt # create empty file or update timestamp
ln -s /path/to/target link # create symlink
# View files
cat file.txt # print file contents
less file.txt # paginated viewer (q to quit)
head -n 20 file.txt # first 20 lines
tail -n 50 file.txt # last 50 lines
tail -f /var/log/app.log # follow log in real time
wc -l file.txt # count lines
wc -w file.txt # count words
# Find files
find /var/log -name "*.log" # find by name
find . -name "*.ts" -not -path "*/node_modules/*"
find . -type f -size +100M # files larger than 100MB
find . -mtime -7 # modified in last 7 days
find . -name "*.log" -delete # find and delete
# Disk usage
df -h # disk space by filesystem
du -sh /var/log/* # size of each item
du -sh . # size of current directory
ncdu # interactive disk usage browser (install separately)Text Processing
# grep — search text
grep "error" app.log # search for pattern
grep -i "error" app.log # case-insensitive
grep -n "error" app.log # show line numbers
grep -r "TODO" ./src # recursive search
grep -v "debug" app.log # invert match (exclude)
grep -E "error|warning" app.log # extended regex (OR)
grep -l "pattern" *.txt # only filenames
# sed — stream editor
sed 's/foo/bar/g' file.txt # replace all foo with bar
sed 's/foo/bar/2' file.txt # replace 2nd occurrence only
sed -i 's/foo/bar/g' file.txt # edit in-place
sed -i.bak 's/foo/bar/g' file.txt # edit with backup
sed '1,5d' file.txt # delete lines 1-5
sed -n '10,20p' file.txt # print lines 10-20
# awk — column processing
awk '{print $1}' file.txt # print first column
awk '{print $NF}' file.txt # print last column
awk -F: '{print $1}' /etc/passwd # colon-separated, field 1
awk '{sum+=$1} END{print sum}' nums # sum first column
awk 'NR==5' file.txt # print line 5
# sort, uniq, cut
sort file.txt # sort alphabetically
sort -n numbers.txt # sort numerically
sort -rn numbers.txt # sort numerically, descending
sort -k2 file.txt # sort by 2nd field
sort file.txt | uniq # unique lines
sort file.txt | uniq -c # count occurrences
cut -d: -f1,3 /etc/passwd # cut fields 1 and 3 (colon delimiter)
cut -c1-10 file.txt # cut first 10 charactersPermissions
# Permission format: -rwxrwxrwx (type, owner, group, others)
# r=4, w=2, x=1
chmod 755 script.sh # rwxr-xr-x (owner all, others read+exec)
chmod 644 file.txt # rw-r--r-- (owner read+write, others read)
chmod +x script.sh # add execute to all
chmod -w file.txt # remove write from all
chmod u+x,g-w file.txt # add exec to owner, remove write from group
chown user:group file.txt # change owner and group
chown -R user:group dir/ # recursive
# Special permissions
chmod +s binary # setuid/setgid
chmod +t /tmp # sticky bit (only owner can delete)
# sudo
sudo command # run as root
sudo -u postgres psql # run as different user
sudo su - # switch to root shell
visudo # edit sudoers safely