[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: quick shell questions

On Tue, Dec 10, 2002 at 01:00:51PM -0500, Drew Cohan wrote:
> 1.  How do I combine these two (JPG vs jpg):
> for f in /path/to/*.JPG; do mv "$f" `date +%N`.jpg; done
> for f in /path/to/*.jpg; do mv "$f" `date +%N`.jpg; done

how about 

for f in `find /path/to -iname '*jpg'`; do mv "$f" `date +%N`.jpg; done

this example lets you search in nested directories as well, which might
or might not be useful. 

note that both this example and your examples will break very ungracefully
for filenames that have spaces in them.  better would be:

find path -iname '*ogg' -print0 | xargs -0 -n1 -i mv "{}" "`date +%N`.jpg"

it's a little less intuitive at first glance, but it's a really cool
trick to know, and it comes in real handy in other situations where the
solution would otherwise be more complicated (like selectively chowning
files owned by a certain user throughout an entire directory tree, or
anything in a windows/smb mount)

> I'm trying to avoid duplicate filenames during my renaming sessions.  

well %N would be a good start at that... you'll probably not have any
trouble, though if you want to be super paranoid, you could replace
mv with a shell script that tests to see if the file exists, and if so
just tries again with a new value for %N.

> 2.  What does the "2>/dev/null>&1" mean that I see in a lot of examples
> (eg ls 2>/dev/null/>&1).

i'm a little confused about what makes this different from "2>/dev/null",
which just means send stderr to oblivion.


Attachment: pgpr8T_hNUzby.pgp
Description: PGP signature

Reply to: