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:
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 *
For display the content of the next level of directories It can be used:
echo */*
For list hidden files we will execute:
echo * .*
Consultation more details in the man pages:
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
For list the contents of a specific directory, you just have to pass the path as an argument:
dir /home/sapoclay/Prueba
We will also be able to list all the content, including files hidden by typing:
dir -a
We can see more details about dir in the man pages:
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 '%s\n' *
For more details, see the man pages:
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 -l '.*' ./*
It can get more information about this command using man:
man grep
Use the command find
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:
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 -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:
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 ./*
See man pages for details about command lsattr.
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 ./*
It can talk to us about this command use man:
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:
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.