left-icon

Linux Succinctly®
by Jason Cannon

Previous
Chapter

of
A
A
A

CHAPTER 5

Viewing File and Directory Details

Viewing File and Directory Details


The ls command was briefly introduced in Chapter 3. It not only lists files and directories, it can provide important details about those files and directories. One of the most common options to use with ls is -l, which displays a long listing format. The following is an example.

$ ls

Desktop  Documents  Downloads  Music  to-do.txt

$ ls -l

total 20

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

$

The information provided by ls -l starts with a series of characters that represent the permissions of the file or directory. Permissions will be covered in Chapter 6. The number that follows the permissions string represents the number of links to the file or directory. Next, the owner is displayed followed by the group name. The file size is then displayed. The timestamp provided represents the modification time. The last item is the name of the file or directory itself.

$ ls -l to-do.txt

-rw-r--r-- 1 jason users 73 Jun 22 19:34 to-do.txt

---------- - ----- ----- -- ------------ ---------

     |     |   |     |    |       |          |

     |     |   |     |    |       |      File Name

     |     |   |     |    |       |

     |     |   |     |    |       +----- Modification Time

     |     |   |     |    |      

     |     |   |     |    +------------- Size in bytes

     |     |   |     |   

     |     |   |     +------------------ Group

     |     |   |

     |     |   +------------------------ User (owner)

     |     |

     |     +---------------------------- Number of Links

     |

     +---------------------------------- Permissions

By default, ls does not display files or directories that begin with a period. In Linux, such files are considered hidden. To display hidden files with ls, use the -a option to include all items. To display all items in a long listing format, use -l and -a. Remember that the options can be combined. These three commands are equivalent: ls -l -a, ls -la, and ls -al.

$ ls

Desktop  Documents  Downloads  Music  to-do.txt

$ ls -a

.  ..  .bash_history  .bash_logout  .bashrc  Desktop  Documents  Downloads  Music  .profile  .ssh  to-do.txt

$ ls -a -l

total 48

drwxr-xr-x 7 jason users 4096 Jun 22 20:36 .

drwxr-xr-x 6 root  root  4096 May  4 10:26 ..

-rw------- 1 jason users 3738 Jun 22 19:37 .bash_history

-rw-r--r-- 1 jason users  220 Mar 30  2013 .bash_logout

-rw-r--r-- 1 jason users 3650 Jun 22 19:41 .bashrc

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music

-rw-r--r-- 1 jason users  675 Mar 30  2013 .profile

drwx------ 2 jason users 4096 May  3 12:44 .ssh

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

$ ls -al

total 48

drwxr-xr-x 7 jason users 4096 Jun 22 20:36 .

drwxr-xr-x 6 root  root  4096 May  4 10:26 ..

-rw------- 1 jason users 3738 Jun 22 19:37 .bash_history

-rw-r--r-- 1 jason users  220 Mar 30  2013 .bash_logout

-rw-r--r-- 1 jason users 3650 Jun 22 19:41 .bashrc

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music

-rw-r--r-- 1 jason users  675 Mar 30  2013 .profile

drwx------ 2 jason users 4096 May  3 12:44 .ssh

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

$ ls -la

total 48

drwxr-xr-x 7 jason users 4096 Jun 22 20:36 .

drwxr-xr-x 6 root  root  4096 May  4 10:26 ..

-rw------- 1 jason users 3738 Jun 22 19:37 .bash_history

-rw-r--r-- 1 jason users  220 Mar 30  2013 .bash_logout

-rw-r--r-- 1 jason users 3650 Jun 22 19:41 .bashrc

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music

-rw-r--r-- 1 jason users  675 Mar 30  2013 .profile

drwx------ 2 jason users 4096 May  3 12:44 .ssh

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

To append a file type indicator to the name of the file or directory in the ls output, use the -F option.

$ ls

Desktop  Documents  Downloads  link-to-to-do  Music  program  to-do.txt

$ ls -F

Desktop/  Documents/  Downloads/  link-to-to-do@  Music/  program*  to-do.txt

$ ls -lF

total 24

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop/

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents/

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads/

lrwxrwxrwx 1 jason users    9 Jun 22 21:01 link-to-to-do -> to-do.txt

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music/

-rwxr-xr-x 1 jason users   13 Jun 22 21:02 program*

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

$

  1. File Type Indicators

File Type

Symbol

Directory

/

Symlink. The file that follows the -> symbol is the target of the link.

@

Executable script or program

*

Socket

=

Door

>

Named pipe

|

A symbolic link, sometimes called a symlink or just link, points to the location of an actual file or directory. The symlink is just a pointer, but you can operate on it as if it were the actual file or directory. Symbolic links are often used to create shortcuts to long names or long paths. Another common use for symlinks is to point to the current version of an application as in the following example.

$ cd /opt/nginx/

$ ls -F

1.6.0/  1.7.1/  1.7.2/  current@

$ ls -lF

total 12

drwxr-xr-x 2 root root 4096 Jun 22 21:12 1.6.0/

drwxr-xr-x 2 root root 4096 Jun 22 21:11 1.7.1/

