2.3 Using Directories and Listing Files (Weight: 2)
📘Linux Essentials (LPI 010-160)
In Linux systems, files and directories are organized in a hierarchical structure. To manage these files effectively, administrators and users must be able to view and inspect the contents of directories. Linux provides several command-line tools to list files, display detailed information about them, and reveal hidden files.
This section explains all the commands and options required to list files and directories, including hidden files, in a way that is important for the Linux Essentials (010-160) exam.
1. The ls Command
The primary command used to list files and directories in Linux is:
ls
The ls command displays the contents of a directory.
Basic syntax
ls [options] [directory]
- options – Modify how the output is displayed
- directory – The directory whose contents should be listed
If no directory is specified, the command lists the contents of the current working directory.
Example
ls
Output example:
config.txt logs scripts backup.tar
This shows files and directories located in the current directory.
2. Listing Files in Another Directory
You can specify a directory path to list files somewhere else in the filesystem.
Example:
ls /etc
This command lists configuration files located in the /etc directory.
In an IT environment, this is useful when:
- Checking configuration directories
- Inspecting system files
- Viewing application directories
3. Listing Files in Long Format (-l)
The long listing format displays detailed information about files.
Command:
ls -l
Example output:
-rw-r--r-- 1 root root 1200 Mar 5 12:10 config.txt
drwxr-xr-x 2 root root 4096 Mar 5 12:12 scripts
-rw-r--r-- 1 root root 5500 Mar 5 12:13 logs.txt
Each column provides specific information.
Breakdown of the columns
| Column | Meaning |
|---|---|
| File type and permissions | Shows file type and permission settings |
| Number of links | Number of hard links |
| Owner | User who owns the file |
| Group | Group ownership |
| Size | File size in bytes |
| Date | Last modification date |
| Filename | Name of the file |
Example:
-rw-r--r--
Meaning:
-→ regular filer→ read permissionw→ write permissionx→ execute permission
If the first character is:
| Symbol | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
4. Listing Hidden Files
Linux allows files to be hidden from normal directory listings.
A hidden file starts with a dot (.).
Example hidden files:
.bashrc
.profile
.gitconfig
These files usually store configuration settings for users, shells, or applications.
5. Viewing Hidden Files with ls -a
To display hidden files, use the -a option.
Command:
ls -a
Example output:
. .. .bashrc .profile config.txt scripts
Explanation:
| Entry | Meaning |
|---|---|
. | Current directory |
.. | Parent directory |
.bashrc | Hidden configuration file |
In Linux systems, many important configuration files are hidden to prevent accidental modification.
6. Listing Hidden Files Without . and .. (-A)
The -A option lists hidden files but does not display . and ...
Command:
ls -A
Example output:
.bashrc .profile config.txt scripts
This is often preferred when administrators want to view hidden files without clutter from directory references.
7. Combining Options
Multiple options can be combined.
Example:
ls -la
This displays:
- Detailed information (
-l) - Hidden files (
-a)
Example output:
drwxr-xr-x 3 user user 4096 Mar 5 12:10 .
drwxr-xr-x 6 user user 4096 Mar 5 11:00 ..
-rw-r--r-- 1 user user 220 Mar 5 11:30 .bashrc
-rw-r--r-- 1 user user 1200 Mar 5 12:10 config.txt
drwxr-xr-x 2 user user 4096 Mar 5 12:12 scripts
This command is commonly used in Linux administration when inspecting directories.
8. Human-Readable File Sizes (-h)
When used with -l, the -h option displays file sizes in a human-readable format.
Command:
ls -lh
Example output:
-rw-r--r-- 1 root root 1.2K Mar 5 12:10 config.txt
-rw-r--r-- 1 root root 5.5M Mar 5 12:11 backup.tar
File sizes appear in:
- KB
- MB
- GB
This makes file sizes easier to read.
9. Listing Directory Information Instead of Contents (-d)
Normally, ls lists the contents of a directory.
To display information about the directory itself, use:
ls -ld directory_name
Example:
ls -ld /etc
This shows the directory’s permissions, owner, and metadata, not the files inside it.
This is useful when checking directory permissions in server environments.
10. Sorting File Listings
The ls command can sort output in several ways.
Sort by modification time
ls -lt
Newest files appear first.
Reverse order
ls -lr
This reverses the sorting order.
Sort by file size
ls -lS
Largest files appear first.
These options help administrators quickly identify:
- recently modified files
- large files
- old files in directories
11. Recursive Listing (-R)
To list files in subdirectories recursively, use:
ls -R
Example:
ls -R /var/log
This command displays:
- files in
/var/log - files inside all subdirectories
This is helpful when reviewing entire directory structures.
12. Listing Only Directories
To display only directories:
ls -d */
Example output:
logs/
scripts/
backup/
This helps administrators quickly see directory structures.
13. Common ls Options Important for the Exam
| Option | Function |
|---|---|
ls | List directory contents |
ls -l | Long listing format |
ls -a | Show hidden files |
ls -A | Show hidden files except . and .. |
ls -lh | Human-readable file sizes |
ls -lt | Sort by modification time |
ls -lr | Reverse order |
ls -lS | Sort by file size |
ls -R | Recursive listing |
ls -ld | Show directory details |
14. Hidden Files in an IT Environment
Hidden files are commonly used to store system and application configuration settings.
Examples include:
.bashrc– Shell configuration.profile– User environment settings.gitconfig– Git version control settings.sshdirectory – SSH authentication configuration
System administrators frequently check hidden files when:
- Troubleshooting user environments
- Reviewing configuration settings
- Managing development tools
- Investigating system behavior
Because these files start with a dot (.), they remain hidden during normal directory listings to reduce clutter and prevent accidental modification.
15. Key Points to Remember for the Exam
- The
lscommand is used to list directory contents. - Without options,
lsshows visible files only. - Hidden files start with a dot (
.). - Use
ls -ato display hidden files. - Use
ls -lfor detailed file information. - Options can be combined, such as
ls -la. ls -lhdisplays human-readable file sizes.ls -Rlists files recursively through subdirectories..represents the current directory...represents the parent directory.
Understanding these commands is essential for navigating and managing Linux file systems efficiently.
