Bootstrap FreeKB - Linux Commands - List files and directories using the ls command
Linux Commands - List files and directories using the ls command

Updated:   |  Linux Commands articles

Arguabely the most popular command in Linux, the ls (list) command can be used to view information about files and directories. The ls command without any options will list the files and directories in a directory. In this example, there is one file in the present directory named example.txt.

~]# ls
example.txt

 

The -l (long) option is commonly used to display the permissions, ownership, size, and last modified date and time of files and directories. In this example, there is one file that has -rw-r--r-- permissions, is owned by root:root, is 123 bytes, and was last modifed on September 18 at 19:02. The ll command can be used instead of ls -l.

~]# ls -l
-rw-r--r--  1  root root 123 Sep 18 19:02 example.txt

 


Show hidden files

The -a or --all option can be used to view hidden files. Files beginning with a single period are considered hidden.

~]# ls -a
example.txt .foo.txt

 


Last accessed or last modified date time

The --time option can be used to determine the last accessed or last modified date and time of a file. atime is used to determine when the file was last accessed. For example, using the cat command to read the contents of the file will update the files last accessed time and not update the files last modified time.

~]# ls -l --time=atime
-rw-r--r--  1  root root 123 Sep 18 19:04 example.txt

 

ctime is used to determine when the file was last modified. For example, redirecting Hello World to a file will update the files last modified time and not update the files last accessed time.

~]# ls -l --time=ctime
-rw-r--r--  1  root root 123 Sep 18 19:04 example.txt

 

Using an editor, such as vi or nano, and modified the contents of the file, will update both the last accessed and last modified date of a file, because the file is being accessed and modifed.

Using an editor, such as vi or nano, and not modifying the contents of the file, will only update the last accessed time of the file.

 


Sort by file size

The --sort=size or -S option can be used to sort the output to list the largest files first, like this.

~]# ls -l --sort=size
-rw-r--r--  1  root root 123 Sep 18 19:04 foo.txt
-rw-r--r--  1  root root  10 Sep 07 23:13 bar.txt

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 74e7fa in the box below so that we can be sure you are a human.