You will learn in this step by step guide, how to use the rm, unlink and rmdir commands to delete files and directories in Linux.
How to Remove Files
You can use rm (remove) or unlink command to remove or delete a file from the Linux command line.
The rm command allows you to remove multiple files at once. With unlink command, you can delete only a single file.
While removing files or directories you should be extra careful, as if the file is deleted, you won’t be able to recover it easily.
- Use the rm or unlink command to delete a single file followed by the file name:
unlink /home/admin/directoryname myfile
You will be notified for confirmation if the file is write-protected, as shown below. Type y and hit Enter key to remove the file, or else, it will be deleted without prompting if the file is not write-protected.
rm: remove write-protected regular empty file ‘file’?
- Use the rm command to delete multiple files at once followed by the file names separated with space:
rm myfile1 myfile2 myfile3
To match multiple files, you can use an asterisk (*) and regular expansions. Execute the following command to remove all .pdf files placed in the current directory:
Run the ls command to list the files if you are using regular expansions so that you can see what files will be deleted before running the rm command.
- Run the rm command with the -i option to verify each file before deleting it:
rm -i /home/admin/directoryname/myfile(s)
- You can run the -f (force) option with rm command to remove the files without prompting even if the files are write-protected:
rm -f /home/admin/directoryname/myfile(s)
- You can also combine rm options. You can run the following command to delete all .txt files in the present directory without a prompt in verbose mode.
rm -fv /home/admin/directoryname/*.txt
How to Remove Directories (Folders)
You can delete/remove directories (folders) by using the rmdir and rm command in Linux.
rmdir is a command-line function that deletes empty directories/folders while rm command deletes directories and their contents recursively.
- Run either rmdir or rm -d command to remove an empty directory followed by the directory name:
rm -d /home/admin/directoryname
- Use the rm command with the -r (recursive) option to delete non-empty directories and all the files inside them:
rm -r /home/admin/directoryname
You will be prompted to confirm the deletion if a directory or a file inside the directory is write-protected.
- Run rm command with the -r (recursive) and -f options to delete non-empty directories and all the files without being prompted:
rm -rf /home/admin/directoryname
- Run the rm -r command followed by the directory names separated by space to delete multiple directories at once.
rm -r directoryname1 directoryname2 directoryname3
You can also use the asterisk (*) and regular expansions to match multiple directories the same as with files.
By now you are aware of how to use the Linux rm, rmdir and unlink commands to delete files and directories safely with the command line.