opscape

Files and directories cheat sheet

The most useful files and directories commands on one printable page, every line checked by running it.

basename Strip the directory part (and optionally a suffix) from a path, leaving the file name.

basename /home/dev/demo/data/users.csv
File name from a path output
basename data/users.csv .csv
Strip an extension too output
f=data/users.csv name=${f##*/} echo "$name ${name%.csv}"
The same without a subprocess output

cd Change the shell's current working directory.

cd data pwd
Move into a directory output
cd pwd cd ~/demo/logs pwd
Go to your home directory output
cd /tmp cd ~/demo/src cd - cd -
Jump back to the previous directory output

chown Change the owner and/or group of files and directories.

chown alice notes.txt ls -l notes.txt
Change the owner output
chown alice:web notes.txt ls -l notes.txt
Change owner and group together output
chown -R deploy:deploy src ls -lR src
Change a whole directory tree output

cp Copy files and directories, optionally keeping their timestamps, permissions and links.

cp notes.txt notes-backup.txt ls -l notes*
Copy a file output
mkdir backup cp todo.txt notes.txt backup/ ls backup
Copy files into a directory output
cp -r src src-copy find src-copy
Copy a whole directory output
cp -a . ../demo-copy ls -l ../demo-copy/link-to-notes
Copy a tree exactly, links included output

dirname Strip the last component from a path, leaving the directory part.

dirname /home/dev/demo/data/users.csv
The directory part of a path output
cd /tmp bash ~/demo/tools/where.sh
Find the directory a script lives in output

file Guess what kind of data a file holds by looking at its contents, not its name.

file notes.txt data/config.json photos/beach.png scripts/backup.sh
Identify a few files output
file -b photos/IMG_2041.png
Just the description output
file --mime-type data/users.csv data/config.json photos/beach.png
MIME type for scripts and servers output

install Copy files into place and set their mode, owner and group in one step; can also create directories.

mkdir -p bin install scripts/greet.sh bin/greet ls -l bin/greet bin/greet installer
Install a script as an executable output
mkdir conf install -m 644 data/settings.conf conf/ install -m 600 .env conf/ ls -lA conf
Install a config file with a specific mode output
install -d -m 750 deploy/releases deploy/shared/logs ls -ld deploy/releases deploy/shared deploy/shared/logs
Create directories with a given mode output
install -D -m 644 src/lib/helpers.py dist/lib/python/helpers.py find dist
Copy into a path that does not exist yet output

ln Make links between files, symbolic (-s) or hard.

ln -s data/config.json config.json ls -l config.json head -3 config.json
Create a symbolic link output
ln todo.txt tasks.txt ls -li todo.txt tasks.txt
Create a hard link output
ln -sr data/users.csv src/users.csv ls -l src/users.csv head -2 src/users.csv
Let ln compute the relative path output
ln -s logs/app.log current.log ln -sfn logs/error.log current.log ls -l current.log
Repoint an existing symlink output

ls List the contents of directories, or the details of individual files.

ls -l
Show permissions, owner, size and date output
ls -la
Include hidden files output
ls -lt logs
Newest files first output
ls -ld data src
Show a directory itself, not its contents output

mkdir Create directories, optionally with all missing parents.

mkdir reports ls -ld reports
Create a directory output
mkdir -p build/output/logs mkdir -p build/output/logs find build
Create a whole path with -p output
mkdir -p webapp/{src/{api,ui},tests,docs} tree webapp
Lay out a project tree in one command output
mkdir -m 700 secrets ls -ld secrets
Create a private directory output

mktemp Create a temporary file or directory with a unique, unpredictable name and print that name.

f=$(mktemp) echo "$f" ls -l "$f"
Create a temporary file output
d=$(mktemp -d) ls -ld "$d"
Create a temporary directory output
bash job.sh ls /tmp
Clean up automatically when the script exits output

mv Move or rename files and directories.

mv todo.txt tasks.txt ls *.txt
Rename a file output
mkdir archive mv logs/app.log.1 logs/error.log archive/ ls archive logs
Move files into a directory output
mv -n todo.txt notes.txt ls todo.txt notes.txt
Refuse to overwrite with -n output
for f in report-*.txt; do mv -- "$f" "${f%.txt}.md" done ls report-*
Rename many files by pattern output

pwd Print the full path of the current working directory.

pwd
Show where you are output
cd lib-link pwd pwd -P
Physical path through a symlink output

readlink Print where a symbolic link points, or with -f the fully resolved path of any file.

readlink link-to-notes
Where does a symlink point output
readlink second-link readlink -f second-link
One level versus fully resolved output
readlink -f src/../data/./users.csv
Canonical path of any file output

realpath Print the absolute, canonical path of a file, with symlinks, . and .. resolved.

realpath notes.txt
Absolute path of a file output
realpath link-to-notes
Resolve a symbolic link output
realpath --relative-to=src/lib data/users.csv realpath --relative-to=. /home/dev/demo/logs/app.log
Compute a relative path between two places output

rm Delete files, and with -r whole directory trees. There is no undo.

rm todo.txt ls todo.txt
Delete a file output
rm -f nosuch.txt echo "exit status: $?"
Ignore missing files with -f output
rm -rv logs
Delete a directory and everything in it output
rm -draft.txt rm -- -draft.txt && echo removed
Delete a file whose name starts with a dash output

rmdir Remove empty directories, and refuse if they are not empty.

rmdir old ls -d old
Remove an empty directory output
rmdir -pv build/out/tmp
Remove a chain of empty directories output
find cache -type d -empty -print -delete find cache
Remove every empty directory in a tree output

stat Show a file's metadata (size, permissions, owner, inode, timestamps), or print chosen fields in your own format.

stat notes.txt
Everything about one file output
stat -c %s access.log
Print just the size in bytes output
stat -c '%A %a %U:%G %s %n' notes.txt scripts/greet.sh
Several fields in one line output
stat -c '%y | %Y | %n' todo.txt
Modification time, human and epoch output

touch Update a file's timestamps, creating it empty if it does not exist.

touch new.txt ls -l new.txt
Create an empty file output
ls -l todo.txt touch todo.txt ls -l todo.txt
Bump the modification time of an existing file output
touch -d "2025-12-31 23:59" report.txt ls -l --time-style=long-iso report.txt
Set a specific date and time output
cp notes.txt notes-copy.txt touch -r notes.txt notes-copy.txt ls -l notes.txt notes-copy.txt
Copy timestamps from another file output

tree Draw a directory and everything below it as an indented tree.

tree src
Show a directory tree output
tree -L 1
Limit the depth output
tree -I 'photos|logs|data'
Hide files matching a pattern output