Linux
Beginner 12 min read Linux

File & Directory Management

The daily bread of every Linux user: listing, navigating, creating, copying, moving and viewing files from the terminal. Master ls, cd, mkdir, cp, mv, rm, cat, less, tail and nano, plus redirection with > and >> — the foundation of every shell script.

lscdpwdmkdircpmvrmcatlesstailredirectionlinux
Introduction

What is this?

File & directory management is the daily bread of every Linux user — listing, navigating, creating, copying, moving and viewing files from the terminal. The commands are small and composable: ls to look, cd to move, mkdir to create, cp and mv to duplicate and relocate, and cat/less/tail to read. Redirection with > and >> turns any command's output into a file, which is the foundation of shell scripting and logging.

Motivation

Why do you need it?

  • Almost everything on a server is a file — configs, logs, sockets — so file commands double as debugging commands.
  • These commands are identical on every distribution and inside every container, so fluency transfers everywhere.
  • Output redirection with > and >> is how every real script, cron job and container entrypoint captures logs.
  • Being fluent with cp, mv and rm prevents the classic accidents: overwriting the wrong file and deleting the wrong directory.
  • Interviewers treat these as a baseline; fumbling ls -l or cp -r is an immediate red flag in any ops role.
Think about it

Mental model

Imagine

A workshop tool rack

Think of

Linux file commands

Each tool has one job — the file cabinet labels (ls), the ladders between floors (cd), the photocopier (cp), the trolley (mv), and the reading lamp (cat/less). Once you know which tool to grab, the whole workshop runs itself.

Mechanics

How it works

  • Every file is tracked by the kernel as an inode; a directory is just a list of names pointing to inodes.
  • ls reads the directory and prints names; with -l it adds permissions, owner, size and timestamps.
  • cp duplicates a file by creating a new inode and copying the contents into it.
  • mv on the same filesystem is a rename — it only updates the directory entry, which is why it is instant.
  • Redirection happens at the shell level: > opens a file for writing (truncating it), >> opens it for appending.

The lifecycle of a file: create, copy, view, move, delete.

mkdir creates a home
echo > file writes
cp duplicates
mv relocates
rm deletes
Reference

Key commands

ls -l

List files with permissions, owner, size and timestamps.

total 12
drwxr-xr-x 2 root root 4096 Aug  8 10:15 docs
-rw-r--r-- 1 root root   18 Aug  8 10:15 README.md
mkdir -p a/b/c && echo ok

Create nested directories in one shot, then confirm success.

ok
cp -r docs backup && ls

Recursively copy a directory and list the result.

backup  docs
mv README.md docs/

Move README.md into docs; exit status 0 confirms success.

0
tail -n 3 /var/log/syslog

Show the last three log lines — the fastest way to see what just happened.

Aug  8 10:16:01 dev-host CRON[1234]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Hands-on

Examples

Scaffold a project directory in two minutes

Scaffold a project directory in two minutes
# Build the skeleton
mkdir -p ~/project/{src,docs,logs}
cd ~/project
# Create a README with redirection
echo "# My Project" > README.md
echo "First release" >> README.md
# Copy and inspect
cp README.md docs/readme.md
cat README.md
ls -R ~/project
# Move and rename
mv docs/readme.md docs/README-copy.md

This walks the whole lifecycle: mkdir -p builds nested directories with brace expansion, > creates and >> appends, cp duplicates, cat shows, and mv renames — every pattern you will repeat in daily work.

Real world

In production

  • Ship artifacts and configs with cp -a to preserve permissions and symlinks.
  • Audit changes with ls -la and stat before and after deploys.
  • Run tail -f application logs during a rollout to catch errors live.
  • Use echo and variables with redirection in provisioning scripts, cloud-init and entrypoints.
  • mv is atomic on a single filesystem — use mv file.tmp file to swap configs without a gap.
Guidelines

Best practices

  • Quote paths with spaces: cp "My File" target.
  • Use -- and -i to stay safe: rm -i prompts before deleting anything.
  • Dry-run destructive commands with --dry-run or -n where supported.
  • Prefer mv over copy-plus-delete for same-filesystem moves — it is atomic and fast.
  • Back up before rm -rf, and always list what you are about to delete first.
Watch out

Common mistakes

  • rm -rf on an empty variable, e.g. rm -rf $DIR/ becoming rm -rf /.
  • Catting huge logs instead of paging with less or tailing the last lines.
  • Using > to append, silently clobbering the file you meant to keep.
  • Assuming mv is always instant — across filesystems it is copy-plus-delete and can fail halfway.
Root cause analysis

Common issues

Error
mv: cannot move 'x' to 'y': Device or resource busy
Root cause

The target file is held open by a running process.

Fix

Find the holder with lsof y, stop it, then retry the move.

Error
cp: omitting directory 'src'
Root cause

cp was used without -r on a directory.

Fix

Use cp -r src dest, or cp -a dest to also preserve attributes.

Error
Permission denied when writing a file you own
Root cause

The parent directory lacks write permission, so new entries cannot be created even though the file is yours.

Fix

Check the parent with ls -ld . and grant write with chmod u+w . or chown the directory to your user.

When things break

Debugging tips

  • ls -la shows hidden files — 'missing' files are usually dotfiles.
  • stat <file> reveals the exact inode, size and modification time.
  • file <name> tells you what a file really is, regardless of its extension.
  • diff -u before.txt after.txt shows exactly what changed.
  • echo $? after cp or mv tells you whether it succeeded (0 = success).
Interviews

Interview checkpoint

Practice

Hands-on lab

Build a project tree and manage its files
15 min · beginner
In progress
Objectives
  • Create a nested directory structure with mkdir -p
  • Create files with echo redirection using > and >>
  • Copy and move files and directories with cp and mv
  • Inspect files with cat, head, tail and wc

Build a project tree and manage its files

15 min · beginner

0/6 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/6
1

Which command deletes a non-empty directory and everything inside it?

2

What does pwd print?

3

What does 'echo hello >> notes.txt' do?

4

mv can be used to rename a file in place.

5

Which command shows the last lines of a file and can follow a growing log live?

6

You must copy an entire directory including hidden files while preserving permissions and timestamps. Which command?

Quick reference

Cheat sheet

Navigate & list
pwdPrint the working directory
lsList files and directories
ls -laList all, including hidden, long format
ls -lS / -ltSort by size / by time
cd ~Go to your home directory
cd -Go back to the previous directory
Create & delete
mkdir newCreate a directory
mkdir -p a/b/cCreate nested parents too
rmdir emptyRemove an empty directory
rm fileDelete a file
rm -r dirDelete a directory tree
rm -i / rm -fPrompt before each / force
Copy & move
cp src dstCopy a file
cp -r src dirCopy a directory recursively
cp -a src dstCopy preserving permissions and symlinks
mv src dstMove or rename (atomic on one filesystem)
mv dir/* .Move everything from dir into here
View files
cat filePrint the whole file
tac filePrint lines in reverse order
less filePage through, scroll both directions
more filePage through, forward only
head -n 5 fileFirst 5 lines
tail -n 10 fileLast 10 lines
tail -f fileFollow a growing file live
Edit & redirect
nano fileSimple terminal editor
vi filePowerful modal editor
echo text > fileOverwrite a file with text
echo text >> fileAppend text to a file
cmd > out.txt 2>&1Capture stdout and stderr to a file
Go deeper

Official documentation