Skip to content

How to delete files and directories in Unix/Linux

Last updated on February 10, 2018

In order to remove, delete or unlink files in Unix/Linux operating system we need to use the rm command.

Syntax :

rm [OPTION]... file or directory 

To delete file we simple write command like this :

sudo rm file_name.txt  (if are in the same directory) 

or

sudo rm /var/www/html/file_name.txt

if you want to see a warning message before deleting file use -i option as shown below

sudo rm -i file_name.txt

if you want to delete folders, its sub-folders and files recursively use -r option as shown below

sudo rm -r folder_name

if you want to remove an empty directory use rmdir command as shown below

sudo rmdir folder_name

sometimes you may want to delete all empty directories, to achieve this we need to use the combination of two commands (find and rm).

below command will find and delete all empty files

sudo find /path/to/dir -empty -type f -delete

here f indicates files

below command will find and delete or remove all empty folders from the given path

find /path/to/dir -empty -type d -delete

here d indicates directories or folders

For more information, see the rm command man page by typing the following command:

sudo man rm

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments