Managing files and directories through the Linux command line is a fundamental skill for users and system administrators alike. Whether you're freeing up space or organizing your system, knowing how to delete files and folders efficiently using the rm
and rmdir
commands is essential.
Deleting Files with the rm
Command
To remove one or multiple files, use the rm
command followed by the filenames:
rm <filename>
rm <file1> <file2>
If a file is write-protected, the system will prompt you for confirmation before deletion. To force delete without any prompts, utilize the -f
(force) option:
rm -f <file1> <file2>
To receive a confirmation prompt for each file, use the -i
(interactive) option, which helps prevent accidental deletions:
rm -i <file1> <file2>
Deleting Directories with rmdir
and rm -r
To remove empty directories, use the rmdir
command:
rmdir <directory_name>
To delete a directory and all of its contents recursively, employ the rm
command with the recursive flag -r
:
rm -r <directory_name>
Be aware that while these commands remove files and directories from the file system, the data may still be recoverable using data recovery tools.
To ensure that your deleted data cannot be recovered, refer to our guide on permanently deleting files and folders in Linux.
READ: How to Permanently Delete Files in Linux
By mastering these commands, you can efficiently manage your Linux system, maintaining organization and ensuring that unwanted files and directories are properly removed.
Member discussion