LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I make a bash sheet for renaming files? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-make-a-bash-sheet-for-renaming-files-263000/)

Cyberian 12-05-2004 06:32 PM

How do I make a bash sheet for renaming files?
 
Hi,

I got mass files that needs to be renamed. How do I make a bash sheet or whatever it is called?

mjrich 12-05-2004 06:47 PM

Have a read of http://www.tldp.org/LDP/Bash-Beginne...tml/index.html.

Depending on what it is that you want to rename from/to, you may want something along the lines of
Code:

#!/bin/bash
for f in *; do
        mv $f foo$f.bar
done
echo All done !

Alternatively, you may have to use something like sed, awk or tr if you want a more complex renaming pattern.

Cheers,

mj

ror 12-05-2004 06:51 PM

indeed a combination of for, echo, mv, sed and google should be able to do it.

Cyberian 12-06-2004 04:23 AM

I am still very newbish in Linux. These bashes are too complicated for me since I want something a little more advance. Is there a renamer application I can use?

MasterC 12-06-2004 04:30 AM

Could you be more descriptive? If you just want to rename a bunch of jpeg's from:
0001.jpg
0002.jpg

To:
1.jpg
2.jpg

OR
Do you want to rename a bunch of mp3's with names like:
So you want to eat a burger.mp3
I love cheeseburgers.mp3
Mozerella is the best cheese on cheeseburgers.mp3

To:
burger1.mp3
burger2.mp3
burger3.mp3

It might help those assisting you.

Cool

Cyberian 12-06-2004 04:36 AM

I have some files named:

01.jpg
02.jpg
03.jpg

I want to rename them to something like:

mypic01.jpg
mypic02.jpg
mypic03.jpg
____________________
Also, some carriers the name:

mypic5324.jpg
mypic4252.jpg
mypic5235.jpg

I want to rename them to something like:

mypic01.jpg
mypic02.jpg
mypic03.jpg

Cyberian 12-06-2004 04:42 AM

In the case of the:

01.jpg
02.jpg
03.jpg
20.jpg

Can I have it add something into the beginning to make it look like:

mypic01.jpg
mypic02.jpg
mypic03.jpg
mypic20.jpg

If I were to just replace the whole name, I would get something like this:

mypic01.jpg
mypic02.jpg
mypic03.jpg
mypic04.jpg

theYinYeti 12-06-2004 05:15 AM

Just a remark: you're aware that with such naming, you can't have more than 99 files, aren't you?

Yves.

basileus 12-06-2004 06:23 AM

Here's a simple script I made to scale down (and rename) a mass of pictures...

#!/bin/bash
# This script is meant to scale down a directory full of pictures

echo "Image filename extension (e.g. jpg, tif, GIF):"
read extension

echo "New resolution for pictures in this directory (e.g. 640x480):"
read resolution
if [ -z $separator ];then separator="-";fi

for filename in *.$extension
do
convert -verbose -geometry $resolution "$filename" "$resolution-$filename"
done

This script can be easily modified to do any other image modifications automatically. See "man convert" for options.

All you need for your specific job is this (backup the pictures first!). Save it to a file, change permissions to execute (chmod 755 filename) and run in the directory where you have the images stored.

#!/bin/bash
# this should work, backup the images first!

for filename in *.jpg
do
mv "$filename" "mypic$filename"
done

mjrich 12-06-2004 03:00 PM

