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 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个文件夹...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2运行内存多大_电脑笔记本满血复活之内存
- 下一篇: 与mysql数据库的交互实战_实战教程丨