File operations: cp, mv, rm, mkdir, find, grep, sort, wc, head, tail, cat, less

Linux Essentials Preparation · lesson 9 of 23 · 25 min

File and Directory Operations

Mastering file operations is core to the Linux Essentials exam. These commands work on all Linux distributions.

cp — copy files/directories. cp file1 file2 (copy file), cp -r dir1 dir2 (recursive copy of directory), cp -p (preserve permissions and timestamps).

mv — move or rename. mv file1 file2 (rename), mv file /path/ (move). No -r needed for directories.

rm — remove files. rm file, rm -r dir (recursive), rm -f (force, no prompt). Warning: Linux has no recycle bin — deleted files are gone.

mkdir — create directories. mkdir dirname, mkdir -p path/to/dir (create parent directories as needed).

rmdir — remove empty directories. Use rm -r for non-empty.

find — search for files. find /path -name "*.txt" (by name), find / -type f -size +100M (files over 100MB), find /etc -mtime -7 (modified in last 7 days), find / -perm /4000 (SUID files).

ln — create links. ln file hardlink (hard link — same inode), ln -s file symlink (symbolic link — like a shortcut). Hard links cannot span filesystems; symlinks can.

Text Processing Commands

grep — search for patterns in files. grep "pattern" file, grep -r "pattern" /dir (recursive), grep -i (case-insensitive), grep -v (invert — show non-matching lines), grep -n (show line numbers), grep -c (count matches).

cat — display file contents. cat file, cat file1 file2 > combined (concatenate).

less / more — paginate file output. less file (scroll up and down), more file (scroll down only). Press q to quit.

head / tail — show beginning/end of file. head -n 20 file (first 20 lines), tail -n 20 file (last 20 lines), tail -f file (follow — live updates, great for logs).

sort — sort lines. sort file (alphabetical), sort -n (numeric), sort -r (reverse), sort -k2 (sort by field 2).

wc — word count. wc -l file (line count), wc -w (word count), wc -c (byte count).

cut — extract columns. cut -d: -f1 /etc/passwd (extract field 1, colon-delimited).

sed — stream editor. sed 's/old/new/g' file (replace all occurrences).

awk — pattern scanning. awk '{print $1}' file (print first field).

Pipes and Redirection

| (pipe) — sends output of one command as input to another: cat /etc/passwd | grep root. > — redirect output to file (overwrite). >> — append to file. 2> — redirect stderr. 2>&1 — redirect stderr to stdout.

Key Terms

Key Terms

Knowledge Check — 12 Questions

Linux — File Operations & Text Processing

Which command recursively copies a directory?

  • cp dir1 dir2
  • cp -r dir1 dir2
  • mv dir1 dir2
  • ln dir1 dir2

Answer: cp -r dir1 dir2
cp -r (recursive) copies a directory and all its contents. Without -r, cp only copies individual files.

Which command shows the last 50 lines of a log file and continues to display new entries?

  • head -50 logfile
  • tail -50 logfile
  • tail -f -n 50 logfile
  • cat logfile | tail

Answer: tail -f -n 50 logfile
tail -f follows the file in real time; -n 50 shows the last 50 lines first. Essential for monitoring live logs.

grep -v “error” logfile will:

  • Show only lines containing “error”
  • Show all lines NOT containing “error”
  • Count lines with “error”
  • Delete lines with “error”
  • Answer: Show all lines NOT containing “error”

grep -v inverts the match — shows lines that do NOT match the pattern.

Which command finds all files with SUID permission set?

  • find / -type f -suid
  • find / -perm /4000
  • ls -suid /
  • grep -r suid /

Answer: find / -perm /4000
find / -perm /4000 finds files with the SUID bit set — important for security auditing.

To count the number of lines in a file, use: wc _____ filename

Answer: -l
wc -l counts lines. wc -w counts words, wc -c counts bytes/characters.

What does the >> operator do?

  • Overwrites a file with command output
  • Appends command output to a file
  • Redirects stderr
  • Pipes output to another command

Answer: Appends command output to a file

appends to a file without overwriting. > overwrites. Use >> for log files to preserve existing content.

A symbolic link differs from a hard link because:

  • Symbolic links cannot be deleted
  • Symbolic links can span filesystems; hard links cannot
  • Hard links are faster
  • Symbolic links share the same inode
  • Answer: Symbolic links can span filesystems; hard links cannot

Symbolic links are pointers to a path — they work across filesystems. Hard links share an inode and must be on the same filesystem.

Which command extracts the first colon-delimited field from /etc/passwd?

  • grep -f1 /etc/passwd
  • cut -d: -f1 /etc/passwd
  • awk -f1 /etc/passwd
  • sed -f1 /etc/passwd

Answer: cut -d: -f1 /etc/passwd
cut -d: sets the delimiter to colon; -f1 extracts the first field — gives you the list of usernames.

mkdir -p /a/b/c will:

  • Fail if /a does not exist
  • Create /a, /a/b, and /a/b/c as needed
  • Create only /a/b/c
  • Prompt before creating each directory
  • Answer: Create /a, /a/b, and /a/b/c as needed

mkdir -p creates the full path including all parent directories — no error if they already exist. sed ‘s/foo/bar/g’ file will:

  • Delete all lines containing “foo”
  • Replace the first “foo” on each line with “bar”
  • Replace all occurrences of “foo” with “bar”
  • Search for “foo” and print matching lines
  • Answer: Replace all occurrences of “foo” with “bar”

The g flag in sed substitution means global — replace all occurrences on each line, not just the first.

Which command shows the first 10 lines of a file by default?

  • tail
  • head
  • less
  • more

Answer: head
head displays the first 10 lines by default. Use -n to specify a different number.

To redirect both stdout and stderr to a file, use:

  • command > file
  • command 2> file
  • command > file 2>&1
  • command | file
  • Answer: command > file 2>&1

file redirects stdout; 2>&1 redirects stderr to the same destination as stdout — both go to the file.


Previous lesson: Getting help: man pages, info, –help, apropos, whatis, tldr
Back to Linux Essentials Preparation
Next lesson: Archiving and compression: tar, gzip, bzip2, zip; package management commands