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.