Skip to content

2.1 File Navigation and Management

Recall that everything starts at /.

  • 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.txt or ../file.txt).
CommandNameFunction
pwdPrint Working DirectoryTells you where you are right now.
cdChange DirectoryMoves you to a new folder.
cd ..Move up one level.
cd ~Move to your home directory.
cd -Go back to the previous directory.
lsListShows files in the current directory.

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).

Wildcards help select multiple files:

  • *.txt: All .txt files.
  • file?.txt: Matches file1.txt, fileA.txt, etc.
  • docs/*/index.md: Any index.md inside a subdirectory of docs/.
CommandFunctionExample
mkdirMake Directorymkdir projects
touchCreate empty filetouch notes.txt
cpCopycp notes.txt backup.txt
cp -rCopy Recursive (Folder)cp -r projects/ projects-backup/
mvMove (or Rename)mv notes.txt doc.txt
rmRemove (Delete)rm file.txt
rmdirRemove empty directoryrmdir old-empty-dir
rm -iInteractive confirmationrm -i file.txt

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 /etc ending 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 user alice.
    • 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.

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).

Locate the binary, source, and manual page files for a command.

  • Example: whereis ls

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 ~/.bashrc or ~/.profile
    • In zsh: add it to ~/.zshrc