Linux与windows 文件搜索、统计与重命名

Linux 文件查询统计与命名
  • 查看当前目录下文件大小

    1
    ls -lh
  • 查看当前目录下的文件数量(包含子目录中的文件) 注意:R,代表子目录

    1
    ls -l|grep "^-"| wc -l
  • 查看当前目录下的文件夹目录个数(不包含子目录中的目录),如果需要查看子目录的,加上R

    1
    ls -lR|grep "^-"| wc -l
  • 对当前目录下所有的符合匹配字符的文件进行数量统计

    1
    2
    3
    ls -l *.wiff|grep "^-"| wc -l
    ls -l *.wiff.scan|grep "^-"| wc -l
    ls -l *blk*.wiff.scan|grep "^-"| wc -l
  • 对当前目录下及子目录所有的符合匹配字符的文件进行数量统计

    1
    2
    3
    4
    find . -type f -name "*GNHS*.wiff.scan" -exec ls -lh {} \;
    find . -type f -name "*GNHS*.wiff" ! -name "*blk*" -exec ls -lh {} \;
    # 在后面加上 >out.log,将结果输出到out.log文件中
    find . -type f -name "*GNHS*.wiff.scan" -exec ls -lh {} \;>out.log
  • 对.wiff文件统一命名成_low.wiff文件

    1
    for var in *.wiff; do mv "$var" "${var%.wiff}_low.wiff"; done
windows下搜索
  • python

    1
    2
    3
    import os
    import fnmatch
    import pandas as pd
  • 对指定目录下所有符合匹配字符(列表形式)文件进行统计

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    def formatSize(bytes):
    ''' 用mb表示文件大小 '''
    bytes = float(bytes)
    kb = bytes / 1024
    mb = kb / 1024
    return mb

    def search_text(search,path):
    ''' 在指定路径搜索指定字段 '''
    res = []
    size = []
    for root, dirnames, filenames in os.walk(os.path.expanduser(path)):
    for extension in search:
    for filename in fnmatch.filter(filenames, extension):
    res.append(os.path.join(root,filename))
    size.append(formatSize(os.path.getsize(os.path.join(root,filename))))
    res_dict=dict(zip(res,size))
    return res_dict

    w_search = ['*GNHS*.wiff*', '*GHNS*.wiff*']
    w=search_text(w_search,'E:\')
  • 对指定目录下所有符合匹配字符(以及不含某些字符)文件进行统计
    =两种指定统计之差

    1
    2
    3
    4
    5
    6
    7
    8
    def remove_text(a,b):
    ''' 返回的是字典格式 '''
    # new=list(set(a) ^ set(b))
    new=dict(a.items()-b.items())
    return new

    df = pd.DataFrame(pd.Series(new), columns=['size'])
    df = df.reset_index().rename(columns={'index': 'filename'})
  • 两个目录直接进行对比分析(寻找B在A中缺少的文件)

    1
    2
    3
    4
    5
    6
    '''首先对已存在文件读取,或者转成DataFrame'''
    def check_diff(df1,df2):
    df1 = df1.append(df2)
    df1 = df1.append(df2)
    df1 = df1.drop_duplicates(subset=['filename'], keep=False)
    return df1
  • 对目录下指定系列文件进行批量重命名(前端覆盖式命名)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import os, sys

    def add_prefix_files(change,text):
    mark = change
    old_names = os.listdir( path )
    for old_name in old_names:
    if old_name!= sys.argv[0]:
    if old_name.endswith(text):
    os.rename(os.path.join(path,old_name),
    os.path.join(path,mark+old_name[len(mark):]))
    print (old_name,"has been renamed successfully!\n
    New name is: ",mark+old_name[len(mark):])

    In[0]:path='E:\'
    change='b23'
    text='.wiff'
    add_prefix_files(change,text)

    out[0]:b22_1.wiff has been renamed successfully!
    New name is:b23_1.wiff
    b22_2.wiff has been renamed successfully!
    New name is:b23_2.wiff

    '''直接加前缀可以改成os.path.join(path,mark+old_name)'''