The ls command is one of the most basic and useful commands in Linux. It is used to list the contents of a directory. In this blog post, you will learn how to use the ls command in different ways, including how to list hidden files, sort files by size or date, and display detailed information about files.
ls command: -Listing Directory Contents in Linux
- Lists the contents of the current directory or a specified directory Usage
- ls [options] [files_or_dirs]
- Example:
- ls -a (include hidden files)
- ls -l (display extra information)
- ls -R (recourse through directories)
- ls -ld (directory and symlink information)
Listing directory contents with ls
ls without arguments lists the file and directory names in the current directory.
amar@amar-pc:/$ ls
ls -a includes so-called “hidden” files and directories whose names begin with a dot:
amar@amar-pc:/$ ls -a
in above image .test2 is hidden folder.
ls lists another file or directory if given as an argument:
amar@amar-pc:~/Desktop$ ls /
Use ls -l for a more detailed “long” listing:
amar@amar-pc:~/Desktop$ ls -l /usr
ls -R recurses through subdirectories, listing their contents too. ls -d lists directory names, not their contents. it has no effect when file names are passed as arguments. This option is also useful with -l:
amar@amar-pc:~/Desktop$ ls -ld /usr
The ls command has many other options. All options can be used in combination with other ls options.