1

How to delete the subdirectory content and files present inside directory? I don't want to delete the structure of subdirectory, just want to delete the content inside the subdirectory and files inside the directory.

Like I have one directory dev; inside that I have 3 files: test1.txt, test2.txt and test3.txt. And I have one subdirectory Node. Inside the subdirectory I have 3 files again: node1.txt, node2.txt, node3.txt.

I just want to delete test1, test2, test3 and node1, node2, node3, not the Node subdirectory.

1 Answers1

2

This will delete all regular files in the specified_directory and in all its subdirectories recursively:

find specified_directory -type f -exec rm -i {} +

-i here is to make the command less dangerous (mostly for people who run random pieces of code from the Internet). After you confirm the command does what you want, start using it without -i.

Other equivalent(-ish) forms are discussed here: find: -exec rm {} \; vs. -delete - why is the former widely recommended?

Change -type f to ! -type d to delete all files that are not of the type directory.