1

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?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Jon
  • 13
  • 3
  • You'll need to do this with a scripting language, like Python. I can't imagine parsing filenames for extensions in `bash` or `awk`... – Blender May 12 '11 at 03:41
  • Blender, I could add the "LOCK" before the filename if need be but when i do that, it looks like this: LOCK./001.jpg. It seems to be piping the file path from find. Is there a way to pass just "001.jpg" instead of "./001.jpg"? – Jon May 12 '11 at 03:50
  • mu is too short, I am working with hundreds of files at a time. – Jon May 12 '11 at 03:51
  • Is the extension always `.jpg`? Are you using Bash or Korn or Bourne or ... which shell are you using? – Jonathan Leffler May 12 '11 at 06:16
  • Jonathan Leffler, the extension is usually .nef but sometimes .jpg. I'm using Bash (default mac). – Jon May 12 '11 at 16:34
  • check here:http://theunixshell.blogspot.com/2013/01/bulk-renaming-of-files-in-unix.html – Vijay Jan 10 '13 at 06:29

1 Answers1

2

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" {} \;
jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • I actually need it to rename the file after chflags succeeds. the only reason that Im unlocking the files is so that I can rename them. I'm not trying to pipe chflags to mv, but trying to pipe the result of "find * -flags uchg" to both "chflags nouchg" and "mv" – Jon May 12 '11 at 16:54
  • jlliagre, this solves problem #2. Thanks! This way still adds the LOCK after the file extension, but I'm not too worried about that. I changed it to the following in order to put the string at the beginning: find * -flags uchg -exec sh -c "chflags nouchg \$0 && mv \$0 LOCK\$0" {} \; – Jon May 12 '11 at 21:52