Deleting and Linking Files: rm, rmdir, ln and Symlinks

Beginner
11 min

Deleting and Linking Files: rm, rmdir, ln and Symlinks

The previous lesson created, copied and moved files; this one covers the two operations that complete the picture. Deleting on the command line is permanent — there is no recycle bin — so you will learn the flags that keep rm safe. You will also learn to create links, which let one file appear in several places and are used everywhere from /usr/bin to your own project deployments.

Deleting Files with rm

rm removes files immediately. It does not ask for confirmation, and the space is reused as soon as no process holds the file open.

bash
rm notes.txt # delete a file rm a.txt b.txt c.txt # delete several rm -i report.pdf # prompt before each deletion rm -v *.tmp # verbose: print each name as it goes

Directories need the recursive flag, and non-empty directories need it too:

bash
rm -r build/ # delete build and everything inside it rm -rf build/ # -f: no prompts, no error if it does not exist

rm -rf is the most dangerous command you will use regularly. A stray space turns rm -rf ~/tmp into rm -rf ~ /tmp, which wipes your home directory. Rules that prevent disasters:

  • Type the path, press Tab to confirm it completes, and look at the line before pressing Enter.
  • Never use rm -rf with a variable you have not checked: rm -rf "$DIR"/ deletes / if DIR is empty. Write rm -rf "${DIR:?}/" so bash aborts when it is unset.
  • Prefer rm -ri when cleaning up interactively; the prompt costs a second and saves a day.
  • To delete a file whose name starts with a dash use rm -- -draft.md or rm ./-draft.md.

rmdir for Empty Directories

rmdir removes a directory only if it is empty, which makes it a safe alternative when you expect emptiness.

bash
rmdir logs # rmdir: failed to remove 'logs': Directory not empty rmdir -p a/b/c # remove c, then b, then a if each becomes empty

Inodes: What a File Really Is

On Linux a file's data and metadata (permissions, owner, timestamps) live in an inode identified by a number. A filename is merely a directory entry pointing at an inode. ls -i shows the number. This model is what makes links possible: several names can point at one inode, and a name can point at another name.

Hard Links

A hard link is a second directory entry for the same inode. Both names are equal; neither is "the original". The data is deleted only when the last link is removed.

bash
echo "v2 config" > config-v2.txt ln config-v2.txt config-hard.txt ls -li
bash
1310745 -rw-r--r-- 2 alice alice 10 Sep 27 10:00 config-hard.txt 1310745 -rw-r--r-- 2 alice alice 10 Sep 27 10:00 config-v2.txt

Both lines show the same inode (1310745) and a link count of 2. Editing through either name changes the shared content. Hard links cannot cross filesystems and cannot point at directories.

Symbolic Links

A symbolic (soft) link is a small file containing a path. It can point at a file or directory on any filesystem, and it is what most people mean by "a link".

bash
ln -s config-v2.txt config.txt ls -l config.txt # lrwxrwxrwx 1 alice alice 13 Sep 27 10:01 config.txt -> config-v2.txt ln -s /var/www/releases/2026-09-27 /var/www/current # link to a directory ln -sfn /var/www/releases/2026-09-28 /var/www/current # atomically repoint it

The l at the start of the permissions and the -> arrow mark a symlink. If the target is deleted the link remains but is dangling; ls colours it red and reading it fails with "No such file or directory". Use readlink -f config.txt to resolve the final target. rm config.txt removes the link, not the file it points to.

| Feature | Hard link | Symbolic link | |---|---|---| | Points at | Inode | Path (by name) | | Survives deleting the original | Yes | No (becomes dangling) | | Can cross filesystems | No | Yes | | Can link directories | No | Yes | | Created with | ln target name | ln -s target name |

Where You Will Meet Links

  • /usr/bin/python3 -> python3.12: versioned binaries with a stable name.
  • /etc/nginx/sites-enabled/app -> ../sites-available/app: enabling a config by linking it.
  • current -> releases/<timestamp>: zero-downtime deploys switch a symlink instead of copying files.
  • /bin -> usr/bin: the merged directory layout on modern distributions.

Common Mistakes

  • Creating a relative symlink from the wrong directory. ln -s ../data/db.sqlite db is resolved relative to the link's location, not to where you ran the command. When in doubt use an absolute target.
  • Reversing the argument order. It is always ln -s TARGET LINKNAME, the same order as cp SOURCE DEST.
  • Using rm -r on a symlink to a directory with a trailing slash: rm -r link/ follows the link and deletes the contents. Use rm link without the slash.
Quick Quiz
Question 1 of 3

What is the safest way to delete a directory tree interactively?

Key Takeaways

  • rm deletes permanently; -r for directories, -i to confirm, -f to suppress prompts and errors.
  • Guard variables in rm -rf "${DIR:?}/" and read the line before pressing Enter.
  • rmdir only removes empty directories, which makes it safe.
  • Hard links share an inode; symbolic links store a path and can dangle.
  • ln -s TARGET LINKNAME — the argument order matches cp.

Next lesson: Viewing Files: cat, less, head, tail — read file contents, page through long output and follow logs as they grow.

Deleting and Linking Files: rm, rmdir, ln and Symlinks - Linux & Command Line | CodeYourCraft | CodeYourCraft