How to remove all files from a directory in Linux?


Now removing all files from a directory in Linux is another work.At some times we are forced into some situation like deleting all files in a directory. So, in this post i have added the commands that are required to Delete all file in a directory. In this post i have given the command to delete the hidden file and the file without any extension. Check out the code guys…………..

Command to delete a file with Extension:

Type the following command.

# rm /path/to/directory/*.*

rm- stands for “remove”

As i said before this command will not work for the files that don’t have any extension.

 

Command to delete Hidden Files:

Type the following command before running the rm command.

shopt -s dotglob

or       

rm -rf /path/to/directory/{*,.*}

 

The above command can be used to delete hidden files in directory.

 

Command to delete the folder with its whole content:

Type the following command.

rm -rf /path/to/directory

The above command will delete the whole folder including all interior folders.

 

Command to delete the content of the folder:

Type the following command.

rm -rf /path/to/directory/*

The above command can be used to delete only the interior folders of a folder and it will not delete the folder containing the interior folder.

 

Command to delete all files from the folder:

Type the following command to delete files.

rm -f /path/to/directory/*

The above command can be used to delete only the files inside the folder and it will not delete the interior folders.

 

 Command to delete only files in the directory:

Type the following command to delete only files in the directory.

find /path/to/directory -type f -print0| xargs -0 rm

or

find /path/to/directory -type f -exec rm {} \;

 

8:05 am