Unix Tip #4311- December 30, 2012


SEARCH : Home : Help : Today's Tip

METAVALUES FROM A SHELL SCRIPT


Getting a file's size, link count or other metavalue in a shell script:

When you want to extract a specific piece of meta information about a
specific file (such as it's size, link count, or one of its time stamps)
you can use the find -printf option and any of the various
%-directives listed in the find(1) man page. Example:

size=$(find some_file -printf "%s" )

(under ksh, bash, and similar shells). Here the $() form
is used as a more readable equivalent to the older `` (back
tick) operator. (Another advantage to the $() is that it
is nestable). To get the user name of the owner of a
file "foo" you could use:

set owner = `find foo -maxdepth 0 -printf "%u"`

(here we're using the csh syntax). This also uses
the -maxdepth option in case "foo" is actually a
directory name; since we don't want find to
spend time traversing directory and printing the
owners to ALL of the entries thereunder. A
maxdepth of zero ensures that this will only print
the detail we want on the specific link that we
named on the command line.

This can be much more flexible than the options provided
by the test command (try to see which of two files is
larger, or if to files are owned by the same user or
assigned to the same group, using just test).



This tip generously supported by: jdennis@linuxcare.com


NOTE: All tips provided are USE AT YOUR OWN RISK. Tips are submitted by various unix admins around the globe. UGU suggest you read and test each tip in a non-volitile environment before placing into production.


LAST 5 TIPS
4310 - CHANGING OWNERS REGULARLY
4309 - CLEANING DIRECTORIES
4308 - REMOVING BLANK LINES
4307 - SORT BY FILE SIZE
4306 - ZERO THOSE LOG FILES


I want to SUBSCRIBE and get a UGU Tip everyday.
I want to UNSUBSCRIBE and NOT get a UGU Tip everyday.

If you have a UNIX TIP let us know, we just may use it:
(All tips become the property of the Unix Guru Universe)
Email Address:

Yes, email me a Hot Unix Tip everday.

Enter Hot Unix Tip (optional):

Yes, I will support this tip

Captcha (not case sensitive):


Please enter the above letters:


HOME | Flavors | Admin | Network | Security | S/W | Help | Events | Vendors | Careers | Internet
About | Add Link | Feedback | Search

Copyright © 1994-2005 Unix Guru Universe