Deleting contents of a directory only

To delete a single file we can use rm command. The same command can be used to delete multiple files. To delete a directory we can use rmdir command. Is there a command which deletes all the contents of a directory but keeps the directory, i.e., emptying a directory. Is there a command for that?

You can use command chaining to delete the directory recursively (using rm) and then create the directory again.

1 Like

you can use this command;
rm ----path—/*
(rm path followed by asterisk)

I have attached a SS for better understanding;

  • “simple” is the directory, and it has 3 files…
  • I deleted all the files using the above command,
  • no output on using ls shows that all files are deleted, and “simple” is empty now
  • and then changed directory to parent directory. used ls to show that my “simple” directory is still present
1 Like