The "ls" command is the most commonly used command to list files and directories in a directory in Unix-like systems. However, there are other ways to list directory contents without using ls command. As you may know, many alternative programs exists to the most popular and widely used existing Linux programs. We have already posted about good alternatives to ‘top’, alternatives to 'du' command and alternatives to 'man' pages. Today, in this tutorial, we will see 11 alternatives ways to list the contents of a directory without using "ls" command in Linux.
List Directory Contents Without Using ls Command
Usually, most of the Linux users use ls command to list files and directories in a directory. For example, we can use the following command to display the contents of a directory named ostechnix.
$ ls ostechnix/
This is how we list files and directories. However, there are ways to do this. As of now, I know the following ten methods only. If I come across any other better method, I will add them too. Also, if you know other ways to do this, please let us know in the comment section below.
1. List files using 'echo' command
The echo command is most commonly used built-in command that is used in shell scripts and batch programs to print the given arguments. You can pass any text/string as an argument and the echo command will display them on standard output or file. This command is not just for printing the given arguments but also can be used to list files.
To list files using in the current directory, just type:
$ echo *
To list all files down one level, use this command:
$ echo */*
To list hidden files, run:
$ echo * .*
For more details, refer man pages.
$ man echo
As you can see, the echo command will only display files, not directories. No problem, the following methods covers how to list both files and directories. Read on.
2. List files and directories using 'printf' command
The printf command is used to format and print text. This command will print the given arguments according to the given format, or execute according to given option.
To list the contents of the current directory using printf command, run:
$ printf '%s\n' *
Unlike the "echo" command, the print command will display both files and directories.
For more details, refer man pages.
$ man printf
3. List files and directories using 'dir' command
The dir command is yet another Linux command to display the contents of a directory. It is often considered to be Windows equivalent, but works in Linux pretty much the same way.
To list the current directory contents, run:
To list the contents of a specific directory, just pass its path as an argument like below.
$ dir /home/sk/ostechnix
To list all contents including hidden files:
$ dir -a
Refer man pages to know more about dir command.
$ man dir
4. List files and directories using 'find' command
The find command is used to search for files in a directory hierarchy. Not just for file searching, we can also use find command to list directory contents like below.
$ find -maxdepth 1
$ find .
As you can see, the first command lists all files and directories down one level in the current directory whereas the second command lists all files and directories recursively.
You can list the contents of a specific directory as well.
$ find dir1/
- How To Find Files Based On their Permissions In Linux
- How To Find Files Bigger Or Smaller Than X Size In Linux
- How To Find And Delete Files That Contains A Specific Text In Their Names In Linux
- How To Find The Oldest File In A Directory Tree In Linux
- How To Find and Copy Certain Type Of Files From One Directory To Another In Linux
- How To Find And Delete Files Older Than X Days In Linux
5. List files and directories using 'stat' command
The stat command is used to display file and filesystem information. Using this command, we can find file properties such as size, permissions, creation and access dates among others.
To list files and directories using "stat" command,
$ stat -c '%A %n' *
As you can see, the stat command not just lists the contents of the current directory, but also the permissions. It is useful when you wanted to view the directory contents including the permissions.
Check man pages for more details about stat command.
$ man stat
6. List files and directories using 'grep' command
The grep command is used to search text files with regular expressions. We wrote a detailed guide about grep usage a while ago. Check the following link to know more about grep command and its variants.
To list the directory contents using grep command, just run:
$ grep -l '.*' ./*
7. List files and directories using 'lsattr' command
The lsattr command is used to list the attributes of files and directories on Linux.
To list files and directories in a current working directory, use lsattr command:
$ lsattr ./*
As you see in the output, the lsattr command displays contents just down one level. In other words, it won't display any sub-directories and its contents and hidden files.
Check man pages for details about lsattr command.
$ man lsattr
8. List files and directories using 'getfacl' command
The setfacl and getfacl are most useful and important commands every system admins must know to set Access control lists(ACLs) of files and directories.
To view the list of files and directories in a current directory, just run:
$ getfacl ./*
One thing I like most about this command is it not just lists the directory contents, but also other useful details such as,
- owner of the file/directory,
- group which has access to it,
- access rights to owner, group and others.
9. List files and directories using 'for loop'
Another method to list directory contents is to use for loop. I tested this in BASH and it worked just fine as expected.
$ for i in *; do echo $i; done
As you see in the above output, we can list the current directory contents using for loop.
10. List files and directories using 'Vim' editor
Vim editor is not just for viewing and editing files. It can also be used to list files and directories. As you may already know, everything in Linux is a file. So technically speaking, a directory is a file that contains a list of files and/or sub-directories.
To list files and directories of current directory using Vim editor, use the following command:
$ vim .
Please note that dot (.) at the end.
To list the contents of a specific directory, do:
$ vim <path-to-dir>
$ vim ostechnix/
Just use the UP/DOWN arrows to navigate through the list. To list a sub-directory contents, just place mouse cursor on it and hit ENTER.
- How To View The Contents Of An Archive Or Compressed File Without Extracting It
- How To Edit Multiple Files Using Vim Editor
- PacVim – A CLI Game To Learn Vim Commands
- How To Comment Out Multiple Lines At Once In Vim Editor
- How To Use Vim Editor To Input Text Anywhere
- How To Use Spell Check Feature In Vim Text Editor
11. List files using 'Tree' command
Tree command is used to view the contents of a directory (and its sub-directories) in a tree-like format.
If you run the tree command without any options, it will display all files and folders (and sub-folders) of the current working directory.
To list the contents of a directory down one level, run:
$ tree -L 1
For more details about the Tree command, refer the following guide.
And, that's all for now. I don't know any practical use cases for these methods right now. The ls command comes pre-installed in almost all Unix-like operating systems, so you can simply use it to easily display directory contents. Just in case the "ls" command is not available in your system, use these alternatives ways to ls without "ls" command.