Listing Files
The
ls
command is used to list the contents of a directory. You've already seen it used a few times before in examples, but this page will help ensure you are comfortable with its use.ls [OPTIONS] [FILE]
By default, when the
ls
command is used with no options or arguments, it will list the files in the current directory:sysadmin@localhost:~$ ls Desktop Documents Downloads Music Pictures Public Templates Videos
To learn the details about a file, such as the type of file, the permissions, ownerships or the timestamp, perform a long listing using the
-l
option to the ls
command. Below, a listing of the /var/log
directory is used as an example, since it provides a variety of output: sysadmin@localhost:~$ ls -l /var/log/ total 832 -rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2 drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt -rw-r----- 1 syslog adm 380 Jul 28 03:45 auth.log -rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log -rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp -rw-r----- 1 syslog adm 324 Jul 28 03:45 cron.log -rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg -rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log -rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog drwxr-xr-x 2 root root 4096 Jun 30 06:53 fsck -rw-r----- 1 syslog adm 106 Jul 28 03:45 kern.log -rw-rw-r-- 1 root utmp 292584 Jul 28 03:45 lastlog -rw-r----- 1 syslog adm 18703 Jul 28 03:46 syslog drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart -rw-rw-r-- 1 root utmp 384 Jul 28 03:45 wtmp
Each line corresponds to a file contained within the directory. The information can be broken down into fields separated by spaces. The fields are as follows:
- File Type
-rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
The first field actually contains ten characters, where the first character indicates the type of file and the next nine specify permissions. The file types are:Symbol File Type Description d
directory A file used to store other files. -
regular file Includes readable files, images files, binary files, and compressed files. l
symbolic link Points to another file. s
socket Allows for communication between processes. p
pipe Allows for communication between processes. b
block file Used to communicate with hardware. c
character file Used to communicate with hardware. The first filealternatives.log
is a regular file-
, while the second fileapache2
is a directoryd
.Permissionsdrwxr-xr-x 1 root root 0 Apr 11 21:58 upstart
Permissions indicate how certain users can access a file. Keep reading to learn more about permissions. - Hard Link Count
-rw-r----- 1 syslog adm 23621 Aug 23 15:17 auth.log
This number indicates how many hard links point to this file. Hard links are beyond the scope of this module, but are covered in the NDG Linux Essentials course. - User Owner
-rw-r----- 1 syslog adm 416 Aug 22 15:43 kern.log
Usersyslog
owns this file. Every time a file is created, the ownership is automatically assigned to the user who created it. - Group Owner
-rw-rw-r-- 1 root utmp 292584 Aug 20 18:44 lastlog
Indicates which group owns this file - File Size
-rw-r----- 1 syslog adm 1087150 Aug 23 15:17 syslog.1
The size of the file in bytes. In the case of a directory, it might actually be a multiple of the block size used for the file system. - Timestamp
drwxr-xr-x 1 root root 32 Jul 17 03:36 fsck
This indicates the time that the file's contents were last modified. - Filename
-rw-r--r-- 1 root root 47816 Jul 17 03:36 bootstrap.log
The final field contains the name of the file or directory.Remember ThisIn the case of symbolic links, the link name will be displayed along with an arrow and the pathname of the original file.lrwxrwxrwx. 1 root root 22 Nov 6 2012 /etc/grub.conf -> ../boot/grub/grub.conf
Symbolic links are beyond the scope of this module, but are covered in greater detail in the NDG Linux Essentials course.
Sorting
By default the output of the
ls
command is sorted alphabetically by filename. It can sort by other methods as well.
Follow Along
The options in examples below will be combined with the
-l
option so the relevant details of the files are displayed. Notice fields corresponding to the search option.
The
-t
option will sort the files by timestamp:sysadmin@localhost:~$ ls -lt /var/log total 840 -rw-r----- 1 syslog adm 27014 Jul 28 00:10 syslog -rw-r----- 1 syslog adm 380 Jul 27 23:10 auth.log -rw-rw-r-- 1 root utmp 292584 Jul 27 23:10 lastlog -rw-rw-r-- 1 root utmp 384 Jul 27 23:10 wtmp -rw-r----- 1 syslog adm 324 Jul 27 23:10 cron.log -rw-r----- 1 syslog adm 106 Jul 27 23:10 kern.log drwxr-xr-x 2 root root 4096 Jun 30 06:56 fsck -rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log -rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg -rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog -rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2 drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt -rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log -rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
The
-S
option will sort the files by file size:sysadmin@localhost:~$ ls -l -S /var/log total 840 -rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log -rw-rw-r-- 1 root utmp 292584 Jul 27 23:10 lastlog -rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg -rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log -rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog -rw-r----- 1 syslog adm 27014 Jul 28 00:10 syslog -rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2 drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt drwxr-xr-x 2 root root 4096 Jun 30 06:56 fsck drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart -rw-rw-r-- 1 root utmp 384 Jul 27 23:10 wtmp -rw-r----- 1 syslog adm 380 Jul 27 23:10 auth.log -rw-r----- 1 syslog adm 324 Jul 27 23:10 cron.log -rw-r----- 1 syslog adm 106 Jul 27 23:10 kern.log -rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp
The
-r
option will reverse the order of any type of sort. Notice the difference when it is added to the previous example:sysadmin@localhost:~$ ls -lSr /var/log total 840 -rw-rw---- 5 root utmp 0 Mar 2 23:10 btmp -rw-r----- 1 syslog adm 106 Jul 27 23:10 kern.log -rw-r----- 1 syslog adm 324 Jul 27 23:10 cron.log -rw-r----- 1 syslog adm 380 Jul 27 23:10 auth.log -rw-rw-r-- 1 root utmp 384 Jul 27 23:10 wtmp drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart drwxr-xr-x 2 root root 4096 Jun 30 06:56 fsck drwxr-xr-x 2 root root 4096 Mar 14 17:45 apt drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2 -rw-r--r-- 1 root root 17869 Mar 14 17:48 alternatives.log -rw-r----- 1 syslog adm 27014 Jul 28 00:10 syslog -rw-r--r-- 1 root root 32064 Mar 14 17:48 faillog -rw-r--r-- 5 root root 47816 Mar 2 23:10 bootstrap.log -rw-r----- 1 root adm 85083 Mar 14 17:48 dmesg -rw-rw-r-- 1 root utmp 292584 Jul 27 23:10 lastlog -rw-r--r-- 1 root root 315196 Mar 14 17:48 dpkg.log
The numbers in file size field switch from descending to ascending.
Used alone the
-r
option with list the files in reverse alphabetical order:sysadmin@localhost:~$ ls -r /var/log wtmp lastlog faillog cron.log auth.log alternatives.log upstart kern.log dpkg.log btmp apt syslog fsck dmesg bootstrap.log
Comments
Post a Comment