2

CakePHP has a convention of putting files called "empty" in empty directories. I'm sure they have a reason, but it really bugs me. (Yeah, OCD, I know...)

I want a one-line linux shell command that will delete every file with the name "empty" in every descendent directory. (I suppose it would be useful to be able to specify wildcards for more general use too.)

Any ideas?

4 Answers4

14

Simplest is:

find . -name empty -type f -exec rm -f {} \;

"." starts at current directory, replace with a directory path if you want to execute it in another location.

"-type f" just makes sure it's a file.

skraggy
  • 1,783
3

Short Summary:

There are multiple options:

find . -name empty -type f -exec rm -f {} \;

will call rm once for every file

find . -name empty -type f -exec rm -f {} +

will call rm only as often as necessary by constructing a command line that uses the maximum length possible

find . -name empty -type f | xargs rm -f

same as above, you may (or rather will, as above - which was my approach) run into problems with filenames that contain characters that need quoting

find -name empty -type f -print0 | xargs -0 rm -f

is probably the best solution Please Upvote Dan C's comment it's his solution. It will as the earlier snippet call rm only as often as need by constructing a command line that uses the maximum lenght possible, the -0 switch means that arguments to the rm command will be separated by \0 so that escaping is done right in the shell.

On a side note about the comment to restrict by using -type f you can also restrict with -size 0 (exactly 0 bytes) I can't verify if CakePHP really adheres to that convention but just about every project I know that uses placeholder files to check empty directories into the source repositories does that.

Also as Matt Simons points out adding a -v flag might be a nice option to see some progress, however do notice that this will definitely slow down the whole process. A better approach might be to follow the process by using strace -p $pid_of_xargs (add addtional options if you want to follow child processes, afaik that is possible)

Another note i just found in the manpage:

find -name empty -type f -delete

after all find has all of that builtin :)

serverhorror
  • 6,538
2

A possible reason why cakePHP might do this is because some version control systems (e.g. Mercurial and Git) cannot track an empty directories.

-2

This might do:

rm -f $(find /var/www -name empty -print)

Find recurses by default.

Berzemus
  • 1,192