批量复制指定文件到新文件夹

原创
07/09 15:48
阅读数 99

从一堆数据源里选出你需要的文件,然后存入新的文件夹。这样就可以剔除无效的存储数据,从而节省存储空间。

data.txt 需要复制的文件路径

# data.txt 的内容
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5027-pageNo-4-a5bb94bc-d104-4b14-81f3-7e6090e04d11.html
2019-03-10/2019-03-10-baidu-websiteId-609-keywordId-6228-pageNo-1-b02a6e2d-fc5f-4a20-830f-bd46da0eb9de.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5028-pageNo-1-1ed02731-b9f4-427c-8453-8cc75e002d86.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5029-pageNo-3-0b02dbc0-84de-48f4-bc24-34a4e4bc67fe.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5033-pageNo-5-4fdab504-84f9-46e3-bed8-80fce05b8bd4.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5034-pageNo-1-d9e9bf83-e41b-43ba-a135-ee5d763786c2.html
2019-03-10/2019-03-10-baidu-websiteId-609-keywordId-6229-pageNo-1-061aa4ae-5706-4faa-ad00-22cf2aab427f.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5035-pageNo-1-94e86226-3fba-4aad-a079-17ec588d454c.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5037-pageNo-7-1c149943-7fba-49e3-a200-87be29878eb5.html
2019-03-10/2019-03-10-baidu-websiteId-419-keywordId-5038-pageNo-4-649df878-7312-40ef-acc6-91d09d077608.html
2019-03-10/2019-03-10-baidu-websiteId-430-keywordId-3253-pageNo-8-f350cd98-11a5-45f6-800d-743cf982f13b.html
2019-03-10/2019-03-10-baidu-websiteId-430-keywordId-3334-pageNo-1-f409faf9-5668-4dd0-bda9-dfea0feb4951.html

执行脚本命令

sh cfiles.sh data.txt

cfiles.sh 脚本

#!/bin/bash
set -e

# sed -i 's/\r//' data.txt && bash data.txt
# data='2020-05-01/1588329069194-2020-05-01-18-31-09.194-baidu-websiteId-716-keywordId-7110-pageNo-1-183B6995-96DF-4583-AB3C-2F241D9F8242.html'

rootPath='/home/back44'
filePath=$1
a=0

function cpFiles() {
    data=$1
    destDir=${data%%/*}
    file=${data##*/}

    if [ ! -f $data ]; then
        echo "文件不存在: ${data}"
        echo ${data} >>nodata.txt
    else
        if [ ! -d "$rootPath/$destDir" ]; then
            mkdir -p "$rootPath/$destDir"
        fi

        cp -r "$data" "$rootPath/$destDir/$file"
        echo "复制完成: ${data}"
    fi

}

while read line; do
    cpFiles $line
    let a=a+1
done <$filePath

echo $a

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部