Tech Support > Operating Systems > Linux / Variants > batch delete and rename files
batch delete and rename files
Posted by Geoff Sullivan on February 25th, 2004


I'm using a process that creates massive amounts of .jpg files. They are
named in numerical order as they are created:

00000001.jpg
00000002.jpg
00000003.jpg
..
..
etc.

I would like a simple(?) one liner or bash script that will delete (or move)
every even numbered file, and then rename the files remaining in correct
order, ie.:

file0001.jpg
file0002.jpg
file0003.jpg
..
..
etc.

I've tried fooling around with reg expressions to do this but I guess I
just don't get it

Thanks,
Geoff
--
** **
G. Sullivan sunfishATshell.gis.net

Posted by Geoff Sullivan on February 25th, 2004


Geoff Sullivan wrote:

OK, I've tried

mv *[02468].jpg /wherever

and it works good enough for my puposes. Now to change the remaining
filenames. The formula for this would be something like x=y/2+.5 where y is
the original number of the file. I'd want to append x to a four-letter
prefix, followed by the extension .jpg.


Posted by Geoff Sullivan on February 25th, 2004


Chris F.A. Johnson wrote:

This works great! Now can you EXPLAIN what goes on here so I can learn from
this instead of being a script leech?

Thanks Chris.

Posted by Chris F.A. Johnson on February 25th, 2004


On Wed, 25 Feb 2004 at 14:17 GMT, Geoff Sullivan wrote:
rm *[02468].jpg
n=1
z=0000
for file in *.jpg
do
f=$z$n
newfile=file${f: -4}.jpg
mv "$file" "$newfile"
n=$(( $n + 1 ))
done

There are other ways to build the new file name:

newfile=`printf "file%04d.jpg" $n`

f=$z$n
newfile=file${f#${f%????}}.jpg

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
================================================== =================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Posted by Garry Knight on February 25th, 2004


In message <c1is26$1jipu2$1@ID-210011.news.uni-berlin.de>, Chris F.A.
Johnson wrote:

Shouldn't that be 'mv "$newfile" "$file"' ?

--
Garry Knight
garryknight@gmx.net ICQ 126351135
Linux registered user 182025

Posted by Garry Knight on February 25th, 2004


In message <1077738290.93232.0@doris.uk.clara.net>, Garry Knight wrote:

Sorry, forget that: brain in C mode.

--
Garry Knight
garryknight@gmx.net ICQ 126351135
Linux registered user 182025

Posted by Chris F.A. Johnson on February 25th, 2004


On Wed, 25 Feb 2004 at 15:51 GMT, Geoff Sullivan wrote:
n=1 ## initialize first file number
z=0000 ## a string for padding with zeroes
for file in *.jpg ## for each file enfing with .jpg
do
f=$z$n ## put the zeroes ahead of the number
newfile=file${f: -4}.jpg ## use the last 4 characters of $f
mv "$file" "$newfile" ## move 'em out
n=$(( $n + 1 )) ## add one to $n
done

Extract the number from the file name:

file=00000009.jpg
num=${file%.jpg} ## remove .jpg

## remove leading zeroes, or the number will be read as octal,
## and the script will barf on 08 or 09
while :
do
case $num in
0*) num=${num#0} ;;
*) break ;;
esac
done

prefix=file
newnum=$(( $num / 2 + 5 ))
padnum=$num ## use any method you like to pad with zeroes
newfile=$prefix$padnum.jpg

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
================================================== =================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Posted by Anonymous Sender on February 25th, 2004


"GS" == Geoff Sullivan <sunfish@REMOVE.shell.gis.net>:
GS> I'm using a process that creates massive amounts of .jpg files. They are
GS> named in numerical order as they are created:
GS> 00000001.jpg
GS> 00000002.jpg
GS> 00000003.jpg
GS> I would like a simple(?) one liner or bash script that will delete (or move)
GS> every even numbered file, and then rename the files remaining in correct
GS> order, ie.:
GS> file0001.jpg
GS> file0002.jpg
GS> file0003.jpg

The folowing bash command will tell you what needs to be done :

c=0;for i in *.jpg;do x=`basename $i .jpg|sed 's@^0*@@';`;if [ 0 = \
$(($x%2)) ];then echo rm $i;else c=$(($c+1));echo mv $i \
`printf file%04s $c`.jpg;fi;done

Removing the 'echo' strings from the above command will silently do it
(probably dangerous without testing first).

Posted by Nick Landsberg on February 25th, 2004




Anonymous Sender wrote:

Anyone who would recommend that particular one-liner to
a newbie, even with the echo command in it, is a sadist!

::chuckle::


--
Ñ
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch


Posted by John W. Krahn on February 26th, 2004


Geoff Sullivan wrote:

perl -e'$_%2?push@o,$_:unlink for<*.jpg>;rename$_,sprintf"file%04d.jpg",++$c for@o'


John
--
use Perl;
program
fulfillment

Posted by Ed Murphy on February 26th, 2004


On Thu, 26 Feb 2004 01:33:02 +0000, John W. Krahn wrote:

Now that's just mean.

After about fifteen minutes of poking around the Python online docs,
and another five for testing and debugging, I came up with this:

#!/usr/bin/python
import glob
import os
import string
for unwantedfilename in glob.glob('*[02468].jpg'):
os.remove(unwantedfilename)
newnumber = 0
filenamelist = glob.glob('*[13579].jpg')
filenamelist.sort()
for oldfilename in filenamelist:
newnumber += 1
os.rename(oldfilename, 'file' + string.zfill(str(newnumber), 4) + '.jpg')



Similar Posts