Linux下同时复制多个文件

方法
1.使用cp命令
cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination/

需要注意的是几个文件之间不要有空格

1.具有共同前缀

cp /home/usr/dir/file{1..4} ./

复制的文件是file1, file2, file3, file4

方法二
1.使用python脚本 shutil库
import os,sys,shutil
### copies a list of files from source. handles duplicates.
def rename(file_name, dst, num=1):
    #splits file name to add number distinction
    (file_prefix, exstension) = os.path.splitext(file_name)
    renamed = “%s(%d)%s” % (file_prefix,num,exstension)

    #checks if renamed file exists. Renames file if it does exist.
    if os.path.exists(dst + renamed):
        return rename(file_name, dst, num + 1)
    else:
        return renamed

def copy_files(src,dst,file_list):
    for files in file_list:
        src_file_path = src + files
        dst_file_path = dst + files
        if os.path.exists(dst_file_path):
            new_file_name =  rename(files, dst)
            dst_file_path = dst + new_file_name

        print “Copying: ” + dst_file_path
        try:
            # 复制操作主要就是这句
            shutil.copyfile(src_file_path,dst_file_path)
        except IOError:
            print src_file_path + ” does not exist”
            raw_input(“Please, press enter to continue.”)

def read_file(file_name):
    f = open(file_name)
    #reads each line of file (f), strips out extra whitespace and
    #returns list with each line of the file being an element of the list
    content = [x.strip() for x in f.readlines()]
    f.close()
    return content

src = sys.argv[1]
dst = sys.argv[2]
file_with_list = sys.argv[3]

copy_files(src,dst,read_file(file_with_list))

2. 将以上代码保存为move.py

3. 运行 $ python move.py /path/to/src/ /path/to/dst/ file.txt

4. file.txt 中定义要复制的文件名字,只要给出名字即可,不需要路径

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Linux下同时复制多个文件 https://www.lizi.tw/soft/10201.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Linux下同时复制多个文件-海报

分享本文封面