The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 09-29-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Many approaches, but here is one

The following example script checks to see if the archive (destination) folder exists; if not, it creates it.
Then it copies the files in the directory (that are called file*) to the archive folder (it displays them on the screen as it does it).
Finally, it lists the files that are now in the archive folder.

If you only want to copy certain files, it would be in that middle section where you could restrict the files to copy - for example
for zf in [X-Z].txt
to copy X.txt and Y.txt and Z.txt files.


Code:
> cat mv2arch 
#! /usr/bin/bash

# check on existence of archive
if [ ! -d archive ]
   then
   mkdir archive
fi

# copy the files
for zf in file*
   do
   echo $zf
   cp $zf "./archive/"$zf".bak"
done

# see what is in the archive folder
ls -l ./archive

exit 0
> mv2arch
file1
file2
file3
total 24
-rw-rw---- 1 xxx dp 21 Sep 29 12:33 file1.bak
-rw-rw---- 1 xxx dp 21 Sep 29 12:33 file2.bak
-rw-rw---- 1 xxx dp 21 Sep 29 12:33 file3.bak