You are not logged in.

#1 2008-08-12 08:59:47

visio159
Member
Registered: 2008-07-03
Posts: 31

Mass renaming...using command line

hi

I can rename a folder containing files with filenames without space to another name by using this command:

i=1;for f in `ls`;do mv -v $f $i\large.png; echo $((i=i+1));done

but the thing is that if ther is a space in the name of a file then it beaks into 2 files.

How to avoid that ? 94.png

Offline

#2 2008-08-12 09:53:20

Hohoho
Member
Registered: 2007-06-23
Posts: 222

Re: Mass renaming...using command line

i=1;for f in `ls`;do mv -v "$f" "$i\large.png"; echo $((i=i+1));done

How about this?

Offline

#3 2008-08-12 11:21:56

visio159
Member
Registered: 2008-07-03
Posts: 31

Re: Mass renaming...using command line

nope it throws this error

`lemo' -> `1large.png'
2
mv: cannot stat `mika': No such file or directory
3
mv: cannot stat `singh': No such file or directory
4
`nade' -> `4large.png'
5

for file named "mika singh" "lemo" and "nade".

I think the problem lies in for f in 'ls' statement, here for takes the names delimited by space character

Offline

#4 2008-08-12 11:34:47

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Mass renaming...using command line

Using for file in `ls` is bad practice. Instead go for: for file in *

Last edited by Procyon (2008-08-12 11:36:59)

Offline

#5 2008-08-12 12:17:43

Hohoho
Member
Registered: 2007-06-23
Posts: 222

Re: Mass renaming...using command line

`ls` is the problem, now I see it.
Use for file in * instead:
i=1;for f in *;do mv -v "$f" "$i\large.png"; echo $((i=i+1));done

tested and now it seems to work

Last edited by Hohoho (2008-08-12 12:25:17)

Offline

#6 2008-08-12 14:47:52

visio159
Member
Registered: 2008-07-03
Posts: 31

Re: Mass renaming...using command line

Thanks for all your help, appreciate your interest

Finally I got the actual answer with your helps:

i=1;for f in *;do mv -v "$f" "$i""large.png"; echo $((i=i+1));done

if i use \ then it gets appended, so I provided quotes to avoid ambiguity

have a good time 77.png

Last edited by visio159 (2008-08-12 14:51:30)

Offline

#7 2008-08-12 14:51:20

davvil
Member
Registered: 2008-05-06
Posts: 165

Re: Mass renaming...using command line

Just for the record, zsh handles this case gracefully, without the need of quoting. In this case this is not a big issue, but when nesting loops and variables, it can become quite helpful.

Offline

#8 2008-08-12 14:52:33

visio159
Member
Registered: 2008-07-03
Posts: 31

Re: Mass renaming...using command line

^^yes but I love bash shell !
If need arises I will use zsh or csh too tongue

Offline

#9 2008-08-12 15:19:50

davvil
Member
Registered: 2008-05-06
Posts: 165

Re: Mass renaming...using command line

visio159 wrote:

^^yes but I love bash shell !
If need arises I will use zsh or csh too tongue

Not that I want to be a zsh-zealot wink, but with zsh you have bash syntax with improvements, it's not like changing to something different (e.g. csh). If you copy the command posted in this topic, it works just as it is. I bet if you sat at a computer with zsh instead of bash, you wouldn't notice at first, until you began to work and things just "work better".

PS: Ok, I know there are some differences (e.g. substring referencing), but the frequent commands are the same.

Offline

#10 2008-08-12 15:57:19

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Mass renaming...using command line

You could rewrite "$i""large.png" as "${i}large.png".

Offline

#11 2008-08-13 03:35:40

visio159
Member
Registered: 2008-07-03
Posts: 31

Re: Mass renaming...using command line

thanks for your feedbacks, i guess i will have to learn shell scripting.
I have bad memory always keep forgetting syntax 58.png

Offline

