How to check hidden files and folders on NTFS partition using python on linux?

I am using an NTFS on a linux machine. I want to identify hidden files and folders in an NTFS on linux using python .

How can I achieve this using python . Any code snippets / links would be appreciated.

Thanks.

+6
source share
3 answers

Assuming you are using ntfs-3g to mount NTFS partitions on Linux (this is the default for most current Linux distributions).

You will need to read the extended file attributes (see attr (5) ), you can use pyxattr for this. NTFS attributes are stored in the system.ntfs_attrib extended attribute as a set of flags whose values ​​are documented in the ntfs-3g documentation .

Here is a sample code for reading and decoding NTFS file system attributes and using them to filter files:

 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() 
+5
source

There seems to be no python interface for NTFS attributes on Linux.

NTFS-3G supports NTFS file attributes and exposes them to the linux getfattr and setfattr for reading and installing.

You can use python subprocess to call getfattr and then getfattr output.

Note: on my ubuntu system, I had to install the attr package to get getfattr and setfattr .

+1
source

If your question is not limited to Python, you can try my example implemented in a shell script.

This is also based on the system.ntfs_attrib_be attribute in NTFS-3G. 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 distribution and use it.

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

0
source

Source: https://habr.com/ru/post/885639/


All Articles