The shell always has a current directory. Relative paths start from there.
pwd # print working directory
cd /var/log # absolute path
cd nginx # relative: /var/log/nginx
cd .. # parent directory
cd ~ (or just cd) # your home directory
cd - # back to the previous directory
ls -la # long listing, including hidden filesCreating, copying and moving:
mkdir -p app/config/env # -p creates missing parents, no error if it exists
cp file.txt backup/ # copy a file
cp -r src/ src-copy/ # -r for directories
mv old.txt new.txt # rename (or move)
rm -r build/ # delete a directory tree. No recycle bin!Globs are expanded by the shell before the command runs:
| Pattern | Matches |
|---|---|
* | any characters |
? | exactly one character |
[abc] | one of a, b, c |
{a,b} | expands to both words (brace expansion, not a glob) |
So rm *.log actually runs rm app.log error.log ….
The classic trap: * does not match hidden files (names starting with .). ls * will not show .env; use ls -a.
Tab completes names; press it twice to see choices. Use it all the time: it prevents typos in paths.
In interviews: "Difference between an absolute and a relative path?" An absolute path starts at / and works from anywhere; a relative path depends on the current directory.