#12 2008-08-13 04:44:53

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: Mass renaming...using command line

another option is to use

set IFS=$'\n'

this will tell bash to use newlines as the field separator and not spaces


☃ Snowman ☃

Offline

#13 2008-08-13 18:32:38

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Mass renaming...using command line

Why don't you just use 'rename' ?

$ rename " " "-" *

Offline

#14 2008-08-13 20:54:34

sevenfourk
Member
Registered: 2008-02-21
Posts: 185

Re: Mass renaming...using command line

It seems that

rename " " "-" *

puts the "-" only on the place of the first space in the file name, but if repeat it once again and again .. it puts the "-" to all the spaces.

Anyway i tried this how:

for file in *; do mv "$file" `echo $file | sed -e 's/  */_/g'`; done

No cause is lost if there is but one fool left to fight for it.

Offline

#15 2008-08-13 21:16:11

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Mass renaming...using command line

sevenfourk wrote:

It seems that

rename " " "-" *

puts the "-" only on the place of the first space in the file name, but if repeat it once again and again .. it puts the "-" to all the spaces.

Anyway i tried this how:

for file in *; do mv "$file" `echo $file | sed -e 's/  */_/g'`; done

Ah, I didn't see the two spaces before the *, that is important for it to work correctly smile
But you can just use + instead :

for file in *; do mv "$file" $(echo $file | sed -e 's/ \+/_/g'); done

pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#16 2008-08-14 01:58:34

chilebiker
Member
From: Zurich, Switzerland
Registered: 2006-07-18
Posts: 161

Re: Mass renaming...using command line

Due to this thread I wrote a script last night, maybe someone finds it useful:

#!/bin/bash

# global variables
Tempfile=/tmp/$$.$RANDOM

# argument check
if [ $# -ne 3 ] ; then
  echo "Usage: `basename $0` directory prefix file_extension"
  echo "       for example: `basename $0` . pic jpg"
  exit 65
fi

# generate file time-sorted file list
find $1 -maxdepth 1 -type f -printf "%T+%p\n" | grep $3 | sort | sed -r 's:(.+)/(.+):\2:' > $Tempfile

# rename files {prefix}{counter}.{extension} with counter starting from 0001
i=1
while read filename
do
  if [ "$i" -lt "10" ] ; then
    mv -v "$1$filename" "${1}${2}000${i}.${3}"
  elif [ "$i" -lt "100" ] ; then
    mv -v "$1$filename" "${1}${2}00${i}.${3}"
  elif [ "$i" -lt "1000" ] ; then
    mv -v "$1$filename" "${1}${2}0${i}.${3}"
  fi
  (( i++ ))
done < $Tempfile

rm $Tempfile

Last edited by chilebiker (2008-08-14 02:00:42)


Don't panic!

Offline

#17 2008-08-15 20:15:23

visio159
Member
Registered: 2008-07-03
Posts: 31

Re: Mass renaming...using command line

^^thanks for the script 81.png

shining wrote:
sevenfourk wrote:

It seems that

rename " " "-" *

puts the "-" only on the place of the first space in the file name, but if repeat it once again and again .. it puts the "-" to all the spaces.

Anyway i tried this how:

for file in *; do mv "$file" `echo $file | sed -e 's/  */_/g'`; done

Ah, I didn't see the two spaces before the *, that is important for it to work correctly smile
But you can just use + instead :

for file in *; do mv "$file" $(echo $file | sed -e 's/ \+/_/g'); done

"+" is one or more occurrence ?
"*" is for zero or more occurrence ?

Thanks for feedback 70.png

Btw I liked Shining !

Last edited by visio159 (2008-08-15 20:17:26)

Offline

#18 2008-08-15 20:29:30

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Mass renaming...using command line

visio159 wrote:

"+" is one or more occurrence ?
"*" is for zero or more occurrence ?

Exactly.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

Board footer

Powered by FluxBB