Skip to content
Linux Administration
Lab 3 of 27·15mBeginner

Create, copy, move and delete without regret

The four commands that change a filesystem, and the flags that stop them destroying something you needed.

You need

  • A Linux system with a shell

Do first

Every one of these commands will silently overwrite or delete without asking. That is not a bug; it is what makes them scriptable. The habit to build is knowing which flag turns the safety on.

1. Make a tree in one command

mkdir -p ~/labs/linux-files/app/{config,logs,data}
cd ~/labs/linux-files
find . -type d

-p creates parents and does not complain if the directory already exists, which is why it is the only form you should use in a script.

Verify

find ~/labs/linux-files -type d | wc -l # 5

2. Copy, and notice what happens without -r

touch app/config/settings.conf
cp app/config/settings.conf app/config/settings.conf.bak
cp app/config app/config-copy

The last one fails: cp refuses a directory unless you pass -r.

cp -r app/config app/config-copy
ls app/config-copy

Verify

ls app/config-copy/settings.conf # app/config-copy/settings.conf

3. Watch cp overwrite something

echo "original" > a.txt
echo "replacement" > b.txt
cp b.txt a.txt
cat a.txt

a.txt now says replacement and the original is gone. No prompt, no backup. Two flags change that:

echo "original" > a.txt
cp -i b.txt a.txt      # asks first
cp -n b.txt a.txt      # refuses silently if the target exists
cat a.txt

Verify

cat a.txt # original — the -n copy refused rather than overwriting

4. Move is rename

mv a.txt renamed.txt
ls
mv renamed.txt app/data/
ls app/data

There is no separate rename command. mv within a filesystem is instant regardless of file size, because only the directory entry changes — the data never moves.

Verify

ls app/data/renamed.txt # app/data/renamed.txt

5. Delete, carefully

rm b.txt
rm -r app/config-copy
ls app

rm -rf on the wrong path is the single most expensive typo in this profession. Two habits that cost nothing: run the ls first with the same glob you are about to delete, and never build the path with an unquoted variable.

ls app/data/*.txt      # look at what will match
rm app/data/*.txt      # then delete exactly that

Verify

ls app/data # empty

Clean up

cd ~ && rm -rf ~/labs/linux-files

Where this goes next

You can change a filesystem. Next: finding out who you are and what you are allowed to do — which is the thing that decides whether these commands succeed at all.