Listing Files & Directories

8 min read·Jan 1, 2025

To output the list of files and directories contained in the current working directory, you can use the ls command — which stands for list:

$ ls

Example

Let's consider this filesystem representation, in which the current working directory is the home directory:

/
├── home
│   └── johndoe         ← Current working directory
│   │   ├── Documents
│   │   └── Downloads

Executing this command will list the content of the /home/johndoe directory in the form of columns:

$ ls
Documents     Downloads

Listing the content of directories

To list the content of one or more directories, you can use the ls command as follows:

$ ls [path ...]

Where path ... is a list of absolute or relative paths to the target directories, separated by a space character.

Note: If the specified paths don't match valid directories in the filesystem, the ls command will output this error message:

ls: path: No such file or directory

Example

Let's consider this filesystem representation, in which the current working directory is the home directory:

/
├── home
│   └── johndoe                  ← Current working directory
│   │   ├── Documents
│   │   │   └── Invoices         ← Target directory
│   │   │   │  └── invoice.pdf
│   │   └── Downloads            
│   │   │   └── ebook.pdf

Executing this command will list the content of the Invoices directory:

$ ls ~/Documents/Invoices
invoice.pdf

And executing this command will list the content of both the Downloads and Invoices directories:

$ ls ./Downloads ./Documents/Invoices
./Downloads:
ebook.pdf

./Documents/Invoices:
invoice.pdf

Listing the content of subdirectories

To list the content of directories, including the content of their subdirectories, and so on, you can use the ls command with the -R flag (short for recursive) as follows:

$ ls -R [path ...]

Example

Let's consider this filesystem representation, in which the current working directory is the home directory:

/
├── home
│   └── johndoe                  ← Current working directory
│   │   ├── Documents
│   │   │   └── Invoices
│   │   │   │  └── invoice.pdf
│   │   └── Downloads            
│   │   │   └── ebook.pdf

Executing this command will recursively list the content of the home directory, including the content of its subdirectories:

$ ls -R
Documents	Downloads

./Documents:
Invoices

./Documents/Invoices:
invoice.pdf

./Downloads:
ebook.pdf

Changing the display of entries

Displaying entries as a list

To list the content of a directory in the form of a list instead of columns, you can use the ls command with the -1 flag as follows:

$ ls -1 [path ...]

Example

Let's consider this filesystem representation, in which the current working directory is the home directory:

/
├── home
│   └── johndoe                  ← Current working directory
│   │   ├── node-project
│   │   │   ├── index.js
│   │   │   ├── logs
│   │   │   ├── node_modules
│   │   │   └── package.json

Executing this command will output the content of the node-project directory in the form of a list:

$ ls -1 node-project
index.js
logs
node_modules
package.json

Distinguishing files from directories

To help you distinguish more easily regular files from directories, you can use the ls command with the -p flag as follows:

$ ls -p [path ...]

Which will add a forward slash character / at the end of each entry that is a directory.

Example

Let's consider this filesystem representation, in which the current working directory is the home directory:

/
├── home
│   └── johndoe                  ← Current working directory
│   │   ├── node-project
│   │   │   ├── index.js
│   │   │   ├── logs
│   │   │   ├── node_modules
│   │   │   └── package.json

Executing this command will output the content of the node-project directory and add a forward slash to the logs and node_modules subdirectories:

$ ls -p node-project
index.js     logs/     node_modules/     package.json

Listing hidden files

On Unix-like operating systems, hidden files and directories, also called dotfiles, are not characterized by the presence of a property that can be turned on and off, but instead, by the presence of a dot character at the beginning of their file name (e.g., .env, .config).

To list the content of a directory, including hidden files and subdirectories, you can use the ls command with the -A flag (short for all) as follows:

$ ls -A [path ...]

Example

Let's consider this filesystem representation, in which the current working directory is the home directory:

/
├── home
│   └── johndoe                  ← Current working directory
│   │   ├── node-project
│   │   │   ├── config
│   │   │   │   ├── .env.development
│   │   │   │   └── .env.production
│   │   │   ├── index.js
│   │   │   └── package.json

Executing this command will not output any files:

$ ls ./node-project/config

Whereas executing this command will display both .env files:

$ ls -A ./node-project/config
.env.development     .env.production

Summary

Here's a summary of what you've learned in this lesson:

  • The ls command outputs the content of directories.
  • The ls -R command outputs the content recursively.
  • The ls -1 command outputs the content in the form of a list.
  • The ls -p command adds a forward slash (/) to subdirectories.
  • The ls -A command includes hidden files starting with a dot (.).

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Listing Files & Directories in Bash | Backend Brewery