|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
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?. |
|
#3
|
|||
|
|||
|
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 ;-)
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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!). |
|
#6
|
|||
|
|||
|
Thank you so much again.
|
|
#7
|
||||
|
||||
|
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...)
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Please help this Unix Newbie |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|