List directory contents using ls -ld
In this step, you'll learn how to list the contents of a directory and view detailed information about the directory itself using the ls
command with the -l
and -d
options.
The ls
command is one of the most frequently used commands in Linux. By default, it lists the files and directories in the current directory.
The -l
option provides a "long listing" format, showing details like file permissions, ownership, size, and modification time.
The -d
option is crucial when you want to see information about the directory itself, rather than its contents. Without -d
, ls -l
would list the contents of the directory in long format. With -d
, it lists the directory entry itself.
Let's combine these options to see the details of your current directory, /home/labex/project
.
Type the following command and press Enter:
ls -ld /home/labex/project
You should see output similar to this:
drwxr-xr-x 2 labex labex 4096 <Date> <Time> /home/labex/project
Let's break down this output:
d
: The first character indicates the file type. d
means it's a directory.
rwxr-xr-x
: These characters represent the file permissions for the owner, group, and others.
2
: The number of hard links to this directory.
labex
: The owner of the directory.
labex
: The group that owns the directory.
4096
: The size of the directory in bytes.
<Date> <Time>
: The last modification date and time.
/home/labex/project
: The name of the directory.
Now, try running ls -l
without the -d
option to see the difference. If there are files or directories inside /home/labex/project
, they will be listed.
ls -l /home/labex/project
If /home/labex/project
is empty, you won't see any output from ls -l
. If it contains items, you'll see a long listing of those items.
Using ls -ld
is a quick way to check the permissions, ownership, and modification time of a specific directory without listing everything inside it.