Copy and append


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy and append
# 1  
Old 03-02-2012
Copy and append

Ok, Here it goes. I have several file in a directory that I want to copy to another directory and append the date to the end of the file. I do not want to zip the I just need a copy. I would like the script to ask the user before copying each file in the directory to keep from making unneeded copies.This is what I have:
Code:
cd ${PACKAGE_HOME}/km/cfg/
 echo " Do you wish to back-up file.cfg ?"
        echo -n "Enter Y or N: "
        read YN
if [[ $YN == y || $YN == Y ]];
        then
                cp file.cfg ${PACKAGE_HOME}/km/cfg/OLD/file.cfg.`date '+%Y%m%d'`;
        echo " Copy Complete "
fi
 echo " Do you wish to back-up file2 ?"
        echo -n "Enter Y or N: "
        read YN
if [[ $YN == y || $YN == Y ]];
        then
                cp file2 ${PACKAGE_HOME}/km/cfg/OLD/file2.`date '+%Y%m%d'`;
        echo " Copy Complete "
fi
 echo " Do you wish to back-up file3 ?"
        echo -n "Enter Y or N: "
        read YN
if [[ $YN == y || $YN == Y ]];
        then
                cp file ${PACKAGE_HOME}/km/cfg/OLD/file3.`date '+%Y%m%d'`;
        echo " Copy Complete "
fi

I would like to do this with one command instead of listing each file like this:
Code:
cd ${PACKAGE_HOME}/km/cfg/
 echo " Do you wish to back-up files.cfg ?"
        echo -n "Enter Y or N: "
        read YN
if [[ $YN == y || $YN == Y ]];
        then
                cp files.cfg ${PACKAGE_HOME}/km/cfg/OLD/files.cfg.`date '+%Y%m%d'`;
        echo " Copy Complete "
fi

I would also like to be able to use this on other databases with minor changes. Is this possible?

Moderator's Comments:
Mod Comment (in case you missed it....) How to use code tags
# 2  
Old 03-02-2012
You can try using a for-loop:

Code:
cd ${PACKAGE_HOME}/km/cfg/

for files in *
do
    echo " Do you wish to back-up $files ?"
    echo -n "Enter Y or N: "
    read YN
    if [[ $YN == y || $YN == Y ]];
    then
        cp $files ${PACKAGE_HOME}/km/cfg/OLD/${files}.`date '+%Y%m%d'`;
        echo " Copy Complete "
    fi

Hope this helps point you in the right direction.

---------- Post updated at 02:17 PM ---------- Previous update was at 02:16 PM ----------

Sorry, got distracted:

Code:
cd ${PACKAGE_HOME}/km/cfg/

for files in *
do
    echo " Do you wish to back-up $files ?"
    echo -n "Enter Y or N: "
    read YN
    if [[ $YN == y || $YN == Y ]];
    then
        cp $files ${PACKAGE_HOME}/km/cfg/OLD/${files}.`date '+%Y%m%d'`;
        echo " Copy Complete "
    fi
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy(append ) the contents of file1 and file2 to file3

Platform : Oracle linux 6.5 I have two log files with the following contents # ls -l total 8 -rw-r--r--. 1 root root 75 Dec 10 20:55 myLogfile1.log -rw-r--r--. 1 root root 51 Dec 10 20:57 myLogfile2.log # # cat myLogfile1.log hello world jaded zombies acted quaintly but kept driving... (9 Replies)
Discussion started by: kraljic
9 Replies

2. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

3. UNIX for Dummies Questions & Answers

Copy value and append value to a list

Hi, sorry if this has been asked before but I couldnt find it. Essentially I am trying to find a value next to an xml tag say abcd 12345678 the number 1-8 is the number i need to copy to a file of order numbers in my home directory ~/tn/ordernumbers.txt say for example there are 3 or... (3 Replies)
Discussion started by: rg01
3 Replies

4. Shell Programming and Scripting

Copy & Append text

Hi, I have a huge text file that contains contents like below. echo 2VPMUM1CMP01_2011-05-10_18_CPU_Stats_1.txt awk -F, '{sub ("%","",$8);sum+=100-$8;if (100-$8>max){max=100-$8}}END{printf "max=%.1f%\navg=%.1f%\n",max,sum/NR}' echo 2VPMUM1CMP01_2011-05-11_10_CPU_Stats_1.txt awk -F,... (2 Replies)
Discussion started by: SunilB2011
2 Replies

5. Shell Programming and Scripting

Append stderr

Hi everybody. I was used to redirect stderr to a file in this way, calling a generic script:./myScript &> output.logBut now I need something more sophisticated...Inside a bash script I launch an executable in this way:${command} >> "${globalLogFile}"So I redirect the stdout into globalLogFile.... (14 Replies)
Discussion started by: canduc17
14 Replies

6. UNIX for Dummies Questions & Answers

Find, copy, and append into one file

Hi, I am trying to do something relatively easy, but am having some trouble getting it to work. I have multiple files called "distances.log" in numerous subdirectories and sub-subdirectories within a directory. I would like the contents of each of these "distances.log" files to be appended to a... (2 Replies)
Discussion started by: euspilapteryx
2 Replies

7. UNIX for Dummies Questions & Answers

What is in-core copy and disk-copy of i-node table?

I have found a question from the exercises of my study mat. The question is "Why are there a in-core copy and a disk-copy of i-node block and super block?" If any one know the proper answer then please send me..... (1 Reply)
Discussion started by: dearanik
1 Replies

8. Shell Programming and Scripting

append a ' to the $1 $2

Hi all , Iam trying to append a ' to the end of the $1 and $2 in the bellow example : awk '{print "exec upload" ,$1,$2,$3 "\ngo"}' so the output would be something like this : exec upload '444042 ','444042 ','919841037265' i am getting : exec upload 444042 ,444042 ,919841037265 ... (2 Replies)
Discussion started by: ppass
2 Replies
Login or Register to Ask a Question