2.1 File Navigation and Management
Hierarchy
Section titled “Hierarchy”Recall that everything starts at /.
Path concepts (file paths)
Section titled “Path concepts (file paths)”- Absolute Path: Starts with
/. Always works, no matter where you are. (e.g.,/home/user/docs/file.txt). - Relative Path: Does not start with
/. Depends on your current location. (e.g.,docs/file.txtor../file.txt).
Navigation Commands
Section titled “Navigation Commands”| Command | Name | Function |
|---|---|---|
pwd | Print Working Directory | Tells you where you are right now. |
cd | Change Directory | Moves you to a new folder. |
cd .. | Move up one level. | |
cd ~ | Move to your home directory. | |
cd - | Go back to the previous directory. | |
ls | List | Shows files in the current directory. |
ls Flags
Section titled “ls Flags”You rarely run just ls. Common flags:
ls -l: Long listing (permissions, owner, size, date).ls -a: All files (shows hidden files starting with.).ls -lh: Long listing with human-readable sizes (MB, GB).ls -la: Long listing of all files (including hidden).ls -ltr: Sort by date (most recent at the end).
Glob patterns (wildcards)
Section titled “Glob patterns (wildcards)”Wildcards help select multiple files:
*.txt: All.txtfiles.file?.txt: Matchesfile1.txt,fileA.txt, etc.docs/*/index.md: Anyindex.mdinside a subdirectory ofdocs/.
File Management
Section titled “File Management”| Command | Function | Example |
|---|---|---|
mkdir | Make Directory | mkdir projects |
touch | Create empty file | touch notes.txt |
cp | Copy | cp notes.txt backup.txt |
cp -r | Copy Recursive (Folder) | cp -r projects/ projects-backup/ |
mv | Move (or Rename) | mv notes.txt doc.txt |
rm | Remove (Delete) | rm file.txt |
rmdir | Remove empty directory | rmdir old-empty-dir |
rm -i | Interactive confirmation | rm -i file.txt |
Finding Files
Section titled “Finding Files”Common tools to locate files on the system:
Searching for files in a directory hierarchy.
- Syntax:
find [path] [expression] - Examples:
find /etc -name "*.conf": Find all files in/etcending with.conf.find . -type f: Find only files in current directory.find . -type d: Find only directories.find /var/log -size +10M: Find files larger than 10MB.find . -perm 777: Find files with 777 permissions.find . -mtime -1: Find files modified in the last 24 hours.find . -user alice: Find files owned by useralice.find . -iname "*.jpg": Case-insensitive match.find . -maxdepth 2 -name "*.md": Limit search depth.find . -not -path "*/node_modules/*": Exclude unwanted directories.find . -type f -name "*.log" -exec ls -l {} \;: Run an action for each result.
locate
Section titled “locate”Find files by name using a prebuilt database (faster than find but might be outdated).
- Syntax:
locate [pattern] - Update DB:
sudo updatedb(updates the database used by locate).
Locate a command.
- Example:
which python(shows the path to the python executable).
whereis
Section titled “whereis”Locate the binary, source, and manual page files for a command.
- Example:
whereis ls
$PATH and command resolution
Section titled “$PATH and command resolution”When you type a command, the shell searches for the executable in each directory in $PATH (separated by :).
- See your PATH:
echo $PATH - Temporarily add a directory:
export PATH="$HOME/bin:$PATH" - Make it persistent:
- In
bash: add the export to~/.bashrcor~/.profile - In
zsh: add it to~/.zshrc
- In