Mass Copy/rename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Mass Copy/rename
# 1  
Old 11-01-2005
Data 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 starting with 'L' but ending with just the '30', while preserving whatever characters were in the file names between the 'L' and the '30'. Do I need to do single cp statements for each file? Yipes!
# 2  
Old 11-01-2005
With ksh....
Code:
$ ls -la
total 2
drwx------    2 foo       bar     1024 Nov  2 08:21 .
drwx------    6 foo       bar     1024 Nov  2 08:21 ..
-rw-------    1 foo       bar          0 Nov  2 08:21 Lbar30.NEW554
-rw-------    1 foo       bar          0 Nov  2 08:21 Lblah30.NEW554
-rw-------    1 foo       bar          0 Nov  2 08:21 Lfoo30.NEW554
$ pwd
/some/dir/tmp
$ ls | while read file; do
>   cp "${file}" ../newtmp/`echo "${file%.*}"`
> done
$ ls -l ../newtmp
total 0
-rw-------    1 foo       bar          0 Nov  2 08:24 Lbar30
-rw-------    1 foo       bar          0 Nov  2 08:24 Lblah30
-rw-------    1 foo       bar          0 Nov  2 08:24 Lfoo30

EDIT: Follow up:
If you have pax installed on your system...
Code:
$ ls -l
total 0
-rw-------    1 foo       bar          0 Nov  2 08:21 Lbar30.NEW554
-rw-------    1 foo       bar          0 Nov  2 08:21 Lblah30.NEW554
-rw-------    1 foo       bar          0 Nov  2 08:21 Lfoo30.NEW554
$ mkdir ../dest
$ pax -r -w -s /\.NEW.*$//g *.NEW* ../dest
$ ls -l ../dest
total 0
-rw-------    1 foo       bar          0 Nov  2 08:21 Lbar30
-rw-------    1 foo       bar          0 Nov  2 08:21 Lblah30
-rw-------    1 foo       bar          0 Nov  2 08:21 Lfoo30

Cheers
ZB

Last edited by zazzybob; 11-01-2005 at 05:40 PM..
# 3  
Old 11-01-2005
Bug You da man

ZazzyBob, that little script worked like a charm! Just gorgeous! Saved me beaucoup time!
Take the rest of the day off with my thanks!

LWilson Smilie
# 4  
Old 11-03-2005
thanks

-------------------------------------------------------------------
$ pwd
/some/dir/tmp
$ ls | while read file; do
> cp "${file}" ../newtmp/`echo "${file%.*}"`
> done
$ ls -l ../newtmp
total 0
-rw------- 1 foo bar 0 Nov 2 08:24 Lbar30
-rw------- 1 foo bar 0 Nov 2 08:24 Lblah30
-rw------- 1 foo bar 0 Nov 2 08:24 Lfoo30
-------------------------------------------------------------------

ZB...
Can u explain this statement....>`echo "${file%.*}"`....How the extension of the files are left out.....
Is it possible to create the new file names as substring of the source files...

example: the new file like
bar instead of Lbar30
bla instead of Lblah30
foo instead of Lfoo30
....ie substring(old_file,2,3) (--oracle syntex)

Thanks lot...

Chandroo
# 5  
Old 11-03-2005
I'm not clear on the echo myself. You can always change something like
x="something"
into
x=`echo something`
and a lot of people like the second form. As for your substring question, my solution:

Code:
#! /usr/bin/ksh

typeset -L3 newname
echo "Lbar30
Lblah30
Lfoo30" | while read name ; do
        newname=${name#?}
        echo $name becomes $newname
done
exit 0

# 6  
Old 11-03-2005
Yep, the echo is certainly superfluous. I like to include it with variable substitutions like this to highlight that some action is taking place. Just personal preference I suppose.

Cheers
ZB
# 7  
Old 11-03-2005
Good ... can any one suggesst me any links, where i can get good stuff about "echo" statement

Thanks .....
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[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 files=`ls -1... (5 Replies)
Discussion started by: jamie_123
5 Replies

2. Shell Programming and Scripting

Rename mass files with text from first line

I have a few hundred text files that are currently numbered files. I would like to rename each one with the text from the first line in the file. I would prefer this is perl script rather than a one liner as it wil be after many alterations to the file via an existing script. Any help would be... (1 Reply)
Discussion started by: GWhizz
1 Replies

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

4. Windows & DOS: Issues & Discussions

Windows mass copy files with same name in differnt folders

I have files existing with same names in the folders with date as display below c:\2010-09-10 <==== folder arr1.jpg arr2.jpg arr3.jpg arr4.jpg c:\2010-09-09 <==== folder arr1.jpg arr2.jpg c:\2010-09-08 <==== folder arr2.jpg arr3.jpg arr4.jpg ... (5 Replies)
Discussion started by: jville
5 Replies

5. UNIX for Dummies Questions & Answers

Need help to mass rename files

Hi. I've got 75 mp3s that have the word 'Émission' in their filename. They are all in this format: Émission bla1 bla1.mp3 Émission bla2 bla2.mp3 Émission bla3 bla3.mp3 etc... I would just like to mass replace 'Émission' by 'Emission'; basically replace 'É' with 'E'. The rest of the... (10 Replies)
Discussion started by: Kingzy
10 Replies

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

7. Shell Programming and Scripting

bash script to rename in mass

Basically, I have a huge amount of files (ripped audiobooks) that all have the same garbage in their filenames. I'm wondering how to go about writing a bash script to mass rename them. Example filenames as they stand now: The First CD - 1x01 - Title 1.mp3 The First CD - 1x02 - Title 2.mp3... (4 Replies)
Discussion started by: audiophile
4 Replies

8. AIX

VI questions : mass changes, mass delete and external insert

Is it possible in VI to do a global change but take the search patterns and the replacement patterns from an external file ? I have cases where I can have 100,200 or 300+ global changes to do. All the new records are inside a file and I must VI a work file to change all of them. Also, can... (1 Reply)
Discussion started by: Browser_ice
1 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

10. UNIX for Dummies Questions & Answers

Easy way to mass rename files?

Hi. What is the easiest way to rename a bunch of files? For example taking all files ending in ".php3" and rename them to end in ".php" I could write a script to do this, but there is probably an easier way... Thanks! (17 Replies)
Discussion started by: Thermopylae
17 Replies
Login or Register to Ask a Question