Wednesday, December 22, 2010

Finding total size of files owned by a particular user

Syntax:

# find <pathname> -user <username> -ls | awk '{sum += $7} END {printf "Total size: %8.4f MB\n", sum/1024/1024}'

Example:
Foldername to search: /nasllm-ih
Username:  rram

[root@hostxyz ~]# find /nasllm-ih -user rram -ls | awk '{sum += $7} END {printf "Total size: %8.4f MB\n", sum/1024/1024}'                   
Total size: 958.7282 MB
[root@hostxyz ~]#


Validation:

Here I have created a folder /root/ashok consists files owned by user “adevaraju” of total size: 160 MB under its various sub-directories.

[root@hostxyz ~]# ls -lRh /root/ashok
/root/ashok:
total 21M
drwxr-xr-x 3 adevaraju root 4.0K Dec 14 06:30 d1
drwxr-xr-x 2 adevaraju root 4.0K Dec 14 06:35 d2
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:21 file1
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file4

/root/ashok/d1:
total 21M
drwxr-xr-x 2 adevaraju root 4.0K Dec 14 06:35 dx
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file2
-rw-r--r-- 1 adevaraju root  10M Dec 14 06:22 file3

/root/ashok/d1/dx:
total 21M
-rw-r--r-- 1 adevaraju root 10M Dec 14 06:30 file5
-rw-r--r-- 1 adevaraju root 10M Dec 14 06:30 file6

/root/ashok/d2:
total 101M
-rw-r--r-- 1 adevaraju root 100M Dec 14 06:30 file7
[root@oralsb11-new ~]#
 [root@oralsb11-new ~]# du -sh ashok/
161M    ashok/

[root@hostxyz ~]# find /root/ashok -user adevaraju -ls | awk '{sum += $7} END {printf "SUM: %8.4f MB\n", sum/1024/1024}'
SUM: 160.0156 MB
[root@hostxyz ~]#


PS: By replacing the search folder to '/', we can find the total size of files owned by a specified user in the entire system.

No comments:

Post a Comment