Rename multiple file from file listing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename multiple file from file listing
# 1  
Old 02-09-2009
Rename multiple file from file listing

I am new at Linux/UNIX programming. Here is my problem.
I had one big file which I split using the command

csplit -k -s -f april.split. april '/^ISA/' '{10000}'

So now I have multiple files with names

april.split.01
april.split.02
april.split.03

But I need the name of the file like

CHRY.850.01
CHRY.850.02
CHRY.850.03

etc.
Can I use following commands on the prompt or do I have to write a script.

for each in `cat april-850-split`
do
cp $each ${each}.850


Thanks
# 2  
Old 02-09-2009
Yes, you can run this type of stuff from the command line.

You'll get a secondary prompt and it's smart enough to know
you're done when it sees the "done" command.

Preferred, however, is this:

Code:
cat april-850-split |
while read each ; do

cp $each $each.850

done

... because then you'll never get the "arglist too long" error.

BTW -- don't think this renaming will give you exactly what you wanted....

don't you need:

Code:
number=${each##*.}
mv $each CHRY.850.$number

But again, I prefer doing things like this:

Code:
cat april-850-split |
while read each ; do

  number=${each##*.}
  echo mv $each CHRY.850.$number

done |
  tee commands.sh

This'll save my intended "mv" ( rename ) commands in a script file named commands.sh

Then I can visually inspect it --- make sure it's going to do what I want... then run it,
saving all the output to an "err" file:

Code:
ksh -xvf commands.sh 2>&1 | tee err

# 3  
Old 02-09-2009
Thanks for your reply. You are correct. I do need CHRY.850.01 etc. When I ran you code, I am getting

mv april.split.03 CHRY.850.
mv april.split.04 CHRY.850.
mv april.split.05 CHRY.850.

without the number.

Here is my code.

$ cat april-850-list | while read each; do number=${each##*.} echo mv $each CHRY.850.$number done | tee repl.sh;

Can you please tell me what is wrong with my code? I ma running on Linux. I don't think that would make a difference.


Thanks
# 4  
Old 02-09-2009
You need to show the contents of the april-850-list file.

What SHELL on Linux?
I'm on Solaris...

We may have to try something like:

number=` echo $each | awk -F. '{print $NF }'`
# 5  
Old 02-09-2009
Quote:
Originally Posted by yshahiac
I have multiple files with names

april.split.01
april.split.02
april.split.03

But I need the name of the file like

CHRY.850.01
CHRY.850.02
CHRY.850.03

Can I use following commands on the prompt or do I have to write a script.

There is no difference between a script entered on the command line and a script in a file. (Apart from the occasional quoting issue.)

Code:
for file in april.split.*
do
  mv "$file" "CHRY.850.${file##*.}"
done

# 6  
Old 02-09-2009
Thank you. It worked.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename multiple file names?

Hi all, I need to rename more file name in one command or script. The files have this structure: XxY - filename.doc where X and Y are numbers and the x is the letter itself. I need to rename these files with this structure: string.S0XEY.filename.doc the string is a suffix that... (8 Replies)
Discussion started by: idro
8 Replies

2. Shell Programming and Scripting

Multiple File Rename based on pattern - one line

All, I wanted to copy the files From: Daily_XYZ_TEST_1.csv Daily_XYZ_TEST_2.csv Daily_XYZ_TEST_3.csv Daily_XYZ_TEST_4.csv To: Daily_ABC_TEST_1.csv Daily_ABC_TEST_2.csv Daily_ABC_TEST_3.csv Daily_ABC_TEST_4.csv I have tried the rename command but it is not working (5 Replies)
Discussion started by: alfredo123
5 Replies

3. Shell Programming and Scripting

Multiple file rename

I've been googling for days but can't find a solution to this problem. I have a number of sets of files on a server file02.dat . . file12.dat /.../fred(1 to n)/bill(1 to m)/tony/joe/ in any "fred" branch there will be one or more "bill"s some joe/'s may not have a fileset and... (4 Replies)
Discussion started by: Sabreur
4 Replies

4. UNIX for Dummies Questions & Answers

Multiple file rename

hi im new to linux and was just wondering if some 1 could help me i have folders with T.V. series in them and i would like to delete part of the filename e.g. (series name).s01e01.(episode name) (series name).s01e02.(episode name) (series name).s01e03.(episode name) (series... (4 Replies)
Discussion started by: stevemcd1990
4 Replies

5. Shell Programming and Scripting

.sh file To rename existing file and copy new file

Hi All, I am very new to shell scripting . In my current task i want to create .sh file that will rename the existing file with appending _bu in it. And then copy new file . e.g if i have file linuxFirst.java then i want to rename it to linuxFirst_bu.java ..Then want replace with latest... (1 Reply)
Discussion started by: maheshkaranjkar
1 Replies

6. Shell Programming and Scripting

Rename multiple file names in a directory

I hope some one can help me I have multiple files in a directory with out extension like as below mentioned. But i want to change all the file names along .DDMMYYYYHHMISS format. And all files should have same DDMMYYYYHHMISS. Scenario: direcory name = /vol/best/srcfiles files in a... (4 Replies)
Discussion started by: hari001
4 Replies

7. UNIX for Dummies Questions & Answers

For Loop To Rename Multiple Files Finds One Non-existant File

Okay so here's something that's confusing me: I have a script that's designed to remove the words "new_" from the front of any file except two exceptions and it looks something like this... for i in new_* do if ] && ]; then j=`echo "$i"|cut -c5-` mv $i $j fi done ... (5 Replies)
Discussion started by: Korn0474
5 Replies

8. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

9. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question