Working with files: cp, mv, rm, mkdir, wildcards and links
In this lesson
Copy, move, rename, create and delete files safely, use wildcards to act on many files at once, and understand hard links versus symbolic links.
On this page
Deploying an app is mostly moving files around: copy a release into place, rename a config, clear old logs, switch a link. This lesson covers the five commands you will use for that, and the one habit that stops you deleting the wrong thing.
Why this matters#
On a server there is no Recycle Bin. rm removes a file immediately, and a wildcard that matches more than you meant can wipe a folder of logs or, worse, a release you needed for a rollback. You will be faster and safer if you know exactly what each command does and how to preview what a pattern will match before you act.
All examples below were run in a scratch folder laid out like a small app: releases/, shared/logs/ and backups/.
Creating folders: mkdir#
mkdir makes a folder. If the parent does not exist it refuses, unless you add -p, which creates every missing level and does not complain if the folder is already there.
mkdir a/b/c
mkdir -p a/b/c
ls -R amkdir: cannot create directory ‘a/b/c’: No such file or directory
a:
b
a/b:
c
a/b/c:Use -p in scripts: it makes them safe to run twice.
Copying: cp#
cp source destination copies a file. Copying a folder needs -r (recursive), and the shell tells you if you forget.
cp shared/.env backups/.env.bak
cp releases/20261202 backups
cp -r releases/20261202 releases/20261203cp: -r not specified; omitting directory 'releases/20261202'The first command copied silently, which is normal: Unix commands print nothing when they succeed. Two options matter beyond -r:
-a(archive) copies a folder and keeps permissions, ownership, timestamps and links. Use it when the copy has to behave like the original, such as a backup of a release.-iasks before overwriting. Without it,cpreplaces an existing destination without warning.
If the destination is an existing folder, the file is copied into it. If it does not exist, the copy gets that name. That one rule explains a lot of surprises, so read your destination twice.
Moving and renaming: mv#
mv has no separate rename command; renaming is just moving to a new name in the same place.
mv releases/20261203/app.js releases/20261203/main.js
mv backups/rel-2 backups/rel-old
ls releases/20261203main.js
server.jsMoving within one disk is instant because only the name changes. Like cp, mv overwrites an existing target silently unless you use -i, or -n to refuse to overwrite.
Deleting: rm and rmdir#
rm deletes files. It does not delete folders unless you ask for -r, and there is no undo.
rm shared/logs/app-2026-11-30.log
rm releases
rmdir releases/20261203
rm -r releases/20261203rm: cannot remove 'releases': Is a directory
rmdir: failed to remove 'releases/20261203': Directory not emptyrmdir removes only empty folders, which makes it a safe way to clean up. rm -r removes a folder and everything inside it. The -v option prints each file as it goes, which is a good habit when you are working on a real server:
rm -v shared/logs/app-2026-12-0*.logremoved 'shared/logs/app-2026-12-01.log'
removed 'shared/logs/app-2026-12-02.log'Add -i to be asked about each file. Avoid rm -rf unless you are certain about the path, especially when the path comes from a variable that could be empty.
Wildcards (globbing)#
Before the command runs, the shell expands patterns into a list of matching names. The command never sees the pattern, only the result.
*matches any run of characters, including none.?matches exactly one character.[12]matches one character from a set, and[a-c]matches a range.
ls shared/logs/*.log
ls shared/logs/app-2026-12-*.log
ls shared/logs/app-2026-1[12]-0?.logshared/logs/app-2026-11-30.log
shared/logs/app-2026-12-01.log
shared/logs/app-2026-12-02.log
shared/logs/error.log
shared/logs/app-2026-12-01.log
shared/logs/app-2026-12-02.log
shared/logs/app-2026-12-01.log
shared/logs/app-2026-12-02.logThe third pattern did not match 11-30, because after 1[12]- it needs 0 and then exactly one character, and 30 starts with 3. When nothing matches, bash leaves the pattern as written, so echo shared/logs/*.txt prints shared/logs/*.txt and a command may then complain about a file with that literal name.
Hidden files are not matched by *. That is why echo * in the app folder listed backups releases shared but not .env.
Links: hard and soft#
A link is a second name for something. There are two kinds, and the difference matters.
A hard link is another name for the same data. Both names are equal; there is no original. A soft link (symbolic link, symlink) is a tiny file that stores a path, like a shortcut. Create them with ln (hard) and ln -s (soft).
echo 'hello' > f.txt
ln f.txt hard.txt
ln -s f.txt soft.txt
ls -li f.txt hard.txt soft.txt590186 -rw-r--r-- 2 root root 6 Dec 2 20:43 f.txt
590186 -rw-r--r-- 2 root root 6 Dec 2 20:43 hard.txt
590187 lrwxrwxrwx 1 root root 5 Dec 2 20:43 soft.txt -> f.txtThe -i option shows the inode number, the identity of the data on disk. f.txt and hard.txt share inode 590186 and show a link count of 2. soft.txt is its own small file with a different inode, and its listing shows where it points. The inode numbers on your machine will differ.
Now delete the original name and see what each link does:
rm f.txt
cat hard.txt
cat soft.txthello
cat: soft.txt: No such file or directoryThe data survives as long as any hard link to it remains. The soft link now points to a path that no longer exists, so it is "dangling", and reading it fails with a confusing message about a file that appears to be there.
Hard links cannot point to folders or across disks. Soft links can do both, which is why deployments use them. Switching the live release is one command:
ln -sfn releases/20261204 current
ls -l currentlrwxrwxrwx 1 root root 17 Dec 2 20:43 current -> releases/20261204Here -s makes a soft link, -f replaces an existing current, and -n stops ln from creating the new link inside the folder the old link points to. Leave out -n and a repeat deploy can produce a stray link inside the old release. Use readlink -f current to see the final real path.
Try it#
- In a scratch folder, make
demo/releases/v1anddemo/shared/logswith a singlemkdir -pcommand, then run it again to see that it does not fail. - Create
a.log,b.logandc.txtwithtouch. Uselswith a wildcard to list only the.logfiles, then with?to list only files whose name is one character plus.log. - Copy
demo/releases/v1todemo/releases/v2withcp -a. Renamev2tov3withmv. Deletev3with the safest command that works. - Make a file, a hard link and a soft link to it. Check
ls -li. Delete the original and show which link still works. - Create
demo/currentas a link toreleases/v1, then change it toreleases/v2usingln -sfn. Confirm withreadlink -f.
Sign in to keep track of the lessons you finish.