Copy one directory to another directory in linux

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 .

Читайте также:  Colors in bash linux

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

Источник

Copy a Directory in Linux – How to cp a Folder in the Command Line in Linux and Unix (MacOS)

Copy a Directory in Linux – How to cp a Folder in the Command Line in Linux and Unix (MacOS)

To copy files or directories in Unix-based operating systems (Linux and MacOS), you use the cp command.

The cp command is a relatively simple command, but its behavior changes slightly depending on the inputs (files vs directories) and the options you pass to it.

To view the documentation or manual for the cp command, run man cp at your terminal:

$ man cp NAME cp -- copy files SYNOPSIS cp [OPTIONS] source_file target_file cp [OPTIONS] source_file . target_directory . 

The basic form of this command takes an input source (or sources) that you want to copy (files or directories) and a destination to copy the files or directories to:

cp [OPTIONS] source_file target_file

How to copy a file to the current directory

To copy a file, pass the file you want to copy and the path of where you want to copy the file to.

If you have a file named a.txt , and you want a copy of that file named b.txt :

$ ls a.txt $ cp a.txt b.txt $ ls a.txt b.txt

By default the cp command uses your current directory as the path.

How to copy a file to another directory

To copy a file to a directory that is different from your current directory, you just need to pass the path of the other directory as the destination:

$ ls ../directory-1/ $ cp a.txt ../directory-1/ $ ls ../directory-1/ a.txt 

After the cp command, the previously empty directory-1 now contains the file a.txt .

By default the copied file receives the name of the original file, but you can also optionally pass a file name as well:

$ cp a.txt ../directory-1/b.txt $ ls ../directory-1/ b.txt

How to copy multiple files to a directory

To copy more than one file at a time you can pass multiple input sources and a directory as destination:

$ ls ../directory-1/ $ cp first.txt second.txt ../directory-1/ $ ls ../directory-1/ first.txt second.txt 

Here the two input sources ( first.txt and second.txt ) were both copied to the directory directory-1 .

Читайте также:  Посмотреть все права файла linux

How to copy a directory to another directory

If you try to pass a directory as the input source, you get this error:

$ cp directory-1 directory-2 cp: directory-1 is a directory (not copied).

To copy a directory, you need to add the -r (or -R ) flag—which is shorthand for —recursive :

$ ls directory-1 a.txt $ cp -r directory-1 directory-2 $ ls directory-1 directory-2 $ ls directory-2 a.txt

Here directory-1 containing the file a.txt is copied to a new directory called directory-2 —which now also contains the file a.txt .

How to copy the entire directory vs the contents of the directory

There is an interesting edge case when you copy a directory: if the destination directory already exists, you can choose whether to copy the contents of the directory or the entire directory by adding or removing a trailing / from your input.

Here’s the description from the -R option of the man page:

If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the source_file ends in a /, the contents of the directory are copied rather than the directory itself.

If you want to copy just the contents of the directory into another directory, add a trailing / to your input.

If you want to copy the contents of the directory and the directory folder itself into another directory, don’t add a trailing / :

$ ls directory-1 directory-2 $ ls directory-2 $ cp -r directory-1 directory-2 $ ls directory-2 directory-1 $ ls directory-2/directory-1 a.txt

Here you can see that because directory-2 already exists—and the input source didn’t have a trailing / —both the contents of directory-1 and the directory itself was copied into the destination.

How to prevent overwriting files with cp

By default, the cp command will overwrite existing files:

$ cat a.txt A $ cat directory-1/a.txt B $ cp a.txt directory-1/a.txt $ cat directory-1/a.txt A

There are two ways to prevent this.

The interactive flag

To be prompted when an overwrite is about to occur, you can add the -i or —interactive flag:

$ cp -i a.txt directory-1/a.txt overwrite directory-1/a.txt? (y/n [n])

The no-clobber flag

Or, to prevent overwrites without being prompted, you can add the -n or —no-clobber flag:

$ cat a.txt A $ cat directory-1/a.txt B $ cp -n a.txt directory-1/a.txt $ cat directory-1/a.txt B

Here you can see that thanks to the -n flag the contents of directory-1/a.txt were not overwritten.

Other options

There are many other useful options to pass to the cp command: like -v for «verbose» output or -f for «force.»

I highly encourage you to read through the man page for all of the other useful options.

If you liked this tutorial, I also talk about topics like this on Twitter, and write about them on my site.

Источник

How to Copy a Directory in Linux Command Line [Beginner’s Tip]

This quick tip for beginners demonstrates how to copy a directory in Linux using the cp command. You’ll also learn a few additional tips around copying directory.

If you are new to Linux command line, you probably have this question in mind: How do I copy a directory and the files inside it to another directory in Linux command line?

Читайте также:  Утилита удаления криптопро linux

Here’s how to copy a directory in Linux:

cp -r source_directory destination_directory

You probably already know that you can use cp command to copy files in Linux. Do you know that you can use the same cp command to copy a folder in Linux command line?

Hah! You already tried that and perhaps got this error:

cp: -r not specified; omitting directory 'test_dir'

Let me show you a thing or two about copying directory in Linux.

Copy directory in Linux command line

You can definitely use the same cp command but with the recursive option -r to copy a folder with its content to another folder. The above mentioned error also hints that you missed the -r option.

All you got to do is to use the command in this fashion:

cp -r source_directory destination_directory

And now if you use ls command on the destination directory, it should have the entire source directory inside it.

The -r option allows the recursive option. This means the entire content of the directory including its own subdirectories, everything in the directory will be copied to the destination.

A few things to note about copying directory in Linux

Here are a couple of things to note and tips about copying folders.

The destination directory doesn’t exist? It will be created

If the destination directory doesn’t exist (but the path exists), it will be created up to one level (explained in the next section). However, it will now copy the contents of the source directory, not the source directory itself.

For example, if you do this:

cp -r source_directory non_existing_directory

The non_existing_directory will be created with the content of the source_directory but it won’t have the source_directory inside it. Just the files of source_directory will be copied. It would be like non_existing_directory will be a replica of source_directory.

[email protected]:~$ ls test_dir [email protected]:~$ cp -r test_dir new_dir [email protected]:~$ tree . ├── new_dir │ ├── c.xyz │ ├── myzip1.zip │ └── myzip2.zip └── test_dir ├── c.xyz ├── myzip1.zip └── myzip2.zip 2 directories, 6 files

Can’t create nested directory while copying

You cannot use the above command to create nested directory structure.

For example, if you try to use cp -r source_dir dir1/dir2/dir3 but dir2 and dir3 don’t exist, it won’t create the nested directory structure and the command fails.

Preserve the original file attributes

One last tip to keep things short. If you use the -a option along with the -r option, it will preserve the original file information such as file permissions, file timestamps etc. It will archive the directory to the new location instead of creating it afresh.

This much information should be enough for you to know how to copy a directory in Linux. If you have questions or suggestions, please feel free to leave a comment.

Источник

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