Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


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

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


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, 04:00 PM   #2
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38


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

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

  cp "$SOURCE_FILE" "/data/newdir/"
Hope this helps
Old 11-25-2008, 04:47 PM   #3
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:

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.


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 07:00 AM
tar without full directory hierarchy anjanesh Linux - Newbie 2 12-22-2007 12:42 PM
SquirrelMail Folder Hierarchy haydenyoung Linux - Newbie 1 10-16-2006 06:09 AM
copy a file to all subdirectories in a folder raj000 Linux - General 6 03-24-2006 03:55 AM
can copy-paste or create new file/folder, if boot from live cd gjo Linux - Distributions 3 09-17-2004 08:43 AM

All times are GMT -5. The time now is 07:57 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration