The s command in Linux: list files and directories

Introduction to command ls

The commandls(list) is one of the most fundamental tools in the Linux command line. It allows you to view the content of a directory, showing files and subdirectories quickly and clearly. Although its basic use is simple,lsoffers a wide variety of options that allow the output to be adapted to almost any need.

Basic syntax

The simplest way to runlsis:

ls [ruta]

If a route is not specified, the command lists the content of the current directory. For example:

ls

It will show all the files and directories visible in the directory where you are.

Most commonly used options

  • -l(long listing): shows detailed information such as permissions, number of links, owner, group, size, date of modification and name.
  • -a(all): includes hidden files, those whose name begins with a point ('.').
  • -h(human-readable): combines with-lto show sizes in legible units (K, M, G).
  • -R(recursive): lists the contents of the subdirectories recursively.
  • -t(sort by time): order the list by modification date, first showing the latest.
  • - S(sort by size): order by size, from greater to smaller.

Combining options

The options oflscan be combined for more specific information. Some common examples:

  • ls -lah: list all files (including hidden files) with long format and readable sizes.
  • ls -lR: shows a recursive list in long format.
  • ls -lt: list ordered by time of modification, more recent first.
  • ls -lS: list sorted by size, from larger to smaller.

Practical examples

Suppose you're in your personal directory and you want to see what's inside:

ls

To also see the hidden configuration files:

ls -a

If you need to know the permissions and sizes of the files in/var/log:

ls -lh /var/log

To get a full tree from a project located in~/proyectos/miapp:

ls -R ~/proyectos/miapp

And if you want to find the ten largest files in your home:

ls -lS ~ | head -n 11

Tips and tricks

  • Use--color=auto(enabled by default in many distributions) so that different file types are shown in different colors, facilitating identification.
  • You can create an alias in your.bashrcor.zshrcso thatlsalways include your favorite options, for example:alias ls='ls -lah --color=auto'.
  • Remember thatlsonly shows what the user has permission to read. If you see permits denied, you may need to usesudoor change the permits.
  • Combinelswith other tools likegreporawkfor filtering results, for example:ls -l | grep '^d'to list only directories.

Using food and patterns

The commandlsaccepts shell comodines as*, ?and[]to filter the results without additional pipes. For example:

  • ls *.txtlist all files with extension.txt.
  • ls foto?.jpgshows names likefoto1.jpg, foto2.jpgetc.
  • ls [A-Z]*list those whose name begins with a capital letter.

These patterns are expanded by the shell beforelsSee them, so they work on any command that accepts routes.

Limitations and alternatives

Althoughlsis very versatile, has some limitations worth knowing:

  • It is not designed to display information from remote file systems without previously mounting them (e.g. bysshfsorNFS).
  • In directories with a huge number of files, the output can be difficult to read; in such cases tools likefindorfdmay be more appropriate.
  • The output format ofls -lcan change slightly between different implementations (GNU choreutils vs BSD), which can affect scripts that depend on exact columns.

For situations where more thin control is needed, alternatives can be considered:

  • find: excellent for searching files according to complex criteria (type, size, date, permissions).
  • tree: shows a hierarchical view of the directories tree.
  • exaorlsd: modern replacements oflswith default colors and more format options.

Conclusion

The commandlsis an essential piece of the arsenal of any Linux user. Dominating your options allows you to navigate the file system efficiently, get detailed information and automate management tasks. Practice by combining your lags and see how much you can improve your productivity in the terminal.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish