Alternatives to the 'ls' command to list the contents of a directory

about alternatives to ls command

In the next article we are going to take a look at different ways of listing the contents of a directory without using the command ls. This command is perhaps the most used to list the contents of a directory on Unix-like systems.

Though ls It works very well, it never hurts to know that we can have other ways of listing the contents of a directory using one of the following alternatives to ls command. As is usual in the Gnu / Linux world, there are many options available.

As is known by all Gnu / Linux users, we can use the following command to display the contents of a directory, called Test in this example:

ls command

This is how most users list files and directories. However, we can also use one of the following ways to do this.

List the contents of a directory with some alternatives to the ls command

Use the command threw out

This command is commonly used in shell scripts and batch programs to print the given arguments. Any text or string can be passed as an argument. This command not only serves to print the given arguments, but can also be used to list files in terminal (Ctrl + Alt + T):

echo command *

echo *

For display the content of the next level of directories It can be used:

cinabdi echo to list files in the next level of directories

echo */*

For list hidden files we will execute:

echo * .*

Consultation more details in the man pages:

echo man command

man echo

Use the command dir

This command many consider it equivalent to the one used in Windows, since it works in Gnu / Linux in more or less the same way. For list the contents of the current directory you just have to write:

dir command

dir

For list the contents of a specific directory, you just have to pass the path as an argument:

dir command to list a route

dir /home/sapoclay/Prueba

We will also be able to list all the content, including files hidden by typing:

dir command to list hidden files

dir -a

We can see more details about dir in the man pages:

dir man pages command

man dir

Use the command printf

The command printf it is commonly used to format and print text. This command will print the arguments according to the given format. We can also use it to list the contents of the current directory:

printf command

printf '%s\n' *

For more details, see the man pages:

printf man command

man printf

Use the command grep

Grep used to search using regular expressions. A colleague wrote an article about how to use this command.

For list directory contents using command grep, you just have to run:

grep command

grep -l '.*' ./*

It can get more information about this command using man:

grep man command

man grep

Use the command find

find command

The command find used to search for files in a directory hierarchy. We can also use this command to view directory content:

find -maxdepth 1

Or we will also be able to use:

find .

The first command shows all files and directories in current directory, while the second command show all files and directories recursively.

We can also view the contents of a specific directory:

find dir1/

We can know more about find in your man page:

find man command

man find

Use the command stat

The command stat used to display file and file system information. With this command, we can view the properties of files and directories such as size, permissions and creation, among others.

We can list files and directories using command stat:

stat command

stat -c '%s %A %n' *

As you can see, the stat command not only lists the contents of the current directory, but also the permissions and size of each file and directory.

We can use man pages to get more information:

stat man command

man stat

Use the command lsattr

The command lsattr It is used to list the attributes of files and directories in Gnu / Linux. For list files and directories in current working directory, we will use this command:

lsattr command

lsattr ./*

See man pages for details about command lsattr.

lsattr man command

man lsattr

Use the command getfacl

The command getfacl together with setfacl, are the most useful and important commands that all system administrators should know when setting access control lists (ACLs).

For view the list of files and directories in the current directory, we will simply execute:

getfacl command

getfacl ./*

It can talk to us about this command use man:

getfacl man command

man getfacl

Use the editor Vim

El editor Vim can also be used for list files and directories. We will only have to write the following, keeping in mind the point after vim:

list files with Vim

vim .

Using the UP / DOWN arrows we can navigate through the list. We will also be able to move through the subdirectories.

These are just a few alternatives to the 'ls' command to list the contents of a directory, which is not other to know.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.