What is recursive copy linux

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.

If you think this would be a improvement you can repost this request on developers channel. Otherwise it was probably programmed long time ago.

@blogger It was programmed long ago, but for a reason. Meaning if someone wants to do basic work in a command line environment, their task should only be as easy as avoiding system failure is hard. Meaning there are good reasons some command line user interaction conventions exists. I expand on this concept in my answer.

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:

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.

Читайте также:  Linux mint установить шрифты

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.

Источник

How to Copy a Directory in Linux with the cp Command

Shittu Olumide

Shittu Olumide

How to Copy a Directory in Linux with the cp Command

Copying directories is an essential task in Linux. It allows you to duplicate directories, create backups, or transfer data between different locations.

One of the most commonly used commands for copying files and directories in Linux is cp .

In this tutorial, we will explore how to copy directories effectively using the cp command, along with various options and techniques to customize the copying process.

The Linux cp command

You use the cp command in Linux to copy files and directories from one location to another. It stands for «copy» and is a fundamental command-line utility for file management.

The cp command follows a specific syntax for copying directories. Understanding the basic structure of the command is crucial for successful directory copies.

The cp command syntax

cp [options] source_directory destination_directory 

Here’s an explanation of each component of the syntax:

  • cp : This is the command itself, which stands for «copy.»
  • [options] : This represents optional flags and parameters that can modify the behavior of the cp command. Options are typically preceded by a hyphen (-) or double hyphen (—) and can be used to specify additional functionalities, such as preserving attributes, enabling recursion, or displaying progress.
  • source_directory : This is the directory that you want to copy. It can be specified as a relative or absolute path.
  • destination_directory : This is the directory where you want to copy the source directory. It can also be specified as a relative or absolute path.
Читайте также:  Linux mint boot screen black

Here are some key concepts and features of the cp command:

  1. Copying files: The cp command can be used to copy individual files. We provide the path and name of the source file, followed by the destination directory or filename.
  2. Copying directories: The cp command can also copy entire directories. To copy a directory and its contents, we need to include the -r (or —recursive ) option, which enables recursive copying. This option ensures that all subdirectories and files within the directory are copied.
  3. Preserving file attributes: By default, the cp command copies files without preserving their attributes such as permissions, timestamps, and ownership. However, we can use the -p (or —preserve ) option to preserve file attributes during the copy process.
  4. Handling existing files: When copying files or directories, the cp command handles conflicts when there are existing files or directories with the same names in the destination location. By default, it overwrites the existing files without prompting. We can use the -i (or —interactive ) option to prompt before overwriting existing files.
  5. Copying across file systems: The cp command can handle copying between different file systems. It automatically adjusts the behavior and performs the copy accordingly.

Let’s demonstrate how to do this:

cp -r /Desktop/welcome /Desktop/tutorial 
  • -r flag stands for «recursive» and allows the cp command to copy directories and their contents.
  • /Desktop/welcome is the path of the directory we want to copy.
  • /Desktop/tutorial is the path where we want to copy the directory to.

Conclusion

With the knowledge and understanding of the cp command’s syntax, we can efficiently copy directories and their contents.

Throughout this article, we have explored the step-by-step process of copying directories to different locations, recursively copying directories with their contents. We have also discussed some important features of the cp command.

So go ahead and try it out 🙂

Let’s connect on Twitter and on LinkedIn. You can also subscribe to my YouTube channel.

Источник

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

NAME cp - copy files and directories -a, --archive same as -dpR -d same as --no-dereference --preserve=links -p same as --preserve=mode,ownership,timestamps -R, -r, --recursive copy directories recursively 

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’.

This is certainly a very straight-forward and elegant solution. Have you ever found any drawbacks to this approach? Distributed file systems? Extremely large copies? Have you found other solutions like cpio, tar, or rsync to be more efficient in some cases?

Читайте также:  Linux connect to unix socket

Unless there’s a physical error I’ve never had a local ‘cp’ fail from memory (I’ve only done TB sized transfers) to any place mounted localy (regardless of NFS or Samba mounting). Checkout ‘USAGE’ under the rsync man page for examples you’ll find instructive. Favourite of mine: $ rsync —partial —progress —rsh=ssh —archive —verbose —compress foo/ user@hosname:~/bar (rsync -avzP)

Well I know that busybox, GNU & BSD cp are ok. Other than legacy Unix boxes I’ve never seen what won’t that work on? I don’t really see the point otherwise although I completely understand Zoredache’s sentiment of «It always seems to get the job done correctly, so I have never really looked to hard to find a replacement.»

@gyaresu Please consider splitting your second comment out into another answer so that we can vote on it.

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.

I like «the ubiquity of tar» argument a lot. I am proficient in vi for similar reasons even though I prefer other editing environments.

-R has long since been part of the standard (opengroup.org/onlinepubs/009695399/utilities/cp.html) though not -a.

It’s also relevant to this one, as Zoredache said he encountered a system that «didn’t have [. ] a cp that would copy recursively». Basically, -R -> portable, -a -> not portable.

This was back in ~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

This is a really good point. Essentially rsync has the ability to compute a «directory diff» and transfer only what is needed to synchronize the directories. So you can repeatedly call something like rsync -a src/ dest to «continually copy» directories recursively (the trailing / on src is needed for proper synchronization) whereas cp -a src/ dest doesn’t work like that. The cp command will make a new src directory in dest after the first cp. You’d need something like cp -au src/* dest for subsequent copies.

The other reason I like rsync is you can pass it the -P flag, which shows a progress bar. It gives you some idea of how long something will take

Источник

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