Operating System - HP-UX
1832237 Members
2526 Online
110041 Solutions
New Discussion

Re: Mass renaming of directory tree using unique names

 
SOLVED
Go to solution
Ron Barak
Advisor

Mass renaming of directory tree using unique names

Hi Gurus,

The problem:
I have a directory tree, filled with JPEG files.
I would like to rename all these jpegs using unique numerical names - say starting at `date +"%Y%m%d%H%M%S"` and making sure that at least one second passes before the next rename operation.

The Request:
Can you suggest a way to do this _without_ writing a script, namely, from the shell command line ?

My non-working solution:
$ find . -type f -name "*jpg" -exec cp {} ../results/`date +"%Y%m%d%H%M%S"`.jpg \; -exec sleep 1 \;

My problem is that find seems to optimize the operation, and calculates date +"%Y%m%d%H%M%S" only once (thus, all the files are copied to one name).

Conclusion:
If you could suggest a way to make the date command to re-calculate for each file renaming, it'd solve the problem.
Of course, full points will be given to shell-command-line solutions other than using find (Perl/sed/awk one-liners are welcome).

Thanks,
Ron.

9 REPLIES 9
Ivan Ferreira
Honored Contributor

Re: Mass renaming of directory tree using unique names

I think that you need a for cycle, so the date is recalculated:

for FILE in `ls $1`; do
cp $FILE ../results/`date +"%Y%m%d%H%M%S"`.jpg
sleep 1

done
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Pete Randall
Outstanding Contributor

Re: Mass renaming of directory tree using unique names

Or:

for FILE in "*.jpg"
do
cp $FILE ../results/`date +"%Y%m%d%H%M%S"`.jpg
done


Pete

Pete
Ron Barak
Advisor

Re: Mass renaming of directory tree using unique names

Hi Ivan,
As I said, I need to rename files in a directory _tree_: I don't think your solution will be able to solve that (it doesn't descend to subdirectories).
Bye,
Ron.
Arunvijai_4
Honored Contributor

Re: Mass renaming of directory tree using unique names

You can use sleep to pause for a while and do the renaming again in a loop. It should definitely help.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Pete Randall
Outstanding Contributor
Solution

Re: Mass renaming of directory tree using unique names

Ron,

Maybe something like this then:

for FILE in `find . -type f -name "*.jpg"`
do
cp $FILE /results/`date +"%Y%m%d%H%M%S"`.jpg
sleep 1
done


Pete

Pete
Ivan Ferreira
Honored Contributor

Re: Mass renaming of directory tree using unique names

Simply by adding ls -R to the script should work. But also, the script above do the job.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Mass renaming of directory tree using unique names

Hi Ron:

You can easily do what you want by pushing the '-exec' arguments into a small script snippet:

#cat /tmp/myexec
cp $1 ../results/`date +"%Y%m%d%H%M%S"`.jpg
sleep 1

...Now do:

# find . -type f -name "*jpg" -exec /tmp/myexec {} \;

Regards!

...JRF...
Denver Osborn
Honored Contributor

Re: Mass renaming of directory tree using unique names

If you're just looking for an aproach to copy files to unique names with or w/out using date and sleep to create the unique name for each, you could use a variable or some combo w/ a variable incremented... etc. could be a little more flexible w/ file names. I know you weren't looking for a script, but one way to do it w/ out much effort was in a loop.

VAR=0
BASE=`date +"%Y%m%d`
for JPG in `find ./path -name "*.jpg"`
do
cp -p $JPG ./newpath/${BASE}-${VAR}.jpg
VAR=$((VAR + 1))
done
Ron Barak
Advisor

Re: Mass renaming of directory tree using unique names

Pete Randall's solution is the most elegant and per my requirment.
Thanks Pete.