Copying files in command line when the Tree is Very Deep
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
The usual way to copy a tree is to use recursive copy:
Code:
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).
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).
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.