pwd — where am I?
pwd (print working directory) prints the absolute path of the directory you
are currently in.
$ pwd
/home/user
ls — list files
ls lists files and directories. Without arguments it shows the current
directory.
$ ls
Documents Downloads Music Pictures
Common flags:
| Flag | Meaning |
|---|---|
-l |
long format: permissions, owner, size, date |
-a |
include hidden files (starting with .) |
-h |
human-readable sizes (1.5M instead of 1529172) |
-t |
sort by modification time (newest first) |
-S |
sort by size (largest first) |
-d |
list the directory itself, not its contents |
$ ls -la
total 20
drwxr-xr-x 3 user user 4096 Aug 5 10:00 .
drwxr-xr-x 3 root root 4096 Aug 5 09:58 ..
-rw-r--r-- 1 user user 123 Aug 5 10:00 notes.txt
Use ls <path> to list another directory:
$ ls /etc/hostname
/etc/hostname
cd — change directory
cd moves you to another directory.
$ cd Documents
$ pwd
/home/user/Documents
Useful forms:
$ cd .. # up one level
$ cd ~ # home directory
$ cd / # root directory
$ cd - # the directory you were in before
Note: cd is a shell built-in, not a separate program — that is why it only
works inside the current terminal session.
find — search by name, type, age, size
find searches a directory tree and is far more powerful than ls. The first
argument is the start directory, then the criteria. (. means the
current directory.)
$ find /home/user -name "*.txt"
/home/user/notes.txt
/home/user/docs/old.txt
Common criteria:
| Criterion | Meaning |
|---|---|
-name "*.txt" |
name pattern (case-sensitive) |
-iname "*.TXT" |
name pattern (case-insensitive) |
-type f |
files only |
-type d |
directories only |
-mtime 0 |
modified in the last 24 hours |
-mtime +7 |
modified more than 7 days ago |
-size 0c |
files with zero bytes |
-maxdepth 1 |
don’t go deeper than one level |
Combine criteria and run a command on the results with -exec:
$ find . -name "*.log" -type f -mtime +7 -exec rm {} \;
The {} is replaced by each result and the trailing \; ends the command.
The + in -mtime +7 means “older than 7 days”; without it, -mtime 7
would only match files modified exactly seven days ago.
Be careful with find -delete and -exec rm — they delete files and there is
no undo.
locate — fast search by filename
locate searches a pre-built database of filenames instead of the disk, so it
is much faster than find on large systems.
$ locate hostname
/etc/hostname
/usr/bin/hostname
Two caveats:
locateis not installed on every system, and the package differs by distro: on Ubuntu installplocate(sudo apt install plocate); on Arch alsoplocate(sudo pacman -S plocate). Fedora desktop shipsplocateby default, but server/minimal installs may not.- The database must be refreshed with
updatedb(usually run automatically by cron/systemd timers), so brand-new files may not show up yet. On Arch theplocatepackage enables a daily update timer when it is installed.