I'm trying to write a bash script that will run on my QNap NAS to touch a directory recursively. I have this:

find $1 -exec touch {} +

However, find that comes with BusyBox 1.01 doesn't support the -exec argument, here are the docs:

BusyBox v1.01 (2011.02.08-16:24+0000) multi-call binary

Usage: find [PATH...] [EXPRESSION]

Search for files in a directory hierarchy.  The default PATH is
the current directory; default EXPRESSION is '-print'

EXPRESSION may consist of:
    -follow     Dereference symbolic links.
    -name PATTERN   File name (leading directories removed) matches PATTERN.
    -print      Print (default and assumed).

    -type X     Filetype matches X (where X is one of: f,d,l,b,c,...)
    -perm PERMS Permissions match any of (+NNN); all of (-NNN);
            or exactly (NNN)
    -mtime TIME Modified time is greater than (+N); less than (-N);
            or exactly (N) days

So, is there another approach I could use instead, to achieve the same goal? Thanks.

  • What shell are you in?/What do you have available? I would hope that one of those would have for or read to take the list from find's -print? – Pricey Apr 24 '11 at 18:09

Your busybox may have the xargs command:

find $1  | xargs touch

This command has the nice effect of being able to call e.g. touch with multiple file names at once, thus shorting the net run time quite a bit.

  • Yes, do have xargs. Seems to work, but some files have ' characters in the file names, and I get "xargs: unmatched single quote". Any ideas? – Jack Sleight Apr 24 '11 at 19:03

I have had the exact same need, and, after experimenting with the differences in the BusyBox implementation, have created this 1-line bash script:

[/share/MD0_DATA] # cat ./touch_all_folders
find -type d | sed 's/[^[:alnum:].\/_-]/\\&/g' | xargs touch -c

You can customize the script with parameters as you see fit. The sed command escapes all the special filename characters.

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.