-   Programming (
-   -   simple shell scripting problem (

noir911 03-11-2006 06:25 PM

simple shell scripting problem
I am copying all the files from /etc/ to another directory. And the copied files should end with a .orig extension. eg. /home/somewhere/etc/rc.local.orig.

cp -p -R /etc/* /home/somewhere/etc/*.orig <-- doesn't seem to be working.

Here's my dummy code -


while $SOURCE_DIR; do
  cp -p -R * $TARGET_DIR/$*.orig

and this doesn't work.

I could first copy the files over and then change the extension. But I'd like to do it in one hit with a one-liner.

Robhogg 03-11-2006 06:57 PM

Sorry, posted something that didn't work. Have to think about it a little more.

Amadaeus09 03-11-2006 09:51 PM

cp -Rp --suffix=.orig /etc/* /home/somewhere/etc/

noir911 03-12-2006 12:00 AM


Originally Posted by Amadaeus09
cp -Rp --suffix=.orig /etc/* /home/somewhere/etc/

Unfortunately there is no "--suffic" switch in OpenBSD. Is there any other alternative way of doing this?

Here's the cp man page for OpenBSD -

jlliagre 03-12-2006 12:50 AM

This should work:

cat > /tmp/mvOrig <<%
for i
  echo mv "\$i" "\$i.orig"
chmod +x /tmp/mvOrig

find $TARGET_DIR -type f -exec /tmp/mvOrig {} +
rm /tmp/mvOrig

kevkim55 03-12-2006 01:41 AM

Run the following from the command prompt and see if this works.
for f in `find /etc` ; do cp $f /home/somewhere/etc/"$f".orig ; done

jlliagre 03-12-2006 05:37 AM


Originally Posted by kevkim55
Run the following from the command prompt and see if this works.
for f in `find /etc` ; do cp $f /home/somewhere/etc/"$f".orig ; done

It chokes on directories (should filtered out) and with filenames containing spaces.

kevkim55 03-12-2006 06:40 AM

Been a long time since I worked with shell scripts. There's an escape sequence which would take care of filenames with spaces in them, I don't remember. May be ${f}.orig would do or may be you should enclose the strings in double quotes like - cp "$f" "/home/somewhere/etc/$f.orig" , I'm not sure.

jlliagre 03-12-2006 07:18 AM

Your suggestions won't work, it's too late as "$f" is already a filename part.
There is probably another solution involving IFS, but it would fail with filenames containing linefeed, a rare occurence though.
I would stay with my script, which is portable and handle all odd filenames.

homey 03-12-2006 08:14 AM

This one seems to work...

cp -R /home/old_folder/\. /home/new_folder
find /home/new_folder -type f -exec mv {} {}.orig \;

jlliagre 03-12-2006 09:17 AM

It doesn't with some find implementations, where {} is interpreted only when alone as a parameter, but it looks like it does with BSD's find

BSD find manual page:

If the string "{}" appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file.

Solaris one doesn't accept that though, the reason why I looked for a more portable solution.

Solaris find manual page:

A command argument {} is replaced by the current path name.

Gnu find manual page:

The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.

noir911 03-14-2006 01:27 AM

Thanks for your replies guys. I settled down for Homey's small and neat hack


cp -R /home/old_folder/\. /home/new_folder
find /home/new_folder -type f -exec mv {} {}.orig \;

All times are GMT -5. The time now is 02:14 AM.