0

I tried using tree command but I didn't know how .(I wanted to use tree because I don't want the files to show up , just the number) Let's say c is the code for permission For example I want to know how many files are there with the permission 751

Eya
  • 3
  • 2

2 Answers2

0

Use find with the -perm flag, which only matches files with the specified permission bits.

For example, if you have the octal in $c, then run

find . -perm $c

The usual find options apply—if you only want to find files at the current level without recursing into directories, run

find . -maxdepth 1 -perm $c

To find the number of matching files, make find print a dot for every file and use wc to count the number of dots. (wc -l will not work with more exotic filenames with newlines as @BenjaminW. has pointed out in the comments. Source of idea of using wc -c is this answer.)

find . -maxdepth 1 -perm $c -printf '.' | wc -c

This will show the number of files without showing the files themselves.

CH.
  • 556
  • 1
  • 5
  • 16
  • 1
    To make this more robust for exotic filenames (containing a newline, for example), check out https://stackoverflow.com/a/50009079/3266847 – Benjamin W. Dec 17 '20 at 18:58
  • @BenjaminW. thanks, I'll add that to the answer. – CH. Dec 17 '20 at 18:59
  • 1
    Okay this worked , and I definitely needed -maxdepth 1 too . Thanks a lot . – Eya Dec 18 '20 at 11:23
  • If it works, please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. – CH. Dec 18 '20 at 11:29
0

If you're using zsh as your shell, you can do it natively without any external programs:

setopt EXTENDED_GLOB # Just in case it's not already set
c=0751
files=( **/*(#qf$c) )
echo "${#files[@]} files found"

will count all files in the current working directory and subdirectories with those permissions (And gives you all the names in an array in case you want to do something with them later). Read more about zsh glob qualifiers in the documentation.

Shawn
  • 47,241
  • 3
  • 26
  • 60