[Solved] copy + rename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] copy + rename
# 1  
Old 07-02-2012
[Solved] copy + rename

Hi,

I was thinking that this should be a synch but I could never get it to work.

I want to copy a bunch of files within a directory (3/4 levels into the directory) to a common place. All these files have the same name, so I prefer to rename them with the path.

I tried with

Code:
files=`ls -1 to\ process/*/num1/New\ folder/plot/level.pdf`

for xx in $files
do
        cp $xx /home/newname$files
done

I get this as error-

Code:
cp: target `folder/plot/level.pdf' is not a directory

How do I escape the New\ folder in this case?
Please help me out...
Thanks.
# 2  
Old 07-02-2012
Never, ever, EVER, use a simple ls to generate a list of filenames for a for-loop. Not only is it unnecessary, it is incapable of handling any whitespace in filenames.

If you need to use ls with some options to generate the list, perhaps because you want it sorted by date, for example, then pipe the output of ls into a while-read loop.

I did not test the following, so there could be an error. In any case, it should point you in the right direction.

Code:
for f in to\ process/*/num1/New\ folder/plot/level.pdf; do
    d=${f#*/}
    d=${d%%/*}
    cp "$f" "/home/$d-${f##*/}"
done

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 07-02-2012
Hi Alister,

Got it Smilie and thanks a lot.

/Jamie
# 4  
Old 07-04-2012
HI,

could someone explain how the %% and # and $ work here? I would like to make some changes, but not sure how to...
# 5  
Old 07-06-2012
Hi Alister
I just noted that you are online Smilie could you please please give a small explanation on the working? or point me to where I can read about it. Thanks a lot!!
# 6  
Old 07-06-2012
Your shell's man page should explain it in the 'parameter expansion' section.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Search, Extract and Rename Multiple Files

Hi, I want perl script for the below requirement. We have lot of files like below name in the directory 750464921-RE-file2.csv 750452173-RE-file1.csv 750385426-RE-file3.csv 750373470-RE-file4.csv And also we have another file as "Group.csv" in the same directory as per the below format... (9 Replies)
Discussion started by: armsaran
9 Replies

2. UNIX for Dummies Questions & Answers

[SOLVED] Rename multiple files

Hi. I have a large number of files with names like: t_ 0.20000E-02.dat There is actually a space after the underscore. These files are numbered numerically, i.e. t_ 0.20000E-02.dat, t_ 0.21000E-02.dat, t_ 0.22000E-02.dat and so on. What I would like to do is rename such that the file with... (8 Replies)
Discussion started by: lost.identity
8 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

4. Shell Programming and Scripting

copy and rename file..

hi all, I have one config folder and updates folder.updates folder contains file tmp_2.0_20201208_45.xml and config folder contains file tmp.xml.When the tmp_2.0_20201208_45.xml file is copied in the config folder the name of this file is changed in config folder again as tmp.xml(old... (8 Replies)
Discussion started by: shubhig15
8 Replies

5. Shell Programming and Scripting

Files rename and copy

hello, I am write a Script and i would listing all Files from Path1 out with DSR*.txt and give a new name an copy to the Path2. I have problems with that to rename. Someone can help me? Sorry, for my english. My english is not gut. I hope you understand my. That is my Script. ... (2 Replies)
Discussion started by: efeijoo
2 Replies

6. Shell Programming and Scripting

Copy files from folder and rename them

hello, I need to build a shell script that receives the folder to copy by parameter and copy all files except thumb.db to another folder and rename them like, file.jpg renamed to file_bb1.jpg. can someone help me Thanks (4 Replies)
Discussion started by: zeker
4 Replies

7. Shell Programming and Scripting

Copy 1 file 5000 times and Rename each +1

Hi, Found lots of stuff that is close but no cigar... I have a file ie. a.txt, and I want to copy it to another directory 5000 times and call it: a1.txt a2.txt ... a5000.txt Struggling to put a loop together in this new world of AIX. please include full script for me to understand... (3 Replies)
Discussion started by: terry2009
3 Replies

8. UNIX for Dummies Questions & Answers

Mass Copy/rename

Don't tell me DOS can do something UNIX can't do! I want to copy a number of files from one directory to another, and at the same time change the names. The name changes would be common, e.g., all files starting with the letter 'L' and ending in '30.NEW554', with the copied or new files also... (6 Replies)
Discussion started by: lwilsonFG
6 Replies

9. UNIX for Dummies Questions & Answers

copy and rename list of files

Hi all, I am a newbie in writng unix..I am using ksh shell..Does anyone know how to copy a list o files from directory A to directory B with differnt names? i.e in Dir A, I have RPT101.555.TXT RPT102.666.TXT and I want to copy those files to dir B with new naming convention.. in Dir B,... (7 Replies)
Discussion started by: kinmak
7 Replies
Login or Register to Ask a Question