UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 12th, 2006, 07:58 PM
gopherit gopherit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Australia
Posts: 4 gopherit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 57 m 2 sec
Reputation Power: 0
Post Please help this Unix Newbie

I have been given the task of copying directories to a new location for later removal at work as the administrator walked off - the directory names are given to me in a simple one coloumn file to move from one location to another. How can I write a script which will read each line of the file and then copy the directories with that name to the new location. At the moment I have to copy the directories individually which takes a long time as there may be a hundred to do. I would really appreciate any help that can be given.

Reply With Quote
  #2  
Old August 12th, 2006, 11:10 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 815 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 36 m 20 sec
Reputation Power: 243
Let's look at this logically and use pseudo-code:

While you have more lines in the file
do
read the file to get a directory name
copy directory to new location
check complete
end

So, we have a few steps which we can look at one at a time and hence not have such a big task sat before us - which is good!

Now, there are a few ways of doing this (or indeed anything) in Unix, but let's go for simple ...

Code:
for dir in `cat /your/list/of_dirnames`
do
done


That would be my initial choice of loop structure - hopefully no directory name inthe file contains spaces? If they do we will need to deal with that.

Now we have the loop, we can populate it:

Code:
cp -pr $dir /target/dir

That, in the above look will copy the 'next' directory from the list (along with contents - the 'r' flag, and preserving permissions and datestaps - the 'p' flag).

So, we have the loop, we have the copy, now ... the big question. Did it work?
Any command will return an exit code - 0 it ok, something else if not. Unix puts that code into a variable for you - $?

So ... tossing it all together:
Code:
for dir in `cat /your/list/of_dirnames`
do
  cp -pr $dir /target/dir
  rc=$?
  if [ $rc -ne 0 ]
  then
    echo "Copy fail for $dir - rc=$rc"
    exit
  fi
done

There are a lot of things to improve with that, tart it up and so on - like checking for space beforehand, etc. - but that will get you up and running, I hope.
__________________
According to Sod's Law, buttered toast lands butter side down, when dropped.
Per nature, cats always land on their feet.
So, what happens when you strap buttered toast to the back of a cat and throw it out a window?
.

Reply With Quote
  #3  
Old August 12th, 2006, 11:42 PM
gopherit gopherit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Australia
Posts: 4 gopherit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 57 m 2 sec
Reputation Power: 0
Thankyou

Thank you so very much for that....I can now see why people speak so highly of communities like this. Hope you have a great day ;-)

Reply With Quote
  #4  
Old August 13th, 2006, 04:05 AM
gopherit gopherit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Australia
Posts: 4 gopherit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 57 m 2 sec
Reputation Power: 0
Sorry did not work

i keep getting the error "cp: cant find cat" anymore suggestions or am I doing something wrong? Thanks in advance.

this is my script

#!/bin/sh
for dir in `cat list`
do
cp -pr $dir /backup/
rc=$?
if [ $rc -ne 0 ]
then
echo "Copy fail for $dir - rc=$rc"
exit
fi
done

Reply With Quote
  #5  
Old August 13th, 2006, 06:22 AM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 815 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 36 m 20 sec
Reputation Power: 243
I would suspect that the "quotes" in the for statement are not backticks, but apostrophes instead.
I am never sure in which shell it is valid but try:
Code:
for dir in $(cat list)

instead. If that declines to wotk give the backticks another go ... should be the character top-left of keyboard (keyboard dependant, of course!).

Reply With Quote
  #6  
Old August 13th, 2006, 02:44 PM
gopherit gopherit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: Australia
Posts: 4 gopherit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 57 m 2 sec
Reputation Power: 0
Thank you so much again.

Reply With Quote
  #7  
Old August 13th, 2006, 06:01 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 815 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 36 m 20 sec
Reputation Power: 243
Small point to consider - by using $(cat list) or `cat list` you will need to ensure that you are in the directory where the file resides before firing off the command. It would be better to specify the full path (and, of course, to add some code beforehand to check the file exists,etc., etc...)

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Please help this Unix Newbie


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT