简体   繁体   中英

In linux, copy files with the same name in multiple directories, to a new directory with the path difference as name

I have aa couple hundred output patient files in sub directories I need to collate and analyse.

/project/folder1/folder2/samplenumber/file.filetype

I need to copy all of these from a hosted area to my own workspace for analysis

I want to end up with

/mydrive/myproject/myfolder/samplenumber.filetype
/mydrive/myproject/myfolder/samplenumber.filetype
/mydrive/myproject/myfolder/samplenumber.filetype

I thought something like

cp -v /project/folder1/folder2/*/file.filetype /mydrive/myproject/myfolder/*.filetype

will this function?

You will need to use the find command for this, parsing the output in a loop

find /project/folder1/folder2 -name "*.filetype" | while read line
do 
    payth=${line%/*.*}
    samp=${payth#/*/*/*/}
    cp "$line" "/mydrive/myproject/myfolder/$samp.filetype"
done

For each line returned from the find command, we set the variable payth to the path of the file (using bash parameter expansion and suffix pattern match). We then use this payth variable to attain the last directory (utilising bash parameter expansion and prefix pattern match) reading this into the variable samp. This variable samp is finally used a the file name in the copy command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM