LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-24-2009, 08:07 PM   #1
crions
LQ Newbie
 
Registered: May 2005
Location: Home
Distribution: slackware/ubuntu
Posts: 17

Rep: Reputation: 1
Question How to copy and create path (directory) if necessary...


Dear Friends,

With this tree in mind
:

/home/
--/myfolder/
-------hey.doc
-------fine.doc
-------not.xls
-------nope.ppt
--/theirfolder/
------this.doc
------that.xls

I want do copy only the documents to the folder /backup/.
But I don't want to create the folders!


/backup/
--/myfolder/ - automatically created
-------hey.doc
-------fine.doc
--/theirfolder/ - automatically created
------this.doc

I was thinking about the find -iname "*.doc" -exec something.

But I must confess I can't figure it out...
Also, I do not want to use RSYNC command.
 
Old 03-25-2009, 12:14 AM   #2
jeff_k
Member
 
Registered: Jan 2008
Location: San Diego, CA USA
Distribution: Debian / Ubuntu
Posts: 51

Rep: Reputation: 17
I haven't tried it, but I think that
cp -R /home /backup
should work.
Try it and see...
 
Old 03-25-2009, 12:53 AM   #3
JaksoDebr
Member
 
Registered: Mar 2009
Distribution: Fedora, Slackware
Posts: 104

Rep: Reputation: 21
cp /home/*/*.doc /backup
will copy ONLY the .doc files at the second directory level within /home, and copying it all into /backup without caring about the directories within backup - if that is what you meant to do.

Linux Archive

Last edited by JaksoDebr; 04-02-2009 at 06:08 AM.
 
Old 03-28-2009, 03:19 PM   #4
crions
LQ Newbie
 
Registered: May 2005
Location: Home
Distribution: slackware/ubuntu
Posts: 17

Original Poster
Rep: Reputation: 1
Thumbs down

None of the above worked...

I just want to copy only the doc files keeping the directory structure.


/home/
--/david/
-------hey.doc
-------fine.doc
-------not.xls
-------nope.ppt
--/mary/
------fine.doc
------that.xls

Both users, Mary and David, have the file "fine.doc", Thats why I need to maintain the directory structure.

I need the result bellow:
Directory /backup before the "magic command".


/backup/

<insert magic command here>

Directory /backup after the "magic command"


/home/
--/david/
-------hey.doc
-------fine.doc
--/mary/
------fine.doc

Remenber: NO RSYNC...
 
Old 03-28-2009, 04:36 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Well, you could use sed or awk to extract tokens from the string which contains the file name, but bash can also use builtins:

Code:
#!/bin/bash

function foo () {
  FILENAME="$1"
  DIR=${FILENAME%\/*}
  if [ ! -d "/path/to/backup/dir/$DIR" ]; then
    mkdir -p "/path/to/backup/dir/$DIR"
  fi
  cp "$FILENAME" "/path/to/backup/dir/$DIR"
}

# Now we just use find using -exec foo

find "$HOME" -name \*.doc -exec foo '{}' \;
Completely untested, but I hope you get the idea.
 
Old 04-12-2009, 08:54 AM   #6
crions
LQ Newbie
 
Registered: May 2005
Location: Home
Distribution: slackware/ubuntu
Posts: 17

Original Poster
Rep: Reputation: 1
Solved

[SOLVED]

Dear folks,

After a few time with the man pages I got one nice solution for the problem above.
Within the home directory type.

# find -iname "*.doc" -exec copy -p --parent '{}' /backup/ ';'

This is the smallest size command I've manage to do what I need. Unfortunately the command does not create the directories without the doc files.
If some one has any more elegant solution. Post it here.

Thanks to all for their contributions.

Last edited by crions; 04-17-2009 at 03:35 PM.
 
  


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
Find a file path and directory path ak.lokesh Linux - Newbie 3 02-19-2009 01:37 PM
How to copy files after search in array from this directory to another directory! phanvinhgiap Programming 4 01-04-2009 07:48 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 12:51 PM
How to rsynce the actual directory path instead of just the directory at the end of Niceman2005 Linux - General 5 04-08-2005 08:58 PM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 07:57 AM


All times are GMT -5. The time now is 06:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration