5

I have a sample dir as follows:

/path/to/dir/
- f1.txt
- f2.txt
- f3.txt

I want to clear or remove all the files under /path/to/dir without actually deleting the directory /path/to/dir itself.

How to do it?

Currently, I am just using the rm -rf /path/to/dir command to remove the directory and the creating the directory again using mkdir /path/to/dir. But this command deletes the directory also.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
user3243499
  • 151
  • 1
  • 1
  • 2

2 Answers2

6

Use wildcard (*)

rm /path/to/dir/*

Will remove all file under /path/to/dir directory.

Liso
  • 15,217
  • 3
  • 50
  • 80
3

Remove all files and directories in directory dir/ including .dot files.

$ find /path/to/dir/ -mindepth 1 -delete
  • 1
    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. – pa4080 Dec 03 '19 at 09:13