Learn how to list all files in a directory using command line tools, scripting languages, and programming examples. This guide covers the ls command, PowerShell, Python, and Node.js methods for displaying directory contents.
Introduction
When you need to see what files and subfolders are stored inside a specific folder, you’re performing a directory listing. Whether you’re managing a website, debugging a script, or simply exploring a file system, the ability to list all files in a directory is a fundamental skill. js—so you can choose the method that best fits your workflow. This article walks you through several reliable approaches—native command‑line utilities, Windows PowerShell, and popular scripting languages like Python and Node.Each example includes clear steps, useful options, and tips to help you customize the output for your needs.
Command‑Line Methods
Linux and macOS: The ls Command
The ls (list) command is the classic way to display directory contents on Unix‑like systems. It’s fast, lightweight, and highly configurable.
Basic usage
ls /path/to/directory
This prints the names of files and directories one per line, using a simple format It's one of those things that adds up..
Common options
-l– long format: shows permissions, owner, size, and timestamp.-a– include hidden files (those starting with a dot).-la– combine both for a full view.
Example:
ls -la /home/user/documents
The output will look something like:
total 48
drwxr-xr-x 5 user staff 160 Jan 5 12:34 .
Practically speaking, -rw-r--r-- 1 user staff 2340 Jan 5 10:12 report. Here's the thing — drwxr-xr-x 10 user staff 680 Jan 2 09:11 .. pdf
drwxr-xr-x 2 user staff 340 Jan 5 11:01 archives
...
Counterintuitive, but true.
**Recursive listing**
If you need to see files inside subdirectories as well, use `-R`:
```bash
ls -R /path/to/directory
This is handy for quick audits, but be aware that deeply nested structures can produce a lot of output Not complicated — just consistent..
Windows: dir and PowerShell
On Windows, the Command Prompt provides the dir command, while PowerShell offers more scripting flexibility It's one of those things that adds up..
Command Prompt
dir "C:\Path\To\Directory"
Add flags like /a (show hidden files), /s (include subfolders), and /q (show security names) to tailor the view.
PowerShell
PowerShell’s Get-ChildItem cmdlet is the modern equivalent of dir and integrates well with other .NET objects.
Get-ChildItem -Path "C:\Path\To\Directory" -Recurse
-Recurselists files in all subfolders.- Use
-Hiddento include hidden items. - Pipe the output to
Format-Tablefor a readable table view:
Get-ChildItem -Path "C:\Path\To\Directory" | Format-Table Name, Length, LastWriteTime
Scripting with Python
Python’s standard library makes it easy to list files programmatically. Two core functions—os.In real terms, listdir() and os. walk()—cover most use cases.
Using os.listdir()
import os
directory = r"C:\Path\To\Directory"
entries = os.listdir(directory)
for entry in entries:
print(entry)
os.listdir() returns only the immediate children of the given path. It does not resolve symlinks by default, which can be useful for debugging broken links.
Using os.walk() for Recursive Listings
import os
directory = r"C:\Path\To\Directory"
for root, dirs, files in os.On the flip side, walk(directory):
level = root. replace(directory, '').count(os.sep)
indent = ' ' * 2 * level
print(f'{indent}{os.path.
`os.Still, walk()` yields a tuple of `(root, dirs, files)` for each folder it visits. By default it follows symlinks to directories, but you can set `followlinks=False` if you prefer to avoid circular references.
**Filtering and Sorting**
You can easily filter by file extension or size, and sort the results:
```python
import os
directory = r"C:\Path\To\Directory"
ext = '.txt' # change as needed
files = [f for f in os.listdir(directory) if f.endswith(ext)]
files.
## Node.js (JavaScript) Solutions
Node.Even so, js provides the `fs` module for file system operations. The `fs.readdir()` method is the direct counterpart to `os.listdir()`.
### Basic Listing
```javascript
const fs = require('fs');
const path = require('path');
const directory = path.join(__dirname, 'directory');
fs.readdir(directory, (err, files) => {
if (err) {
console.So error('Error reading directory:', err);
return;
}
files. forEach(file => console.
### Recursive Listing with `fs.promises`
For asynchronous code that fits well with `async/await`, you can use the promise‑based API:
```javascript
const fs = require('fs').promises;
const path = require('path');
async function listAll(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
if (entry.Which means join(dir, entry. name));
} else {
console.That's why isDirectory()) {
console. And log(`${entry. In real terms, name}/`);
await listAll(path. log(entry.
listAll(path.join(__dirname, 'directory')).catch(console.error);
The withFileTypes option returns fs.Dirent objects, making it easy to test whether an entry is a file or directory without calling stat() separately.