Rename multiple files, changing prefix, extension and dropping characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename multiple files, changing prefix, extension and dropping characters
# 1  
Old 05-15-2012
Rename multiple files, changing prefix, extension and dropping characters

I'm currently only able to perform some very basic functions, so hope this makes sense...

I have a set of about 27 files that need to be renamed from something like this:

Code:
000012ABCDEFGHIJ.XXX.YYY.ZZZ
000078KLMNO.XXX.YYY.ZZZ
000099PQ.XXX.YYY.ZZZ

to something like this:

Code:
newa012.abc
newa078.abc
newa099.abc

The file names currently all start with '0000', then a 2-digit value, then a name that can be anywhere from 2 to 10 characthers, then the same extension. They need to be changed to a new prefix + 0 + the existing 2 digit value, dropping the next 2 to 10 characters, and changing the extension on each file to the new one. All are case sensitive as indicated.

I was able to use one-liners to get to a file name like 'newa012PQ.abc', and am having the most trouble trying to understand how to drop the 'PQ' etc.

As I said I'm not familiar with much of this and see a lot of references to sed and awk etc. but short of copying code I'm not sure the meaning of, or messing with scripting that I've never touched before I figured I'd try here first.

Any help is appreciated!
Thanks

Last edited by Scrutinizer; 05-15-2012 at 04:08 PM.. Reason: code tags
# 2  
Old 05-15-2012
bash
Code:
$ for file in 0000*; do new=${file#0000}; new=newa0${new:0:2}.abc; echo mv "$file" "$new"; done
mv 000012ABCDEFGHIJ.XXX.YYY.ZZZ newa012.abc
mv 000078KLMNO.XXX.YYY.ZZZ newa078.abc
mv 000099PQ.XXX.YYY.ZZZ newa099.abc

# 3  
Old 05-15-2012
POSIX:
Code:
$ f=000012ABCDEFGHIJ.XXX.YYY.ZZZ
$ f=${f%${f#??????}}
$ echo "newa${f#???}.abc"
newa012.abc

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-17-2012
Quote:
Originally Posted by neutronscott
bash
Code:
$ for file in 0000*; do new=${file#0000}; new=newa0${new:0:2}.abc; echo mv "$file" "$new"; done
mv 000012ABCDEFGHIJ.XXX.YYY.ZZZ newa012.abc
mv 000078KLMNO.XXX.YYY.ZZZ newa078.abc
mv 000099PQ.XXX.YYY.ZZZ newa099.abc


I'm getting this error:

-bash: syntax error near unexpected token `do'
# 5  
Old 05-17-2012
You can try this in working directory :
Code:
ls -1 | sed 's/\(^0\{1,4\}\)\([0-9][0-9]\)\(.*\)/mv & newa0\2.abc/ ' # | sh

After checkin the output pipe it to sh (remove the #) Smilie
This User Gave Thanks to Peasant For This Post:
# 6  
Old 05-17-2012
I'd suggest piping it into 'cat' instead of 'sh' first, to see if what it prints makes any sense.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 05-17-2012
@neutronscott - I'm assuming that I would use this in a script??? If this is something we'd have to run infreuqently I'm guessing that makes more sense just not sure how to do that.
Anyhoo... @Peasant - running the command in the working directory using your code works!!! I did take your advice @Corona688 - and piped to 'cat' instead of 'sh' first then via 'sh' and I see what it does, but can someone explain more what it's doing one vs the other?
Thanks to all again. This has been most helpful and very much appreciated!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl command to download multiple files with a file prefix

I am using the below curl command to download a single file from client server and it is working as expected curl --ftp-ssl -k -u ${USER}:${PASSWD} ftp://${HOST}:${PORT}/path/to/${FILE} --output ${DEST}/${FILE} let say the client has 3 files hellofile.101, hellofile.102, hellofile.103 and I... (3 Replies)
Discussion started by: r@v!7*7@
3 Replies

2. Shell Programming and Scripting

How to rename all files at a time by appending some characters at the begining?

Hi I have a list a filename in a directory starting with particular pattern for example: abc_1234.txt abc_7565.txt abc_7676.txt abc_7765.txt i need to rename all these files by appending bck. or bck_ Expected output: bck.abc_1234.txt bck.abc_7565.txt bck.abc_7676.txt... (1 Reply)
Discussion started by: Little
1 Replies

3. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

4. Red Hat

How to rename files to files with mv extension?

currently in my directories $ ls -lrth total 32K -rw-r--r-- 1 oracle oinstall 864 Feb 25 16:01 cor_bin_gateway_cnt.sql -rw-r--r-- 1 oracle oinstall 782 Feb 25 16:01 mer_bin_gateway_cnt.sql I want to rename files with *.sql to *.mv extension, but when I execute the following $ mv... (1 Reply)
Discussion started by: jediwannabe
1 Replies

5. Shell Programming and Scripting

Rename all files (filename with spaces) to different extension

Hi, I have files with filenames as below. SGM Daily Sales Email-en-us-05312012.xlwa I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders. I have the following script. #!/bin/ksh for oldfile in $(find... (1 Reply)
Discussion started by: mac4rfree
1 Replies

6. Shell Programming and Scripting

ksh command to rename all files with no extension

hi! i want to rename all files with no extension with the extension DAT. with this command ls |grep -v "\\." i can list files but i dont know how i am going to rename them.. so i tried FILE_LIST=ls |grep -v "\\." for TEST_FILE in ${FILE_LIST} do mv $TEST_FILE... (2 Replies)
Discussion started by: kouppoua
2 Replies

7. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

8. Shell Programming and Scripting

Combining multiple files into one with the same name/different extension

I've been trying to find information in regard to creating a script that will generate HTML files. I currently have a series of files that contain code I need to surround with a <textarea> tag for easy viewing. I have about a thousand files that contain code, one file that contains the HTML code up... (10 Replies)
Discussion started by: 12o
10 Replies

9. UNIX for Dummies Questions & Answers

Removing prefix from multiple files and renaming file extension

Hello i have the files in this format pdb1i0t.ent pdb1lv7.ent pdb1pp6.ent pdb1tj2.ent pdb1xg2.ent pdb2b4b.ent pdb2ewe.ent Now i have to remove the prefix pdb from all the files and also i need to change the extension of .ent to .txt The new file should look like this ... (3 Replies)
Discussion started by: empyrean
3 Replies

10. UNIX for Dummies Questions & Answers

Renaming multiple files, to get rid of extension

I have a good script to rename multiple files, but what's the best way I can remove some text from multiple filenames? Say I have a directory with 35 files with a .XLS at the end, how can I rename them to remove the .XLS but keep everything the same, without having to mv manually. Thanks. (6 Replies)
Discussion started by: nj78
6 Replies
Login or Register to Ask a Question