LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

Files & Folders

Navigating

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

Listing Contents

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

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]
copy
$ less [fileName]
copy
$ bat [fileName]
copy
Show only the beginning or end of a file.
$ head -n 20 [fileName]
copy
$ tail -n 20 [fileName]
copy
-f keeps following a file as it grows, ideal for logs.
$ tail -f [logFile]
copy
Identify what kind of file something is.
$ file [fileName]
copy
Count lines, words, and bytes. Use -l for just the line count.
$ wc [fileName]
copy

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]
copy
$ mkdir [folderName]
copy
$ mkdir -p path/to/nested/folder
copy
$ > [fileName]
copy

Copying, Moving, Renaming

cp copies a file. Use -r to copy a directory and everything in it.
$ cp [fileName] [newFileName]
copy
$ cp -r [folder] [folderCopy]
copy
mv both moves and renames; there is no separate rename command.
$ mv [oldName] [newName]
copy
$ mv [fileName] [targetFolder]/
copy
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]
copy
$ rm -r [folderName]
copy
rmdir removes a directory only if it is already empty.
$ rmdir [emptyFolderName]
copy
Safer than rm: move files to the desktop trash, where they can be restored.
$ gio trash [file]
copy
$ trash-put [file]
copy
List or empty the trash.
$ gio trash --empty
copy

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]
copy
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]
copy
Resolve a link to the real path it points at.
$ readlink -f [linkName]
copy

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]
copy
$ stat [file]
copy
chmod changes permissions, either symbolically or with octal digits (read=4, write=2, execute=1).
$ chmod +x [script]
copy
$ chmod 644 [file]
copy
$ chmod 755 [folder]
copy
$ chmod -R u+rwX [folder]
copy
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]
copy
$ chown -R [user]:[group] [folder]
copy
$ chgrp [group] [file]
copy

Searching File Contents

grep searches inside files, rg (ripgrep) does the same recursively by default and much faster.
$ grep "phrase" [fileName]
copy
$ grep -rn "phrase" [folder]
copy
$ rg "phrase"
copy
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]
copy
$ diff -r [folder1] [folder2]
copy

Disk Usage

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

Mounting Filesystems

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

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]
copy
$ vi [fileName]
copy
$ emacs [fileName]
copy
$ micro [fileName]
copy
Copied to clipboard
Kai