How to change automatically the file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change automatically the file names
# 1  
Old 08-10-2005
How to change automatically the file names

Hi all,

I need to replace automatically all special characters of one filename with some corresponding characters

For example >

ö --> oe

ä --> ae

....

If the special character comes more than one time, then all the coccuerences have to be replaced.

I would like to have a acript, which could do the replacement through the whole file system.

Can somebody help me please

Thanks in advance

MAKY
# 2  
Old 08-10-2005
Try this.

Code:
#! /bin/sh

for file in `find /dir/to/search -name '*.*'`
do
PATH=${file%/*}
FILE_OLD=${file#$PATH}
FILE_NEW={FILE_OLD//<special-char>/<replacement>}
mv ${PATH}/${file} ${PATH}/${FILE_NEW}
done

Not tested tho'.

<special-char> is the char you wish to replace.
<replacement> is the replacement.

Vino

Last edited by vino; 08-10-2005 at 08:42 AM..
# 3  
Old 08-10-2005
Vino, thanks for the quick reply, but I couldn't get any result yet.
I have written your script in the following form, making the test for replacing all the z letters with d letters


#! /bin/sh

for file in `find /tmp/ -name '*.*'`
do
PATH=${file%/*}
FILE_OLD=${file#$PATH}
FILE_NEW={FILE_OLD//<z>/<d>}
mv ${PATH}/${file} ${PATH}/${FILE_NEW}
done


The script doesn't give any error but also doesn't replace any charecters.

Thanks in advance
MAKY
# 4  
Old 08-10-2005
Code:
#! /bin/sh

for file in `find /tmp/ -name '*.*'`
do
PATH=${file%/*}
FILE_OLD=${file#$PATH}
FILE_NEW=${FILE_OLD//z/d}
mv ${PATH}/${file} ${PATH}/${FILE_NEW}
done



vino

Last edited by vino; 08-10-2005 at 10:31 AM.. Reason: Missed the $
# 5  
Old 08-10-2005
Sorry, but I still do not receive any result.
Did you try it ay your system and get the result ?
Thanks again
MAKY
# 6  
Old 08-10-2005
Code:
#! /bin/sh

for file in `find /tmp/ -name '*.*'`
do
PATH=${file%/*}
FILE_OLD=${file#$PATH/}
FILE_NEW=${FILE_OLD//z/d}
mv ${file} ${PATH}/${FILE_NEW}
done

Vino

Last edited by vino; 08-10-2005 at 11:52 AM.. Reason: input for mv was wrong
# 7  
Old 08-10-2005
Vino, your mv command is invalid, must be :
Code:
mv ${file} ${PATH}/${FILE_NEW}

Another way to do the work
Code:
# Transcodification table
Trans_table="ö=oe,ä=ae"

# Build sed script to do file name transcodification
sed_file=/tmp/$$.sed

echo "$Trans_table" | tr '=,' ' \n' |
while read from to
do
   echo "s/$from/$to/g"
done  > $sed_file

# Rename all files in the system
for file in `find /`
do
   path=`dirname $file`
   file_old=`basename $file`
   file_new=`echo $file_old | sed -f $sed_file`
   [ "$file_old" != "$file_new" ] && mv $path/$file_old $path/$file_new
done

rm -f $sed_fil

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to change file names

I have a landing directory on my unix (solaris) server, that receives the following files: MLH4301I AAOT-hhslog.610.20150805.txt MLH4301I AAOT-hhslog.611.20150805.txt MLH4301I AAOT-hhslog.612.20150805.txt MLH4301I AAOT-hhslog.613.20150805.txt and I need to add to this files the number 10000... (6 Replies)
Discussion started by: fretagi
6 Replies

2. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

3. Shell Programming and Scripting

Challenge to change file names

Hi, How can I change following file name in a bash script? From file names: myfile-module-1.0-3.0.el6.x86_64.package To file names: myfile-module1_0-1.0-3.0.el6.x86_64.package ^ ^ ^ ^ ^ ^ ^ ^ Basically, the digit 1.0 is a version number, the digit 3.0 is... (11 Replies)
Discussion started by: hce
11 Replies

4. UNIX for Dummies Questions & Answers

Change sequence names in fasta file

I have fasta files with multiple sequences in each. I need to change the sequence name headers from: >accD:_59176-60699 ATGGAAAAGTGGAGGATTTATTCGTTTCAGAAGGAGTTCGAACGCA >atpA_(reverse_strand):_showing_revcomp_of_10525-12048 ATGGTAACCATTCAAGCCGACGAAATTAGTAATCTTATCCGGGAAC... (2 Replies)
Discussion started by: tyrianthinae
2 Replies

5. Red Hat

How to change name to get rid of name in front of file names?

admin.campaign.sql admin.cardnumber_filter.sql understand that rename is using mv command but how do I rename such that it become the following: campaign.sql cardnumber_filter.sql thanks (2 Replies)
Discussion started by: jediwannabe
2 Replies

6. Shell Programming and Scripting

change multiple file names

Hi is it possible to change multiple files (~10k) names with out disturbing the data in it. ? input Hynda|cgr10(+):100027702-1000312480|.txt Hynda|cgr10(+):100027702-1000312483|.txt Hynda|cgr10(+):100027702-1000312484|.txt Hynda|cgr10(+):100027702-1000312482|.txt output... (4 Replies)
Discussion started by: quincyjones
4 Replies

7. Shell Programming and Scripting

Change multiple file names

Hello, I have some files in a directory like: 01_07_2010_aa.txt 01_07_2010_bb.txt 01_07_2010_cc.txt 01_07_2010_dd.txt 01_07_2010_ee.txt 01_07_2010_ff.txt I want to change their names to : 3nm_aa.txt 3nm_bb.txt 3nm_cc.txt 3nm_dd.txt 3nm_ee.txt 3nm_ff.txt (8 Replies)
Discussion started by: ad23
8 Replies

8. Shell Programming and Scripting

Change root password automatically

I need to change root password automatically in some servers all three months. I want to run this process in one of the servers and reply it to the others. The password must be known by the administrator. (3 Replies)
Discussion started by: Alrica
3 Replies

9. Shell Programming and Scripting

Change file privileges automatically

Hi, Is it possible to write and run a shell script for specific directory( Apache/htdocs) that changes root privilege read/write to chmod 755 when a program uploads a file (word,PPT,XSL,..) to that directory Thanks, Mk (3 Replies)
Discussion started by: mkohan
3 Replies

10. UNIX for Dummies Questions & Answers

Change All File Names in a Directory

Hi, If I have a directory full of say 100 random files, and I would like to organize them, for example: FILE001, FILE002, FILE003, FILE004, etc. How would I do this from Terminal, instead of manually changing each file? I'm using Mac OS X, if that makes a difference. Thank you in advance... (8 Replies)
Discussion started by: andou
8 Replies
Login or Register to Ask a Question