Linux copy all recursive

Linux recursive copy files to its parent folder

I’m prone to say that this should go into Unix & Linux. An answer to your question is probably rsync(1) , though.

Do you always have two levels of directories? What do you want to happen to a file in folderA ? Should it stay there or be moved to . ? What should happen if folder1 contains further sub-directories? Also, I’m uncertain whether you mean to move or to copy the files.

3 Answers 3

I suspect that while you say copy, you actually mean to move the files up to their respective parent directories. It can be done easily using find :

$ find . -name '*.txt' -type f -execdir mv -n '<>' ../ \; 

The above command recurses into the current directory . and then applies the following cascade of conditionals to each item found:

  1. -name ‘*.txt’ will filter out only files that have the .txt extension
  2. -type f will filter out only regular files (eg, not directories that – for whatever reason – happen to have a name ending in .txt )
  3. -execdir mv -n ‘<>‘ ../ \; executes the command mv -n ‘<>‘ ../ in the containing directory where the <> is a placeholder for the matched file’s name and the single quotes are needed to stop the shell from interpreting the curly braces. The ; terminates the command and again has to be escaped from the shell interpreting it.

I have passed the -n flag to the mv program to avoid accidentally overwriting an existing file.

The above command will transform the following file system tree

dir1/ dir11/ file3.txt file4.txt dir12/ file2.txt dir2/ dir21/ file6.dat dir22/ dir221/ dir221/file8.txt file7.txt file5.txt dir3/ file9.dat file1.txt 
dir1/ dir11/ dir12/ file3.txt file4.txt dir2/ dir21/ file6.dat dir22/ dir221/ file8.txt file7.txt dir3/ file9.dat file2.txt file5.txt 

To get rid of the empty directories, run

$ find . -type d -empty -delete 

Again, this command will traverse the current directory . and then apply the following:

  1. -type d this time filters out only directories
  2. -empty filters out only those that are empty
  3. -delete deletes them.

Fine print: -execdir is not specified by POSIX, though major implementations (at least the GNU and BSD one) support it. If you need strict POSIX compliance, you’ll have to make do with the less safe -exec which would need additional thought to be applied correctly in this case.

Finally, please try your commands in a test directory with dummy files, not your actual data. Especially with the -delete option of find , you can loose all your data quicker than you might imaging. Read the man page and, if that is not enough, the reference manual of find . Never blindly copy shell commands from random strangers posted on the internet if you don’t understand them.

Читайте также:  File system and directories 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 .

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

Читайте также:  Обновление ядра linux debian

Источник

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.

Читайте также:  Linux удалить пакет apt get

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

Источник

Copy all files in a directory to a local subdirectory in linux

I’d like to make a copy of all the existing files and directories located in this directory in new_subdir . How can I accomplish this via the linux terminal?

3 Answers 3

This is an old question, but none of the answers seem to work (they cause the destination folder to be copied recursively into itself), so I figured I’d offer up some working examples:

Copy via find -exec:

find . ! -regex ‘.*/new_subdir’ ! -regex ‘.’ -exec cp -r ‘<>‘ new_subdir \;

This code uses regex to find all files and directories (in the current directory) which are not new_subdir and copies them into new_subdir. The ! -regex ‘.’ bit is in there to keep the current directory itself from being included. Using find is the most powerful technique I know, but it’s long-winded and a bit confusing at times.

Copy with extglob:

cp -r !(new_subdir) new_subdir

If you have extglob enabled for your bash terminal (which is probably the case), then you can use ! to copy all things in the current directory which are not new_subdir into new_subdir.

Copy without extglob:

mv * new_subdir ; cp -r new_subdir/* .

If you don’t have extglob and find doesn’t appeal to you and you really want to do something hacky, you can move all of the files into the subdirectory, then recursively copy them back to the original directory. Unlike cp which copies the destination folder into itself, mv just throws an error when it tries to move the destination folder inside of itself. (But it successfully moves every other file and folder.)

Источник

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