Copying files with increment in destination


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying files with increment in destination
# 1  
Old 03-18-2010
Copying files with increment in destination

Hi..
i have a source and destination directories & want to copy files from source to destination those files are unique to source directory . And those files are common to both the directory copy source to destination with a numeric increment as extension.
If in the destination there are more than 4 source directory of same files then that file won't copy to destination further.
i've done it with time stamp , Need your help for numeric increment and checking number of file condition before copy.
Code:
$ls  aa/
a.gif
a.txt
k.xml
$ls bb/
a.gif                          a.txt                        k.xml
a.gif_2010003181244    a.txt_2010003181244  k.xml_2010003181244

Code:
#!/bin/sh  
s_dir=~/acct/a/aa
d_dir=~/acct/a/bb
ls -1 $s_dir|sort>temp_1
ls -1 $d_dir|sort>temp_2

if [  `comm -23 temp_1 temp_2|wc -l` -gt 0 ];then
for file in `comm -23 temp_1 temp_2`
do 
cp $s_dir/$file $d_dir/$file
done
fi

ls -1 $d_dir|sed 's/\(.*\)\_.*/\1/'|sort|uniq -c | awk '{print $1,$2 }'>temp_3

for val in `awk '{print $1}' temp_3`
do

if [ $val -lt 4 ];then
for file in `awk '{print $2}' temp_3` 
do 
cp $s_dir/$file $d_dir/$file`date +"_%Y%m%d%H%M"`
done 
fi
done
rm temp_1 temp_2

But i need the destination directory should be as
Code:
a.gif       a.txt      k.xml
a.gif_1    a.txt_1   k.xml_1

and for next running of script it should be
Code:
a.gif       a.txt      k.xml
 a.gif_1    a.txt_1   k.xml_1
a.gif_2    a.txt_2   k.xml_2

Thank & Regards
posix

Last edited by zaxxon; 03-18-2010 at 04:51 AM.. Reason: added more code tags
# 2  
Old 03-18-2010
You try the following script.

Code:
echo "Enter the First(Source) Directory:"
read sdir
echo "Enter the Second(Destination) Directory:"
read ddir

if [ -d $sdir ]
then
ls $sdir > stemp
else
echo "Given Source Directory $sdir isn't available"
exit 1
fi

if [ -d $ddir ]
then
ls $ddir > dtemp
else
echo "Given Destination Directory $ddir isn't available"
exit 1
fi

comm -12 stemp dtemp > files
no=`comm -13 stemp dtemp | sort |sed -nr '$s/.*_(.*)/\1/p'`
if [[ $no -eq 4 ]];then
echo "Limit 4 reached.Can't copy the files from source $sdir to destination $ddir"
rm -rf stemp dtemp files
exit 0
fiecho "Enter the First(Source) Directory:"
read sdir
echo "Enter the Second(Destination) Directory:"
read ddir

if [ -d $sdir ]
then
ls $sdir > stemp
else
echo "Given Source Directory $sdir isn't available"
exit 1
fi

if [ -d $ddir ]
then
ls $ddir > dtemp
else
echo "Given Destination Directory $ddir isn't available"
exit 1
fi

comm -12 stemp dtemp > files
no=`comm -13 stemp dtemp | sort |sed -nr '$s/.*_(.*)/\1/p'`
if [[ $no -eq 4 ]];then
echo "Limit 4 reached.Can't copy the files from source $sdir to destination $ddir"
exit 0
fi
let no+=1
while read file
do
target=$file`echo _$no`
cp $sdir/$file $ddir/$target
done < files

rm -rf stemp dtemp files

# 3  
Old 03-18-2010
Wrench

It is not working for me , It is not sure that every file in the source is in the same count numbers it may differ from files to files.
Code:
a.gif       a.txt      k.xml
 a.gif_1    a.txt_1   k.xml_1
a.gif_2     a.txt_2
a.gif_3

It's extension changing for each run of script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preserve destination timestamp while copying

Hi Guys, I am trying to copy a file from source to destination, I need to preseve timestamp of destination cd desitnation ll file.txt Aug 17 17:22 file.txt cp -p source/file.txt destination/ I need to preseve same timestamp after copying cd desitnation ll file.txt Aug 17... (9 Replies)
Discussion started by: rohit_shinez
9 Replies

2. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

3. UNIX for Dummies Questions & Answers

Rsync not deleting destination files/folders

Hi All, I want delete all files from destination folder. I am trying below commands rsync -avFF --delete-after /home/vizion/source/ /home/vizion/destination/ I need to be delete all file/fodders from destination Any body have idea ? Please correct me. (3 Replies)
Discussion started by: Chenchireddy
3 Replies

4. Shell Programming and Scripting

Rsync not deleting files on destination

I've got a rsync script that backups file from disk1 to disk3. This works great however if there are extra files on disk3 they do not get deleted by the sync. I'm syncing all folders from F-J rsync --progress -v --delete --delete-excluded -av --recursive /home/disk1/Files/*... (0 Replies)
Discussion started by: gmccarthy
0 Replies

5. Linux

rpmbuild, how to specify a different source and destination path for files

I'd like to specify a different build and deployment path for files, by default the same path is used for both build and install, I wasn't able to find a way to make these different. With Solaris pkgadd, one can specify different paths in prototype, so I would assume something like that is possible... (0 Replies)
Discussion started by: tiburblium
0 Replies

6. UNIX for Dummies Questions & Answers

copy files grabbing destination folder from file name

Hi all... Below is what I am trying to do: 1. Having the following folder with the files... /source_folder/dodiddone.tar.gz /source_folder/gowentgone.tar.gz /source_folder/gowentgone.log 2. I need to copy and chown files with extension .tar.gz to another folder copy... (1 Reply)
Discussion started by: pedroz
1 Replies

7. UNIX for Dummies Questions & Answers

Moving Multiple files to destination files

I am running a code like this foreach list ($tmp) mv *_${list}.txt ${chart}_${list}.txt #mv: when moving multiple files, last argument must be a directory mv *_${list}.doc ${chart}_${list}.doc #mv: when moving multiple files, last argument must be a... (3 Replies)
Discussion started by: animesharma
3 Replies

8. Shell Programming and Scripting

Move all files from source to destination directory based on the filename

Move all files starting with a specific name to different directory. This shell script program should have three parameters File Name Source Directory Destination Directory User should be able to enter ‘AB_CD*' in file name parameter. In this case all the files starting with AB_CD will... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

9. Shell Programming and Scripting

Moving extremely large number of files to destination

Hello Techies, m here with a small issue. Have read out all the relavent threads on unix.com but it was not so good sol. for me. It's a simple & known issue. I want to move lots of files to dest folder. Actually I want to pick up 1 year older files, but that is even taking lots of... (16 Replies)
Discussion started by: kedar.mehta
16 Replies

10. UNIX for Advanced & Expert Users

rsync: taking advantage of files in different directory other than destination

Dear Folks, I have to backup pgsql database dump everynight on a routine. The database dump actually contains sql(text) statements. The actual size of the database dump is aroung 800 MB. Between two days backup, only few lines of statements are modified/added/deleted. I dont want to do... (1 Reply)
Discussion started by: rssrik
1 Replies
Login or Register to Ask a Question