I am trying to remove all empty files that are older than 2 days. Also I am ignoring hidden files, starting with dot. I am doing it with this code:
find /u01/ -type f -size 0 -print -mtime +2 | grep -v "/\\." | xargs rm
It works fine until there are spaces in the name of the file. How could I make my code ignore them?
OS is Solaris.
Option 1
Install GNU
findand GNUxargsin an appropriate location (not/usr/bin) and use:(Note that I removed (what I think is) a stray
-printfrom yourfindoptions. The options shown removes empty files modified more than 2 days ago where the name does not start with a., which is the condition that your originalgrepseemed to deal with.)Option 2
The problem is primarily that
xargsis defined to split its input at spaces. An alternative is to write your ownxargssurrogate that behaves sensibly with spaces in names; I’ve done that. You then only run into problems if the file names contain newlines — which the file system allows. Using a NUL ('\0') terminator is guaranteed safe; it is the only character that can’t appear in a path name (which is why GNU chose to use it with-print0etc).Option 3
A final better option is perhaps:
This avoids using
xargsat all and handles all file names (path names) correctly — at the cost of executingrmonce for each file found. That’s not too painful if you’re only dealing with a few files on each run.POSIX 2008 introduces the notation
+in place of the\;and then behaves rather likexargs, collecting as many arguments as will conveniently fit in the space it allocates for the command line before running the command:The versions of Solaris I’ve worked on do not support that notation, but I know I work on antique versions of Solaris. GNU
finddoes support the+marker and therefore renders the-print0andxargs -0workaround unnecessary.