简体   繁体   English

如何在linux上使用python检查NTFS分区上的隐藏文件和文件夹?

[英]How to check for hidden files & folders on NTFS partition using python on linux?

I am using NTFS partition on linux machine. 我在linux机器上使用NTFS分区。 I want to identify hidden files and folders on my NTFS partition on linux using python . 我想使用pythonlinux上的NTFS分区上识别隐藏的文件和文件夹。

How can I achieve this using python . 我怎样才能使用python实现这一目标。 Any code snippet / links would be appreciated. 任何代码片段/链接将不胜感激。

Thanks. 谢谢。

Assuming you are using ntfs-3g to mount your NTFS partitions on linux (this is default on most current linux distributions). 假设您使用ntfs-3g在Linux上安装NTFS分区(这在大多数当前的Linux发行版中是默认的)。

You will need to read file extended attributes (see attr(5) ), you can use pyxattr for this. 您将需要读取文件扩展属性(请参阅attr(5) ),您可以使用pyxattr NTFS attributes are stored in system.ntfs_attrib extended attribute as a set of flags which values are documented in ntfs-3g documentation . NTFS属性作为一组标志存储在system.ntfs_attrib扩展属性中,这些标志值记录在ntfs-3g文档中

Here is a sample code to read and decode NTFS file system attributes and use them to filter files: 下面是一个示例代码,用于读取和解码NTFS文件系统属性并使用它们来过滤文件:

import os, struct, xattr

# values from http://www.tuxera.com/community/ntfs-3g-advanced/extended-attributes/
attributes = ['readonly', 'hidden', 'system', 'unknown',
              'unknown', 'archive', 'unknown', 'unknown',
              'temp', 'unknown', 'unknown', 'compressed_dir',
              'offline', 'not_content_indexed' ] + ['unknown']*18

def ntfs_attr(path):
    attr = struct.unpack("i", xattr.get(path, "system.ntfs_attrib"))[0]
    for shift, attribute in enumerate(attributes):
        if (attr >> shift) & 1 == 1:
            yield attribute

def main():
    import sys
    if len(sys.argv) != 3:
        print "Usage: %s path attribute" % sys.argv[0]
        a = set(attributes)
        a.remove('unknown')
        print "where attribute is one of:", ' '.join(a)
        sys.exit(1)

    path = sys.argv[1]
    attribute = sys.argv[2]
    print "Files with %s attribute in %s:" % (attribute, path)
    for filename in os.listdir(path):
        fullname = os.path.join(path, filename)
        if attribute in ntfs_attr(fullname):
            print fullname


if __name__ == '__main__':
    main()

There seems to be no python interface for NTFS attributes under linux. linux下的NTFS属性似乎没有python接口。

NTFS-3G supports NTFS file attributes and exposes them for the linux tools getfattr and setfattr to read and set. NTFS-3G支持NTFS文件属性 ,并为Linux工具getfattrsetfattr公开它们以进行读取和设置。

You can use python's subprocess to invoke getfattr and then parse the output. 您可以使用python的subprocess getfattr来调用getfattr ,然后解析输出。

Note: on my ubuntu system i had to install the package attr to get the commands getfattr and setfattr . 注意:在我的ubuntu系统上,我必须安装包attr以获取命令getfattrsetfattr

If your question is not limited to Python, you can try my example implemented in shell script. 如果您的问题不仅限于Python,您可以尝试在shell脚本中实现我的示例。

This is also based on system.ntfs_attrib_be attribute in NTFS-3G. 这也基于NTFS-3G中的system.ntfs_attrib_be属性。 If you are just going to use it and don't care about how it is implemented (in Python or shell), just download it, install getfattr and setfattr from your distro, and use it. 如果你只是想使用它而不关心它是如何实现的(在Python或shell中),只需下载它,从你的发行版安装getfattrsetfattr ,然后使用它。

https://gist.github.com/Explorer09/ac4bf6838c271a9968b3 https://gist.github.com/Explorer09/ac4bf6838c271a9968b3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM