Cpio all *.txt-files out of folders to just one directory

I need a hint for reading manpage (I did rtfm really) of cpio to do this task as in the headline described. I want to put all files of a certain type, lets say all *.txt files or any other format. Spread in more than hundreds of subdirectories in one directory I would like to select them and just move, copy them all in one directory. As using the following comand it gets me some trouble, may someone could give me a hint.

find /path/to/folder .*.txt -depth -print0 | cpio --null -pvd ../new/folder

Thanks in advance!!!

The issue with using cpio is that it preserves the directory hierarchy. You could go with:

find /.../sourcedir -type f -name '*.txt' -exec cp {} /.../targetdir

but that would generate a process for every file, and cp is not most efficient way to copy files.

Are you trying to just flatten the directory hierarchy? If so, perhaps something like:

find /.../sourcedir -type f -name '*.txt' -print0 | xargs -0 ln -t /.../targetdir

which would create hardlinks to all the files in the target directory. Use the -s option of man ln (linux) if the target directory is in a different filesystem.

If you need to make a copy of the files:

TMPDIR=$(mktemp -d)
trap "rm -f ${TMPDIR}" EXIT
find /.../sourcedir -type f -name '*.txt' -print0 | xargs -0 ln -s -t ${TMPDIR}
cd ${TMPDIR}
ls -1fA | cpio -pLvum /.../targetdir

Be advised that nothing is being done for filename collisions.

You also specified find /path/to/folder .*.txt . The -name option is missing, and use single-quotes. Otherwise the shell globbing expansion may give you a surprise.

1 Like

Thats helping, I see also one mistake of mine, as you mentioned the -name flag and single quotes, therefore I tried instead -iname without any quotes.

But this is rrreaally helpful, yes to flaten the structure. Thanks a lot.

No. Always use the quotes for every -name or -iname primary operand. (You can get by without it under certain circumstances and with the lack of files matching a pattern in your current working directory; but when you try the same script later after some files have been added or when you run it in a different directory, your script with stop working. Even when the quotes aren't required, adding them will never cause you a problem.)