SH

Navigation & Files

Bash & Linux · 11 entries

Change Directory

syntax
cd [directory]
example
cd /var/log
cd ~
cd ..
cd -

Note cd with no argument returns to your home directory. cd - switches to the previous directory you were in, which is great for toggling between two locations.

List Directory Contents

syntax
ls [options] [directory]
example
ls -la /etc
ls -lhS ~/Downloads
ls -lt --color=auto
output
drwxr-xr-x  5 deploy staff  160 Mar 12 09:14 config
-rw-r--r--  1 deploy staff 2.4K Mar 11 18:30 app.js

Note -l for long format, -a includes hidden files (dotfiles), -h for human-readable sizes, -S sorts by size, -t sorts by modification time (newest first), -R lists recursively.

Print Working Directory

syntax
pwd [-P]
example
pwd
output
/home/deploy/projects/web-app

Note Use -P to resolve symlinks and show the physical path rather than the logical one.

Create Directories

syntax
mkdir [options] directory...
example
mkdir -p src/components/auth
mkdir -m 750 secrets

Note -p creates the full path including any missing parent directories and will not error if the directory already exists. -m sets permissions at creation time.

Copy Files & Directories

syntax
cp [options] source destination
example
cp config.yml config.yml.bak
cp -r templates/ /opt/app/templates/
cp -i report.csv ~/Desktop/

Note -r is required for directories (recursive copy). -i prompts before overwriting. Without -i, cp silently overwrites the destination. Use -a to preserve permissions, timestamps, and symlinks.

Move or Rename Files

syntax
mv [options] source destination
example
mv old_name.js new_name.js
mv *.log /var/archive/
mv -i data.db /backups/

Note mv both renames and relocates. It silently overwrites the target by default. Use -i to prompt before overwriting or -n to never overwrite.

Remove Files & Directories

syntax
rm [options] file...
example
rm debug.log
rm -r old_build/
rm -ri temp_data/

Note WARNING: rm is permanent. There is no trash can. -r removes directories recursively, -f forces removal without prompts. Never run rm -rf / or rm -rf * without double-checking your current directory with pwd first. Use -i for interactive confirmation on important files.

Create Empty File / Update Timestamp

syntax
touch [options] file...
example
touch index.html
touch -t 202601151030 report.pdf

Note If the file exists, touch updates its modification timestamp without changing content. With -t, you can set a specific timestamp in [[CC]YY]MMDDhhmm[.ss] format.

Find Files by Criteria

syntax
find [path] [expression]
example
find /var/log -name '*.log' -mtime -7
find . -type f -size +100M
find src/ -name '*.ts' -exec wc -l {} +
output
/var/log/syslog
/var/log/auth.log

Note -name is case-sensitive; use -iname for case-insensitive. -mtime -7 means modified in the last 7 days. -exec runs a command on each result; use + instead of \; to batch them for better performance.

Display Directory Tree

syntax
tree [options] [directory]
example
tree -L 2 --dirsfirst
tree -I 'node_modules|.git' -a
output
.
├── src/
│   ├── index.ts
│   └── utils/
├── package.json
└── tsconfig.json

Note -L limits depth, -I excludes patterns (pipe-separated), -a shows hidden files, --dirsfirst groups directories at the top. Not installed by default on all systems; install via your package manager.

Create Links

syntax
ln [options] target link_name
example
ln -s /opt/app/current/config.yml ~/config.yml
ln -sf /usr/bin/python3 /usr/local/bin/python

Note Without -s, a hard link is created (shares the same inode; cannot span filesystems). Symbolic links (-s) are like shortcuts and can point anywhere, including directories. -f replaces an existing link.