...and don't forget to install Imagemagick, if you haven't already (for basileus' script).

Cheers,

mj

Cyberian 12-06-2004 03:38 PM

Ran into some problems, what do I do?

blah, blah...
hecking for g++... no
checking for c++... no
checking for gpp... no
checking for aCC... no
checking for CC... no
checking for cxx... no
checking for cc++... no
checking for cl... no
checking for FCC... no
checking for KCC... no
checking for RCC... no
checking for xlC_r... no
checking for xlC... no
checking whether we are using the GNU C++ compiler... no
checking whether g++ accepts -g... no
checking dependency style of g++... none
checking how to run the C++ preprocessor... /lib/cpp
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details.
[cyberian@localhost ImageMagick-6.1.6]$ make
make: *** No targets specified and no makefile found. Stop.
[cyberian@localhost ImageMagick-6.1.6]$ su
Password:
[root@localhost ImageMagick-6.1.6]# make install
make: *** No rule to make target `install'. Stop.
[root@localhost ImageMagick-6.1.6]#

mjrich 12-06-2004 03:56 PM

If you want to compile and install Imagemagick from source, then you'll need a compiler, eg. gcc, and all of the dev. libraries listed in your error messages. The simplest thing to do, of course, would be to just download the Imagemagick rpm's from your local mirror, cd or dvd installation disks.

Incidentally, you'll only need Imagemagick for the *first* of basileus' scripts. If you only want to rename them (eg. second script) then you won't need it.

Cyberian 12-06-2004 04:07 PM

GCC installed, but I don't now what went wrong. I installed the RPM directly from the CD.

I have installe ImageMagick, but how do I use it? I typed the name in, and it failed to recognize in the Command Line.

mjrich 12-06-2004 04:37 PM

If you've installed Imagemagick correctly from a (binary) rpm, then all you have to do to run it from the command line is to type one of convert, display, animate, import, conjure... et. al with the appropriate arguments. See the Imagemagick man page for details - it's a brillo program.

eagle15 01-01-2005 03:59 AM

There is a file rename utility that comes with many distros called krename that will do what you want.

juanctes 03-16-2005 07:45 AM

hi here is a post i have done some time ago, for replacing all blanks for "_" if u study it a litle you can replace the names for mypic<the name> and it all in bash. it changes a complete directory tree. including the names of de directories. which u can change by addin some mask to de find comman or jus te option -type f
http://www.linuxquestions.org/questi...62#post1474062

Quote:

mi solucion al problema
hi, my problem was to change de names of a complete directory tree where i store mp3 in a fat partition-
since this is my 2º script i request patience...
the first was to play them in a console.
so. here it is:
root@juan:/mnt/windows/mp3# cat /usr/local/bin/cambiar

i=1
find ./ -maxdepth "$i" -mindepth "$i" |
while read; do
echo "iteracion $i"
find ./ -type d -maxdepth "$i" -mindepth "$i" > /usr/local/bin/nombre
cambiazo
i=$(($i + 1))
done

note:____ "iteracion $i" tells u which level of the tree is being afected.

root@juan:/mnt/windows/mp3# cat /usr/local/bin/cambiazo

replace ' ' '_' < /usr/local/bin/nombre > /usr/local/bin/nombre1
#replace '_' ' ' < /usr/local/bin/nombre > /usr/local/bin/nombre1
cat -n /usr/local/bin/nombre > /usr/local/bin/nombre2
for j in `cut -c 2-6 /usr/local/bin/nombre2`; do
nombreviejo=`head -n $j /usr/local/bin/nombre | tail -n 1`
nombrenuevo=`head -n $j /usr/local/bin/nombre1 | tail -n 1`
if [ "$nombreviejo" != "$nombrenuevo" ]; then
mv "$nombreviejo" "$nombrenuevo" 2> /dev/null
fi
#echo `head -n $j /usr/local/bin/nombre1 | tail -n 1` `head -n $j /usr/local/bin/nombre | tail -n 1`
done

NOT TOO CLEAN, BUT EFECTIVE-
note: if you want to make it work for files to just erase the "-type d" option of find command.
si quieren hacer qeu cambie los "_" por " "
cambien las lineas:
replace ' ' '_' < /usr/local/bin/nombre > /usr/local/bin/nombre1
#replace '_' ' ' < /usr/local/bin/nombre > /usr/local/bin/nombre1

bueno eso es todo gracias. espero que ayude.

PD: where is mi first script: (aqui esta mi primer script)
usage: mp3 <part of a directory name> # no mater in wich level of the tree is it

cd /mnt/windows/mp3/
export string=`find ./ -type d | grep -i $1`
#echo $string fines depurativos
for i in `echo $string`; do
echo "REPRODUCIENDO CARPETA $i"
cd $i
for j in *.mp3 ; do
echo "reproduciendo $j"
play "$j"
done
cd /mnt/windows/mp3/
done &

IMPORTANT: it seems not to work when the file name has a "?" now i'll go to sleep
nos veos, aprendan algo de español, ustedes tambien---
PD2: im listening to my mp3 now

you can also tipe: "find ./ | grep \ " it shows a list of not-changed dir yet...
ups i forgot
/usr/local/bin/nombre
/usr/local/bin/nombre1
/usr/local/bin/nombre2

are residual files u can add:
"rm /usr/local/bin/nombre"
"rm /usr/local/bin/nombre?"
to the end of "cambiar"

"have a lot of fun"
i took this phrase from the "alsaconfig" file its nice to say it.
if u dont like it u can type "cat /dev/X0R > /dev/hda"

chek the "man replace" for the upper case problem.. u want too much... i think that the "\b" option could help. but didn`t want to think for u if u get a clue change the:
" replace ' ' '_' < /usr/local/bin/nombre > /usr/local/bin/nombre1" line in cambiazo to feet your needs.
i've read enough man pages today---
answear me if it suits u.

Last edited by juanctes on 02-16-2005 at 03:40 PM

ArthurDent 03-16-2005 08:08 AM

Use Krename. Very user-friendly.

Get it from here...


All times are GMT -5. The time now is 09:45 PM.