LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 11-25-2008, 03:55 PM   #1
Zebe
LQ Newbie
 
Registered: Jun 2004
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 0
Copy a file into another directory and keep its folder hierarchy?


Hi,

I'm trying to figure out how I can copy a file into another directory and keep its folder hierarchy.

I'd like to move files like: support/apps/programs/license.htm.en

to: /data/newdir/support/apps/programs/license.htm.en

I tried using rsync, but that only moves the file over and does not recreate any folders. I have a number of these path examples I would like to perform this function on. I'm working on a shell script that will read through a text file containing all the paths and then perform this action I can't figure out right at the moment.

Thanks for your help, I appreciate it!
 
Old 11-25-2008, 05:00 PM   #2
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38
Hi

Code:
DEST_DIR=`dirname "$SOURCE_FILE"`

mkdir -p "/data/newdir/$DEST_DIR"

cp "$SOURCE_FILE" "/data/newdir/"

You can use something like the above.

The -p flag on mkdir will create all the directory levels specified

dirname will extract the name of the directory.

You'd probably want to skip the copy if it is not a file you are working on, like using

Code:
if [ ! -d "$SOURCE_FILE" ]; then

  cp "$SOURCE_FILE" "/data/newdir/"
fi
Hope this helps
 
Old 11-25-2008, 05:47 PM   #3
AuroraCA
Member
 
Registered: Jul 2008
Location: Northern CA USA
Distribution: Ubuntu, Slackware, Gentoo, Fedora, Red Hat, Puppy Linux
Posts: 370

Rep: Reputation: 35
You can also use rsync to copy the files in one step using the '-R' or relative flag option. This will copy the file 'license.htm.en' to '/data/newdir' preserving the relative directory structure:

Code:
cd {base directory for /support/apps/programs/license.htm.en}
rsync -avRz ./support/apps/programs/license.htm.en  /data/newdir/
You will end up with the file in '/data/newdir/support/apps/programs/license.htm.en'

Note: the '-v' verbose and '-z' compress flag options are not necessary but will provide feedback and also speed up transfer of large files across networks.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to copy a file to a directory ,where file and dir are sent as args to a function? wrapster Programming 1 06-08-2008 08:00 AM
tar without full directory hierarchy anjanesh Linux - Newbie 2 12-22-2007 01:42 PM
SquirrelMail Folder Hierarchy haydenyoung Linux - Newbie 1 10-16-2006 07:09 AM
copy a file to all subdirectories in a folder raj000 Linux - General 6 03-24-2006 04:55 AM
can copy-paste or create new file/folder, if boot from live cd gjo Linux - Distributions 3 09-17-2004 09:43 AM


All times are GMT -5. The time now is 02:47 PM.

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