Change multiple file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change multiple file names
# 1  
Old 07-01-2010
Question Change multiple file names

Hello,

I have some files in a directory like:

Code:
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 :

Code:
3nm_aa.txt
3nm_bb.txt
3nm_cc.txt
3nm_dd.txt
3nm_ee.txt
3nm_ff.txt

I know about 'rename' , but I don't want to change the file extension.
Any inputs??
# 2  
Old 07-01-2010
Code:
$ ls -1
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

$ ls * | sed "p; s/.*_/3nm_/g" | xargs -n2 mv

$ ls -1
3nm_aa.txt
3nm_bb.txt
3nm_cc.txt
3nm_dd.txt
3nm_ee.txt
3nm_ff.txt

# 3  
Old 07-01-2010
MySQL

Thank you very much.
# 4  
Old 07-01-2010
Code:
for i in $(ls); do mv $i "3nm${i:10:7}"; done

# 5  
Old 07-01-2010
Quote:
Originally Posted by fubaya
Code:
for i in $(ls); do mv $i "3nm${i:10:7}"; done

Never ever use plain ls to generate a file list. It's truly pointless. It is less efficient and less safe than a simple fileglob. Spread the good word Smilie

A tweaked version of your approach:
Code:
for i in *; do mv "$i" 3nm_"${i##*_}"; done

Regards,
Alister
# 6  
Old 07-02-2010
Question

Thank you all very much!!!

---------- Post updated at 03:31 PM ---------- Previous update was at 03:24 PM ----------

Just a follow-up question...

Is there a way to change my file names to the corresponding directory names?
My file structure is :

Code:
/abc/2010_06_07_aaa/Info.txt
/abc/2010_06_07_bbb/Info.txt
/abc/2010_06_07_ccc/Info.txt
/abc/2010_06_07_ddd/Info.txt
/abc/2010_06_07_eee/Info.txt

The output :
Code:
/abc/2010_06_07_aaa.txt
/abc/2010_06_07_bbb.txt
/abc/2010_06_07_ccc.txt
/abc/2010_06_07_ddd.txt
/abc/2010_06_07_eee.txt

Thanks!
# 7  
Old 07-02-2010
Off the top of my head, without testing:
Code:
for f in /abc/*/Info.txt; do mv "$f" "${f%/*}".txt; done

Regards,
Alister
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

Trying to do multiple dir's and multiple file names etc.

What am I missing? find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing conjunction find: 0652-009 There is a missing... (3 Replies)
Discussion started by: xgringo
3 Replies

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. UNIX for Dummies Questions & Answers

Editing multiple file names in one go

Hi there, I have a folder full of pdf's and I've run a compression on the to reduce the size, the output of the compress places a '-o' in the name of the file. Before 12345.pdf After 12345-o.pdf Now I've got around 50000 files that I need to change back to the previous name, is... (3 Replies)
Discussion started by: KeesH
3 Replies

10. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: MAKY
6 Replies
Login or Register to Ask a Question