What is recursive copy linux

How do I copy folder with files to another folder in Unix/Linux? [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 8 years ago .

I am having some issues to copy a folder with files in that folder into another folder. Command cp -r doesn’t copy files in the folder.

3 Answers 3

The option you’re looking for is -R .

  • If destination doesn’t exist, it will be created.
  • -R means copy directories recursively . You can also use -r since it’s case-insensitive.
  • To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 ‘s / @Anton Krug ‘s comment):

You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.

If the directory you’re copying is called dir1 and you want to copy it to your /home/Pictures folder:

Linux is case-sensitive and also needs the / after each directory to know that it isn’t a file.

is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command pwd .

When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:

at a terminal prompt.

Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.

Источник

Thread: What does copying recursively mean?

Thread Tools
Display

What does copying recursively mean?

I use the command «cp -r» quite often. I know that it means to copy files recursively, but what difference doest that make in comparison to, lets say plain simple «cp»?

Re: What does copying recursively mean?

Without the -r function the directories will not be copied. Paste from the man file:

Re: What does copying recursively mean?

Thanks for your answer, but I have already read the manual. I want to know why that «-r» exists and what it means, technically speaking. If you needed it for everything, wouldn’t it just be «cp» instead of «cp -r»?

Re: What does copying recursively mean?

You don’t need the ‘-r’ for everything, you only need it if you want to copy recursively.

Some of the other options work recursively already like the ‘-a’ option, which I use much more commonly than the ‘-r’ option since I typically want my permissions, ownership and symlinks to be preserved.

Читайте также:  Как обновить операционную систему windows 10 pro

Re: What does copying recursively mean?

cp without the recursive flag won’t copy directories.

Re: What does copying recursively mean?

Thanks for all your answers, but I still dont know what ubuntu really does when I run «cp -r». I know recursive functions from programming, but what does it have to do with copying a file?

Concerning «cp -a»: Where does it put the archives or backups? The manual isnt very clear on that.

I’m just trying to get a more in-depth understanding on what happens with certain commands.

Re: What does copying recursively mean?

Lets play that you want to copy a directory called «/directory»

in the directory, you have a lot of pictures but and folders with pictures.

will only copy the pictures in the actual directory, not the ones in deeper folders.

it goes where you want it to go, for instance

Re: What does copying recursively mean?

In simpler terms, it means copy the directory and all its files and subdirectories and all their files and subdirectories of the subdirectories and all their files, and on and on, recursively, to the bottom of the directory tree from the starting point.

Re: What does copying recursively mean?

Now it makes sense, thanks a lot!

But what is the difference between «cp -a» and «cp -r» ?
In the manual it says:

What does that mean, or for what would you need to use «cp -a»?

Re: What does copying recursively mean?

the «archive» implication the -a flag means that, among other things, attributes such as permissions and ownership are preserved, which is desirable when using ‘cp’ to make a backup of files. Normally, when copying, the permissions and owner of the new file are attuned to the user performing the copy. Try using sudo to copy a file out of your $HOME and you’ll see what I mean. Because of this, the -a flag is used to prevent this from occurring.

‘cp -a’, as the man page says, is the same as using ‘cp -dr —preserve=all’. So it’s a recursive copy that wont follow symlinks and won’t alter the permissions and ownership of the source files.

Источник

Command line recursive/deep directory copy in Linux?

What is a good, general way to make a recursive/deep directory copy in Linux that works in most cases? I’ve used simple things like cp -R as well as fairly elaborate cpio incantations. Are there any significant strengths or weaknesses that cause you to prefer one over the other? Which one do you use most often?

4 Answers 4

So in answer to your question:

Copy everything recursively from directory /foo to directory /bar while preserving symbolic links and file/directory ‘mode’ ‘ownership’ & ‘timestamps’.

I use a command like «cd $srcdir ; tar -c . | tar -C $destdir -x « most often. But I also use rsync -a $src $dst.

The biggest strength of the tar solution is that it is what I had to use on a system many years ago that didn’t have cpio, rsync or a cp that would copy recursively. Tar is pretty much everywhere. It is stuck on my head because I used it a lot, there probably are more elegant ways. It always seems to get the job done correctly, so I have never really looked to hard to find a replacement.

Читайте также:  Windows 10 второй системой uefi

95 and the system was running a late 80’s version of Unix. I don’t think -R was an option for cp, but I am not certain. I learned that tidbit reading the usenet.

Take a look a rsync . I like it because you copy less data when keeping two directories up to date . it can also work remotly. In its simplest form rsync -a /src /dest

Источник

How can I recursively copy files by file extension, preserving directory structure?

