Linux.com

Author Message
Joined: Dec 18, 2008
Posts: 1
Other Topics
Posted Dec 18, 2008 at 4:13:21 PM
Subject: script help please
I need to copy/move a file to another directory, adding a sequence to avoid overwriting other files. Example: file name is 'filename' in original directory. Target directory already contains 'filename'. When the script moves the file it should become 'filename001' and so on for more files.
Back to top Profile Email Website
thobbs
Joined Oct 12, 2008
Posts: 238
Location:Texas!

Other Topics
Posted: Dec 19, 2008 7:27:49 AM
Subject: script help please
A bash script? Perl? Something else? If it's your choice, it wouldn't be too tough to whip out a 4-line Perl script.
Back to top Profile Email Website
thobbs
Joined Oct 12, 2008
Posts: 238
Location:Texas!

Other Topics
Posted: Dec 19, 2008 8:05:52 AM
Subject: script help please
Ok, so I lied about the length. You're lucky I like writing Perl scripts ; ). This should do what you're looking for (I hope it's not homework!) [code=xml] #!/usr/bin/perl -w $olddir = shift; $newdir = shift; @files = `ls $olddir`; chomp(@files); foreach $file (@files) { $oldfile = $olddir . $file; $newfile = $newdir . $file; $i = 0; $changingfile = $newfile; while (-e $changingfile) { $i++; $changingfile = $newfile . $i; } `cp $oldfile $changingfile`; } [/code] This expects the argument directories to end with a '/'. I tested it for basic cases, and it works. I wouldn't do anything crucial with it until you've tested it more rigorously though. Example usage for those who want to know: [code=xml] > ./script.pl dir1/ dir2/[/code] This assumes you've placed the script in [i]script.pl[/i] and have [i]chmod u+x[/i]ed it. Have fun! [Modified by: thobbs on December 19, 2008 02:07 AM]
Back to top Profile Email Website
proopnarine

Joined Apr 03, 2008
Posts: 590
Location:San Francisco

Other Topics
Posted: Dec 20, 2008 4:29:52 AM
Subject: script help please
That's very nice thobbs! I didn't think that it could be too short :-)

Take the red pill

Climate Change Blog

Food Weblog

Back to top Profile Email Website
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya