0

is it possible to search file in the linux OS that have particular extended attribute as this: ---S--l---

---S--l---  1  root    root          0 Mar  1004:25/opt/csTuner/iba/wys/tuer_lolk

What is S in the permissions:

S - the changes are written synchronously on the disk; this is equivalent to the `sync' mount option applied to a subset of the files.

My goal is to search a files that are written synchronously on the disk.

Sven
  • 98,649
  • 14
  • 180
  • 226
maihabunash
  • 443
  • 1
  • 11
  • 25

1 Answers1

7

find can't search for file attributes (this is something else then permissions!) on it's own.

One way:

find /location -type f  -print0 | xargs -0 lsattr | grep '^...S'

On my system, the S is in the third column of the lsattr output, so to make it more flexible, we can use a more complicated regular expression:

find /location -type f  -print0 | xargs -0 lsattr | grep '^[^ S]*S[^ S]* '

where grep '^[^ S]*S[^ S]* ' is supposed to find anything that has an S in the first column.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • I learnt about the `-exec command {} +` syntax while writing up my answer. It seems to be equivalent to `xargs` + pipe chaining – tucuxi Jun 11 '15 at 13:50
  • 2
    @tucuxi: `xargs` is much more effective if you have a large number of results, as `-exec` will spawn a new process for every file found, while `xargs` handles them in bulk. While this doesn't work for every possible command you might want to execute, it can make a dramatic difference in most cases when you get thousands of results. – Sven Jun 11 '15 at 13:53
  • It may be slower, but it should be safer: if the number of results is large (as in over `ARG_MAX`), then `xargs` will not be able to pass the full output of `find` to `lsattr`, and you will lose results. I have not tested this; going by http://stackoverflow.com/questions/19354870/bash-command-line-and-input-limit – tucuxi Jun 11 '15 at 16:09
  • 2
    @tucuxi Nice catch, but naturally `xargs` knows about this issue and spawns multiple processes if the command line would get too long. You still end up with a dramatically reduced number of forked processes. – Sven Jun 11 '15 at 16:12
  • Hmm - you are right. I was just editing my comment to say this. Learning a lot today... – tucuxi Jun 11 '15 at 16:13
  • 1
    Be sure to add -print0 to your find command and -0 to your xargs command so that you don't run into issues with files containing spaces in their names. – sa289 Jun 11 '15 at 23:29