At the Linux command line, I’d like to copy a (very large) set of .txt files from one directory (and its subdirectories) to another.

I need the directory structure to stay intact, and I need to ignore files except those ending in .txt .

8 Answers 8

You can use find and cpio to do this

find . -name ‘*.txt’ -exec rsync -R <> path/to/dext \;

Easiest way that worked for me:

one catch is you have to navigate to the «desired» directory before so the «parent path» is correct.

Also make sure that you enabled recursive globs in bash:

how about you first copy it over with

then go to the new folder and run

Edit: ok you want one command which filters (I have not tested this because my system doesn’t have the cpio command!). Here is where I found it: http://www.gnu.org/software/findutils/manual/html_mono/find.html#Copying-A-Subset-of-Files

Please test this first, because I haven’t tried it yet. If someone would verify, that would be great.

I was trying to do the same thing on macOS, but none of the options really worked for me. Until i discovered ditto .

I had to copy many .wav files, and have it skip Video files. So here is what I came up with:

find . -type f -iname «*.wav» -ls -exec ditto <> /destination/folder/<> \;

find . — Runs find in current folder. make sure you cd /source/folder before you start

-type f — Specifies to only look for files

  • -iname «*.wav» — This tells it to look for case insensitive *.wav
  • -ls — This shows you the file that it is working on. Otherwase it shows nothing.
  • -exec ditto <> /destination/folder/<> \; — Does all the work of copying and creating the files with the same directory tree.
  • Источник

    Why is -r recursive necessary when copying a directory in Linux?

    My question is why is it required to use the -r (recursive) flag when making a copy of a directory? I.e., why do this:

    When would I not want this behavior when copying a directory?

    Isn’t a recursive copy of a directory really the “default” behavior; the behavior we want nearly all the time?

    It feels like this is a superfluous flag.

    8 Answers 8

    The way filesystems work, a directory is not actually a folder containing files but rather a directory is a file that contains inode pointers to “child” files connected to it. Meaning, from a file system perspective, a file is a file, but a directory is just a file containing list of connected files.

    So from the command line perspective, doing this:

    Читайте также:  Report issue windows 10

    Would basically mean copy the file named, dir1 to a new file named copyDir1 . And as far as the file system is concerned, dir1 is just a file anyway; the fact it’s a “directory” will only be apparent when the filesystem actually checks dir1 to see what that pile of bits actually is.

    The -r flag tells the file system to recursively roll down the file/directory tree and copy any & all contents that might be a “child” of that file to a new place.

    Now as to why that might seem superfluous or redundant, this really comes down to historic methods of dealing with file systems. As well as creating a system that is safe from all types of user related errors; accidental as well as intentional.

    Meaning, let’s say you have a

    /bin file in your home directory you want to copy but accidentally left out the

    —because you are a human and make mistakes—so its just /bin like this:

    With the “safety net” of /bin being a directory combined with the need for the -r flag you will avoid accidentally copying the whole binary root of the system you are on into your home directory. If that safety net did not exist, a minor—or possibly major—disaster would happen.

    The logic here being that in the days pre-GUI (graphical user interfaces) logical/behavioral conventions need to be set to avoid having user created mishaps that can potentially kill a system. And using the -r flag is now one of them.

    If that seems superfluous, then need look no further than modern GUI system one can place above Linux file systems. A GUI addresses basic user issues like this by allowing one to drag and drop files and directories with ease.

    But in the case of the realm of text-based interfaces, lots of the “user experience” within that world is basically just logical and hueristic-based road bumps that help keep the user in check so potential disaster can be averted.

    Similarly this is why Linux/Unix filesystems don’t have 777 permissions and sudo rights set by default and how real system administrators wince when a user sets 777 permissions or grants everyone sudo rights. These are the basic things one does to ensure the system is stable and as “user proof” as possible; anyone rushing to short-circuit those conventions will most likely cause damage to their system without even knowing it.

    ADDITIONAL INFO: Another answer here on the Unix Stack Exchange site gives a good explanation of why a non-recursive copy of a directory is problematic; emphasis is mine.

    Well, without the -R flag, it’s only possible to copy files, because it’s rather unusual that someone wants to non-recursively copy a directory: A non-recursive copy would just result in a second name for the directory, pointing to directly the same directory structure. Because that’s rarely what people want, and there is actually a separate program that does this (ln), a non-recursive copy of directories is not allowed.

    So if a directory is just really a file with inode items inside of it, making a straight copy of that file would just be the equivalent of how a hard link would work. Which is not what anyone wants.

    Источник

    Оцените статью