2

I would like to tag generated files with generated: true. Then later I'd like to find and remove all generated files rm -rf ....

Right now I'm including generated in the filename filename.generated.go where I can remove them with rm -rf **/*.generated.* - but I'm wondering if there's a way to omit it from the filename

1 Answer 1

5

In Linux see man 7 xattr:

Extended attributes are name:value pairs associated permanently with files and directories, similar to the environment strings associated with a process. An attribute may be defined or undefined. If it is defined, its value may be empty or non-empty.

[…]

User extended attributes may be assigned to files and directories for storing arbitrary additional information such as the mime type, character set or encoding of a file. The access permissions for user attributes are defined by the file permission bits: read permission is required to retrieve the attribute value, and writer permission is required to change it.

You can set an extended attribute with setfattr:

Add extended attribute to user namespace:

$ setfattr -n user.foo -v bar file.txt

In your case the command will be like:

setfattr -n user.generated -v true filename.go

You can retrieve the value with getfattr:

getfattr --only-values -n user.generated filename.go

The following command will test if the value is equal* to true (by comparing strings):

[ "$(getfattr --only-values -n user.generated filename.go 2>/dev/null)" = true ]

Exit status 0 indicates equality.

*Note because of the way $() works, all trailing newlines are removed before comparison.

Now we can use this command as a test in find:

find . -type f -exec sh -c '
    [ "$(getfattr --only-values -n user.generated "$1" 2>/dev/null)" = true ]
' find-sh {} \; -print

Notes:

  • Add -delete if you want.

  • find-sh is explained here: What is the second sh in sh -c 'some shell code' sh?

  • You tagged linux, macos, posix. My expertise is in Linux, I know nothing about macOS. Extended attributes are certainly beyond POSIX.

  • For extended attributes to work, the filesystem must support them. Not all filesystems do.

  • Extended attributes may be lost when a file is moved to another filesystem or copied. GNU cp with right options is able to copy them (see man 1 cp where it mentions xattr).

  • There are other tools that can be used instead of setfattr/getfattr. At least attr and xattr. Examples:

    attr -qg generated filename.go
    xattr -p user.generated filename.go
    

    See man 1 attr and man 1 xattr for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .