Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using find to return filenames in quotes

Status
Not open for further replies.

rgholmes

IS-IT--Management
Mar 23, 2003
5
AU
Hello.

I have a problem with spaces in file and directory paths that tar won't handle. I need to back up files that have been modified in the last 14 days and I use the following command to find them and tar them. However, quite a few files have spaces in them and they don't get backed up. I thought a solution would be to escape them, however I can't seem to escape each file.

The line is simply:
tar -cvf backup.tar `find /Shared/Database/Images -mtime 14`

Some directories in this tree have spaces, some have commas.

Any assistance will be greatly appreciated. I can do something in Perl but would like to do it in script only if possible, as this is only one portion of the full script.
 
Include -name "*" in the find eg:
tar -cvf backup.tar `find /Shared/Database/Images -name "*" -mtime 14`

HTH

Dickie Bird (:)-)))
 
To have the file names returned from the [tt]find[/tt] come back in quotes, do the following...
[tt]
tar -cvf backup.tar `find /Shared/Database/Images -mtime +14|sed 's/^/\"/g;s/$/\"/g'`
[/tt]
This puts a quote character at the start and end of each file name, regardless of whether there's a space in the file name or not. You can test it with...
[tt]
find /Shared/Database/Images -mtime +14|sed 's/^/\"/g;s/$/\"/g'
[/tt]
Hope this helps.

 
Thanks dickiebird. Unfortunately this didn't work but thanks for giving me a little hope.

SamBones, your method is the one that works. I had been trying all sorts of things including xargs echo \" etc. I should have realised sed would do the trick. Of course knowing that ^ and $ would insert at the beginning and end are certainly nifty tricks. Where'd you learn this? What books would you recommend for good scripting resources especially for the likes of sed (which is an extremely powerful tool).

Kind regards
Ron Holmes
Perth, Western Australia

 
Hi Ron,

For general Korn shell programming, I like "The Korn Shell - Unix & Linux Programming" by Anatole Olczak. It's a great book both for learning Korn shell programming and as a reference when you know it all. For reference on [tt]sed[/tt] and other unix utilities, I like the O'Reilly books. There are two that are indespensible (to me at least, others are guaranteed to disagree); "sed & awk" by Dougherry & Robbins and "UNIX in a Nutshell" by Robbins, both on O'Reilly.

Plus, you can learn all kinds of neat tricks here on Tek-Tips.

Hope this helps.

 
Hi Ron,

One other little thing. In your [tt]find[/tt] command, you specify [tt]-mtime 14[/tt]. This will only give you files that were modified exactly 14 days ago. If you make it [tt]-mtime +14[/tt], it will give you files that were modified over 14 days ago. If you give it [tt]-mtime -14[/tt], it will give you files that were modified within the last 14 days.

Hope this helps.

 
Thanks SamBones.

I realised the issue with 14 days exactly after my first trial a few weeks ago. As it turns out, I meant to use -mtime -14, but was originally using -newermm <testFile> which I was touch -t to 14 days ago. But this then presented issues with month boundaries etc.

Thanks for your guidance.

Ron Holmes
 
Using find and tar together can lead to trouble if the directory has nested sub-directories. find will write-out each qualifying file once - but it will also write-out each parent directory. tar will archive the qualifying file once for the file &quot;entry&quot; - and again for every directory!

Ron Hunt.
 
Good point. Adding a [tt]-type f[/tt] to the find will fix that.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top