Copy all files recursively 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 .

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

Читайте также:  Криптопро линукс корневой сертификат

Источник

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?

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

Читайте также:  Клиент под линукс 1с

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

Источник

Copy Files Recursively in Linux

Copy Files Recursively in Linux

Linux terminal is an easy and fast way to copy files and directories. Throughout this article, we will explain how to copy files in Linux with the cp command.

We will also use a wildcard * to copy files with similar names and recursively copy multiple files and directories.

The sample files and directories we will use throughout the article are below.

Files and directory structure

Use the cp Command to Copy Files in Linux

After the cp command, type the file sources you want to copy and the destination file or directory. The use of a slash / after the directory name is optional.

cp Folder/file1.txt Folder3/ 

Copy Files With the cp

Use Wildcard * to Copy Files With Similar Filenames in Linux

An asterisk * is called wildcard , and it gives every file that starts with the specified name as a parameter to the cp command.

We want to copy all files with the name file1 , even if the extension is different. We use a wildcard instead of specifying the extension at the end of the filename.

Use Wildcard for Similar Filenames

Use Wildcard * to Copy Files With Same Extension in Linux

This time, we want to copy all files with the same extension, even if their names are different. We use a wildcard instead of the filename then write the extension.

Use Wildcard for Same Extension

Use the -r Flag to Copy Files Recursively in Linux

The -r or -R flag allows you to copy directories and their contents recursively. Type the directory name you want to copy after the cp -r command and the destination directory.

We can also use the -a flag. It is similar in functionality to the -r flag, but it copies the files without changing their metadata, such as creation date.

Use the -r for copy recursively

Use the find Command to Copy Files Recursively in Linux

We can use the find command to recursively find and copy files with similar extensions or filenames from a directory and its sub-directories. The find command is used with the exec .

find Folder/ -name '*.txt' -exec cp -r <> Folder3 \; 

Use find with exec

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

Related Article — Linux File

Copyright © 2023. All right reserved

Источник

Recursively copy files from one directory to another

I have all my music in a folder /media/kalenpw/MyBook/Music/ABunchOfOtherFoldersWithFilesInside . I want to copy all of the mp3s to /media/kalenpw/HDD/Music so I used:

cp -R /media/kalenpw/MyBook/Music/*.mp3 /media/kalenpw/HDD/Music 

however this only copied the mp3s in the root music folder and did not open any of the artist subdirectories and copy those files. I was under the impression -R would recursively copy all the files. How can I achieve said goal?

Читайте также:  Hashcat установка kali linux

5 Answers 5

find /media/kalenpw/MyBook/Music/ -name '*.mp3' -exec cp <> /media/kalenpw/HDD/Music \; 

The reason for your command not working is that names containing wildcards ( *.mp3 ) are expanded before the command is run, so if you had three files ( 01.mp3 , 02.mp3 , 03.mp3 ) your effective command was:

cp -R /media/kalenpw/MyBook/Music/01.mp3 /media/kalenpw/MyBook/Music/02.mp3 /media/kalenpw/MyBook/Music/03.mp3 /media/kalenpw/HDD/Music 

As you can see -R has no effect in this case.

You have specifically mentioned the files(s)/directory(ies) to be copied as using *.mp3 i.e. any file/directory name ending in .mp3 .

So any file ending in .mp3 in /media/kalenpw/MyBook/Music/ directory and similarly, any directory ending in .mp3 in /media/kalenpw/MyBook/Music/ will be copied, recursively. If there is no such matched file/directory, nothing will be copied.

Now to copy all .mp3 files from /media/kalenpw/MyBook/Music/ recursivley to directory /media/kalenpw/HDD/Music/ :

shopt -s globstar cp -at /media/kalenpw/HDD/Music /media/kalenpw/MyBook/Music/**/*.mp3 
find /media/kalenpw/MyBook/Music -type f -name '*.mp3' -exec cp -at /media/kalenpw/HDD/Music <> + 

The simplest way to do this would be to use a copy command with no wildcards and only directory names:

cd /media/kalenpw/HDD cp -r /media/kalenpw/MyBook/Music . 

If the current directory didn’t already have a directory named Music, this would create a new Music directory in the current directory. If you already have a directory named Music, It would copy the contents of it to the existing Music directory. This, of course, would get all the files, not just the .mp3 files, so it might not give you the flexibility you need.

Suppose that you have a bunch of .docx , .mp3 , .txt and .xlsx files stored in this directory structure:

/files/ /files/dir1/ /files/dir1/dir11/ /files/dir1/dir12/ /files/dir1/dir13/ /files/dir2/ /files/dir3/ /files/dir3/dir31/ /files/dir3/dir32/ /files/dir4/ /files/dir5/ /files/dir51/ /files/dir52/ /files/dir53/ /files/dir54/ 

. and you want to recurse into all such directories in order to copy all found .mp3 files to /home/me/music/ but you do not want to preserve such directory tree in the destination (i.e. you want all found .mp3 files to be copied to /home/me/music/ instead of copied to respective directories such as /home/me/music/dir1/ , /home/me/music/dir1/dir11/ et cetera).

In such case, at the shell terminal (bash) first run this command in order to access the root of your file search:

. and then run this command:

for i in `find . -iname '*.mp3'`; do cp $i /home/me/music/; done 

In case you do want to preserve the source’s directory tree in the destination, run this command instead (after running cd /files ):

find . -iname '*.mp3' | cpio -pdm /home/me/music/ 

On the above commands, the search is case-insensitive (i.e. matches .mp3 , .MP3 , .mP3 and .Mp3 ). Use -name instead of -iname if you want the search to be case-sensitive (e.g. using -name for the .mp3 string of characters will match files ending with .mp3 but not those ending with .MP3 , .mP3 nor .Mp3 ).

It’s also important to point out that, in the case of the command that preserves the source directory tree/structure, those folders whose content doesn’t match the search criteria won’t be copied to the destination. Hence, if e.g. no .mp3 file is found in /files/dir5/ , then no /home/me/music/dir5 directory will be created, but if at least one .mp3 file is found in /files/dir5/ , then /home/me/music/dir5 will be created so such .mp3 file(s) can be stored inside of it.

Источник

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