tech katta

Working with files: cp, mv, rm, mkdir, wildcards and links

Lesson 03 of 23 · 5 min readFree

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
  1. Why this matters
  2. Creating folders: mkdir
  3. Copying: cp
  4. Moving and renaming: mv
  5. Deleting: rm and rmdir
  6. Wildcards (globbing)
  7. Links: hard and soft
  8. Try it

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.

Shell
mkdir a/b/c
mkdir -p a/b/c
ls -R a
Text
mkdir: 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.

Shell
cp shared/.env backups/.env.bak
cp releases/20261202 backups
cp -r releases/20261202 releases/20261203
Text
cp: -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.
  • -i asks before overwriting. Without it, cp replaces 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.

Shell
mv releases/20261203/app.js releases/20261203/main.js
mv backups/rel-2 backups/rel-old
ls releases/20261203
Text
main.js
server.js

Moving 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.

Shell
rm shared/logs/app-2026-11-30.log
rm releases
rmdir releases/20261203
rm -r releases/20261203
Text
rm: cannot remove 'releases': Is a directory
rmdir: failed to remove 'releases/20261203': Directory not empty

rmdir 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:

Shell
rm -v shared/logs/app-2026-12-0*.log
Text
removed '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.
Shell
ls shared/logs/*.log
ls shared/logs/app-2026-12-*.log
ls shared/logs/app-2026-1[12]-0?.log
Text
shared/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.log

The 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.

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).

Shell
echo 'hello' > f.txt
ln f.txt hard.txt
ln -s f.txt soft.txt
ls -li f.txt hard.txt soft.txt
Text
590186 -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.txt

The -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:

Shell
rm f.txt
cat hard.txt
cat soft.txt
Text
hello
cat: soft.txt: No such file or directory

The 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:

Shell
ln -sfn releases/20261204 current
ls -l current
Text
lrwxrwxrwx 1 root root 17 Dec  2 20:43 current -> releases/20261204

Here -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#

  1. In a scratch folder, make demo/releases/v1 and demo/shared/logs with a single mkdir -p command, then run it again to see that it does not fail.
  2. Create a.log, b.log and c.txt with touch. Use ls with a wildcard to list only the .log files, then with ? to list only files whose name is one character plus .log.
  3. Copy demo/releases/v1 to demo/releases/v2 with cp -a. Rename v2 to v3 with mv. Delete v3 with the safest command that works.
  4. Make a file, a hard link and a soft link to it. Check ls -li. Delete the original and show which link still works.
  5. Create demo/current as a link to releases/v1, then change it to releases/v2 using ln -sfn. Confirm with readlink -f.