Linux cp specific files from a text list of files to subdirectories from a text list too?

Solution 1:

The way I've done this in the past is using tar as a go-between -- but I'm sure there are other answers that are more elegant than this.

Where we have a list of files that meet a specific criteria IE: all files in /usr smaller than 1M

$ find /usr -type f -size -1M

That we want to copy to the location /mnt/dst.

You can use tar as a vector to pack/unpack the data. IE

$ find /usr -type f -size -1M | tar --files-from=- -c | tar -xv -C /mnt/dst

The first tar takes the --files-from which expects a line by line list of full paths to files and creates a tarball to stdout.

The second tar switches to the destination path with -C and unpacks the tarball received from the pipe.

This results in the following output (when using -v in the second tar command).

usr/lib/grub/i386-pc/fdt.lst
usr/lib/python3.6/site-packages/pip/_vendor/html5lib/filters/__init__.py
usr/lib/python3.6/site-packages/pip/_vendor/urllib3/contrib/__init__.py
usr/lib/python3.6/site-packages/pip/_vendor/urllib3/contrib/_securetransport/__init__.py
usr/lib/python3.6/site-packages/pip/_vendor/urllib3/packages/backports/__init__.py
usr/lib/python3.6/site-packages/pip/operations/__init__.py
usr/lib/python3.6/site-packages/pkg_resources/_vendor/__init__.py
usr/lib/python3.6/site-packages/setuptools/_vendor/__init__.py
usr/lib/python3.6/site-packages/slip/__init__.py
usr/lib/python3.6/site-packages/slip/_wrappers/__init__.py
usr/lib/python3.6/site-packages/asn1crypto/_perf/__init__.py
...
...

The resulting destination directory produces the (pruned for readability) tree which should be what you're looking for..

# tree -L 3 /mnt/dst
/mnt/dst
└── usr
    ├── lib
    │   ├── grub
    │   ├── node_modules
    │   └── python3.6
    ├── lib64
    │   └── python3.6
    ├── local
    │   └── share
    └── share
        ├── crypto-policies
        ├── doc
        ├── groff
        ├── microcode_ctl
        ├── mime
        ├── pki
        ├── texlive
        ├── texmf
        ├── vim
        └── X11

20 directories, 0 files