Operating System - HP-UX
1823920 Members
3275 Online
109667 Solutions
New Discussion юеВ

Re: How to get info on file attributes ? Like lenght of file

 
SOLVED
Go to solution
Jerry_109
Super Advisor

How to get info on file attributes ? Like lenght of file

HP-UX B.11.11 U 9000/800/rp3410
#########################################

Hello All,

Is there a way to determine the lenght of a file? or to get the attributes of a file ?
19 REPLIES 19
IT_2007
Honored Contributor
Solution

Re: How to get info on file attributes ? Like lenght of file

if you know the path for the file then do ll

ll /home/file1

which will show you modification date and time, size of the file and ownership etc..
Sean Dale
Trusted Contributor

Re: How to get info on file attributes ? Like lenght of file

you mean the permissions?

ls -al

when you say length, I am assuming you mean how many lines are in the file:

cat filename | wc -l

I wouldn't use cat on a binary file!

use file filename to determine filetype first
Live life everyday
James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi Jerry:

By file attributes, I presume that you mean things like the modification timestamp, the lastaccess timestamp, the last inode change timestamp, the size of the file in characters, for instance.

The 'ls' command offers the above information. Look at the manpages for 'ls' along with the manpages for 'stat(2)'. It's the 'stat' structure that holds these attributes.

If you mean the "type" or "kind" of file, use the 'file' command. The manpages for 'file' will enlighten you on how the "magic" [pun intended!] is performed to deduce whether a file is a "text" or a "binary" file for instance.

Regards!

...JRF...

A. Clay Stephenson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

One of the best ways is the Perl stat() function. It will load an array with all of the attributes of a file and you can pick and choose the ones you want. You would probably feed the timestamps returned by stat() to localtime() to convert the epoch seconds into a more user-friendly format.
If it ain't broke, I can fix that.
Sean Dale
Trusted Contributor

Re: How to get info on file attributes ? Like lenght of file

Good call, Clay! I didn't even think of that (and I use Perl for everything)!
Live life everyday
James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi (again):

In addition to using 'stat()' in a Perl script, Perl offers a plethora of file test operators to make life easy, too. These can make "asking" questions of the stat() information easy. For instance, instead of dissecting the octal mode from 'stat()' use Perl's "-u" with the file to test if the 'setuid' bit is set.

In reality, the shell 'test' operator has the same ability to determine if a file is readable, or has a size other than zero, or has the setuid bit set. See the 'test(1)' manpages.

Regards!

...JRF...
Jerry_109
Super Advisor

Re: How to get info on file attributes ? Like lenght of file

Can someone give me an example of how to use the "stat" command ?
James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi Jerry:

An example, using Perl, to report the 'mtime' (modification timestamp) of a file would be:

# perl -le 'print scalar localtime ((stat($ARGV[0]))[8])' file

...which might look like:

# touch /tmp/me
# perl -le 'print scalar localtime ((stat($ARGV[0]))[8])' /tmp/me
Wed Sep 6 18:39:51 2006

...where my TZ=EST5EDT

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi (again):

...and in keeping with our running dialog, I should add this alternative:

# perl -le '$t=-M $ARGV[0];print $ARGV[0], " is ", $t*86400, " seconds old"' file

Regards!

...JRF...
Jerry_109
Super Advisor

Re: How to get info on file attributes ? Like lenght of file

Very cool. Thanks. Now what's the command that delivers all the info on a file?

Yes, I know I need to read a perl book, but I'm in a rush. Thanks........... :)
James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi Jerry:

Mother Nature abhors rushing. However, the top of this page (URL) offers you the answer and the documentation of why for 'mtime' I should have chosen the nineth and not the eight element of 'stat()' in my original example, above :-))

http://perldoc.perl.org/functions/stat.html

Regards!

...JRF...
Jerry_109
Super Advisor

Re: How to get info on file attributes ? Like lenght of file

Thanks for the info. I located the following, whic hlooks like what I needed
" size of file, in bytes ":

7 size total size of file, in bytes
####################################

but when I entered the following :

# perl -le 'print scalar size ((stat($ARGV[0]))[7])' /tmp/foo

Undefined subroutine &main::size called at -e line 1.

Seems I just do not get it. I'll take some time to review perl. Thanks


James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi:

# perl -le 'print ((stat($ARGV[0]))[7])' /tmp/foo

...returns the size in characters.

Regards!

...JRF...
Jerry_109
Super Advisor

Re: How to get info on file attributes ? Like lenght of file

Thank you James, you'er fantastic........ :>)
Jerry_109
Super Advisor

Re: How to get info on file attributes ? Like lenght of file

I got excited too soon. The 694 is the same number I got from ls. Can a file be measured in binary lenght ?
A. Clay Stephenson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

I got excited too soon. The 694 is the same number I got from ls. Can a file be measured in binary lenght ?

I have no idea what this question means. The value representing the length of a file is in bytes and it is stored in the inode in binary format but in any event the value 694 (decimal) can be converted to whatever radix you choose including base 2 and Perl can do this as well. Now it is also possible that this lenfth doesn't mean what you think it means. Consider the case of writing a single byte at offset 0 and then doing an lseek() to offset 999,999 and writing another siggle byte and then closing the file. How big is this file? ls -l (and stat(), either the system call or the Perl function) will tell you it is 1MB but other utilities that look at the filesystem will tell you that this 1MB file is only 2 blocks (typically 1KiB) long --- and both are right --- they are simply measuring different things --- welcome to "sparse" files. When this file is read, the "missing" bytes are silently filled in with NUL's. To make it more confusing, if you copy this file, the copy will not be sparse and the file will truly be 1MB in length.

If it ain't broke, I can fix that.
Arturo Galbiati
Esteemed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi Jerry,
ti get info about a file I use this simple ksh script:
#!/usr/bin/ksh
typeset tmp=$(head -1 $1)
typeset wcr=$(wc $1)
echo "$(basename $1): Reclen=${#tmp}, Lines=$(echo $wcr|awk '{print $1}'), Size=$(echo $wcr|awk '{print $3}'), \
Type='$(echo $(file $1)|awk -F ": " '{print $2}')'"
#eof

i.e:
finfo finfo
finfo: Reclen=14, Lines=6, Size=232, Type='commands text'

Take care that teh reclen info is valid only if all the record of your file have teh same length.

If you are interested to know info about records length just use:
awk '{printf "%2d %6i\n", NR,length($0)}' finfo

i.e:
awk '{printf "%2d %6i\n", NR,length($0)}' finfo
1 14
2 25
3 20
4 112
5 51
6 4

HTH,
Art
Jerry_109
Super Advisor

Re: How to get info on file attributes ? Like lenght of file

Thank you all for the ongoing good information. I took some good notes, and learned a lot. I believe I was getting too
detailed, and it was not really needed. I now see the "ls" cmd delivers all the attributes I needed. I did learn other ways to obtain info needed. Thanks for putting up with me.

Your Friend.
Jerry Sims
James R. Ferguson
Acclaimed Contributor

Re: How to get info on file attributes ? Like lenght of file

Hi (again) Jerry:

As I originally noted, one hopes that 'ls' and 'stat()' return the same values! After all, 'stat()' is what 'ls' uses :-))

Regards!

..JRF...