Navigation
Paths: . is the current one, .. is the parent, ~ is home, / is root.
Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, terminal copy-paste.
ls -lah
detailed directory listing
cd dir
go into a directory
pwd
where am I now
cp -a src dst
copy, preserving permissions/time
mv a b
move/rename
rm -i f
remove with confirmation
mkdir -p a/b
create with intermediate directories
touch f
create an empty file / update its time
ln -s tgt link
a symbolic link
tail -f log
follow a log in real time
less file
page through a file (q to quit)
head -n20 f
the first 20 lines
cmd1 | cmd2
pipe the first command's output into the second
cmd > out 2>&1
stdout and stderr into a file
Ctrl+R (history) · Ctrl+L (clear) · !! (repeat)
Working at the command line almost always starts with the same few actions: figuring out where you are, listing a directory's contents, moving to another one, copying or renaming a file, peeking inside a text file. These are the commands gathered here.
They all work with paths. A path can be absolute (from the root,
/etc/nginx) or relative (from the current directory, ../logs). pwd
shows the current directory; ~ denotes the home directory. More on paths —
paths.
Filenames are case-sensitive, and spaces in names must be escaped or quoted.
Hidden files start with a dot and are invisible without ls -a.
Below are the main flags of ls, cp, mv, rm, mkdir; each command has
its own detailed article (see "See also") — this is the minimum for basic work.
| Option | What it does | Example |
|---|---|---|
-l |
Long format — permissions, owner, size, date. | ls -l |
-a --all |
Show hidden files too (those starting with a dot). | ls -a |
-r --recursive |
Copy a directory recursively. | cp -r dir1 dir2 |
-i --interactive |
Ask for confirmation before overwriting — mv silently overwrites by default. | mv -i a b |
-r --recursive |
Remove a directory recursively along with its contents. dangerous | rm -r dir |
-f --force |
Never ask for confirmation and ignore nonexistent files. dangerous | rm -rf dir |
-p --parents |
Create missing parent directories, and don't error if it already exists. | mkdir -p a/b/c |
-h --human-readable |
Human-readable sizes (K, M, G) together with -l. | ls -lh |
-a --archive |
Preserve permissions, timestamps, and symlinks when copying. | cp -a src dst |
-i --interactive |
Ask for confirmation before overwriting an existing file. | cp -i a b |
-i --interactive |
Ask for confirmation before each removal. | rm -i file |
Nothing found. Try a shorter query.
Paths: . is the current one, .. is the parent, ~ is home, / is root.
rmhas no trash can.rm -rfis irreversible. Double-check the path; run risky deletions throughlswith the same pattern first (see "Danger zone").
Pipes and redirection:
There's no recovery: rm doesn't move files to a trash can. Make a habit of
rm -i (asks for confirmation) and checking the pattern with ls before
deleting:
Usually it's a path issue: ls shows the file in the current directory, but
the command runs from a different one. Check pwd and give the full path.
No read permission on the source, or no write permission on the destination
directory. Check ls -l, and use sudo if needed — see permissions.
A directory needs recursive copying: cp -a src dst. Without -a (or -r),
cp refuses and prints omitting directory.
As long as a process keeps a deleted file open, the space isn't reclaimed.
Find the holder with lsof and restart the service — see disk-usage.
rm -rf without checking the pathrm has no trash can: files are removed immediately and permanently, and
recovery by filesystem means is not guaranteed. rm -rf also never asks for
confirmation and recurses through directories — a typo in the path (an extra
space before /, a wrong variable in a script) deletes the wrong tree
entirely. Before a risky command, run the same pattern through ls to see
the list before deleting:
mv over an existing filemv src dst, if dst already exists, silently overwrites it with no
confirmation (unlike cp -i/rm -i). For an interactive confirmation, use
mv -i src dst.
Found an inaccuracy? Report an error in the article