Home Miscellaneous How to find total size of all files under the ownership of a user?

How to find total size of all files under the ownership of a user?

by SupportPRO Admin

Consider a domain hosted in a server. Let us say a user test has got all the privileges for managing the domain (i.e. all files related to the domain is present under the home directory of the user:: ~test) . The user test may create several temp files while running various processes (associated with apache) in the/tmp directory of the server. In order to find the size of all files (individual file size +total size) created by the user in the /tmp directory we can use the command

find /tmp/ -user test -type f -exec du -ch {} +

Clarification

Commands used

find # to find files in file system

du # Disc Usage, report the amount of disk space used by the specified files and for each subdirectory

du, options used

-c # print grant total after all arguments have been processed)

-h # output in human readable format, ie size in K(kilobyte) or M (megabyte) instead of in bytes

Command Syntax

Find command with options -user -type and -exec

find /path/to/directory/ -user <uname> -type< filetype> -exec [command] {} +

-user uname # this option is used with find to pull out all the files owned by a specific user:: replace uname with the name of the user

-user test # to pull up all files owned by user type

-type filetype # specify the type of file

filetype maybe replaced with

f for regular file

d for directory

l for symbolic link

b for block special device file and

c character special device file

-type f # used to specify that only files of type regular file should be pulled up (directories are excluded)

-exec command {} +

-exec is used to run the specified command on the selected files (in this case the files in the output of find command)

The command field in -exec command {} + maybe replaced by any linux command which can work with files

{} (in this case) specifies that the input of the command comes from the output of the -exec option

+ is part of the exec syntax

; is normally used with exec in most cases instead of + , but in this case the use of ; wont output the total size, i.e. if the option -exec du -ch \; ( \ is used to escape out ; ) is given instead of option -exec du -ch + it wont give the total size of all the files found using the find command

This occurs since, ; when used with -exec will cause execution of the command du -ch separately for each file found using the find command. Thus the total size with du -ch will mean the size of each file instead of the size of all three

Let us say the output of the find command gives 3 files a, b and c; then

-exec du -ch {} \; # after find will mean

du -ch a

du -ch b

du -ch c

but if

-exec du -ch {} + # is used then the command will execute as follows

du -ch a b c # this will give the individual size of files a b and c along with the total size of all three at the bottom.

Example::

Finding the size (total and individual file size) of all files owned by user mobile in folder /var/mobile/balu

root@Balu~#find/var/mobile/balu/-usermobile-typef-execdu-ch{}+
496K /var/mobile/balu/books/GhostStory-JimButcher.epub
1.4M /var/mobile/balu/books/PatrickRothfuss-KingkillerChronicle02-TheWiseMan’sFear.epub
1.3M /var/mobile/balu/books/RichardAKnaak-Wolfheart.epub
648K /var/mobile/balu/books/Stackpole,MichaelA-TalionRevenant.epub
104K /var/mobile/balu/deb/com.chronic-dev.greenpois0n.corona_1.0-8.deb
8.0K /var/mobile/balu/deb/cowsay_1.1.deb
3.0M /var/mobile/balu/deb/eu.heinet.ifile_1.6.1-2.deb
8.0K /var/mobile/balu/info/arguments.txt
8.0K /var/mobile/balu/info/balu.txt
8.0K /var/mobile/balu/info/dont_delete
8.0K /var/mobile/balu/info/theos_installation
293M /var/mobile/balu/ipa/AralonSwordandShadowHD(v3.9os30).ipa
4.3M /var/mobile/balu/sng/Ninne-VineethSreenivasan.mp3
6.7M /var/mobile/balu/sng/SonicAdventure-OpenYourHeart.mp3
311M total

If you require help, contact SupportPRO Server Admin

Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value

Leave a Comment