Search code examples
unixfindrenameflagslocked

UNIX: rename files piped from find command


I basically want to add a string to all the files in a directory that are locked. I'm having trouble passing the filenames to a mv command:

find . -flags uchg -exec chflags nouchg "{}" | mv "{}" "{}"_LOCK \;

The above code obviously doesnt work but I think it explains what I'm trying to do.

I'm facing two problems:

  1. Adding a string to the end of a filename but before the extension (001_LOCK.jpg).
  2. Passing the output of the find command twice. I need to do this because it won't let me change the names of the files while they are locked. So I need to unlock the file and then rename it.

Does anyone have any ideas?


Solution

  • This should be a good start.

    I assume you do not pipe chflags to mv, which doesn't make sense, but just rename the file if chflags fails. Processing the extension is more tricky but is certainly doable.

    find . -flags uchg -exec sh -c "chflags nouchg \$0 || mv \$0 \$0_LOCK" {} \;
    

    Edit: rename if chflags succeeds:

    find . -flags uchg -exec sh -c "chflags nouchg \$0 && mv \$0 \$0_LOCK" {} \;