Linux File and File Descriptor Information: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 7: Line 7:
* [[Ls#ls_and_File_Time_Information|<tt>ls</tt> and File Time Information]]
* [[Ls#ls_and_File_Time_Information|<tt>ls</tt> and File Time Information]]


=File Descriptors Overview=
=File Size=
==File Size with <tt>wc</tt>==
<syntaxhighlight lang='bash'>
wc -c <path-to-file> | awk '{print $1}'
</syntaxhighlight>
 
==File Size with <tt>stat</tt>==
<syntaxhighlight lang='bash'>
stat -c %s "/etc/passwd"
stat --format=%s "/etc/passwd"
</syntaxhighlight>
On Mac:
<syntaxhighlight lang='bash'>
stat -f %z "/etc/passwd"
</syntaxhighlight>
 
==File Size with <tt>du</tt>==
<syntaxhighlight lang='bash'>
du --apparent-size --block-size=1  "/etc/passwd"
</syntaxhighlight>
 
==File Size with <tt> find </tt>==
<syntaxhighlight lang="bash">
find "/etc/passwd" -printf "%s"
</syntaxhighlight>
 
=File Descriptors=


There's a ''per-process'' maximum number of allowed [[Linux_General_Concepts#File_Descriptor|file descriptors]] - that can be set on a per-user basis - and a ''per-system'' limit.  
There's a ''per-process'' maximum number of allowed [[Linux_General_Concepts#File_Descriptor|file descriptors]] - that can be set on a per-user basis - and a ''per-system'' limit.  

Latest revision as of 03:01, 25 December 2020

External

Internal

File Size

File Size with wc

wc -c <path-to-file> | awk '{print $1}'

File Size with stat

stat -c %s "/etc/passwd"
stat --format=%s "/etc/passwd"

On Mac:

stat -f %z "/etc/passwd"

File Size with du

du --apparent-size --block-size=1  "/etc/passwd"

File Size with find

find "/etc/passwd" -printf "%s"

File Descriptors

There's a per-process maximum number of allowed file descriptors - that can be set on a per-user basis - and a per-system limit.

For more information about file descriptors see:

Unix File Descriptors

Number of Allowed File Descriptors

Allowed File Descriptors Per System

    cat /proc/sys/fs/file-max 

To change it:

    echo "200000" > /proc/sys/fs/file-max 

or set the following in /etc/sysctl.conf:

fs.file-max = 200000

Allowed File Descriptors Per Process

    ulimit -n

To change it: ulimit - Set a Value.

More on ulimit:

ulimit

Number of Used File Descriptors

Used File Descriptors per System

    cat /proc/sys/fs/file-nr

The results show, in order: total allocated open file descriptor, total free allocated file descriptors, maximum.

Used File Descriptors per Process

The number of opened file descriptors per process can be inferred by listing the content of /proc/<pid>/fd directory. This directory contains links representing the open file descriptors of the process in question and the files they represent:

ls -l /proc/<pid>/fd
total 0
lr-x------. 1 vagrant vagrant 64 May  3 09:45 0 -> /dev/null
lrwx------. 1 vagrant vagrant 64 May  3 09:45 1 -> /dev/pts/0 (deleted)
...

The number of open file descriptors can be obtained with:

ls -l /proc/<pid>/fd | wc -l

and subtract 1 from it (discard "total 0" line).

Information about a specific open file is provided in the /proc/<pid>/fdinfo/<fd> files. A typical fdinfo file contains a "pos" field, which represents the current offset of the opened file in decimal form and "flags" which is the octal O_xxx mask the file has been created with.

The contents of /proc/<pid>/fd and /proc/<pid>/fdinfo are in sync.

Open Files

All files opened on the system can be obtained with:

lsof

All files opened by one process can be obtained with:

lsof -p <pid>

Note that the output of lsof includes all information that can be obtained by listing the /proc/<pid>/fd directory, and also other files that have a special significance for the process and are not associated with file descriptors, such as the current working directory, the root directory, memory mapped files, etc.

More about lsof:

lsof