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
# 8  
Old 05-17-2012
cat shows the commands.

sh runs the commands.
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 05-17-2012
I suppose that's OK if your filenames have absolutely no chance of containing whitespace or special characters. It's not good practice to parse ls or evaluate user data.

Probably the command was copied wrong. Did you try post #3?

proof of concept
Code:
$ ls -l
total 0
-rw-r--r-- 1 mute mute 0 May 17 17:34 bad file?echo fail
$ ls -1 | sed 's/\(^0\{1,4\}\)\([0-9][0-9]\)\(.*\)/mv & newa0\2.abc/ ' | sh
sh: bad: not found
fail


Last edited by neutronscott; 05-17-2012 at 02:33 PM..
# 10  
Old 05-17-2012
The filenames are the same every day but I've been reading about concerns using 'ls' so now I'm a little worried. There are other files in this directory, though they are not named anything similar in prefix or extension. When running
Code:
ls -1 | sed 's/\(^0\{1,4\}\)\([0-9][0-9]\)\(.*\)/mv & newa0\2.abc/ ' | sh

it returns
Code:
sh: line 7: ./(other file names here): Permission denied

I reran this
Code:
for file in 0000*; do new=${file#0000}; new=newa0${new:0:2}.abc; echo mv "$file" "$new"; done



and I was copying the $ at the beginning before but when I dropped that it allows me to see the correct results...But to use this, I'm still not understanding how I then get it to run to actually change the file names rather than just showing it?

Last edited by Scrutinizer; 05-17-2012 at 03:47 PM.. Reason: code tags, formatting
# 11  
Old 05-17-2012
Oh, sorry. remove the word "echo".
This User Gave Thanks to neutronscott For This Post:
# 12  
Old 05-17-2012
AWESOME. THAT'S IT!!!!

Appreciate all the help from everyone. It all helps, and will be useful as I try to piece this stuff together going forward.

Sorry for so many questions, appears I have a lot of work to do to get up to speed as a "Dummie"
 
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