失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > ubuntu 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个

ubuntu 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个

时间:2023-05-17 17:57:49

相关推荐

ubuntu 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个

只需从命令行一次复制多个文件

有几种方法可以实现这个,我见过的最容易的是cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination/

语法使用cp命令,后跟所需文件所在目录的路径,所有要复制的文件都用方括号括起来并用逗号分隔。

或者,如果所有文件都有相同的前缀,但结尾不同,则可以执行以下操作:cp /home/usr/dir/file{1..4} ./

将复制file,file,file3和file4的位置。

使用python处理重复项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))

ubuntu 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个文件夹...

如果觉得《ubuntu 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。