- How to list only files and not directories of a directory Bash?
- 13 Answers 13
- ls command in Linux/Unix
- ls syntax
- ls command options
- ls command examples
- ls code generator
- Learning the dir Command in Linux Through Examples
- Prerequisites
- Listing Files and Folders (Basic dir Command in Linux)
- Listing Files and Directories in a Specific Path
- Listing Hidden Files and Directories
- Retrieving Detailed List of Files and Directories
- Showing File Type Information
- Customizing Output Format
- Ignoring Files with a Specific Pattern
- Sorting the Output of the dir Command
- Conclusion
How to list only files and not directories of a directory Bash?
How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?
13 Answers 13
Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.
After my comment to mklement0’s answer, I realized that «find ./*.png -maxdepth 1 -type f > pngs.txt» would probably accomplish the same. It does. Without installing a script.
@Tim: -type and -maxdepth aren’t options in the normal sense; BSD find (as used on OS X) calls them primaries, and they must come after the filename operand(s) ( . , in this case; note that, unlike on Linux, the BSD version needs at least one explicit filename operand); the command in this answer definitely works on a Mac.
@AlexHall: That’s a clever solution (though I suggest find *.png -maxdepth 0 -type f to avoid the ./ prefix in the output filenames; also note the -maxdepth of 0 , not 1 ), as long as all you need is the file names in alphabetical order. If you want what ls can otherwise do for you (different output format/ordering, inline control over whether hidden items are included or not), supplemented with [multi-]type filtering, the script from my answer can help.
To contrast find * -maxdepth 0 -type f (an alternative derived from @AlexHall’s comment) with find . -maxdepth 1 -type f from the answer: find . . invariably includes hidden items, invariably prefixes output filenames with ./ , and, with GNU find (Linux), typically outputs an unsorted list. find * . , due to letting the shell perform globbing up front, by default excludes hidden items (can be changed with shopt -s dotglob ), outputs mere filenames (no prefix), sorted alphabetically. Neither approach includes symlinks to files; use option -L to do so.
ls command in Linux/Unix
ls is a Linux shell command that lists directory contents of files and directories.
ls syntax
$ ls [options] [file|dir]
ls command options
option | description |
---|---|
ls -a | list all files including hidden file starting with ‘.’ |
ls —color | colored list [=always/never/auto] |
ls -d | list directories — with ‘ */’ |
ls -F | add one char of */=>@| to enteries |
ls -i | list file’s inode index number |
ls -l | list with long format — show permissions |
ls -la | list long format including hidden files |
ls -lh | list long format with readable file size |
ls -ls | list with long format with file size |
ls -r | list in reverse order |
ls -R | list recursively directory tree |
ls -s | list file size |
ls -S | sort by file size |
ls -t | sort by time & date |
ls -X | sort by extension name |
ls command examples
You can press the tab button to auto complete the file or folder names.
List directory Documents/Books with relative path:
List directory /home/user/Documents/Books with absolute path.
List user’s home directory (e.g: /home/user):
List with long format and show hidden files:
Recursive directory tree list:
List only text files with wildcard:
ls redirection to output file:
List files and directories with full path:
ls code generator
Select ls options and press the Generate Code button:
Learning the dir Command in Linux Through Examples
As a system administrator, you’d typically work most of the time in a command line environment, and one essential skill is navigating your file system. With the dir command in Linux, you’re one step closer to mastering that skill.
The dir command allows you to view the contents of a directory, a handy command to know in finding files and folders on your system. And in this tutorial, you’ll learn how to navigate your file system through a series of examples.
Ready? Stay tuned and never be astray in finding your files and folders!
Prerequisites
This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have a Linux system. This tutorial uses Ubuntu 20.04, but any modern Linux distribution will work.
Listing Files and Folders (Basic dir Command in Linux)
The most basic use of the dir command is to list the files and folders in the current directory, which is handy, especially if you’re working in a terminal.
The basic syntax for the dir command is as follows where:
- – is the path to the directory you want to list
- – is an optional parameter to control the behavior of the dir command (will be covered in more detail later).
Suppose you want to see all files and folders under the current working directory. If so, you’ll only need the bare minimum.
Run the dir command below with no options and no path to list all files and folders in your working directory.
The output below shows you the names of all files and folders in your current directory, in this case, your home (root) directory.
Listing Files and Directories in a Specific Path
Perhaps you want to list all files and folders in a specific path rather than the ones in your current working directory. In that case, appending the option comes in handy.
You can use an absolute path or a relative path with the dir command:
- An absolute path is a full path from the root of your file system.
- A relative path is a path from your current working directory.
Run the below dir command to list files and directories under the /etc directory.
Listing Hidden Files and Directories
You may have noticed that when you run the dir command without any options, you’ll only see visible files listed in the output. But what about the hidden files?
In Linux, hidden files and directories start with a ‘.’ (dot) symbol. These files and directories are usually configuration files or folders.
Run the following dir command to list all files and directories, including the hidden ones ( -a ).
The output below shows all files and directories in your current directory, including hidden ones such as .bashrc, a configuration file for the Bash shell.
You can also see two symbolically linked (symlinks) directories at the beginning of the list, which are called:
Symlink | Description |
. | Link to your current directory |
. . | Link to your current directory’s parent directory |
Perhaps you prefer to exclude the symlinks from the list. If so, run the same dir command, but this time, append the -A option in uppercase. dir -A
Retrieving Detailed List of Files and Directories
The output from running the dir command is helpful but can be confusing and challenging to determine which is which, especially when there are many files and directories. As a solution, append the -l option in your dir command.
Run the below command to list all directories and files and their detailed information.
As you can see below, each file and directory is listed on its own line. Notice each item’s format (Desktop, for example) below.
This behavior is advantageous when you want to take a quick look at the permissions on a file or directory.
Permissions | Owner | Group Owner | Size | Last Modified Date and Time |
drwxr-xr-x | root | root | 4096 | Jul 16 16:45 |
Some of these columns might not have any information. Why? Not all file types have permissions, owners, or groups. For example, symbolic links don’t have any of these attributes.
Showing File Type Information
For more experienced Linux users, knowing what file type you’re dealing with can be helpful. If you’re unsure of the file type (i.e., a regular file or a directory), appending the -F option with the dir command will do the trick.
Run the following dir command to list all files and directories, including their file type.
As you can see below, for each file and directory listed, there is a symbol at the end of the line that indicates their file type.
The most common symbols are as follows:
Symbol | File Type Equivalent |
/ | Directory |
* | Executable File |
@ | Symbolic Link |
= | Socket |
| | FIFO (first in, first out) file |
Customizing Output Format
By default, the dir command’s output is formatted in columns. But if you want to see the output differently, the —format option can help.
1. Run the below dir command to list the files and directories in a single column.
As you can see below, the output shows the list of files and directories in one column, making a list more readable.
2. Next, run the same dir command below to list all files and directories. But this time, change the –format option’s value to commas.
This command changes the output’s format to comma-separated values, which can be used for machine parsing.
As you can see below, all files and directories are listed and separated by commas.
3. Now, run the following command to list all files and directories in –verbose to show more information.
Below, you can see the –format=version option outputs the same as dir -l command.
Ignoring Files with a Specific Pattern
The dir command is often used in Bash scripts, to ignoring certain file types might be helpful to speed up the script execution. For example, if you’re looking for all the regular files in a directory, you might want to ignore any symbolic links. Lucky for you, the —ignore option lets you do the magic.
Run the below dir command to list all files and directories in the current directory, except for any symbolic links ( @ ).
As you can see below, only regular files and directories are listed, and any symbolic links that might be present in the directory are ignored.
Now, run the following command to list all files and directories while ignoring any executable files ( *.exe ).
You can confirm in the output below that this command extensively narrows files and directory listings since you’re excluding executable files.
Sorting the Output of the dir Command
Naturally, when you run the dir command, files and directories are listed in ascending order alphabetically. But perhaps you want to list the files and directories in a certain order, for example, in size order or by the last modified date.
In that case, you can use other options for the dir command to sort the list of files and directories in the output.
Run the below command to list all files and directories in the current directory by size, with the largest ones listed first.
Finally, run the following dir command to list files and directories sorted by time. Note that the topmost file or directory is the one that was most recently modified.
Conclusion
The essential skill any Linux user needs is the ability to list the files and directories in their file system. And In this tutorial, you’ve looked at some of the more common options for the dir command in Linux to navigate your file system effectively.
At this point, you should be confident enough to work with your file system via your terminal as long as you have the dir command. Why not try writing a script that automatically finds files and directories to manage on your system?
Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.