Linux copy dir recursive

How can I recursively copy a directory into another and replace only the files that have not changed?

The export directory contains many of the same files/folders that the root does, however the root contains additional ones not found in export.

I’d like to merge all of the contents of export with my webroot with the following options:

  1. Overwriting the file in webroot if export’s version contains different code than what is inside of webroot’s version (live)
  2. Preserve the permissions/users/groups of the file if it is overwritten (the export version replacing the live version) *NOTE I would like the webroots permissions/ownership maintained, but with export’s contents
  3. No prompting/stopping of the copy of any kind (ie not verbose)
  4. Recursive copy — obviously I would like to copy all* files folders and subfolders found in export

I’ve done a bit of research into cp — would this do the job?:

cp -pruf ./export /path/to/webroot 

Note that your first criterion is not clear cut. The answers mostly assume you mean ‘if the file in webroot is newer than the file in export, leave it alone’. If you truly mean ‘compare contents and copy export version if there is a difference’, then simply copy everything . continued.

. continuation. as the new files will appear, and the old files that are the same in both will still be the same after the copy — except perhaps for the modification time.

3 Answers 3

It might, but any time the corresponding files in export and webroot have the same content but different modification times, you’d wind up performing an unnecessary copy operation. You’d probably get slightly smarter behavior from rsync :

rsync -pr ./export /path/to/webroot 

Besides, rsync can copy files from one host to another over an SSH connection, if you ever have a need to do that. Plus, it has a zillion options you can specify to tweak its behavior — look in the man page for details.

EDIT: with respect to your clarification about what you mean by preserving permissions: you’d probably want to leave off the -p option.

Читайте также:  Какая видеокарта задействована linux

Источник

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.

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.
Читайте также:  Редактирование pdf файла linux

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.

Источник

How do I create a copy of a directory in Unix/Linux? [closed]

I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).

3 Answers 3

The option you’re looking for is -R .

cp -R path_to_source path_to_destination/ 
  • 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):
cp -a path_to_source/. path_to_destination/ 

i wonder why this exact command in dockerfile copies all source directory files into destination, instead of copying just whole directory.

I believe the ‘/’ on the end makes a difference and that might account for your experience. If the source includes the trailing slash it will copy what is in the directory only. If it does not include the trailing slash, it will copy the directory as well and then the contents inside of it. My memory is this behavior varies by command and maybe event by OS a bit. Here’s a reference with more info.

Читайте также:  Слетел загрузчик линукс минт

I would say if you don’t want to include the source and you want to make sure everything is copied (symlinks, hidden files) without copying the source parent folder is to use -ra source/. destination. This will make sure the content of the folder is copied, but not the parent folder itself, which is sometimes handy. And the difference is the /.

Note the importance of «Slash dot» on your source in cp -r src/. dest I know it is mentioned but I still seem to miss it every time.

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:

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.

There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R , -r , and —recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r . It’s important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).

Источник

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