DEV Community

Cover image for Power of ls command
Fazal ur Rehman
Fazal ur Rehman

Posted on • Edited on

Power of ls command

what is a command?

The simplest answer is that a command is a software program that, when executed on the CLI, performs any action.
some commands take additional input to run correctly, This input is known as options and arguments

command [options] [arguments]

  • Options are used to modify the behavior of command.
  • Arguments are used to provide additional information.

Today we'll discuss the fundamentals of ls command and try to cover some basic parameters. we use this command daily basic and frequently even though we never aware of all the available options.

ls: By itself, the ls command lists files and directories contained in the current working directory.
Alt Text

Arguments

The ls command takes the argument as a name of the directory, it lists the contents of the directory, In the following example, the /etc/dpkg directory is used as an argument.
Alt Text
The ls command also accepts multiple arguments. To list the contents of both the /etc/ppp and /etc/ssh directories
Alt Text

Options

options used with commands to expend or modify the way a command behaves.
-a : The ls command omits hidden files by default. A hidden file is any file(or directory) that begins with a dot . character. with the used of -a option we can display all filed including hidden files.
Alt Text

-l : With this option we get results in a long listing, providing additional information about the files that are listed, such as the permission, size, creation date, and other information. (I will try to cover permission in the next post. this is a very important and fundamental part of Linux file system)
Alt Text

-h : If the -h option added with -l the file sizes will be displayed in human readable order (or you can write full word form; --human-readable)
Alt Text
note: The order of the combined options isn't important. The output of all of these examples would be the same:
ls -l -h
ls -hl
ls -lh

-r : By default ls print the results in ascending alphabetical order, with the help of -r option we print the result in reverse order.
Alt Text

-s : To see the file (or directory) size. very use full with flag -h
Alt Text

-S : To sort files by size. it is most useful when used with the -l option so the file sizes are visible and -h option to display human-readable file sizes.
Alt Text

-R : With the help of this flag we can display all of the files in a directory as well as all of the files in all subdirectories under that directory. This is called a recursive listing.
Alt Text

-t Sorted by modified time or created time
-m List all the files and directories separated with comma

I hope this article will help you to learn a few options of ls commands in Linux. Please refer to the manual pages by using the man ls command to explore in-depth

Thanks! happy coding :))

Top comments (2)

Collapse
 
cyberjack profile image
CyberJack • Edited

I recommend adding the -F option.

-F, --classify
    append indicator (one of */=>@|) to entries
Enter fullscreen mode Exit fullscreen mode

Basically it makes it easier to see the difference between files, executable files, directories, links, whiteouts, sockets and FIFO without depending on colors.

Collapse
 
iamfazal profile image
Fazal ur Rehman

Yeah, sure I will.

Thanks for spending your time on my post 😊