Creating, Copying, Moving & Deleting
The everyday file commands in depth. mkdir and brace expansion, touch and timestamps, cp with its important options, mv for moving and renaming, rm and rmdir, and wildcards, with the safety habits that stop you deleting the wrong thing.
Six commands you'll use every day
| Command | Short for | Job |
|---|---|---|
mkdir | make directory | create folders |
touch | create empty files, or update a file's timestamp | |
cp | copy | copy files and folders |
mv | move | move and rename |
rm | remove | delete files (and folders with -r) |
rmdir | remove directory | delete empty folders |
Two facts shape how you use them:
- There is no recycle bin.
rmdeletes for good.cpandmvoverwrite existing files without asking. Nothing here can be undone. - Silence means success. These commands print nothing when they work. Add
-v(verbose) to see what they did, and check afterwards withls.
Creating folders: mkdir
mkdir reports # one folder, in the current directory
mkdir reports invoices logs # several at once
mkdir /srv/data # anywhere, with an absolute path-p: parents, and no complaints
mkdir only creates the last folder in a path. If a parent is missing, it fails:
$ mkdir /root/app/src
mkdir: cannot create directory '/root/app/src': No such file or directory-p creates every missing parent, and also stays quiet if the folder already exists. That second part makes it safe in scripts that might run twice:
mkdir -pv /root/app/{src,config,logs}| Part | Meaning |
|---|---|
-p | create parents as needed, no error if it exists |
-v | verbose: print each folder created |
/root/app/{src,config,logs} | brace expansion (explained next): three paths in one |
mkdir: created directory '/root/app'
mkdir: created directory '/root/app/src'
mkdir: created directory '/root/app/config'
mkdir: created directory '/root/app/logs'/root/app didn't exist, so -p created it first, then the three folders inside.
Brace expansion: one word becomes many
Before running a command, the shell expands {a,b,c} into several words. echo shows what the shell produces:
echo app-{dev,staging,prod}.confapp-dev.conf app-staging.conf app-prod.confOne word turned into three. The command never sees the braces, only the result.
It also does ranges: echo log-{1..5}.txt gives log-1.txt log-2.txt ... log-5.txt. That's how people create a whole project skeleton in one line:
mkdir -p myapp/{src,tests,docs,config/{dev,prod}}Creating files and timestamps: touch
touch has two jobs:
- If the file doesn't exist, it creates it, empty.
- If it exists, it leaves the contents alone and sets its "last modified" time to now.
touch /root/app/config/app-{dev,staging,prod}.conf
touch -d "2026-01-15 10:00" /root/app/config/app-dev.conf
ls -l /root/app/config| Command | What it does |
|---|---|
touch .../app-{dev,staging,prod}.conf | creates three empty files |
touch -d "2026-01-15 10:00" ... | sets a specific date instead of now |
total 0
-rw-r--r-- 1 root root 0 Jan 15 2026 app-dev.conf
-rw-r--r-- 1 root root 0 Nov 24 15:17 app-prod.conf
-rw-r--r-- 1 root root 0 Nov 24 15:17 app-staging.confAll three are 0 bytes (empty). app-dev.conf now shows January 15 because of -d. Setting timestamps is mostly useful for testing scripts that clean up "files older than 30 days".
Other ways to create a file with content: echo "port=8080" > app.conf writes one line (and replaces anything already in the file), and opening a new file in nano or vim creates it when you save. Redirection (>) gets its own lesson soon.
Create folders and files
Build the folder structure for the new shop project.
Every project on a server starts as a set of folders: one for the code, one for configuration, one for logs. Creating them by hand, consistently, is the first thing you'll do when setting up a new service. mkdir creates folders and touch creates empty files you'll fill in later.
1. Create a folder
mkdir /root/shop-project
ls -ld /root/shop-project| Part | Meaning |
|---|---|
mkdir | make a directory |
/root/shop-project | its path (here absolute, but relative paths work too) |
ls -ld | list the folder itself (-d) in long format (-l), instead of listing what's inside it |
drwxr-xr-x 2 root root 4096 Nov 24 15:00 /root/shop-projectThe d at the start confirms it's a directory. 4096 is the space a directory entry takes on disk, not the size of its contents.
2. Nested folders: the -p option
Try to create a folder two levels deep:
mkdir /root/shop-project/src/utilsmkdir: cannot create directory '/root/shop-project/src/utils': No such file or directorymkdir refuses, because the parent src doesn't exist yet. It only creates the last folder in the path.
mkdir -p /root/shop-project/src/utils
tree /root/shop-projectWhat -p does: creates every missing parent along the way. It also stays quiet if the folder already exists, which makes it safe to use in scripts that may run twice.
/root/shop-project
└── src
└── utils
3 directories, 0 files3. Several at once
You can give mkdir and touch several names in one command, separated by spaces:
cd /root/shop-project
mkdir config logs docs
touch README.md src/app.py config/app.conf
tree| Command | What it does |
|---|---|
mkdir config logs docs | creates three folders in the current directory |
touch README.md src/app.py config/app.conf | creates three empty files. The paths are relative, so src/app.py lands in /root/shop-project/src/. |
tree | with no path, draws the current directory |
.
├── README.md
├── config
│ └── app.conf
├── docs
├── logs
└── src
├── app.py
└── utils
6 directories, 3 filesThe . at the top means "the current directory", which is /root/shop-project.
touch on a file that already exists doesn't empty it. It only updates its "last modified" time to now. That's its original job, and creating empty files is a handy side effect.
Build this structure in /root/shop-project (you may already have part of it):
/root/shop-project
├── README.md
├── config
│ └── app.conf
├── docs
├── logs
└── src
├── app.py
└── utilsExpected result: tree /root/shop-project shows 6 directories, 3 files.
Hint: the commands, explained
mkdir -p /root/shop-project/src/utils /root/shop-project/config /root/shop-project/logs /root/shop-project/docs
cd /root/shop-project && touch README.md src/app.py config/app.confWhat each part does
mkdir -p A B C D: create all four folders in one command;-palso creates missing parents (likesrcforsrc/utils) and doesn't complain if a folder exists.cd /root/shop-project && touch ...: go into the project, then (only ifcdworked, that's what&&means) create three empty files with relative paths.
Copying: cp
cp always takes the source first and the destination last:
cp SOURCE DESTINATION| What you write | What happens |
|---|---|
cp app.conf app.conf.bak | copy to a new name in the same folder |
cp app.conf /tmp/ | copy into a folder, keeping the name |
cp a.conf b.conf c.conf /tmp/ | several sources: the last argument must be a folder |
cp *.conf /tmp/ | everything matching a wildcard, into a folder |
cp versus mv
Backing up before editing
The single most important habit when touching a config file on a server:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakIf your change breaks something, cp nginx.conf.bak nginx.conf puts things back in seconds. Many engineers add the date: nginx.conf.2026-11-24.
Copying folders: -r
cp /root/app /root/app-backupcp: -r not specified; omitting directory '/root/app'By default cp copies files only, and it even tells you the fix.
cp -r /root/app /root/app-backup
ls /root/app-backupWhat -r does: recursive: copy the folder, everything in it, and everything in its subfolders.
config
logs
srcapp-backup was created as a copy of app.
Running the same cp -r twice. The first time, /root/app-backup doesn't exist, so cp creates it as a copy. The second time it does exist, so cp copies app into it, and you get /root/app-backup/app/:
$ cp -r /root/app /root/app-backup && ls /root/app-backup
app
config
logs
srcThe meaning of the destination changes depending on whether it already exists. Check with ls before and after copying folders.
-a: an exact copy, for backups
A plain copy is a new file: it gets the current time as its timestamp, and if you're not root, you become its owner. For backups you usually want an exact replica. That's -a (archive): recursive, and it preserves permissions, owner, group and timestamps.
cd /root/app/config
cp app-dev.conf /tmp/copy-plain.conf
cp -a app-dev.conf /tmp/copy-archive.conf
ls -l app-dev.conf /tmp/copy-plain.conf /tmp/copy-archive.conf-rw-r--r-- 1 root root 0 Jan 15 2026 /tmp/copy-archive.conf
-rw-r--r-- 1 root root 0 Nov 24 15:17 /tmp/copy-plain.conf
-rw-r--r-- 1 root root 0 Jan 15 2026 app-dev.confThe original is dated January 15. The plain copy says today; the -a copy kept January 15. For backups, logs and anything where "when was this last changed?" matters, use -a.
The cp options worth knowing
| Option | Effect |
|---|---|
-r | copy folders recursively |
-a | archive: recursive + keep permissions, owner, timestamps, links |
-i | interactive: ask before overwriting an existing file |
-n | no clobber: never overwrite |
-u | update: copy only if the source is newer than the destination |
-v | verbose: show each file copied |
With -v, you can watch a wildcard copy:
cp -v /root/app/config/*.conf /tmp/'/root/app/config/app-dev.conf' -> '/tmp/app-dev.conf'
'/root/app/config/app-prod.conf' -> '/tmp/app-prod.conf'
'/root/app/config/app-staging.conf' -> '/tmp/app-staging.conf'Each line reads source -> destination.
With -i, cp stops and asks cp: overwrite '/tmp/plain.conf'? Type y and Enter to overwrite, anything else to skip.
Copy and back up
Back up the config file and the whole config folder before anyone edits them.
Rule number one before editing any config file on a server: make a copy first. If your change breaks something, you can put the original back in seconds. The usual habit is to copy app.conf to app.conf.bak (or add a date, like app.conf.2026-11-24) right next to it.
cp always takes the source first and the destination second: cp FROM TO.
First, give the config file some content so the copy has something in it:
printf 'port=8080\ndebug=false\n' > /root/shop-project/config/app.conf
cat /root/shop-project/config/app.conf1. Copy a file
cd /root/shop-project
cp config/app.conf config/app.conf.bak
ls -l config| Part | Meaning |
|---|---|
cp | copy |
config/app.conf | the source: the file to copy |
config/app.conf.bak | the destination: the new file's name |
total 8
-rw-r--r-- 1 root root 22 Nov 24 15:00 app.conf
-rw-r--r-- 1 root root 22 Nov 24 15:00 app.conf.bakTwo files of the same size (22 bytes): an identical copy. Now you can edit app.conf safely, and undo with cp config/app.conf.bak config/app.conf.
2. Copy into a folder
If the destination is an existing folder, cp puts the copy inside it and keeps the name. Add -v to see what happens:
cp -v config/app.conf /tmp/What -v does: verbose. The command prints each file it copies, as source -> destination.
'config/app.conf' -> '/tmp/app.conf'3. Copy a whole folder: -r
cp /root/shop-project/config /root/backupscp: -r not specified; omitting directory '/root/shop-project/config'By default cp only copies files. It skipped the folder and even tells you the fix.
mkdir -p /root/backups
cp -r /root/shop-project/config /root/backups/config
tree /root/backupsWhat -r does: recursive. It copies the folder, everything inside it, and everything inside those, all the way down.
/root/backups
└── config
├── app.conf
└── app.conf.bak
2 directories, 2 filesThe whole folder and its two files were copied.
cp overwrites silently. If the destination file exists, it's replaced without asking. Use cp -i (interactive) to be asked first, or cp -n to never overwrite. To also keep the original owner, permissions and timestamps (important for backups), use cp -a (archive), which includes -r.
- Back up
/root/shop-project/config/app.confasapp.conf.bakin the same folder. - Copy the whole
/root/shop-project/configfolder to/root/backups/config.
Expected result: ls /root/shop-project/config shows app.conf and app.conf.bak, and tree /root/backups shows the copied config folder.
Hint: the commands, explained
cp /root/shop-project/config/app.conf /root/shop-project/config/app.conf.bak
mkdir -p /root/backups
cp -r /root/shop-project/config /root/backups/configWhat each part does
cp SOURCE DEST: copy one file under a new name (.bak= backup).mkdir -p /root/backups: make sure the destination folder exists.cp -r SOURCE DEST: copy a whole folder and everything in it (recursive). Without-r, cp refuses to copy folders.
Moving and renaming: mv
There is no separate "rename" command on Linux. Renaming is moving a file to a new name. mv works on files and folders alike, and needs no -r:
| What you write | What happens |
|---|---|
mv old.conf new.conf | rename, in the same folder |
mv app.conf /etc/myapp/ | move into a folder, same name |
mv app.conf /etc/myapp/main.conf | move and rename in one go |
mv a.log b.log c.log archive/ | several sources into a folder |
mv project project-old | rename a whole folder |
mv -v /root/app/config/app-dev.conf /root/app/config/app-development.confrenamed '/root/app/config/app-dev.conf' -> '/root/app/config/app-development.conf'mv calls it "renamed" even when you move to another folder: on the same disk, moving a file just changes its name and location in the directory tree. The data itself isn't copied, which is why moving a 50 GB file within one disk is instant.
Like cp, mv overwrites silently. mv -i asks first, mv -n never overwrites.
Move and rename
Collect all log files into one folder and give the release notes a clean name.
Linux has no separate "rename" command. Renaming is moving: mv old-name new-name moves the file to a new name in the same folder. And mv file folder/ moves it into another folder, keeping its name. Unlike cp, mv leaves nothing behind.
You'll also meet the most useful shortcut in the shell: the wildcard *, which means "any characters". *.log matches every name that ends in .log.
Go to the messy downloads folder and look around:
cd /root/downloads
ls -ltotal 40
-rw-r--r-- 1 root root 26 Nov 24 15:00 Release Notes.txt
-rw-r--r-- 1 root root 20 Nov 24 15:00 app-2026-11-01.log
-rw-r--r-- 1 root root 24 Nov 24 15:00 app-2026-11-02.log
-rw-r--r-- 1 root root 512 Nov 24 15:00 cache1.tmp
-rw-r--r-- 1 root root 512 Nov 24 15:00 cache2.tmp
-rw-r--r-- 1 root root 512 Nov 24 15:00 cache3.tmp
-rw-r--r-- 1 root root 28 Nov 24 15:00 error.log
-rw-r--r-- 1 root root 16 Nov 24 15:00 important.txt
-rw-r--r-- 1 root root 4096 Nov 24 15:00 invoice-1042.pdf
drwxr-xr-x 2 root root 4096 Nov 24 15:00 old-stuffThree log files, three temporary files, a file name with a space in it, two files to keep and an empty folder.
1. Rename a file
mv error.log errors.log
ls| Part | Meaning |
|---|---|
mv | move |
error.log | the source: what to move |
errors.log | the destination: here, just a new name in the same folder |
Release Notes.txt app-2026-11-02.log cache2.tmp errors.log invoice-1042.pdf
app-2026-11-01.log cache1.tmp cache3.tmp important.txt old-stufferror.log is gone and errors.log is there. Same file, new name.
2. Move many files with a wildcard
mkdir logs
ls *.log
mv *.log logs/
ls logs| Part | Meaning |
|---|---|
*.log | every name in this folder that ends in .log |
ls *.log | preview which files the wildcard matches, before doing anything |
mv *.log logs/ | move all of them into the logs folder. The trailing / makes it clear logs is a folder. |
app-2026-11-01.log app-2026-11-02.log errors.log
app-2026-11-01.log app-2026-11-02.log errors.logThe first line is the preview, the second is what's now inside logs/. One command moved all three.
How it works: the shell replaces *.log with the matching names before running mv. So mv actually receives mv app-2026-11-01.log app-2026-11-02.log errors.log logs/. That's why previewing with ls (or echo *.log) shows exactly what will happen.
3. Names with spaces need quotes
mv Release Notes.txt release-notes.txtmv: target 'release-notes.txt': No such file or directoryThe shell splits commands at spaces, so mv received three names: Release, Notes.txt and release-notes.txt. With more than two names, mv thinks the last one must be a folder to move the others into, and there's no such folder.
Put the name in quotes so the shell treats it as one argument:
mv "Release Notes.txt" release-notes.txt
lsYou could also escape the space with a backslash: mv Release\ Notes.txt release-notes.txt. Tab completion adds the backslash for you.
cache1.tmp cache2.tmp cache3.tmp important.txt invoice-1042.pdf logs old-stuff release-notes.txtThis is why people avoid spaces in file names on servers and in projects: use - or _ instead (release-notes.txt). And like cp, mv overwrites silently if the destination exists. Use mv -i to be asked first.
In /root/downloads:
- Move all
.logfiles into a folder calledlogs(inside/root/downloads). - Rename
Release Notes.txttorelease-notes.txt.
Expected result: ls /root/downloads/logs shows three log files, no .log file is left directly in /root/downloads, and release-notes.txt exists.
Hint: the commands, explained
cd /root/downloads
mkdir -p logs
mv *.log logs/
mv "Release Notes.txt" release-notes.txtWhat each part does
mv *.log logs/:*.logmatches every file ending in.log; mv moves them all intologs/.mv "Release Notes.txt" release-notes.txt: rename a file. The quotes are needed because the name contains a space; otherwise the shell would see two words.
Deleting: rm and rmdir
rm -v /tmp/plain.confremoved '/tmp/plain.conf'rm refuses to delete folders on its own:
$ rm /root/app-backup
rm: cannot remove '/root/app-backup': Is a directoryThere are two ways to delete folders:
| Command | Deletes | Safety |
|---|---|---|
rmdir folder | the folder, only if it's empty | safe: it refuses if anything is inside |
rm -r folder | the folder and everything in it, all the way down | no questions asked |
rm -rv /root/app-backupremoved directory '/root/app-backup/src'
removed '/root/app-backup/config/app-staging.conf'
removed '/root/app-backup/config/app-prod.conf'
removed '/root/app-backup/config/app-dev.conf'
removed directory '/root/app-backup/config'
...
removed directory '/root/app-backup'rm -r works from the inside out: first the files, then each emptied folder, and finally the top folder itself.
The rm options and what they really mean
| Option | Effect |
|---|---|
-r | recursive: delete folders and their contents |
-i | ask before every file |
-I | ask once if deleting more than 3 files or recursively. A good compromise. |
-f | force: never ask, and don't complain about missing files |
-v | show each file deleted |
-f changes two things. It silences prompts, and it hides errors about files that don't exist:
$ rm /tmp/nope.txt
rm: cannot remove '/tmp/nope.txt': No such file or directory
$ rm -f /tmp/nope.txt; echo "exit code $?"
exit code 0That's why scripts use rm -f for clean-up steps: "delete it if it's there" without failing. It's also why rm -rf is the most dangerous command on Linux: recursive, no questions, no errors, no undo.
A classic production disaster: a script runs rm -rf $BUILD_DIR/* and the variable is empty because of a typo, so the command becomes rm -rf /*. Protect yourself: use rm -rf "${BUILD_DIR:?}/"* in scripts (it refuses to run if the variable is empty), always run pwd before deleting with relative paths, and never type rm -rf on autopilot.
Wildcards (globbing)
Wildcards let one pattern match many names. The shell expands them into the list of matching names before the command runs, so the command never sees the *.
| Pattern | Matches | Example | Matches |
|---|---|---|---|
* | any characters, any number (even none) | *.conf | app.conf, nginx.conf (but not hidden files, see below) |
? | exactly one character | app-???.conf | app-dev.conf, not app-prod.conf |
[abc] | one character from the list | [ap]*.conf | names starting with a or p |
[0-9] | one character from a range | log-[0-9].txt | log-1.txt … log-9.txt |
[!abc] | one character not in the list | [!a]* | names not starting with a |
cd /root/app/config
ls app-???.conf app-????.confapp-dev.conf
app-prod.conf??? matched the three letters of dev, ???? the four of prod. staging has seven letters, so neither pattern matched it.
Two surprises
1. * skips hidden files. Names starting with a dot are not matched unless the pattern itself starts with a dot:
cd /root/app
echo *
echo .* *config logs src visible.txt
.hidden config logs src visible.txtSo rm * in a folder leaves .env and other dotfiles behind, and cp * backup/ doesn't copy them either. Use cp -a folder/. backup/ to copy a folder's full contents, hidden files included.
2. When nothing matches, the pattern stays as it is:
$ ls *.yaml
ls: cannot access '*.yaml': No such file or directoryThe shell found no .yaml files, so it passed the literal text *.yaml to ls, which then looked for a file with that strange name.
Preview first, always
Because the shell expands the pattern first, you can see exactly what a command will touch by running the same pattern with ls or echo:
ls /var/log/app/*.log.old # 1. preview: read the list
rm /var/log/app/*.log.old # 2. press ↑, change ls to rm, runThis two-step habit costs two seconds and prevents most deletion accidents.
Delete safely with wildcards
Remove the temporary files and the empty folder, and keep everything else.
rm deletes for good: no recycle bin, no undo. Experienced engineers follow one simple routine whenever they delete with a wildcard:
- Preview with
lsusing exactly the same pattern. - Read the list. Is everything on it safe to delete? Is anything missing?
- Only then press
↑, replacelswithrm, and run it.
Wildcards are powerful in both directions: rm *.tmp deletes three files, and a stray space in rm * .tmp deletes everything in the folder.
1. The wildcards
| Pattern | Matches | Example |
|---|---|---|
* | any characters, any number of them (even none) | *.tmp → cache1.tmp, cache22.tmp |
? | exactly one character | cache?.tmp → cache1.tmp but not cache22.tmp |
[abc] | one character from the list | cache[12].tmp → cache1.tmp, cache2.tmp |
2. Preview first
cd /root/downloads
ls *.tmpcache1.tmp cache2.tmp cache3.tmpExactly the three temporary files, nothing else. Safe to delete.
3. Delete files
rm -v *.tmp| Part | Meaning |
|---|---|
rm | remove (delete) files |
-v | verbose: print each file as it's deleted |
*.tmp | every name ending in .tmp |
removed 'cache1.tmp'
removed 'cache2.tmp'
removed 'cache3.tmp'4. Delete folders
rm old-stuffrm: cannot remove 'old-stuff': Is a directoryrm refuses to delete folders on its own. There are two ways:
| Command | Deletes | Safety |
|---|---|---|
rmdir old-stuff | the folder only if it's empty | safe: it refuses if anything is inside |
rm -r old-stuff | the folder and everything in it | dangerous: no questions asked |
rmdir old-stuff
lsimportant.txt invoice-1042.pdf logs release-notes.txtOnly the files worth keeping are left.
rm -i asks "remove ...?" for every file, which is good when you're unsure. rm -rf (recursive + force, never ask, ignore errors) is the command behind many famous outages: always pwd and preview with ls before using it, and never run it with a variable or path you haven't double-checked.
In /root/downloads, delete all .tmp files and the empty old-stuff folder. Keep everything else.
Expected result: ls /root/downloads shows only important.txt, invoice-1042.pdf, logs and release-notes.txt.
Hint: the commands, explained
cd /root/downloads
ls *.tmp
rm *.tmp
rmdir old-stuffWhat each part does
ls *.tmp: check first which files the pattern matches.rm *.tmp: delete them. There is no undo and no recycle bin on Linux.rmdir old-stuff: delete a folder, but only if it's empty, which makes it a safe way to remove folders.
File names: the rules
- Case matters:
Report.txtandreport.txtare two different files. - Names can contain almost anything except
/, but avoid spaces and special characters (*,?,&,$, quotes). They work, but every command then needs quotes:rm "my file.txt". - Use lowercase,
-and_:release-notes.txt,backup_2026-11-24.tar.gz. - Extensions like
.txtare just part of the name. Linux decides what a file is from its contents (filecommand), not its extension. - A leading
.makes a file hidden.
"How do you copy a directory and keep permissions and timestamps?" cp -a source destination. -r alone copies recursively but doesn't preserve everything.
"How do you rename a file?" mv old new. There's no separate rename command (some distros have a rename tool for bulk renaming with patterns).
"What's the difference between rm -r and rmdir?" rmdir deletes only empty directories; rm -r deletes a directory and everything inside it.
"Why is rm -rf dangerous?" It deletes recursively, never asks, ignores errors, and Linux has no recycle bin. Combined with an empty variable or a wrong current directory, it can wipe a server.
Summary
mkdir -pcreates nested folders safely;{a,b}brace expansion creates many names at once.touchcreates empty files or updates timestamps (-dfor a specific date).cp SOURCE DEST:-rfor folders,-afor exact backups,-i/-nto avoid overwriting. Back up configs before editing.mvmoves and renames, files and folders alike, and overwrites silently unless you use-i.rmis permanent.rmdironly removes empty folders;rm -rremoves everything inside;-fnever asks.- Wildcards
*,?,[...]are expanded by the shell, skip hidden files, and should always be previewed withlsbeforerm.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.