手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下同时复制多个文件

[日期:2018-08-14] 来源:Linux社区  作者: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 中定义要复制的文件名字,只要给出名字即可,不需要路径

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址:https://www.linuxidc.com/Linux/2018-08/153494.htm

linux
相关资讯       Linux复制多个文件 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款