Files & Folders

Navigating

pwd prints the directory you're currently in.
$ pwd
cd moves you to another directory: give it a path, or use a shortcut.
$ cd [path]
Go up one level, to the parent directory.
$ cd ..
Go to your home directory (plain cd does the same).
$ cd ~
Jump back to the directory you were in before.
$ cd -

Listing Contents

ls lists a directory. -l shows details, -a includes hidden files (names starting with a dot), -h prints human-readable sizes.
$ ls
$ ls -lah
Sort by modification time, newest first. Add -r to reverse any sort order.
$ ls -lt
Show nested directories as a tree.Modern replacements add colors, icons, and git status.
$ eza -la
$ lsd -la

Viewing Files

cat prints a whole file, less lets you scroll and search through it (press q to quit, / to search). bat is a cat clone with syntax highlighting.
$ cat [fileName]
$ less [fileName]
$ bat [fileName]
Show only the beginning or end of a file.
$ head -n 20 [fileName]
$ tail -n 20 [fileName]
-f keeps following a file as it grows, ideal for logs.
$ tail -f [logFile]
Identify what kind of file something is.
$ file [fileName]
Count lines, words, and bytes. Use -l for just the line count.
$ wc [fileName]

Creating

touch creates an empty file (or updates the timestamp of an existing one). mkdir -p creates nested directories in one step and does not complain if they already exist.
$ touch [fileName]
$ mkdir [folderName]
$ mkdir -p path/to/nested/folder
$ > [fileName]

Copying, Moving, Renaming

cp copies a file. Use -r to copy a directory and everything in it.
$ cp [fileName] [newFileName]
$ cp -r [folder] [folderCopy]
mv both moves and renames; there is no separate rename command.
$ mv [oldName] [newName]
$ mv [fileName] [targetFolder]/
Careful: both cp and mv overwrite existing targets without asking. Add -i to be prompted first, or -n to never overwrite.

Deleting

rm removes files. -r removes a directory and everything in it. There is no undo - deletion is permanent and immediate, so double-check first. Use rm -ri to confirm each deletion when unsure.
$ rm [fileName]
$ rm -r [folderName]
rmdir removes a directory only if it is already empty.
$ rmdir [emptyFolderName]
Safer than rm: move files to the desktop trash, where they can be restored.
$ gio trash [file]
$ trash-put [file]
List or empty the trash.
$ gio trash --empty

Links

A symbolic link points to a path. It breaks if the target moves, but it's what you want in almost all cases.
$ ln -s [target] [linkName]
A hard link is a second name for the same file content, working until all names are deleted. Hard links can't span filesystems or point at directories.
$ ln [target] [linkName]
Resolve a link to the real path it points at.
$ readlink -f [linkName]

Permissions & Ownership

Every file has an owner, a group, and permission bits for owner/group/others. ls -l shows them, stat shows everything.
$ ls -l [file]
$ stat [file]
chmod changes permissions, either symbolically or with octal digits (read=4, write=2, execute=1).
$ chmod +x [script]
$ chmod 644 [file]
$ chmod 755 [folder]
$ chmod -R u+rwX [folder]
ModeDescription
644Owner reads and writes, everyone else reads (typical file)
755Owner full access, everyone else reads and enters (typical directory or script)
600Only the owner reads and writes (private keys, secrets)
700Only the owner has any access (private directory)
chown changes the owner and group, chgrp just the group. Changing the owner requires root.
$ chown [user]:[group] [file]
$ chown -R [user]:[group] [folder]
$ chgrp [group] [file]

Searching File Contents

grep searches inside files, rg (ripgrep) does the same recursively by default and much faster.
$ grep "phrase" [fileName]
$ grep -rn "phrase" [folder]
$ rg "phrase"
To find files by name, size, or age instead, see the Search & Find basics page.

Comparing Files

diff -u shows differences in the familiar patch format.
$ diff -u [file1] [file2]
$ diff -r [folder1] [folder2]

Disk Usage

du measures what directories contain, df shows free space per filesystem.
$ du -h --max-depth=1
$ df -h
Interactive and prettier alternatives make it easy to find what eats your disk.
$ duf

Mounting Filesystems

List block devices, identify the filesystem on a partition, then mount it to a directory.
$ lsblk -f
$ mount /dev/[device] [path]
$ umount [path]

Editing Text Files

Any of these editors works in the terminal. nano is the easiest to start with; vim and emacs have their own basics pages.
$ nano [fileName]
$ vi [fileName]
$ emacs [fileName]
$ micro [fileName]