List the contents of a directory using the LS command in Linux

Home > Search > Linux commands
  by

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 myFile.

~]# ls
myFile

 

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 myFile

 


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
myFile .myHiddenFile

 


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 myFile

 

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 myFile

 

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.



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments