Linux - List files filtered by owner and permissions
This is quite an interesting and common question in linux:
List all the files in a folder that are owned by a the user "trinh", with permissions "-rw-r--r--"
1. The easiest way is to use ls command and chain filter with grep:
ls -lt . | grep trinh | grep '\-rw\-r\-\-r\-\-'
2. But, if you don't want to escape these permission characters, you can use awk to translate them into number. So, we will translate "-rw-r--r--" as following:
+ "-" : the first character indicates whether it is a file or a directory
+ "rw-" : the next 3 characters is the owner's right, can be translated to 110 which means 6 in decimal system.
+ "r--": next 3 characters is the group's right, is 100 in binary and 4 in decimal.
+ "r--": the last 3 characters is the other users's right, 100 in binary, and 4 in decimal.
So, "-rw-r--r--" is 644 in decimal system.
ls -lt . | grep trinh | 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}' | grep 644
3. And even more complicated, get the files inside every directory and sub-directory of the current location with permission 644:
ls -lt . | grep trinh | find -perm 644
List all the files in a folder that are owned by a the user "trinh", with permissions "-rw-r--r--"
1. The easiest way is to use ls command and chain filter with grep:
ls -lt . | grep trinh | grep '\-rw\-r\-\-r\-\-'
2. But, if you don't want to escape these permission characters, you can use awk to translate them into number. So, we will translate "-rw-r--r--" as following:
+ "-" : the first character indicates whether it is a file or a directory
So, "-rw-r--r--" is 644 in decimal system.
ls -lt . | grep
ls -lt . | grep trinh
Comments
Post a Comment