Removing Files & Directories
6 min read·Jan 1, 2025
⚠️ USAGE WARNING ⚠️
Unlike when using the graphical user interface, the action of removing files and directories from the command-line interface will not place the files you want to remove into the trash folder, but will immediately and irrevocably erase them from your system.
You should therefore use the following commands with extreme caution as there is no undelete command or other tricks to get your files back.
Once again, when deleting files and directories using the
rmcommand, they are gone forever!
Removing regular files
To remove one or more regular files, you can use the rm command — which stands for remove — as follows:
$ rm file ...
Where file ... are the paths to the files you want to remove.
Note: Upon successful removal, the
rmcommand doesn't produce any output to the terminal window.
Example
This command will remove the index.js file located in the current directory:
$ rm index.js
Removing empty directories
To remove empty directories, you can use the rm command with the -d flag (short for directory) as follows:
$ rm -d directory ...
Where directory ... are the paths to the empty directories you want to remove.
Alternatively, you can also use the rmdir command, which is specifically designed for that purpose:
$ rmdir directory ...
Note: If the directory is not empty, the
rmcommand will output the following error:rm: directory: Directory not empty
Example
Let's consider this directory:
.
├── tmp
└── utils
├── math.js
└── string.js
This command will remove the tmp directory:
$ rm -d tmp
This command will output an error:
$ rm -d utils
rm: utils: Directory not empty
Removing non-empty directories
To recursively remove directories, including all the files and subdirectories they contain, you can use the rm command with the -r flag (short for recursive) as follows:
$ rm -r directory ...
Example
Let's consider this directory:
.
└── utils
├── math.js
└── string.js
This command will remove the utils directory, including the math.js and string.js files it contains:
$ rm -r utils
Safely removing files and directories
As removing files using the rm command can cause irreversible damages to both the operating system and your personal files, here are some good practices you can follow to mitigate that risk.
Preserving system files
As a rule of thumb, you should never use the rm command on directories that begin with a forward slash character / as they usually are sensitive directories used by the operating system.
Removing entries in interactive mode
To be prompted for confirmation before deleting any file or directory, you can use the rm command with the -i flag (short for interactive):
$ rm -i [-d|-r] entry ...
Once executed, you can either type y (short for yes) to continue with the deletion of the specified entry or n (short for no) to preserve this entry in the filesystem, then press the ENTER key to confirm your choice.
Example
This command will prompt for confirmation before attempting to remove the index.js file:
$ rm -i index.js
remove index.js?
Testing pathname expansions
When using pathname expansions such as wildcards or braces to match multiple files or directories, you should always test your pattern first using the echo or ls command before replacing it with the rm command to actually remove the matched entries.
Example
This command will output all the entries present in the current directory with a .js or .json file extension:
$ echo *.{js,json}
index.js package-lock.json package.json
And this command will remove the entries matched by the specified pattern:
$ rm *.{js,json}
Summary
Here's a summary of what you've learned in this lesson:
- The
rmcommand is used to remove files. - The
rm -dorrmdircommands are used to remove empty directories. - The
rm -rcommand is used to recursively remove non-empty directories. - The
rm -icommand is used to remove entries in interactive mode. - You should not remove entries starting with a forward slash
/. - You should test pathname expansions with
echoorlsbefore usingrm.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery