Operating System - HP-UX
1822231 Members
3561 Online
109642 Solutions
New Discussion юеВ

Re: How to copy and rename files in a one shot command

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

How to copy and rename files in a one shot command

Hi

I've 100 files with the txt extension.
I'd like to copy all this files in a ./bkp/ directory with another extension.

I could write a script to do this but is exists an Unix commnand able to do this:

of course cp *.txt ./bkp/*.txt.001 doesn't work.

Any idea ?

Regards
Den

10 REPLIES 10
Bill Hassell
Honored Contributor
Solution

Re: How to copy and rename files in a one shot command

No, there is no way to do this without a loop. The reason is that cp has specific format requirements. The way to how confused cp would be with your command is to display what the shell is seeing after processing the filename expansion:

echo "cp *.txt ./bkp/*.txt.001"

The shell does not carry anything from the front of the list to the destination. The shell would be trying to run a command like this:

cp 1.txt 2.txt 3.txt 4.txt ./bkp/*.txt.001

where the "*" cannot be expanded so it just becomes the new name of the file: *.txt.001

But, there is no need to write a script at all -- do everything from the command line:

for FILE in *.txt
do
cp $FILE ./bkp/${FILE}.001
done

Type everything at the command line. What you will see is:

# for FILE in *.txt
> do
> cp $FILE ./bkp/${FILE}.001
> done
#

where > is the PS2 prompt which indicates that the shell wants more to continue. When the last line is typed, the entire command line is run.


Bill Hassell, sysadmin
Yogeeraj_1
Honored Contributor

Re: How to copy and rename files in a one shot command

hi Den,

you could also TAR them to bkp directory as follows:

tar -cvf ./bkp/ALLFILES-$(date +%H%M-%y%m%d).TAR ./*.txt

This will allow you to tar all files into one file and which would look as follows:

ALLFILES-0910-080506.TAR

i.e. Backup of all file as at 09:10 today 06/05/2008

hope this helps too!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Safarali
Valued Contributor

Re: How to copy and rename files in a one shot command

Hi Den
do the following

save all file name in a file example filelist

ls *.txt > filelist

then do the following
for i in `cat filelist`
do
cp $i /backup/$i.txt1 ( or whatever you want)
done

Regards
Safar
Rasheed Tamton
Honored Contributor

Re: How to copy and rename files in a one shot command

Hi Den,

Little bit fine tuned from the above posts:

for i in `ls -1 *txt`
do
cp $i ./bkp/$i.001
done

Regards,
Rasheed Tamton.
Dennis Handly
Acclaimed Contributor

Re: How to copy and rename files in a one shot command

>I could write a script to do this

This is what you should do until you feel comfortable with one liners.

Bill has the right solution but you may want to use something like the following to test it:
for FILE in *.txt; do
echo cp $FILE ./bkp/${FILE}.001
done

Remove the echo if you have it as you want.

>Rasheed: Little bit fine tuned from the above posts:
for i in `ls -1 *txt`

While this is much better than Safar's, it can't beat Bill's. And there is no reason to use ls(1).
OFC_EDM
Respected Contributor

Re: How to copy and rename files in a one shot command

I haven't used either of these but I've heard people say good things about a utility called mmv.

But apparently the perl "rename" is even better.

As the quote from http://www.phys.unsw.edu.au/~mcba/newunix/node6.html mentions.

Hope this is useful.

A more powerful alternative to mmv is rename, a perl script that uses regular expressions to provide extraordinary flexibility in changing filenames. For example, to convert filenames from uppercase to lowercase, you could do

rename y/A-Z/a-z/ *
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: How to copy and rename files in a one shot command

And then there's xargs

Found this old code I had. Believe it renames all the dat files found in a dir to dat.1 then dat.2 etc.

Don't have access to a system to test right now...so TEST first :)

Think its the last example below you need.

> $ ls
> a.dat b.dat c.dat
>
> To rename *.dat as *.dat.1
> $ ls *.dat | xargs -i mv {} {}.1
>
> But how to rename *.dat.1 as *.dat.2 using xargs?
>


/bin/ls -1 ./*.1 | xargs -l -i sh -c 'mv "$1" "`basename \"$1\" .1`.
2"' '{}' '{}' \;
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: How to copy and rename files in a one shot command

You can download the MMV utility for HP from the Porting and Archive center for HP-UX. Looks like an old version though.

http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/mmv-1.01b/

Some other sites with mmv (Don't know if good for HP-UX though)

http://pkgsrc.se/misc/mmv
http://openports.se/misc/mmv

The Devil is in the detail.
Steven Schweda
Honored Contributor

Re: How to copy and rename files in a one shot command

> No, there is no way to do this without a
> loop. [...]

You're counting "find -exec" as a loop? It's
certainly not a "for" loop.

> And then there's xargs

or "find -exec":

find -name '*.txt' -exec mv {} ./bkp/{}.001 \;

(That's untested, but plausible.)

There is seldom only one way to solve a
problem like this.
Leo The Cat
Regular Advisor

Re: How to copy and rename files in a one shot command

First of all, thank you very much guys !
good posts and very good answers.

regards
Den