|
|
|
|
UGU: Unix Guru Universe - Unix Tip #4642- November 26, 2023
- Home : Help
: Today's Tip
Unix Tip #4642- November 26, 2023
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
4641
- CHANGING OWNERS REGULARLY
4640
- CLEANING DIRECTORIES
4639
- REMOVING BLANK LINES
4638
- SORT BY FILE SIZE
4637
- 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)
|
|
|
Copyright 1994-2024 Unix Guru Universe |
|
|
|
|