0

I'd be really happy about some help. Lets imagine a snapshot of a directory structure:

../test1/test6/test62.txt
../test1/test6/test65.txt
../test1/test6/test67.txt
../test1/test6/test69.txt
../test1/test6/test60.txt
../test1/test6/test68.txt
../test1/test6/test63.txt
../test1/test6/test66.txt
../test1/test6/test64.txt
../test1/test6/test61.txt
../test2/test1/test2.txt
../test2/test1/test9.txt
../test2/test1/test1.txt
../test2/test1/test6.txt
../test2/test1/test3.txt
../test2/test1/test8.txt
../test2/test1/test4.txt
../test2/test1/test10.txt
../test2/test1/test5.txt
../test2/test1/test7.txt

My end goal is to copy all the .txt files in one new directory ( e.g target ).

There are many more directories in reality.

I found a very nice cp command:

cp ../**/**/*.txt .

But there is an issue. It's a lot of files, like 250GB of images, so cp errors with:

Argument list too long

I read a bit about how to do it with rsync, which seems the right way, but I can't wrap my head around writing the command and what to exactly exclude and include.

2
  • Do you really want to copy, or would moving or linking be more useful? Makes no significant difference to the resulting command but can make a massive difference on the total disk space used Sep 27, 2023 at 21:14
  • How do you want to handle name collisions?
    – Kusalananda
    Sep 27, 2023 at 22:15

2 Answers 2

0

It's a little strange having all the directory paths start with ... Personally I'd recommend you go up a level and copy/move/link the files into the nominated child directory.

However, this will allow for the .. and ensure you don't end up trying to copy files that have previously been copied

find ../ -mindepth 3 -type f -name '*.txt' -exec cp --target-directory . -- {} +

You can drop in ln for cp if linking is sufficient instead of copying. Likewise mv if moving is the desired intent. Use cp -p if you want to try and keep permissions, timestamps, etc.

Note that if you have two or more files with the same name, all but the last copied will be overwritten. If you're using mv all but the last will be lost. If you're using ln only the first will be successfully linked.

-1

I found one possible solution that works. It's using find with rsync:

find ../. -type f -name "*.txt" -exec rsync -av {} . \;
1
  • That's close but you'll potentially end up trying to copy files you've already copied Sep 27, 2023 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .