7

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.

0

2 Answers 2

7

Use wildcard (*)

rm /path/to/dir/*

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

1
  • Or, rm -r /path/to/dir/* if there are directories under /path/to/dir/
    – FedKad
    Dec 3, 2019 at 8:27
4

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

$ find /path/to/dir/ -mindepth 1 -delete
1
  • 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 3, 2019 at 9:13

Not the answer you're looking for? Browse other questions tagged .