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


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.

  • 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


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.


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

find ../. -type f -name "*.txt" -exec rsync -av {} . \;
  • 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 .