dir Command in Linux VPS
The dir
command is a fundamental tool for exploring your computer’s filesystem. It allows you to list files and directories, providing valuable insights into your data organization.
The basic syntax of the dir
command is as shown below:
# dir [OPTION] [FILE]
To view all files and directories in your current working directory, you can simply run the dir
command without any options:
dir
Prerequisites to Use dir Command in Linux VPS
Provide the options below to let this tutorial work correctly and move on.
- A Server running Linux VPS.
- A non-root user with
sudo
privileges.
Practical Examples of Linux dir Command
The below examples help you to be an expert in Linux dir
command usage.
1. List Files and Directories
To get a simple overview of a directory’s contents, use dir /path/to/directory
. Replace /path/to/directory
with the actual path you want to explore.
For example, running the below command will list all files and subdirectories within the /etc
directory:
# dir /etc
By default, dir
shows a single line for each entry.
To view a more detailed listing, including file permissions and owner information, use the -l
flag.
If you prefer a cleaner output with each file or directory on a separate line, run:
dir -1 /etc
It provides a more comprehensive view of the /etc
directory.
2. List Hidden Files and Directories
Hidden files are files prefixed with a dot (“.
“) at the beginning of their names. They’re typically system configuration files or user-created files meant to be hidden from regular view.
dir -al /path/to/directory
combines the -a
flag (show all files) with the -l
flag (long format).
This provides a detailed listing for each file, including permissions, owner, group, and size, alongside hidden files:
dir -a /path/to/directory
dir -al /path/to/directory
3. List Directory Entries
To focus solely on directory entries, excluding the actual content within directories, you can run:
dir -d /path/to/directory
The below command builds upon -d
by adding the -l
flag (long format). This provides a detailed view of directory entries, including permissions and owner information for each subdirectory:
dir -al /path/to/directory
4. List Index Number of Files
The inodes (index nodes) are unique identifiers assigned to each file on a Linux system.
They act like pointers, storing information about a file (like size, permissions, and ownership) but not the actual file content itself.
To display a listing with inodes in the first column, run:
dir -il /path/to/directory
This can help troubleshoot file permissions or ownership issues.
5. List Files based on Size
To view the size of each file in bytes by default, run:
dir -s /path/to/directory
The below command combines -s
with -h
(human-readable) to display file sizes in a more user-friendly format, such as kilobytes (KB) or megabytes (MB):
dir -sh /path/to/directory
To sort the directory listing by file size, with the largest files appearing first, use:
dir -S /path/to/directory
Remember to use -h
for human-readable sizes when sorting.
Also, you can use the following command to sort the directory listing by modification time, with the most recently modified files appearing first.
dir -t /path/to/directory
This can be useful for finding files you’ve recently edited.
6. List Files Without Owners
You can exclude owner names from the directory listing while still showing all files using the below command:
dir -ahg /path/to/directory
This can be helpful when focusing on file permissions or file sizes without needing owner information.
The below command is similar to -ahg
, but excludes group owner names instead:
dir -aG /path/to/directory
To view the owner, group, permissions, and author of each file in a detailed listing (using -l and –author), run:
dir -al --author /path/to/directory:
This can be useful for identifying who created a specific file.
7. List Directories Before Files
To prioritize the directories in the listing, you can use the command below:
dir -l --group-directories-first /path/to/directory
This displays all subdirectories before regular files, making it easier to visualize the directory structure.
The letter “d
” before the permissions indicate a directory, while “a
” represents a regular file.
Additionally, you can see subdirectories recursively. This means that by using the -R
option, you can list every other subdirectory within a directory.
# dir -R
8. List Files with User and Group IDs
User and group IDs are numerical identifiers assigned to users and groups on a Linux system. They are used to controlling file permissions and ownership.
By default, dir
displays usernames and group names. To display the numerical user and group IDs instead of their names, run:
dir -nl --author /path/to/directory
This can be useful for scripting purposes or when working with system configurations where user/group names might not be readily available.
9. List Files Separated by Commas
The below command uses the -m
flag to output a comma-separated list of filenames within the specified directory.
This list can then be used in scripts or piped to other commands for further processing.
dir -am /path/to/directory
Help and Version Information:
The dir
command also offers built-in help and version information for your reference.
To display the command’s help manual, providing detailed explanations for all available options and flags:
dir --help
To view the version information of the dir
command you’re using, run:
dir --version
10. List Contents With Separate Colors
While dir
doesn’t color directories by default (unlike ls
), you can achieve visual separation with this command:
dir --color
This activates color coding, making directories stand out from files, and enhancing readability in your terminal listings.
11. List Contents with File Type Indicators
Check the below table, which shows how the dir
command uses some special characters that indicate the content type.
Indicator Character | Indications |
---|---|
/ | A directory. |
* | An executable file. |
@ | A symbolic link. |
% | A whiteout. |
= | A socket. |
| | A FIFO (First In, First Out) |
For example, the -F
or --classify
option in dir
adds a handy indicator to each filename, revealing its type at a glance:
dir -F /path/to/directory
This command will list your directory contents, with each filename accompanied by its corresponding type symbol, making it easier to distinguish between different file types.
12. List File Types Clearly
The --file-type
option (same as -F
) helps identify file types in your dir
listings.
However, unlike -F
, it avoids adding an asterisk (*
) to executable filenames.
This provides a cleaner output without altering the actual file names:
dir --file-type /path/to/directory
This will display file types alongside their names, but executable names won’t have an asterisk appended.
13. List only Active Files
The -B
or --ignore-backups
option instructs the dir
command to skip backup and list any files that end with a tilde (~
). This streamlines your directory output, presenting only the primary files without the clutter of backups:
dir -B /path/to/your/directory
It helps enhance readability, reducing clutter, and focusing on primary files.
14. List Directories in your preferred Format
The --format=WORD
option in the dir
command empowers you to customize the layout of your directory listings.
It accepts various keywords (WORD) to achieve different formatting styles:
dir --format=long /path/to/directory
This command will list your directory contents with detailed information for each file.
15. List Directories Selectively
The --hide=PATTERN
or --ignore=PATTERN
option in the dir
command empowers you to streamline your directory output by excluding specific files or groups of files that match a defined pattern.
This can be particularly useful when working with directories containing numerous files, and you only want to focus on relevant ones:
dir --hide=*.txt --hide=__* /path/to/directory
This command will list the contents of the directory, excluding all files with the .txt
extension and any files whose names begin with double underscores (__
).
16. List Directories Sorted
--sort=PARAMETER
lets you arrange files according to various criteria.
Choose size
(-S) for the largest first, time
(-t) for the newest first, or extension
(-X) to group by file type. Skip --sort
and use flags directly: -S,
-t
, or -X
.
dir --sort=PARAMETER /path/to/directory
This command empowers you to arrange files based on your preference.
17. List the Directories with a specified Format
# dir -x (across)
# dir -m (Commas)
# dir -x (Horizontal)
# dir -l (Long)
# dir -1 (Single-column)
# dir -l (Vebose)
# dir -c (Vertical)
That’s it! Remember, you can replace the example paths with your directories to tailor these commands to your specific needs.
To make use of numerous more options, view your system’s manual entry for the dir
command using # man dir
command.
dir command vs ls Command
Both dir
and ls command list directory contents but with slight differences.
ls
is the standard command in Unix-based systems like Linux and macOS, offering a more versatile feature set.
dir
, often available on Linux, provides a simpler alternative with consistent output regardless of whether it’s displayed in the terminal or redirected elsewhere (like piping to another command).
If you’re new to Linux, dir
might be easier to grasp initially, but ls offers more power and flexibility for experienced users.
Conclusion
By mastering Linux dir
command functionalities, you can efficiently list files and directories, view detailed information like permissions and sizes, and explore directory structures.
From filtering specific details to sorting by modification time, dir
command empowers you to manage your data with ease.