CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / rm

rm

Remove files and directories

Beginneressentialfilesystemdangerous

rm deletes names from directories; with -r it deletes whole trees. There is no trash can and no undo. It also explains a classic incident: you delete a huge log, but the disk stays full because a process still has the file open.

Syntaxrm [OPTION]... FILE...

Watch it run

You are cleaning up a copy of the source tree you made for testing.

devops@devops-lab: ~/app — zsh — 96×28
➜ ~/app
Step 1 of 4
rm -v src-copy/routes.js

rm with a file name removes it immediately; -v confirms. There is no trash folder to restore from.

Anatomy of a command

$
commandrm

Remove directory entries. When the last name and the last open handle are gone, the space is freed.

How it works

rm doesn't erase data. For each argument it calls unlinkat(), which removes one name from its directory and decrements the inode's link count. With -r it walks the tree depth-first (using fts), unlinking files and then removing each emptied directory with unlinkat(AT_REMOVEDIR). The kernel frees the inode and its data blocks only when two counts reach zero: the link count (names pointing to it) and the number of open file descriptors. So if a process still has a log open, rm makes the name disappear but df shows no space freed; lsof +L1 lists such deleted-but-open files. Permission to delete depends on the directory, not the file: you need write and execute on the directory. On a write-protected file rm asks first unless -f is given, and in sticky directories like /tmp you can only delete files you own. rm refuses / unless you pass --no-preserve-root.

rm removes a name; the kernel frees the data only when no names and no open file descriptors are left.

rununlinkat()remove namelinks - 1writefree blocksYourm app.logrmcoreutilsKernel VFSunlinkat()/var/log/appapp.log -> inodeInode 1048602links 1, fds 1node appPID 2741Root diskdf: 100% used
1/6
The app writes its log

The node app opened /var/log/app/app.log at startup and writes through file descriptor 21. The inode has one name and one open descriptor.

1Links1Open fds100%Disk
← → to step

Options you'll actually use

-r, -RRecursive: remove directories and their contents
-fForce: no prompts, no error for missing files
-iPrompt before every removal
-IPrompt once if removing more than 3 files or recursing
-vPrint each removed file and directory
-dRemove empty directories (like rmdir)
--End of options: allows names starting with -
--one-file-systemWith -r, skip directories on other mounted filesystems
--preserve-rootDefault: refuse to operate recursively on /

Common mistakes

rm -rf $DIR/* in a script

If DIR is empty or misspelled this becomes rm -rf /*. Unquoted, a path with spaces also splits into several targets.

rm -rf -- "${DIR:?}"/* and set -euo pipefail at the top of scripts
Deleting a huge log of a running service to free disk space

The process keeps the inode open, so no space is freed until it closes the file.

Truncate instead: sudo truncate -s 0 /var/log/app/app.log (or : > file), then fix rotation
sudo rm -rf /usr /local/old-app (meant /usr/local/old-app)

One stray space turns one path into two, and /usr goes first. The --preserve-root failsafe only protects / itself.

ls the exact path first, then recall the line and swap ls for rm; rm -rI asks once before a big recursive delete
rm *.log -f in a directory where a file named -rf exists

Globs can produce names starting with -, which rm then reads as options.

rm -- *.log or rm ./*.log

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create ~/tmp-test with two files inside, then delete the whole directory, printing each removal.

  2. 2

    Create a file literally named --help in your home, then delete it.

  3. 3

    Prove that rm doesn't free space while a file is open: open a big file with a background process, delete it, and find it with lsof.

Related commands

rmdirRemove empty directoriesfindSearch the filesystem by name, size, age, owner and morelsofList open files, sockets and the processes holding themdfReport used and free space per filesystemmvMove or rename files and directories
PreviousmvNextmkdir
Linux / File & Directory Management / rm

rm

Remove files and directories

Beginneressentialfilesystemdangerous

rm deletes names from directories; with -r it deletes whole trees. There is no trash can and no undo. It also explains a classic incident: you delete a huge log, but the disk stays full because a process still has the file open.

Syntaxrm [OPTION]... FILE...

Watch it run

You are cleaning up a copy of the source tree you made for testing.

devops@devops-lab: ~/app — zsh — 96×28
➜ ~/app
Step 1 of 4
rm -v src-copy/routes.js

rm with a file name removes it immediately; -v confirms. There is no trash folder to restore from.

Anatomy of a command

$
commandrm

Remove directory entries. When the last name and the last open handle are gone, the space is freed.

How it works

rm doesn't erase data. For each argument it calls unlinkat(), which removes one name from its directory and decrements the inode's link count. With -r it walks the tree depth-first (using fts), unlinking files and then removing each emptied directory with unlinkat(AT_REMOVEDIR). The kernel frees the inode and its data blocks only when two counts reach zero: the link count (names pointing to it) and the number of open file descriptors. So if a process still has a log open, rm makes the name disappear but df shows no space freed; lsof +L1 lists such deleted-but-open files. Permission to delete depends on the directory, not the file: you need write and execute on the directory. On a write-protected file rm asks first unless -f is given, and in sticky directories like /tmp you can only delete files you own. rm refuses / unless you pass --no-preserve-root.

rm removes a name; the kernel frees the data only when no names and no open file descriptors are left.

rununlinkat()remove namelinks - 1writefree blocksYourm app.logrmcoreutilsKernel VFSunlinkat()/var/log/appapp.log -> inodeInode 1048602links 1, fds 1node appPID 2741Root diskdf: 100% used
1/6
The app writes its log

The node app opened /var/log/app/app.log at startup and writes through file descriptor 21. The inode has one name and one open descriptor.

1Links1Open fds100%Disk
← → to step

Options you'll actually use

-r, -RRecursive: remove directories and their contents
-fForce: no prompts, no error for missing files
-iPrompt before every removal
-IPrompt once if removing more than 3 files or recursing
-vPrint each removed file and directory
-dRemove empty directories (like rmdir)
--End of options: allows names starting with -
--one-file-systemWith -r, skip directories on other mounted filesystems
--preserve-rootDefault: refuse to operate recursively on /

Common mistakes

rm -rf $DIR/* in a script

If DIR is empty or misspelled this becomes rm -rf /*. Unquoted, a path with spaces also splits into several targets.

rm -rf -- "${DIR:?}"/* and set -euo pipefail at the top of scripts
Deleting a huge log of a running service to free disk space

The process keeps the inode open, so no space is freed until it closes the file.

Truncate instead: sudo truncate -s 0 /var/log/app/app.log (or : > file), then fix rotation
sudo rm -rf /usr /local/old-app (meant /usr/local/old-app)

One stray space turns one path into two, and /usr goes first. The --preserve-root failsafe only protects / itself.

ls the exact path first, then recall the line and swap ls for rm; rm -rI asks once before a big recursive delete
rm *.log -f in a directory where a file named -rf exists

Globs can produce names starting with -, which rm then reads as options.

rm -- *.log or rm ./*.log

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create ~/tmp-test with two files inside, then delete the whole directory, printing each removal.

  2. 2

    Create a file literally named --help in your home, then delete it.

  3. 3

    Prove that rm doesn't free space while a file is open: open a big file with a background process, delete it, and find it with lsof.

Related commands

rmdirRemove empty directoriesfindSearch the filesystem by name, size, age, owner and morelsofList open files, sockets and the processes holding themdfReport used and free space per filesystemmvMove or rename files and directories
PreviousmvNextmkdir