0

I need to remove files and sub-directories in a particular folder but not that folder itself.

I am using below command:

find . -type d -depth -mtime +7 -exec rm -rf {} \;

But it throws following error message:

rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’

What should be the correct command I avoid or handle this error message? Is it happening because of option "-depth" in my find command?

Linux version:
NAME="Red Hat Enterprise Linux Server"
VERSION="7.1 (Maipo)"

4

If you have /path/to/folder, and would like to remove non-hidden files inside, but not that folder, just run:

rm -rf /path/to/folder/*

Note that this won't remove hidden files (ones starting with a dot).

And given that you have right permissions and don't need this folder on your system all the time, you can simply remove it and create a new empty folder:

rm -rf /path/to/folder; mkdir /path/to/folder
| improve this answer | |
  • 1
    That won't remove filenames that start with "." – ShadSterling Oct 12 '17 at 19:14
  • Agree with @ShadSterling. Plus: In folders with a lot of files you'll end up with a very long command line (/path/to/folder/* will be expanded by the shell to the list of all files in that directory). You may get a command line too long error in these cases. – dasup Nov 29 '18 at 14:15
  • @dasup Is that ever a problem in practice? On my current OS, ARG_MAX is 2097152. This means you could have 10k files in there that are 209 characters long. I believe that by the time it is possible for you to encounter this problem, you should have enough knowledge to know how to delete those, too. – MatthewRock Nov 29 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.