The `ls -R` command in Unix/Linux generates a **recursive directory listing**, which means it lists files and directories within the current directory and then repeats the listing for all subdirectories and their contents, continuing down through all nested directories.
Here's how it works:
Basic Syntax:
ls -R [directory_name]
- Without specifying `[directory_name]`, `ls -R` runs recursively from your current directory.
Example: Consider the following directory structure:
myfolder/
├── file1.txt
├── dir1
│ ├── file2.txt
│ └── file3.txt
└── dir2
└── subdir1
└── file4.txt
Running the command:
ls -R myfolder
Will produce:
myfolder:
dir1 dir2 file1.txt
myfolder/dir1:
file2.txt file3.txt
myfolder/dir2:
subdir1
myfolder/dir2/subdir1:
file4.txt
Explanation:
- First, it lists the contents of the specified directory.
- Then, it moves into each subdirectory, listing its contents in turn, repeating the process recursively.
Useful Variations:
- To add more details such as file permissions, size, and modification dates, combine it with `-l`:
ls -lR
- To include hidden files (starting with `.`), add the `-a` option:
ls -aR
Common Use Cases:
- Quickly viewing the entire structure of a directory hierarchy.
- (Child element 1 - if applicable)
- (Child element 2 - if applicable)
- (Child element 3 - if applicable)
- Inspecting files nested deep within directories.
- (Child element 1 - if applicable)
- (Child element 2 - if applicable)
- (Child element 3 - if applicable)
-
Verifying file arrangements or directory layouts.
- (Child element 1 - if applicable)
- (Child element 2 - if applicable)
- (Child element 3 - if applicable)
Redirecting Output:
To save this recursive listing to a file for easier review:
ls -R > directory_listing.txt
This command stores the entire recursive structure of your current directory into `directory_listing.txt`.
Summary:
The command `ls -R` recursively lists directory contents, providing a comprehensive view of all nested files and directories beneath the current or specified directory.
The
ls -d
option is used to list the directory itself, rather than its contents.
When the -d option is used with ls, the command will only display the directory name, and not the files or subdirectories within it.
- Example 1:
$ ls -d /path/to/directory
/path/to/directory
-
Example 2:
It can be useful when you only want to see the directory name and check the permission or ownership of that directory,
without displaying its contents. It can also be used with wildcard characters to list multiple directories matching a pattern.
$ ls -d /path/to/dir*
/path/to/dir1/ /path/to/dir2/
You can use this -d option along with other options like -l or -h to see the long format of the directory, or show size of the directory in
human-readable format.
A special file is a file that has no associated storage but can be used to gain access to a device. The goal here is to be able to access a device using the same mechanisms by which regular files and directories can be accessed. Thus, callers are able to invoke open(), read(), and write() in the same way that these system calls can be used on regular files. One noticeable difference between special files and other file types can be seen by issuing an ls command as follows
$ ls -l /dev/vx/*dsk/homedg/h
brw------ 1 root root 142,4002 Jun 5 1999 /dev/vx/dsk/homedg/h
crw------ 1 root root 142,4002 Dec 5 21:48 /dev/vx/rdsk/homedg/h
In this example there are two device files denoted by the b and c as the first character displayed on each line. This letter indicates the type of device that this file represents. Block devices are represented by the letter b while character devices are represented by the letter c. For block devices, data is accessed in fixed-size blocks while for character devices data can be accessed in multiple different sized blocks ranging from a single character upwards. Device special files are created with the mknod command as follows:
mknod name b major minor
mknod name c major minor
For example, to create the above two files, execute the following commands:
# mknod /dev/vx/dsk/homedg/h b 142 4002
# mknod /dev/vx/rdsk/homedg/h c 142 4002
The major number is used to point to the device driver that controls the device, while the minor number is a private field used by the device driver. The mknod command is built on top of the mknod() system call:
#include <sys/stat.h>
int mknod(const char *path, mode_t mode, dev_t dev);
The mode argument specifies the type of file to be created, which can be one of the following:
S_IFIFO. FIFO special file (named pipe).
S_IFCHR. Character special file.
S_IFDIR. Directory file.
S_IFBLK. Block special file.
S_IFREG. Regular file.
The file access permissions are
ls [options] [names]
If no names are given, list the files in the current directory. With one or more names, list files contained in a directory name or that match a file name.
The options let you display a variety of information in different formats. The most useful options include -F, -R, -a, -l, and -s. Some options do not make sense together; e.g., -u and -c. Modern versions of ls pay attention to the LC_COLLATE environment variable. Its default value, en_US, (in the United States) causes ls to sort in dictionary order (i.e., ignoring case). You may prefer to set LC_COLLATE to C to restore the traditional Unix behavior of sorting in ASCII order.
Common Options
Command |
Purpose |
-a, --all |
List all files, including the normally hidden . files. |
-A, --almost-all |
Like -a, but exclude . and .. (the current and parent directories). |
-b, --escape |
Show nonprinting characters in octal. |
-c, --time-ctime, --time=status |
List files by inode modification time. |
-C, --format=vertical
| List files in columns (the default format, when displaying to a terminal device). |