Ls: 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:
List files in a directory.
List files in a directory.


=<tt>-d</tt>=
=Options=
 
==-d==


Lists directories themselves, not their contents.
Lists directories themselves, not their contents.
==-i==
List the [[Linux_7_Storage_Concepts#inode|inode]] number.


=<tt>ls</tt> and File Time Information=
=<tt>ls</tt> and File Time Information=
Line 39: Line 45:
     ls -lu [--full-time] <my-file>
     ls -lu [--full-time] <my-file>
</pre>
</pre>
=Order files in the Descending Order of Last Changed Time=
<pre>
ls -lt *
</pre>
To list just the file name without additional information:
<pre>
ls -t *
</pre>
The content of the directories is display. To avoid that, use -d.


=Syntax Coloring=
=Syntax Coloring=
Line 57: Line 77:


@ at the end of the ls attribute output means the file has extend attributes. They can be listed with ls -l@ or xattr -l  
@ at the end of the ls attribute output means the file has extend attributes. They can be listed with ls -l@ or xattr -l  
=Order files in the Descending Order of Last Changed Time=
<pre>
ls -lt *
</pre>


=Generate a Comma-Separated List with the Files and Sub-Directories=
=Generate a Comma-Separated List with the Files and Sub-Directories=

Latest revision as of 20:22, 3 April 2018

Internal

Overview

List files in a directory.

Options

-d

Lists directories themselves, not their contents.

-i

List the inode number.

ls and File Time Information

Intro

Under POSIX, each file has three distinct associated timestamps:

  • the time of last data access (access time, atime) - this is the time the file was last read. Can be read with ls -lu.
  • the time of last data modification (data modification time, mtime) - this is the time when the file data was modified.
  • the time the file status last changed ('status change time', ctime) - this is the time when the inode associated with the file changes. Every time mtime changes, ctime changes. However, ctime changes few extra times, for exmaple when you change the owner or the permission of the file. Can be read with ls -l (which is equivalent with ls -lc).

These values are returned in the file characteristics structure struct stat.

By default, ls lists last modification time (ctime).

Note that if the filesystem was mounted with "noatime" for performance reasons, then the atime will likely show the creation time. Given that "noatime" results in a massive performance boost (by removing a disk write for every time a file is read), it may be a sensible configuration option.

ls Time Commands

By default ls list the last status change time ctime (reflecting both data modification or metadata changes). It is equivalent with

    ls -lc [--full-time] <my-file>

If we want it to list access time, use:

    ls -lu [--full-time] <my-file>

Order files in the Descending Order of Last Changed Time

ls -lt *

To list just the file name without additional information:

ls -t *

The content of the directories is display. To avoid that, use -d.

Syntax Coloring

-G

It is equivalent with defining CLICOLOR in the environment (for Mac).

export CLICOLOR=cons25

LSCOLOR can provide more details on colors.

ls on Mac

@ at the end of the ls attribute output means the file has extend attributes. They can be listed with ls -l@ or xattr -l

Generate a Comma-Separated List with the Files and Sub-Directories

ls -m

This option can be used to reliably determine whether a directory is empty - the result will be the empty string.

Is the Directory Empty?

if [ -z "$(ls -m /tmp)" ]; then
    echo "directory is empty"
fi