Linux List Command
Linux List Command
List command
Dr. Nyo Nyo Htwe
How do I list or view directories?
o To see directories and files in the current MS-DOS directory, use the dir
command.
o In Linux to view directories and files in the current directory, you'd use the ls
command.
o Long Listing Format
Long Listing Format (ls –l)
When the long listing format is used, the ls command will display the following file
information:
1. The file type
2. The file permissions
3. Number of hard links to the file
4. File owner
5. File group
6. File size
7. Date and Time
8. File name
ls command options
option description
ls -a list all files including hidden file starting with '.'
ls --color colored list [=always/never/auto]
ls –d/ ls -f list directories/file - with ' */'
ls -i list file's inode index number
ls -l list with long format - show permissions
ls -la list long format including hidden files
ls -lh list long format with readable file size
ls -ls list with long format with file size
ls -r list in reverse order
ls -R list recursively directory tree
ls -s list file size
ls -S sort by file size
ls -t sort by time & date
ls -X sort by extension name
Questions
1. List files/directory with alphabetic order ls -l
2. list hidden files/directory - ls -a
3. display specific directory information ls –d tmp
4. display files in human readable format - ls -lh
5. display files/directories with alphabetic reverse order. ls -r
6. How to sort ls command output with file extensions. ls -X
7. Display files with file size ls -lS
8. Display all files sorted by size in reverse order ls -lSr
9. Display files only filenames and file sizes ls –s -h
10. How to make ls hide a particular type of files ls --hide=*.txt
11. How to change ls command output format ls --format=comma
12. Display One File Per Line Using ls -1.
13. Display Directory Information Using ls –ld
14. Display directory/files with modification time ls –lt / ls –ltr (reverse order)
1. List Files using ls with no option
ls with no option list files and directories in bare format
o where we won’t be able to view details like file types, size, modified date and time,
permission and links etc.
o It lists the contents sorted in alphabetical order.
How to list hidden files/directories
The ls command doesn't display hidden files/directories by default.
However, you can force the tool to do that using the -a command-line
option.
Display Specific Directory Information
• You can display specific directory information by using the ls command with -d option. In
the following example,
• it will display information of the /etc directory in the long format:
Display files in Human Readable Format
• To display files in human readable format -h option should be used with ls command. In
the following example,
• the command will display all files in human readable long format:
Display files without color
• You can display files without color by using --color option with ls command like given
below:
How to display files/directories in reverse order
• To sort the ls command output alphabetically based on file extensions, use the -
X command-line option.
Sort Files by File Size
With combination of -lS displays file size in order, will display big in size first.
Display All Files Sorted by Size in Reverse Order
To display all files sorted by file size in reverse order -r and -s should be used
with ls command additionally.
It displays all the files sorted by size in the directory in reverse order:
#ls -lSr
How to make ls display only filenames and file sizes in output
• If you want the ls command output to only contain file/directory names and their
respective sizes, then you can do that using the -h option in combination with -l/-s
command line option.
• ls -s –h
Ls –s Documents
How to display author information
• For this, you have to combine --author option with -l.
List Directory Information
• If you want, you can also force the ls command to hide a particular type of files in
output.
• The --hide option (which requires a shell pattern to be passed to it) lets you do this
• ls --hide=*.txt
How to sort files based on modification time
• If you want to quickly know which files or directories were modified recently,
then you can use the ls command's -t option.