Assistance with 'find -exec cp' shell script
Hello:
I have a script that I use to aide in data recovery when the host OS will not boot. Booting with a linux liveCD I execute a script with commands similar to: Code:
find "$drive" -type f -name "*.pst" -exec cp {} /mnt/appcd/"$name" --parents --no-dereference \; Is this possible? Thanks in advance for any assistance |
My first thought is to take the verbose output from cp, and then trim it for prettiness with something like awk.
Let's see... standard 'cp -v' output is Code:
`srcfile' -> `destfile' Code:
cp -v /mnt/appcd/"$name" --parents --no-dereference | awk '{print($1)}' | sed -e "s/[\`\']//g" Of course, if I am wrong, I'll be very happy to hear about it and learn how to do it! ;) |
That is in the right ball park, but the added code is only showing me the first level of the path rather than the actual file.
Here's my code with the suggested addition of awk and sed: Code:
find "$drive" -type f -name "*.pst" -exec cp {} /mnt/appcd/"$name" -v --parents --no-dereference \; | awk '{print($1)}' | sed -e "s/[\`\']//g" So it seems very close to what I'm looking to accomplish, but need it to show only the file that is being copied - I don't want to see the path as that will be documented in the output from due to the --parents switch. Thanks for your help - again this is getting me closer. |
Ah, for only getting the file I believe there's a very handy tool called basename that will do exactly what you want. :) Unfortunately, it won't take output through a pipe so we can't just stick it on the end as you'd expect. We can either pass basically the entire script into it as an argument (which works) or use some scripting tool to cut off all characters up to and including the last non-escaped forward slash. I'm going to go with the former, since although it doesn't look quite as nice it works fine and doesn't require extra thinking ;)
As for the sed not doing anything, does your cp -v surround the file names in ticks/quotes? Mine did, which is why I sed-ed them away, but if yours doesn't that's fine too. So now we're up to Code:
find "$drive" -type f -name "*.pst" -exec basename $(cp {} /mnt/appcd/"$name" -v --parents --no-dereference | awk '{print($1)}' | sed -e "s/[\`\']//g") Andrzej |
Thanks for the quick reply.
I'll have a look at basename. I wasn't familiar with SED (until now) hence my uninformed statement about SED doing nothing (it actually was, just wasn't noticing). I did however, through some tweaking, feel I've reached a compromise. Code:
find "$drive" -type f -name "*.pst" -exec cp {} /mnt/appcd/"$name" -v --parents --no-dereference \; | awk '{print $3}' | sed -e "s/['\`\]//g" Thanks again for your help! |
I don't know if you missed it - but there was a subtle change in the code that means basename is being called. By using the shell's "execute expression" brackets around basically the whole of the exec command, you can tell the shell to evaluate that part first then pass it in to basename. Note the part immediately after the -exec: basename $(cp... and then the extra bracket to close it all off at the end.
It's very easy to overlook when you have to scroll the little window too. |
OK, I knew there was some way of controlling the display of characters to a console, and the answer is via tput.
There are (by the looks of it) many fancy things you can do with tput - but the following four should be enough for your application: Code:
tput lines # Returns the number of lines on the current console At this point I'd like to say that while I think these commands will work fine, I've only just come across the command and haven't looked into the weird and wacky errors that may arise from strange configurations and unexpected use. I'd definitely recommend you have a look around for tput HOWTOs and learn its abilities and limitations for yourself. It's pretty tasty, though. Edit: oh, and while man tput isn't that information, man 5 terminfo gives you a long list of capabilities that you can get/set/otherwise exploit with tput. Some of these are more device-dependent than others, YMMV. :) HTH, Andrzej |
All times are GMT -5. The time now is 07:42 PM. |