Home Linux Commands Different Ways To List Directory Contents Without Using ls Command In Linux

Different Ways To List Directory Contents Without Using ls Command In Linux

By sk
Published: Last Updated on 24.7K views

The "ls" command is the most commonly used command to list files and directories in a directory in Linux and 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 the 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/
List Directory Contents Using ls Command
List Directory Contents Using ls Command

This is how we list files and directories. However, there are ways to do this. As of now, I know the following 11 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 using echo command, just type:

$ echo *
List Files using echo Command
List Files using echo Command

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' *
List Files and Directories using printf Command
List Files and Directories using printf Command

Unlike the echo command, the printf 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:

$ dir
List Files and Directories using dir Command
List Files and Directories using dir Command

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 the find command to list directory contents like below.

$ find -maxdepth 1

Or,

$ find .
List Files and Directories using find Command
List Files and Directories using find Command

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/

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' *
List Files and Directories using stat Command
List Files and Directories using stat Command

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.

The Grep Command Tutorial With Examples For Beginners

To list the directory contents using grep command, just run:

$ grep -l '.*' ./*
List Files and Directories using grep Command
List Files and Directories using grep Command

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 ./*
List Files and Directories using lsattr Command
List Files and Directories using lsattr Command

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 ./*
List Files and Directories using getfacl Command
List Files and Directories using getfacl Command

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
List Files and Directories using for loop
List Files and Directories using for loop

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 the dot (.) at the end.

To list the contents of a specific directory, do:

$ vim <path-to-dir>

Eg:

$ vim ostechnix/
List Files and Directories using Vim Editor
List Files and Directories using Vim Editor

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.

11. List files using tree command

The 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.

$ tree

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.

Conclusion

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 if the ls command is not available in your system, use these alternatives ways to ls without "ls" command.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More