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 - Code:
SOURCE_DIR="/etc/" 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. |
Sorry, posted something that didn't work. Have to think about it a little more.
|
cp -Rp --suffix=.orig /etc/* /home/somewhere/etc/
|
Quote:
Here's the cp man page for OpenBSD - http://www.openbsd.org/cgi-bin/man.c...6&format=ascii |
This should work:
Code:
#!/bin/sh |
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 |
Quote:
|
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.
|
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. |
This one seems to work...
Code:
cp -R /home/old_folder/\. /home/new_folder |
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. |
Thanks for your replies guys. I settled down for Homey's small and neat hack
Code:
cp -R /home/old_folder/\. /home/new_folder |
All times are GMT -5. The time now is 04:11 AM. |