ls - Linux Command
ls command lists the contents of a directory. Consider ls as the abbreviation of "listing". You can use the "ls" command to list information about the files and sub-directories of a directory (the current directory by default).When you use the command "ls", you get the listing of contents (i.e. files and sub-directory names and information) of a directory (or even for multiple directories). ls command gives comprehensive details about the contents of a directory including showing the hidden files, showing recursive listing, sorting the list, showing a long list, author name, file size and so on.
How to show the contents of a directory using "ls" command:
To show the contents of a directory you are currently in, just type "ls" and press enter. You can also pass the directory name to the 'ls' command and in this case, the content list of the directory you passed would be printed in alphabetical order. If there is no content to show (the directory is empty) it will list nothing.Example:
To view the list of contents in a directory, enter:
/var$ ls
Sample outputs:In this example, /var is your current working directory. Since you have not passed any directory name after the "ls" command, files and directories of the "/var" directory have been printed out in alphabetic order. So to view the contents of '/var/my_files' directory, for example, you need to pass the directory name ('/var/my_files' in this case) after the "ls" command.cache lib lock log
myfile.txt
run spool tmp www
/var$ ls /var/my_files
Sample outputs:To list the files and directories in reverse/descending order use ls -r command.audio.mp3 blank_file.txt docker logs
myfile3.doc
running tmp zist
Sample outputs:/var$ ls
/var/my_files
-r
zist
tmp
running
myfile3.doc
logs
docker
blank_file.txt
audio.mp3
If your terminal supports colours, your file and directory listings may be shown in different colours.
The most important usages of "ls":
1. To list the contents of a directory: ls
2. To show the contents including hidden files and folders: ls -acache lib lock log
myfile.txt
run spool tmp www
3. To show long listing: ls - l. ..
.arh cache
dev .filos
.htac
lib32 lost+found sys zips
total 30
drwxr-xr-x 1 root root 512 May 14 2019 backups
drwxr-xr-x 1 root root 512 Aug 3 14:40 cache
drwxr-xr-x 1 root root 512 Aug 3 14:40 lib
drwxrwsr-x 1 root staff 512 May 14 2019 local
lrwxrwxrwx 1 root root 9 Aug 3 14:40 lock -> /run/lock
drwxr-xr-x 1 root root 512 Aug 3 14:40 log
drwxrwsr-x 1 root mail 512 Aug 3 14:40 mail
drwxr-xr-x 1 root root 512 Aug 3 14:40 opt
lrwxrwxrwx 1 root root 4 Aug 3 14:40 run -> /run
drwxr-xr-x 1 root root 512 Aug 3 14:40 spool
drwxrwxrwt 1 root root 512 May 14 2019 tmp
4. To show one file or folder per line: ls -1root:$ ls /var -1
backups
cache
lib
local
lock
log
mail
opt
run
spool
tmp
5. To show files & folders and sort them by file size: ls -lS
root:$ ls /var -lS
total 30
drwxr-xr-x 1 root root 870 May 14 2019 backups
drwxr-xr-x 1 root root 618 Aug 3 14:40 cache
drwxr-xr-x 1 root root 512 Aug 3 14:40 lib
drwxrwsr-x 1 root staff 256 May 14 2019 local
drwxr-xr-x 1 root root 128 Aug 3 14:40 log
drwxrwsr-x 1 root mail 124 Aug 3 14:40 mail
drwxr-xr-x 1 root root 96 Aug 3 14:40 opt
drwxr-xr-x 1 root root 51 Aug 3 14:40 spool
drwxrwxrwt 1 root root 12 May 14 2019 tmp
lrwxrwxrwx 1 root root 9 Aug 3 14:40 lock -> /run/lock
lrwxrwxrwx 1 root root 4 Aug 3 14:40 run -> /run
6. To show files & folders and sort them by modification time: ls -lt
root:$ ls /var -lS
total 30
drwxr-xr-x 1 root root 512 Aug 3 14:40 cache
drwxr-xr-x 1 root root 512 Aug 3 14:40 lib
drwxr-xr-x 1 root root 512 Aug 3 14:40 spool
drwxr-xr-x 1 root root 512 Aug 3 14:40 log
lrwxrwxrwx 1 root root 9 Aug 3 14:40 lock -> /run/lock
drwxrwsr-x 1 root mail 512 Aug 3 14:40 mail
drwxr-xr-x 1 root root 512 Aug 3 14:40 opt
lrwxrwxrwx 1 root root 4 Aug 3 14:40 run -> /run
drwxr-xr-x 1 root root 512 May 14 2019 backups
drwxrwsr-x 1 root staff 512 May 14 2019 local
drwxrwxrwt 1 root root 512 May 14 2019 tmp
7. To show a recursive listing and view folders and files within a folder: ls -R
root:$ ls -R /var
/var:
backups cache lib local lock log mail opt run spool tmp
/var/backups:
/var/cache:
apt debconf ldconfig
/var/cache/apt:
archives
...
...
...
"ls" use and options:
ls: lists the files and directories of the current directory (alphabetically).ls /home/john: lists the files and directories of the '/home/john' directory.
ls -a: lists all files and directories, including hidden files that start with "." (hidden files in Linux starts with ".") ["-a" stands for all.]
ls -A: lists all files and directories, including hidden files. It is like the command "ls -a" but it excludes "." and "..". ["-A" stands for all excluding . and ..]
ls -1: lists files and directories of a directory. "-1" forces to print each entry on a separate line. ["1" one, stands for one entry in one line.]
ls -F: appends character that reveals the type of the contents. (for example, * for an executable, or / for a directory. Regular files have no suffix.) ["-F" stands for File type]
ls -p: lists contents of a directory but append "/" after the directory.
ls -l: lists files and directories in long format, that displays Unix file types, file permissions, number of hard links, file/ directory owner, group, size, last-modified date and filename. ["-l" stands for long listing]
ls -R: lists all files and directories within a directory recursively. ["-R" stands for Recursive]
Other interesting use of "ls" command:
ls --help: shows help and guideline about what you can possibly do with the "ls" command.ls ~: lists the contents of the parent directory.
ls /: lists all the top-level directories of your Linux OS.
ls ../: lists contents of the directory one level above.
ls ../../: lists contents of the directory two levels above.
ls -lX: lists the contents using the file extension.
ls -lt: lists files and directories in long format and sorts the list of files by last access date and time.
ls -lh: lists files and directories in human-readable format by showing the file size in KB, MB, GB etc. ["h" stands for human (readable)]
Further reading about Linux command "ls":
ls Wikipedia page
ls die.net page
ls LinOxide page
Comments
Post a Comment