Linux dir command: lists directory or file information.
Linux dir command Function Description
Use the dir command to list information about a directory or file.
Linux dir command Syntax
dir [Option] [File]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-a |
Do not hide anything that starts with a . The beginning of the item. |
-A |
List all the characters except . and . . |
-l |
List information in a longer format. |
-i |
Displays the inode number of each file. |
-m |
All items are separated by commas and fill the entire line width. |
-R |
Recursive display of subdirectories |
-r |
Preserve order when sorting. |
-L |
When displaying information about a symbolic link file, the information about the object indicated by the symbolic link is displayed instead of the symbolic link itself. |
-s |
Displays the size of each file allocation as a number of blocks. |
-d |
List the directory itself instead of the files in the directory when you encounter it. |
-S |
Sort by file size. |
-h |
Use with the -1 option to output file sizes in an easy-to-read format, such as 1K, 234M, 2G. |
-n |
Similar to the -1 option, but with UID and GID numbers listed. |
-U |
List items in catalog order without sorting. |
-t |
Sort by modification time. |
-v |
A natural ordering of numbers (versions) in text. |
-Q |
Enclose the entry name in double quotation marks. |
-t |
Sort by modification time. |
-N |
Output the unprocessed project name (if control characters are not specifically processed). |
-o |
Similar to the -1 option, but no information about the group is listed. |
-C |
List items from top to bottom in each column. |
-q |
With ? Characters to replace unprintable characters |
-f |
If no sorting is performed, the -au option takes effect and the -ist option is invalid. |
-k |
means –block-size=IK |
-B |
Do not list any items that end with the ~ character |
-c |
with -lt: sort by ctime and display ctime (when the file status was last changed); with -l: display ctime but sort by name; other cases: sort by ctime |
-u |
Use with -lt to sort and display the ctime time; use with -1 to sort by file name and display the ctime time; in other cases, sort by ctime time |
--author |
List the author of each file when used in conjunction with the -1 option |
-g |
Similar to the -1 option, but without listing the owner |
-H |
Follow the symbolic links listed in the command line |
--block-size=<Size> |
Blocks are in bytes of the specified size |
-F |
Add the file type indicator (*, /, =, @, 1, one of them) |
-G |
In the form of a long list, no group names are printed |
-1 <Mode> |
Does not display any items that match the specified Shell mode |
--hide=<Mode> |
Hide items that match the pattern |
--indicator-style=<Style> |
Specify how to add the indicator to each item name: none (default), classify (-F), file-type (-p) |
--show-control-chars |
Display unprintable characters directly (this is the default, unless the name of the calling program is ls and the result is output in the terminal) |
--lcontext |
Display Security context |
-Z |
Displays only most security context information (schema, user, group, security context, and file name) |
-w |
Assume the screen width, not the current value |
-x |
List entries by row rather than column |
-l |
List one file per line |
-X |
Sort alphabetically by entry extension |
--si |
Similar to -h, but using 1000 as the base instead of 1024 |
--scontext |
Only the security context and file name are displayed |
Linux dir command Example
List information about the /root directory
[root@rhel ~]# dir /root
anaconda-ks.cfg install.log install.log.syslogPublic Templates Videos Images Documents Downloads Music Desktop
Lists information about the /root directory, even hidden files
[root@rhel ~]# dir -a /root
. .dbus .ICEauthority .ssh
.. .esd_auth .imsettings.log .targetcli
.abrt .gconf install.log .tcshrc
anaconda-ks.cfg .gconfd install.log.syslog Public
.bash_history .gnome2 .local Templates
.bash_logout .gnome2_private .nautilus Videos
.bash_profile .gnote .pulse Images
.bashrc .gnupg .pulse-cookie Documents
.cache .gstreamer-0.10 .recently-used.xbel Downloads
.config .gtk-bookmarks .redhat Music
.cshrc .gvfs .rnd Desktop
List the information in the /root directory in a longer format
total 208
drwxr-xr-x 3 root root 4096 Aug 1 08:34 aaa-apidemos
drwxrwxrwx 2 root root 4096 Jul 30 10:11 apidemos
-rw-r--r-- 1 root root 8 Aug 5 08:29 apidemos.a1.txt
-rw-r--r-- 1 root root 8 Aug 5 08:31 apidemos.a2.txt
-rw-r--r-- 1 root root 8 Aug 5 08:29 apidemos.a2.txt.orig
drwxr-xr-x 3 root root 4096 Aug 2 07:27 apidemos-aaa
-rw-r--r-- 1 root root 102400 Aug 2 08:14 apidemos.com.split.txt
-rwxr-xr-x 1 root root 56 Aug 4 07:58 apidemos.com.txt
-rw-r--r-- 1 root root 56 Aug 4 07:59 apidemos.com.unexpand.txt
-rw-r--r-- 1 root root 143 Aug 5 08:30 apidemos.diff.txt
-r-x--x--T 1 root root 0 Jul 31 15:55 apidemos_mknod
-rw-r--r-- 1 root root 225 Aug 10 22:11 apidemos.py
-rw-r--r-- 1 root root 282 Aug 2 08:22 apidemos.server.log
lrwxrwxrwx 1 root root 17 Aug 1 12:48 apidemos(symlink).py -> /root/apidemos.py
drwxr-xr-x 3 root root 4096 Apr 12 2020 deepinout.com.test
drwxr-xr-x 2 root root 4096 Dec 19 2020 demo
-rw-r--r-- 1 root root 135 Aug 2 08:43 fff
lrwxrwxrwx 1 600 700 22 Jul 30 11:30 file(symlink).txt -> /root/apidemos.com.txt
-rw-r--r-- 1 root root 35072 Aug 5 08:07 man_patch.txt
drwxr-xr-x 2 root root 4096 Aug 1 07:18 nwdir
drwxr-xr-x 8 root root 4096 Oct 3 2020 oneinstack
-rw-r--r-- 1 root root 430 Feb 13 2020 ReadMe
drwxr-xr-x 3 root root 4096 Dec 7 2020 tools
Display the inode number of the /root/install.log file
[root@rhel ~]# dir -i /root/install.log
3670018 /root/install.log
List the information in the /root directory in a longer format, sorted by modification time
total 208
-rw-r--r-- 1 root root 225 Aug 10 22:11 apidemos.py
-rw-r--r-- 1 root root 8 Aug 5 08:31 apidemos.a2.txt
-rw-r--r-- 1 root root 143 Aug 5 08:30 apidemos.diff.txt
-rw-r--r-- 1 root root 8 Aug 5 08:29 apidemos.a2.txt.orig
-rw-r--r-- 1 root root 8 Aug 5 08:29 apidemos.a1.txt
-rw-r--r-- 1 root root 35072 Aug 5 08:07 man_patch.txt
-rw-r--r-- 1 root root 56 Aug 4 07:59 apidemos.com.unexpand.txt
-rwxr-xr-x 1 root root 56 Aug 4 07:58 apidemos.com.txt
-rw-r--r-- 1 root root 135 Aug 2 08:43 fff
-rw-r--r-- 1 root root 282 Aug 2 08:22 apidemos.server.log
-rw-r--r-- 1 root root 102400 Aug 2 08:14 apidemos.com.split.txt
drwxr-xr-x 3 root root 4096 Aug 2 07:27 apidemos-aaa
lrwxrwxrwx 1 root root 17 Aug 1 12:48 apidemos(symlink).py -> /root/apidemos.py
drwxr-xr-x 3 root root 4096 Aug 1 08:34 aaa-apidemos
drwxr-xr-x 2 root root 4096 Aug 1 07:18 nwdir
-r-x--x--T 1 root root 0 Jul 31 15:55 apidemos_mknod
lrwxrwxrwx 1 600 700 22 Jul 30 11:30 file(symlink).txt -> /root/apidemos.com.txt
drwxrwxrwx 2 root root 4096 Jul 30 10:11 apidemos
drwxr-xr-x 2 root root 4096 Dec 19 2020 demo
drwxr-xr-x 3 root root 4096 Dec 7 2020 tools
drwxr-xr-x 8 root root 4096 Oct 3 2020 oneinstack
drwxr-xr-x 3 root root 4096 Apr 12 2020 deepinout.com.test
-rw-r--r-- 1 root root 430 Feb 13 2020 ReadMe
List information about the /root directory in a longer format, sorted by file size
total 208
-rw-r--r-- 1 root root 102400 Aug 2 08:14 apidemos.com.split.txt
-rw-r--r-- 1 root root 35072 Aug 5 08:07 man_patch.txt
drwxr-xr-x 3 root root 4096 Aug 1 08:34 aaa-apidemos
drwxrwxrwx 2 root root 4096 Jul 30 10:11 apidemos
drwxr-xr-x 3 root root 4096 Aug 2 07:27 apidemos-aaa
drwxr-xr-x 3 root root 4096 Apr 12 2020 deepinout.com.test
drwxr-xr-x 2 root root 4096 Dec 19 2020 demo
drwxr-xr-x 2 root root 4096 Aug 1 07:18 nwdir
drwxr-xr-x 8 root root 4096 Oct 3 2020 oneinstack
drwxr-xr-x 3 root root 4096 Dec 7 2020 tools
-rw-r--r-- 1 root root 430 Feb 13 2020 ReadMe
-rw-r--r-- 1 root root 282 Aug 2 08:22 apidemos.server.log
-rw-r--r-- 1 root root 225 Aug 10 22:11 apidemos.py
-rw-r--r-- 1 root root 143 Aug 5 08:30 apidemos.diff.txt
-rw-r--r-- 1 root root 135 Aug 2 08:43 fff
-rwxr-xr-x 1 root root 56 Aug 4 07:58 apidemos.com.txt
-rw-r--r-- 1 root root 56 Aug 4 07:59 apidemos.com.unexpand.txt
lrwxrwxrwx 1 600 700 22 Jul 30 11:30 file(symlink).txt -> /root/apidemos.com.txt
lrwxrwxrwx 1 root root 17 Aug 1 12:48 apidemos(symlink).py -> /root/apidemos.py
-rw-r--r-- 1 root root 8 Aug 5 08:29 apidemos.a1.txt
-rw-r--r-- 1 root root 8 Aug 5 08:31 apidemos.a2.txt
-rw-r--r-- 1 root root 8 Aug 5 08:29 apidemos.a2.txt.orig
-r-x--x--T 1 root root 0 Jul 31 15:55 apidemos_mknod
Show the size of each file allocation in the /root directory as a block number
total 208
4 aaa-apidemos 4 apidemos-aaa 0 apidemos_mknod 4 demo 4 oneinstack
4 apidemos 100 apidemos.com.split.txt 4 apidemos.py 4 fff 4 ReadMe
4 apidemos.a1.txt 4 apidemos.com.txt 4 apidemos.server.log 0 file(symlink).txt 4 tools
4 apidemos.a2.txt 4 apidemos.com.unexpand.txt 0 apidemos(symlink).py 36 man_patch.txt
4 apidemos.a2.txt.orig 4 apidemos.diff.txt 4 deepinout.com.test 4 nwdir
Sort by modification time, preserving the order when sorting, and listing the information in the /root directory in a longer format
dir -ltr /root
Output: