Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-26-2011, 09:58 AM   #1
Registered: Oct 2010
Posts: 30

Rep: Reputation: 1
Copying files in command line when the Tree is Very Deep

When using the 'cp' command, have you ever had a problem where the tree was too deep for you to commit to memory? For instance, I want to copy files from "this" directory (.) to some completely different location that has a tree depth of around 15. For now, I write the path down, lol, but I imagine that there's a more efficient way to do this.
Old 11-26-2011, 10:12 AM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
The usual way to copy a tree is to use recursive copy:
cp -pr source destination
The -p says, "don't follow symbolic links" and the -r says, "copy directories recursively." This simply means that cp will "walk the tree," copying as it goes -- the tree will be copied to the destination -- all directories and the files within them.

You don't want to follow symbolic links (because you'll copy everything the link points to and you probably don't want to do that).

Hope this helps some.
Old 11-26-2011, 10:34 AM   #3
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Originally Posted by sungchoiok View Post
For now, I write the path down, lol, but I imagine that there's a more efficient way to do this.
There is:

If you find yourself referencing the same directory frequently you can always assign it to a variable.

Bash provides a number of conveniences which can help navigating complex hierarchies, such as the directory stack and command history.
Old 11-26-2011, 11:56 AM   #4
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Or you can copy and paste using the mouse (assuming a GUI terminal window). Just select to copy and middle-click to paste (no key shortcuts or right-click menus).
1 members found this post helpful.
Old 11-26-2011, 12:12 PM   #5
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Assuming you are not logged into runlevels 1 or 3 (text only modes), open your file manager and drill down through the directory tree to the directory into which you want to copy the files. The path to that directory is displayed in the URL bar of your file manager.

Then open a terminal and cd to the directory which contains the files to be copied.

No need to write down the path to the destination; just copy it from the URL bar of the file manager.

Then use the copy -pr command recommended by tronayne.
Old 11-26-2011, 12:54 PM   #6
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
cd to the source, cd to the destination, cp -Pr "$OLDPWD" . Note the capital P, that's the real copy-symbolic-links-as-is option.

Or tar cC source .| tar xC destination

Or if echo $SHELL says /bin/bash (others have similar-but-different facilities, bash is usual), you can use history expansion. Remember a piece of a command? cp -pr "!?piece?$" . if you want its last argument, or replace $ with :number. Or type history and !number$ will get you the last arg of command "number" in that list.

"man bash" (and then /^HISTORY, in virtually every pager) to find how much you can do with history expansion.

Or ln -s "$(readlink -ne place/i/want/to/remember)" ~/bookmarks/shortname for longterm use, spot="$(that readlink)" for short-term to can use "$spot" in place of "$OLDPWD" above.
Old 11-26-2011, 11:15 PM   #7
Registered: Oct 2010
Posts: 30

Original Poster
Rep: Reputation: 1
thanks for all the replies! you guys are so kind.


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
Command line progress bar for copying Maverick1182 Linux - Newbie 6 03-03-2012 01:55 AM
ssh command line copying from linux to smb share fails due to security florenceit Linux - Networking 3 08-28-2010 10:18 PM
doubt in cp command for copying recursively directories and files mq15 Linux - Newbie 3 04-19-2010 11:47 AM
command tofind files by extension through a directory tree, and copy them all to the budword Linux - Newbie 2 06-27-2009 01:48 PM
How to copy a directory tree without copying the files in it? system Linux - General 2 01-15-2002 10:36 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:24 PM.

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