Batch renaming all files in a directory?
I have various directories contaning a large number of jpeg images in no
particular order, some have spaces in the filenames, some with upper case
".JPG" and some with lower case ".jpg" or ".Jpeg" etc., ie. a real mess!
The only thing all have in common is the period in the filename.
I would like to rename all in one sweep as they appear in the directory to:
000001.jpg
000002.jpg
000003.jpg
etc.
Does anyone here happen to have a nice command line that could do exactly
this on a p/directory basis including defining the start start number which
could be 000001.jpg or a higher number?
Re: Batch renaming all files in a directory?
On 2005-11-23, Fred wrote:[color=blue]
> I have various directories contaning a large number of jpeg images in no
> particular order, some have spaces in the filenames, some with upper case
> ".JPG" and some with lower case ".jpg" or ".Jpeg" etc., ie. a real mess!
>
> The only thing all have in common is the period in the filename.
>
> I would like to rename all in one sweep as they appear in the directory to:
>
> 000001.jpg
> 000002.jpg
> 000003.jpg
> etc.
>
> Does anyone here happen to have a nice command line that could do exactly
> this on a p/directory basis including defining the start start number which
> could be 000001.jpg or a higher number?[/color]
n=666 ## Adjust to taste
for file in *.[Jj][Pp][Gg] *.[Jj][Pp][Ee][Gg]
do
if [ -f "$file" ]
then
np=000000${n%.*}
mv "$file" "${np#"${np%??????}"}.jpg"
n=$(( $n + 1 ))
fi
done
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Re: Batch renaming all files in a directory?
On comp.unix.questions, in <dm0ufr$mc9$00$1@news.t-online.com>, "Fred" wrote:
<body not downloaded>
This person is using the "X-No-Archive: yes" header. This header
causes groups.google.com to delete the post from the Usenet
Archives in a week or so.
This header is used almost exclusively by trolls (or worse) that
are hiding their tracks.
If someone doesn't think that their posts are valuable enough to
archive, then why would anyone think they are valuable enough to
read? Who would take the advice of someone who can deny that they
ever posted it in week or so?
Sensible people don't even bother reading the posts of people who
employ the "X-No-Archive: yes" header.
He's also using a common name for an alias, which means that
it will be impossible to pick his posts out from amongst all
the other Freds in the Archives: He has effectively erased his
posting history.
Also, there's no IP in his headers, and no User-Agent header,
which has to be deliberately removed by the user.
Why would someone go to all this trouble to hide their identity
if they aren't up to something that isn't very nice?
Fortunately, he doesn't know much about Linux/Unix, or he
wouldn't have to ask such a basic question.
Clearly, he's a windoze-weenie-script-kitty that knows a little
DOS and is probably up to no good.
I wonder _whose_ computer he plans (to try) to rename a lot of
files on...
The tcp/ip protocols are the same for all OSs, or the Internet
wouldn't work, but crackers from the windoze world find
themselves stray when they encounter a nix server...
AC
--
URLs of possible interest in my headers.
Re: Batch renaming all files in a directory?
On comp.unix.questions, in <3jcd53-grt.ln1@rogers.com>, "Chris F.A. Johnson" wrote:
<body not downloaded>
Figures. Chris would help a terrorist if they called themselves
"Fred" and SAID they wanted the information for some other
reason than killing a lot of American civilians.
He probably has, and couldn't care less.
Looking at the headers and thinking about what they might mean
is just too much trouble: He's got to be the FIRST person to
answer a question he's answered a thousand times before, and thatany newbie could answer with ease.
He doesn't care about anything but proving to the world that he's
smarter than the average guy.
He's like one of the a-moral scientists that make weapons of mass
destruction.
There are too many such people on these groups and in the world.
AC
--
URLs of possible interest in my headers.
~
~
Re: Batch renaming all files in a directory?
> done[color=blue]
>[/color]
Thanks - works great!
Re: Batch renaming all files in a directory?
On Wed, 23 Nov 2005 06:12:36 +0100, Fred wrote:[color=blue]
> I have various directories contaning a large number of jpeg images in no
> particular order, some have spaces in the filenames, some with upper case
> ".JPG" and some with lower case ".jpg" or ".Jpeg" etc., ie. a real mess!
>
> The only thing all have in common is the period in the filename.
>
> I would like to rename all in one sweep as they appear in the directory to:
>
> 000001.jpg
> 000002.jpg
> 000003.jpg
> etc.
>
> Does anyone here happen to have a nice command line that could do exactly
> this on a p/directory basis including defining the start start number which
> could be 000001.jpg or a higher number?[/color]
autoload -U zmv
n=1 # or any other start number
for dir in **/*(/) .; do # loop through sub-directories
(
cd -- $dir &&
zmv -Q '*.(#i)jp(e|)g(-.)' '${(l:6::0:)$((n++))}.jpg'
)
done
(with zsh)
--
Stephane
Re: Batch renaming all files in a directory?
Fred wrote:
[color=blue]
> I have various directories contaning a large number of jpeg images in no
> particular order, some have spaces in the filenames, some with upper case
> ".JPG" and some with lower case ".jpg" or ".Jpeg" etc., ie. a real mess!
>
> The only thing all have in common is the period in the filename.
>
> I would like to rename all in one sweep as they appear in the directory to:
>
> 000001.jpg
> 000002.jpg
> 000003.jpg
> etc.
>
> Does anyone here happen to have a nice command line that could do exactly
> this on a p/directory basis including defining the start start number which
> could be 000001.jpg or a higher number?[/color]
/bin/ls *.[Jj][Pp][Gg] | awk '{ printf("mv %c%s%c
%06d.jpg\n",34,$1,34,NR);}' | sh
Leave off the '| sh' until you get the rest right.
Note: I specified the path to ls to avoid any alias that might decorate
the file names, like 'ls -F'.
-- ced
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
Re: Batch renaming all files in a directory?
Chuck Dillon worte:[color=blue]
> ... | awk '{ printf("mv %c%s%c %06d.jpg\n",34,$1,34,NR);}' | ...[/color]
Hi Chuck,
does anything speak against
awk '{ printf("mv \"%s\" %06d.jpg\n",$1,NR);}'
?
Regards Rainer
Re: Batch renaming all files in a directory?
On Wed, 23 Nov 2005 15:44:27 +0100, Rainer Temme wrote:[color=blue]
> Chuck Dillon worte:[color=green]
>> ... | awk '{ printf("mv %c%s%c %06d.jpg\n",34,$1,34,NR);}' | ...[/color]
>
> Hi Chuck,
>
> does anything speak against
>
> awk '{ printf("mv \"%s\" %06d.jpg\n",$1,NR);}'
>
> ?[/color]
[...]
Same as for Chuck's solution, if fails if the file names contain
", $, `, \ or newline characters.
Using:
find .//. -type f \( -name '*.[jJ][pP][gG]' -o \
-name '*.[jJ][pP][eE][gG]' \) -print | awk "
function escape(s) {
gsub(/'/, \"'\\\\\\''\", s)
return \"'\" s \"'"'"
}
function process_file(file) {
match(file, /.*\//)
dir = substr(file, 1, RLENGTH - 1)
n[dir]++
printf("mv -f %s %06d.jpg\n", escape(file), n[dir])
}
{
if ($0 ~ /\/\//) {
if (NR > 1)
process_file(file)
file = $0
} else
file = file "\n" $0
}
END {
process_file(file)
}' | sh
may be more accurate (equivalent to the zsh solution I posted
except for the non-sorting of files in the directories before
renaming).
--
Stephane
Re: Batch renaming all files in a directory?
On 23 Nov 2005 15:21:06 GMT, Stephane Chazelas wrote:
[...][color=blue]
> printf("mv -f %s %06d.jpg\n", escape(file), n[dir])[/color]
[...]
printf("mv -f %s %s/%06d.jpg\n", escape(file), escape(dir), n[dir])
sorry.
--
Stephane
Re: Batch renaming all files in a directory?
Fred <spam@mailinator.com> wrote:[color=blue]
> I have various directories contaning a large number of jpeg images in no
> particular order, some have spaces in the filenames, some with upper case
> ".JPG" and some with lower case ".jpg" or ".Jpeg" etc., ie. a real mess!
>
> The only thing all have in common is the period in the filename.
>
> I would like to rename all in one sweep as they appear in the directory to:
>
> 000001.jpg
> 000002.jpg
> 000003.jpg
> etc.
>
> Does anyone here happen to have a nice command line that could do exactly
> this on a p/directory basis including defining the start start number which
> could be 000001.jpg or a higher number?[/color]
mkdir /tmp/x
n=0
for i in *.*; do
n=$((n+1))
to=`printf '%06d.jpg' $n`
mv "$i" /tmp/x/$to
done
--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
[url]http://home.eol.ca/~parkw/thinflash.html[/url]
BashDiff: Super Bash shell
[url]http://freshmeat.net/projects/bashdiff/[/url]
Re: Batch renaming all files in a directory?
Rainer Temme wrote:[color=blue]
> Chuck Dillon worte:
>[color=green]
>> ... | awk '{ printf("mv %c%s%c %06d.jpg\n",34,$1,34,NR);}' | ...[/color]
>
>
> Hi Chuck,
>
> does anything speak against
>
> awk '{ printf("mv \"%s\" %06d.jpg\n",$1,NR);}'
>
> ?[/color]
Nope. I use tcsh as my interactive shell and it unfortunately doesn't
handle escaping double quotes inside of double quotes by default. The
above would work in tcsh/csh because the single quote protects the
double quote gymnastics. But I tend to limit nesting quotes because of
the csh/tcsh limitation. Especially when there's an easy method
available to do so.
I know, I know, I know ... but I still prefer interactive tcsh...
-- ced
[color=blue]
>
> Regards Rainer
>[/color]
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.