Score:1

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

sn flag

I ask for your help to solve my problem because I am stuck. I explain the situation to you: I want to copy files whose path I have on a txt file in specific subdirectories specified in a second file (I also have a complete csv file including these 2 columns: name of the subdirectories ($value1), file path ($value2))

I was able to automatically create the subdirectories using this command:

xargs mkdir -p </scripts/repertoires.csv

I know how to copy all files to a single directory using this command:

cat /scripts/fichiers.csv | xargs -I% cp% / destinationfolder

But I can't copy each line corresponding to a file in the subdirectory that suits it, To try to be clearer I want to copy $value2 in /destination folder/$value1

Score:1
jo flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.