Where to start First steps track · 15 stages
Linux Labs

Basic file commands

8 min Linux · bash

Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, terminal copy-paste.

Cheat sheet

15 commands Printable version
  • 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
  • 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)

Overview

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.

Options

11

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.

Recipes

4

Navigation

bash
pwd                               # where am I
ls -lah                           # contents: long, hidden, human-readable sizes
cd /etc                           # go there
cd -                              # back to the previous directory
cd                                # home (~)
tree -L 2                         # a tree, 2 levels deep (if installed)

Paths: . is the current one, .. is the parent, ~ is home, / is root.

Files and directories

bash
cp file1 file2                    # copy
cp -r dir1 dir2                   # recursively (a directory)
cp -a src dst                     # preserve permissions/time/links (archive mode)
mv old new                        # move / rename
rm file                          # remove a file
rm -r dir                         # remove a directory recursively
rm -i file                        # with confirmation
mkdir -p a/b/c                    # create with intermediate directories
touch file                        # create empty / update its timestamp
ln -s /target linkname            # a symbolic link

rm has no trash can. rm -rf is irreversible. Double-check the path; run risky deletions through ls with the same pattern first (see "Danger zone").

Viewing text

bash
cat file                          # print it all (small files)
less file                         # page through it (q to quit, / to search, G for the end)
head -n 20 file                   # the first 20 lines
tail -n 50 file                   # the last 50 lines
tail -f /var/log/syslog           # watch it being appended to (logs!)
wc -l file                        # number of lines

Pipes and redirection:

bash
cmd > out.txt                     # stdout to a file (overwrite)
cmd >> out.txt                    # append
cmd 2> err.txt                    # stderr to a file
cmd > all.txt 2>&1                # both stdout and stderr
cmd1 | cmd2                       # cmd1's output into cmd2's input

Terminal and copy-paste

bash
# Copy/paste in a terminal (NOT Ctrl+C — that's an interrupt!):
#   the trainer's terminal: select with the mouse → Ctrl+Insert,
#   paste — Shift+Insert (or Copy/Paste in the right-click menu)
#   a regular Linux terminal: Ctrl+Shift+C / Ctrl+Shift+V
clear                             # clear the screen (or Ctrl+L)
history | tail                    # recent commands
!!                                # repeat the previous command
sudo !!                           # repeat the previous one with sudo
Ctrl+R                            # search history (type part of a command)
Ctrl+C                            # interrupt the current command
Ctrl+A / Ctrl+E                   # jump to the start / end of the line
Tab                               # autocomplete

Troubleshooting

5

rm deleted the wrong thing

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:

bash
ls *.log        # first look at what the pattern would match
rm -i *.log     # then delete with confirmation

No such file or directory, even though the file is right there

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.

Permission denied while copying

No read permission on the source, or no write permission on the destination directory. Check ls -l, and use sudo if needed — see permissions.

cp didn't copy a directory fully

A directory needs recursive copying: cp -a src dst. Without -a (or -r), cp refuses and prints omitting directory.

The file is busy, space wasn't freed after deleting it

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.

Danger zone

2

rm -rf without checking the path

rm 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:

bash
ls -la /var/www/site/tmp/*.cache    # first see what the pattern matches
rm -i /var/www/site/tmp/*.cache     # then delete with confirmation

mv over an existing file

mv 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.

See also

  • ls — directory contents, permissions and sizes.
  • cp — copying files and directories.
  • mv — moving and renaming.
  • rm — deleting, with no trash.
  • cat — print a whole file.
  • less — page through a long file with search.
  • head — the first lines of a file.
  • tail — the last lines and following a log.
  • grep — Searching for patterns in text; regex, recursion, context.
  • find — Finding files by name, size, time, permissions; acting on what's found.
  • chmod-chown — Changing permissions (chmod) and owner/group (chown) — numeric and symbolic syntax.
  • cd — Moving around the filesystem: cd, cd -, cd ~, cd .., pushd/popd, CDPATH.
  • mkdir — Creating directories: -p (with parents), -m (permissions right away), nested {brace} expansion, rmdir.
  • pwd — The current directory's path and the -L/-P difference.
Loading…