LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-11-2006, 07:25 PM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
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/"
TARGET_DIR="/home/somewhere/etc/"
cd $SOURCE_DIR
while $SOURCE_DIR; do
  cp -p -R * $TARGET_DIR/$*.orig
done
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.

Last edited by noir911; 03-11-2006 at 07:26 PM.
 
Old 03-11-2006, 07:57 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

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

Last edited by Robhogg; 03-11-2006 at 08:10 PM.
 
Old 03-11-2006, 10:51 PM   #3
Amadaeus09
LQ Newbie
 
Registered: Jun 2005
Distribution: Slackware
Posts: 4

Rep: Reputation: 0
cp -Rp --suffix=.orig /etc/* /home/somewhere/etc/

Last edited by Amadaeus09; 03-11-2006 at 10:52 PM.
 
Old 03-12-2006, 01:00 AM   #4
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Quote:
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 -

http://www.openbsd.org/cgi-bin/man.c...6&format=ascii
 
Old 03-12-2006, 01:50 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
This should work:
Code:
#!/bin/sh
cat > /tmp/mvOrig <<%
for i
do
  echo mv "\$i" "\$i.orig"
done
%
chmod +x /tmp/mvOrig

SOURCE_DIR="/etc/"
TARGET_DIR="/home/somewhere/etc/"
cp -R $SOURCE_DIR $TARGET_DIR
find $TARGET_DIR -type f -exec /tmp/mvOrig {} +
rm /tmp/mvOrig
 
Old 03-12-2006, 02:41 AM   #6
kevkim55
Member
 
Registered: Dec 2005
Location: Edmonton
Distribution: BLFS, Gentoo
Posts: 353

Rep: Reputation: 32
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
 
Old 03-12-2006, 06:37 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Quote:
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.
 
Old 03-12-2006, 07:40 AM   #8
kevkim55
Member
 
Registered: Dec 2005
Location: Edmonton
Distribution: BLFS, Gentoo
Posts: 353

Rep: Reputation: 32
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.
 
Old 03-12-2006, 08:18 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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.
 
Old 03-12-2006, 09:14 AM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
This one seems to work...
Code:
cp -R /home/old_folder/\. /home/new_folder
find /home/new_folder -type f -exec mv {} {}.orig \;
 
Old 03-12-2006, 10:17 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
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.
 
Old 03-14-2006, 02:27 AM   #12
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Thanks for your replies guys. I settled down for Homey's small and neat hack

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting problem ecsjohn Programming 7 02-03-2006 07:52 PM
Simple Shell Scripting Question hellomynameisphil Programming 2 08-27-2005 04:41 AM
shell scripting: variable problem XST1 Programming 7 01-30-2005 02:45 PM
Shell Scripting Problem linuxboy69 Linux - Software 2 03-31-2004 08:36 PM
problem in shell scripting visu Linux - Newbie 4 01-06-2004 05:16 PM


All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration