- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How to copy and rename files in a one shot com...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-05-2008 04:50 PM
тАО05-05-2008 04:50 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-05-2008 06:17 PM
тАО05-05-2008 06:17 PM
Solutionecho "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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-05-2008 09:13 PM
тАО05-05-2008 09:13 PM
Re: How to copy and rename files in a one shot command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-05-2008 10:42 PM
тАО05-05-2008 10:42 PM
Re: How to copy and rename files in a one shot command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-05-2008 10:52 PM
тАО05-05-2008 10:52 PM
Re: How to copy and rename files in a one shot command
Little bit fine tuned from the above posts:
for i in `ls -1 *txt`
do
cp $i ./bkp/$i.001
done
Regards,
Rasheed Tamton.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-06-2008 02:22 AM
тАО05-06-2008 02:22 AM
Re: How to copy and rename files in a one shot command
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-06-2008 03:16 AM
тАО05-06-2008 03:16 AM
Re: How to copy and rename files in a one shot command
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/ *
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-06-2008 03:24 AM
тАО05-06-2008 03:24 AM
Re: How to copy and rename files in a one shot command
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"' '{}' '{}' \;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-06-2008 03:27 AM
тАО05-06-2008 03:27 AM
Re: How to copy and rename files in a one shot command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-06-2008 04:02 AM
тАО05-06-2008 04:02 AM
Re: How to copy and rename files in a one shot command
> 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-06-2008 05:51 AM
тАО05-06-2008 05:51 AM
Re: How to copy and rename files in a one shot command
good posts and very good answers.
regards
Den