Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove only files in directory on linux NOT directories [closed]

Tags:

linux

rm

What delete command can be run to remove only files in given directory

  • NOT directories
  • NOT sub-directories
  • NOT files in these sub-directories.

Some files don't have extensions so rm *.* wont work...

There are thousands of files in this folder.

Any advice?

like image 966
AndrewC Avatar asked Oct 10 '11 15:10

AndrewC


People also ask

How do you delete all files in a directory without deleting the directory?

Open the terminal application. To delete everything in a directory run: rm /path/to/dir/* To remove all sub-directories and files: rm -r /path/to/dir/*

How do I delete a file without deleting a folder in Linux?

dot files. In order to delete the files but keep the sub directories, you could add -type f after the depth option. Also you could add -maxdepth 1 if you want to keep the content of the sub directories.


1 Answers

find PATH -maxdepth 1 -type f -delete 

BUT this won't prompt you for confirmation or output what it just deleted. Therefore best to run it without the -delete action first and check that they're the correct files.

like image 66
James Avatar answered Oct 13 '22 03:10

James