Windows – How to preserve Windows file attributes when copying files under Linux

fileswindows

I'm using a custom Bash script running on a Linux machine to mass-duplicate FAT32-formatted USB drives.

I need to preserve Windows file attributes such as "hidden".

I use rsync (or dd with find and mkdir in newer version to force I/O sync for better stability and optimal bandwidth use).

I blindly tried -X and -A switches for rsync, that are said to preserve some file attributes, but they don't seem to work for Windows file attributes.

Is there any way to read and write Windows file attributes in a Linux environment?

I could apply them after copying if there's no easier way.

I don't want to dd an entire source filesystem, because that is going to be badly unoptimized, especially when you need to write hundreds of drives. Sometimes the drives are 16GB in capacity, and only a few GBs of files on them – and a dumb dd would copy all 16GB, taking way to long for this to work in production.

I've searched the web, but looks like Linux extended attributes have nothing to do with Windows file attributes, and I haven't found any way to read and write this information (or just copy it with the files).

Best Answer

I can think of a couple of ways to handle this.

The first is to use Mtools to copy the files. For this to work, you’d need to define “drives” (say A: for the source drive, B: for the target), then you should be able to copy everything, preserving attributes and timestamps, using

mcopy -s -p -m a: b:

(untested).

The second is to post-process the attributes using fatattr (which is available in most distributions). It handles mounted file systems, so you’d copy your file systems in the same way you currently do, then use fatattr to list all the FAT attributes of all the source files, and parse that to instruct fatattr to set the attributes appropriately on the target files.

Related Question