Many people starting with Unix/Linux refer ‘ls’ as is command in Linux. I observed this question asked multiple times in one of the Ubuntu forums.
Since ls command is one of the most widely used Linux commands a UNIX developer would encounter. Therefore it becomes of utmost importance to understand the command in and out.
If you are interested in more such guides, I have created a few more on understanding RSyslog and Syslog and the best download managers for Linux.
So I thought of explaining the basics of the command in the easiest way possible.
What is the ls command?
“ls” is a shell command in Linux that lists the content of a directory such as files and folders.
ls command Syntax
$ ls [options] [file|dir]
Flags and options
Here is a list of few options that are most widely used. To see all the flags with very descriptive details, run below command.
$ man ls
Here is a list of most widely used options with a short description :
Basic ‘ls’ command examples
ls -a
: List all the files and directories including hidden files or directories.
Output :
. .. .git .github .gitignore .idea CHANGELOG.md LICENSE Procfile README.md cljbin.iml doc pom.xml project.clj resources runner src target test
ls -color
: Display a colored list of contents. Mostly differentiate between files and directories with different colors.
Output :
total 96 drwxr-xr-x 3 kshitij 96 Jan 25 22:43
test
drwxr-xr-x 3 kshitij 96 Feb 16 16:47
target
drwxr-xr-x 5 kshitij 160 Feb 16 15:36
src
-rw-r--r-- 1 kshitij 69 Feb 16 16:53
runner
drwxr-xr-x 3 kshitij 96 Feb 16 17:30
resources
-rw-r--r-- 1 kshitij 730 Feb 16 16:37 project.clj -rw-r--r-- 1 kshitij 3346 Feb 16 13:22 pom.xml drwxr-xr-x 3 kshitij 96 Jan 25 22:43 doc -rw-r--r-- 1 kshitij 6004 Jan 27 16:40 cljbin.iml -rw-r--r-- 1 kshitij 975 Jan 25 22:43 README.md -rw-r--r-- 1 kshitij 83 Jan 27 16:43 Procfile -rw-r--r-- 1 kshitij 14199 Jan 25 22:43 LICENSE -rw-r--r-- 1 kshitij 766 Jan 25 22:43 CHANGELOG.md
ls -d */
: Lists all the directories within the current directory.
Output :
doc/ resources/ src/ target/ test/
ls -i
: List all files and folders with inode index numbers.
Output :
5983099 CHANGELOG.md 6059115 Procfile 5983719 cljbin.iml 6739981 pom.xml 6772530 resources 5983105 src 5983108 test 5983100 LICENSE 5983101 README.md 5983102 doc 5983104 project.clj 6772943 runner 6796218 target
ls -la
: List all files and folder with more details such as date of update, user permissions, file owner, and size of the file in bytes.
Output :
total 104 drwxr-xr-x 19 kshitij staff 608 Feb 16 16:53 . drwxr-xr-x 16 kshitij staff 512 Jun 20 15:20 .. drwxr-xr-x 15 kshitij staff 480 Jun 25 08:22 .git drwxr-xr-x 3 kshitij staff 96 Jan 25 23:32 .github -rw-r--r-- 1 kshitij staff 112 Jan 25 22:43 .gitignore drwxr-xr-x 10 kshitij staff 320 Feb 16 17:49 .idea -rw-r--r-- 1 kshitij staff 766 Jan 25 22:43 CHANGELOG.md -rw-r--r-- 1 kshitij staff 14199 Jan 25 22:43 LICENSE -rw-r--r-- 1 kshitij staff 83 Jan 27 16:43 Procfile -rw-r--r-- 1 kshitij staff 975 Jan 25 22:43 README.md -rw-r--r-- 1 kshitij staff 6004 Jan 27 16:40 cljbin.iml drwxr-xr-x 3 kshitij staff 96 Jan 25 22:43 doc -rw-r--r-- 1 kshitij staff 3346 Feb 16 13:22 pom.xml -rw-r--r-- 1 kshitij staff 730 Feb 16 16:37 project.clj drwxr-xr-x 3 kshitij staff 96 Feb 16 17:30 resources -rw-r--r-- 1 kshitij staff 69 Feb 16 16:53 runner drwxr-xr-x 5 kshitij staff 160 Feb 16 15:36 src drwxr-xr-x 3 kshitij staff 96 Feb 16 16:47 target drwxr-xr-x 3 kshitij staff 96 Jan 25 22:43 test
ls -lah
: It gives almost the same output as the previous command but changes file size to be more readable. (in MB and GB).
Output :
total 104 drwxr-xr-x 19 kshitij staff 608B Feb 16 16:53 . drwxr-xr-x 16 kshitij staff 512B Jun 20 15:20 .. drwxr-xr-x 15 kshitij staff 480B Jun 25 08:22 .git drwxr-xr-x 3 kshitij staff 96B Jan 25 23:32 .github -rw-r--r-- 1 kshitij staff 112B Jan 25 22:43 .gitignore drwxr-xr-x 10 kshitij staff 320B Feb 16 17:49 .idea -rw-r--r-- 1 kshitij staff 766B Jan 25 22:43 CHANGELOG.md -rw-r--r-- 1 kshitij staff 14K Jan 25 22:43 LICENSE -rw-r--r-- 1 kshitij staff 83B Jan 27 16:43 Procfile -rw-r--r-- 1 kshitij staff 975B Jan 25 22:43 README.md -rw-r--r-- 1 kshitij staff 5.9K Jan 27 16:40 cljbin.iml drwxr-xr-x 3 kshitij staff 96B Jan 25 22:43 doc -rw-r--r-- 1 kshitij staff 3.3K Feb 16 13:22 pom.xml -rw-r--r-- 1 kshitij staff 730B Feb 16 16:37 project.clj drwxr-xr-x 3 kshitij staff 96B Feb 16 17:30 resources -rw-r--r-- 1 kshitij staff 69B Feb 16 16:53 runner drwxr-xr-x 5 kshitij staff 160B Feb 16 15:36 src drwxr-xr-x 3 kshitij staff 96B Feb 16 16:47 target drwxr-xr-x 3 kshitij staff 96B Jan 25 22:43 test
ls -r
: Prints the output of command but in reverse order.
Output:
test target src runner resources project.clj pom.xml doc cljbin.iml README.md Procfile LICENSE CHANGELOG.md
ls -R
: List the directory tree in recursive order. What it means that shows the directory tree for current as well as child directories.
Output:
Not printing as command is output is too large.
ls -s
: List file size for all of them but without any other additional details.
Output:
8 CHANGELOG.md 32 LICENSE 8 Procfile 8 README.md 16 cljbin.iml 0 doc 8 pom.xml 8 project.clj 0 resources 8 runner 0 src 0 target 0 test
ls -S
: Sort files by file sizes of ascending order
Output:
LICENSE cljbin.iml pom.xml README.md CHANGELOG.md project.clj src doc resources target test Procfile runner
ls -t
: Sort files by time and date modified.
Output:
resources runner target project.clj src pom.xml Procfile cljbin.iml test doc README.md LICENSE CHANGELOG.md
ls -x
: Sort files by file extension type.
Output:
CHANGELOG.md LICENSE Procfile README.md cljbin.iml doc pom.xml project.clj resources runner src target test
ls command examples :
[su_note note_color=”#FFF9C4″ ]You can always use the tab button to autocomplete directory names with ls / is command in linux[/su_note]
- List with relative paths.
$ ls Documents/Books
- ls redirection to the output file.
$ ls > out.txt
- List root directory.
$ ls /
- List parent directory.
$ ls ..
- Show all files and directories with full path.
$ ls -d $PWD/*
- List user’s home directory (e.g: /home/user).
$ ls ~
or
$ ls /home/user
- List all subdirectories.
$ ls *
- List only png files with a wildcard.
$ ls *.png
Pro tip :
If you use ls command a lot, which I would assume any Linux developer would, here is a lifesaver trick. You can make alias out of the most used command and use the shortcuts instead of full commands.
Let’s understand with an example :
Suppose you always want to see file details instead of just name when you run ls command in Linux. You can create an alias out of it and use it in the future.
Here’s what I personally do:
$ alias ls="ls -lah"
Now whenever I use just ls
, it displays file details also.
You can see all alias created with the command :
$ alias
To remove previously create alias you can use command.
$ unalias ls
Do let us know if you liked the tutorial on how to use ls / is command in Linux. We would love to see any commands if we have missed it. Let us know in the comments.