1

Is it possible to count the files of a directory by their permission? I have tried the following but 1)it does not have a counter 2)it doesnt find the files

echo "The number of the files of the owner"
find . -user $(whoami) -perm -006

echo "only for a group"
find . -user $(whoami) -perm -005

echo "noones permission"
ls -a | grep "^\."`
dantsi
  • 67
  • 1
  • 9

3 Answers3

4

Print permission for each file and then sort and count.

$ find . -exec stat -c %a {} + | sort | uniq -c
      67 444
      32 644
     121 755

Let's make it more readable!

$ find . -exec stat -c %A {} + | sort | uniq -c |
> awk '{print "There are "$1" "($2 ~ /^d/?"directories":"files")" with "substr($2,2)" permission."}'
There are 67 files with r--r--r-- permission.
There are 32 files with rw-r--r-- permission.
There are 26 files with rwxr-xr-x permission.
There are 95 directories with rwxr-xr-x permission.
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • it gives me this output 2 500 1 511 1 600 1 660 8 664 is it this? – dantsi Jun 30 '21 at 08:22
  • It's not clear what "Is it this?" really means. The output you got tells you that you have two files whose permissions are 500 (readable and exeutable for owner, no permissions for others), one whose permissions are 511 (executable for everyone, otherwise like previously), one whose permissions are 600 (readable and writable for owner), etc. Perhaps notice that without `-type f` some of the entries will represent directories (the 5xx ones probably are directories, for example) – tripleee Jun 30 '21 at 08:35
0

Just use wc -l
wc for word count, with the -l for count lines flag.

echo "The number of the files of the owner"
find . -user $(whoami) -perm -006 | wc -l 
FMa
  • 88
  • 7
0

You could achieve this by using awk language

ls -la | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/) \
                 *2^(8-i));if(k)printf("%0o ",k);print}' | awk 'NR>1{ arr[$1]++ } END { for (a in arr) print a, arr[a]}' 

to understand this refer to below links, as I just merge the solutions there to get what you want.

Awk/Unix group by

Can the Unix list command 'ls' output numerical chmod permissions?

Diya'
  • 78
  • 6
  • [Don't parse `ls` output.](https://mywiki.wooledge.org/ParsingLs) Piping Awk to Awk is often inefficient, and silly. – tripleee Jun 30 '21 at 08:37
  • @tripleee can you tell more about this, why it's silly ? it's been 1 week since I started learning/using awk. – Diya' Jun 30 '21 at 08:41
  • You should easily be able to make the first Awk script produce the output you want. Just add things to the associative array instead of printing, and then include the `END` block from the second script more or less verbatim. – tripleee Jun 30 '21 at 08:42