You are not logged in.
Pages: 1
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 ?
Offline
i=1;for f in `ls`;do mv -v "$f" "$i\large.png"; echo $((i=i+1));done
How about this?
Offline
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
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
`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
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
Last edited by visio159 (2008-08-12 14:51:30)
Offline
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
^^yes but I love bash shell !
If need arises I will use zsh or csh too
Offline
^^yes but I love bash shell !
If need arises I will use zsh or csh too
Not that I want to be a zsh-zealot , 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
You could rewrite "$i""large.png" as "${i}large.png".
Offline
thanks for your feedbacks, i guess i will have to learn shell scripting.
I have bad memory always keep forgetting syntax
Offline
another option is to use
set IFS=$'\n'
this will tell bash to use newlines as the field separator and not spaces
☃ Snowman ☃
Offline
Why don't you just use 'rename' ?
$ rename " " "-" *
Offline
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
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
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
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
^^thanks for the script
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
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
Btw I liked Shining !
Last edited by visio159 (2008-08-15 20:17:26)
Offline
"+" 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
Pages: 1