Operating System - HP-UX
1753767 Members
5797 Online
108799 Solutions
New Discussion юеВ

Looking for a modified copy command [ Prefrably a C shell Script]

 
Shreeraj Karulkar
Occasional Contributor

Looking for a modified copy command [ Prefrably a C shell Script]

As a sysadmin I often find myself backingup files before changing them with something like

% cp -p filename filename.backup

I am looking for something which copies a individual file in a directory (probably called "Archive") in the same place. It should create a directory if one does not exist.

example:

% mycp /etc/resolv.conf

should make a copy of the file "/etc/resolv.conf" as

/etc/Archive/resolv.conf.backup

3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: Looking for a modified copy command [ Prefrably a C shell Script]

Hi:

You really (already) have your solution -- write a wrapper script for 'cp'. Do NOT use the C-shell. It is dysfunctional. See:

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

That said:

# cat ./mycp
#!/usr/bin/sh
FILE="$@"
ARCHIVE=/var/tmp
[ -z "${FILE}" ] && { echo "Usage: $0 file"; exit 1; }
[ -f "${FILE}" ] || { echo "File expected" ; exit 1; }
[ -d "${ARCHIVE}" ] || { echo "Missing '${ARCHIVE}' directory"; exit 1; }
SUBDIR=${ARCHIVE}/$(dirname ${FILE})
mkdir -p ${SUBDIR}
cp -p ${FILE} ${SUBDIR}/$(basename ${FILE}).backup
exit

...run as :

# ./mycp full_path_to_file

For example:

# ./mycp /etc/hosts

# ./mycp /etc/rc.config.d/netconf

Modify the ARCHIVE=/var/tmp to your tastes.

Regards!

...JRF...
Shreeraj Karulkar
Occasional Contributor

Re: Looking for a modified copy command [ Prefrably a C shell Script]

James
Thanks for your prompt reply. My objective is to have an "Archive" directory wherever the file is being backed up which I understand maybe a little complicated.
Howevery I am getting a syntax error as below.


surya{root}50% ./mycp /etc/resolv.conf
./mycp: syntax error at line 7: `SUBDIR=${ARCHIVE}/$' unexpected
surya{root}51%
James R. Ferguson
Acclaimed Contributor

Re: Looking for a modified copy command [ Prefrably a C shell Script]

Hi (again):

Hmmm. I copied-and-pasted from my posting here and can't seem to repeat your error.

Regards!

...JRF...