drwxr-xr-x 2 root root 4096 Jun 22 21:11 1.7.2/

lrwxrwxrwx 1 root root    5 Jun 22 21:12 current -> 1.7.2/

$

To sort the output of the ls command by time, use the -t option. This displays the most recently modified items first. If you want to reverse the order, use -r. This can come in handy when you have a directory that contains many files. When you sort them by time in reverse the old files will scroll off the top of your screen, while the newest files will be displayed right above your prompt.

$ ls -t

program  link-to-to-do  to-do.txt  Music  Downloads  Documents  Desktop

$ ls -lt

total 24

-rwxr-xr-x 1 jason users   13 Jun 22 21:02 program

lrwxrwxrwx 1 jason users    9 Jun 22 21:01 link-to-to-do -> to-do.txt

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop

$ ls -lrt

total 24

drwxrwxr-x 2 jason users 4096 May  3 08:33 Desktop

drwxrwxr-x 2 jason users 4096 May  3 08:35 Documents

drwxrwxr-x 2 jason users 4096 May  3 08:38 Downloads

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music

-rw-r--r-- 1 jason users   73 Jun 22 19:34 to-do.txt

lrwxrwxrwx 1 jason users    9 Jun 22 21:01 link-to-to-do -> to-do.txt

-rwxr-xr-x 1 jason users   13 Jun 22 21:02 program

$

To perform a recursive listing, use the -R option.

$ ls -R

.:

Desktop  Documents  Downloads  link-to-to-do  Music  program  to-do.txt

./Desktop:

./Documents:

cat.jpg  report.txt

./Downloads:

./Music:

JohnColtrane

./Music/JohnColtrane:

giant-steps.mp3

$

To accomplish the same goal, but in a more visually appealing way, use the tree command. To view only the directory structure, use tree -d. For colorized output, use tree -C. The tree command is not always installed by default so you may have to rely on the ls command.

$ tree

.

|-- Desktop

|-- Documents

|   |-- cat.jpg

|   |-- report.txt

|-- Downloads

|-- link-to-to-do -> to-do.txt

|-- Music

|   |-- JohnColtrane

|       |-- giant-steps.mp3

|-- program

|-- to-do.txt

5 directories, 6 files

$ tree -d

.

|-- Desktop

|-- Documents

|-- Downloads

|-- Music

    |-- JohnColtrane

5 directories

$

When the ls command is run against a directory, the contents of the directory are displayed. To have ls operate on just the directory, use the -d option.

$ tree Music/

Music/

|-- JohnColtrane

    |-- giant-steps.mp3

1 directory, 1 file

$ ls Music/

JohnColtrane

$ ls -l Music/

total 4

drwxrwxr-x 2 jason users 4096 Jun 22 21:39 JohnColtrane

$ ls -d Music/

Music/

$ ls -ld Music/

drwxrwxr-x 3 jason users 4096 Jun 21 21:16 Music/

$

To colorize the output of the ls command, use the --color option. Much like the -F option, this option allows for the differentiation of file types.

$ ls --color

Desktop  Documents  Downloads  link-to-to-do  Music  program  to-do.txt

The following is a recap of the ls options covered in this chapter. Even though ls has many more options, these few will cover the most common use cases.

  1. Commonly Used ls Options

Description

Option

Display all files, including hidden files.

-a

Colorize output.

--color

List directories and not their contents.

-d

Use the long listing format.

-l

Reverse the order.

-r

List files recursively.

-R

Sort by time.

-t

Escaping Spaces and Special Characters

Even though spaces are permitted in file and directory names, it can be easier to avoid them if possible. Instead of using spaces consider using hyphens or underscores. Another good option is to use CamelCase. For example, instead of naming a file my to do list, name it my-to-do-list, my_to_do_list, or even MyToDoList.

Even if you choose to avoid using spaces in file names, you may encounter file names created by others that do include spaces. The two ways of operating on files with spaces in their names is to use quotation marks or escaping. To operate on a file named my to do list, enclose it in quotation marks like so: "my to do list". To escape the file name, precede the spaces with a backslash like so: my\ to\ do\ list. Escaping is like using quotation marks except that it is for single characters.

If you are unsure how to escape a file or directory name, let ls show you by using the -b option. Quoting and escaping not only applies to space, but to other special characters including |, &, ', ;, (, ), <, >, space, and tab.

$ ls

my to do list

$ ls -l

total 4

-rw-r--r-- 1 jason users 73 Jun 22 22:16 my to do list

$ ls -l my to do list

ls: cannot access my: No such file or directory

ls: cannot access to: No such file or directory

ls: cannot access do: No such file or directory

ls: cannot access list: No such file or directory

$ ls -l "my to do list"

-rw-r--r-- 1 jason users 73 Jun 22 22:16 my to do list

$ ls -l my\ to\ do\ list

-rw-r--r-- 1 jason users 73 Jun 22 22:16 my to do list

$ ls -lb

total 4

-rw-r--r-- 1 jason users 73 Jun 22 22:16 my\ to\ do\ list

$

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.