How to Generate a FileName Listing Quickly in Windows, macOS, and LinuxCreating a filename listing — a simple list of files and optionally folders within a directory — is a common task for inventorying, auditing, sharing, or processing files. Below are fast, practical methods for Windows, macOS, and Linux covering built-in tools, brief scripting examples, and tips for common options like recursion, filtering, and exporting to CSV or JSON.
1) What to consider before you start
- Purpose: Do you need names only, full paths, sizes, dates, or metadata?
- Depth: Single directory or recursive (include subfolders)?
- Output format: Plain text, CSV, JSON, Excel-friendly, or piping into other tools?
- Encoding: Use UTF-8 for portability, especially with non-ASCII filenames.
- Permissions: Ensure you have read access to directories and files.
2) Windows
Using File Explorer (quick, manual)
- Open a folder, press Ctrl+A to select, hold Shift and right-click → “Copy as path” to copy full paths of selected files to clipboard.
- Paste into Notepad or Excel and clean up if you need only filenames (use Excel formulas to strip paths).
Using Command Prompt (cmd)
- Non-recursive filenames only:
dir /b > filenames.txt
- Recursive (include subdirectories):
dir /b /s > filenames.txt
- Include file sizes and dates (human-readable):
dir /s > dirfull.txt
- Notes: /b gives bare format (names only). /s recurses subdirectories.
Using PowerShell (recommended for flexibility)
- Non-recursive, filenames only:
Get-ChildItem -File | Select-Object -ExpandProperty Name > filenames.txt
- Recursive, full paths:
Get-ChildItem -Recurse -File | Select-Object -ExpandProperty FullName > filenames.txt
- Recursive with size and last write time, export to CSV:
Get-ChildItem -Recurse -File | Select-Object FullName, Length, LastWriteTime | Export-Csv -Path files.csv -NoTypeInformation -Encoding UTF8
- Filter by extension:
Get-ChildItem -Recurse -File -Filter *.pdf | Select-Object FullName > pdf_list.txt
3) macOS
Using Finder (manual)
- Select files, right-click → “Copy” then paste into a text editor — Finder copies icons, so prefer Terminal commands for textual listings.
Using Terminal (zsh/bash)
- Non-recursive filenames only:
ls -1 > filenames.txt
- Recursive with paths:
find . -type f > filenames.txt
- Limit depth:
find . -maxdepth 1 -type f > filenames.txt
- Include file size and modification time (tab-separated):
find . -type f -print0 | xargs -0 stat -f "%N %z %m" > files.tsv
- Export to CSV (with header):
printf "path,size,mtime " > files.csv find . -type f -print0 | xargs -0 stat -f ""%N",%z,%m" >> files.csv
- Notes: macOS stat format differs from Linux; use -f with format tokens.
4) Linux
Using Terminal (bash)
- Non-recursive filenames:
ls -1 > filenames.txt
- Recursive with full paths:
find /path/to/dir -type f > filenames.txt
- Include size and modification time:
find /path/to/dir -type f -printf "%p %s %TY-%Tm-%Td %TH:%TM:%TS " > files.tsv
- Export to CSV (escaping quotes):
printf "path,size,mtime " > files.csv find /path/to/dir -type f -printf ""%p",%s,%TY-%Tm-%Td %TH:%TM:%TS " >> files.csv
- Use GNU tools like tree for a tidy view:
tree -if --noreport /path/to/dir > tree_listing.txt
5) Cross-platform scripting
Python (works on Windows/macOS/Linux)
- Basic recursive listing to CSV:
#!/usr/bin/env python3 import os, csv root = "/path/to/dir" with open("files.csv", "w", newline="", encoding="utf-8") as csvfile: writer = csv.writer(csvfile) writer.writerow(["path","size","mtime"]) for dirpath, _, filenames in os.walk(root): for f in filenames: full = os.path.join(dirpath, f) stat = os.stat(full) writer.writerow([full, stat.st_size, int(stat.st_mtime)])
- Notes: Replace root with “.” for current directory.
Node.js (for JavaScript environments)
- Example using fs and path modules to write JSON or CSV. (Not shown — can provide if needed.)
6) Filtering and patterns
- By extension: PowerShell -Filter or Get-ChildItem .pdf; find -name “.pdf” on Unix.
- By date: PowerShell’s Where-Object with LastWriteTime; on Unix, use find -mtime/-newermt.
- By size: PowerShell -Where Length -gt X; find -size on Unix.
7) Encoding and CSV pitfalls
- Use UTF-8 when filenames contain non-ASCII. PowerShell Export-Csv supports -Encoding UTF8.
- Enclose paths in quotes in CSV and escape quotes inside names. Python csv module handles this.
8) Examples of common tasks (quick snippets)
- Get only top-level filenames (macOS/Linux):
find . -maxdepth 1 -type f -printf "%f " > top_files.txt
- Create a list of file paths with relative paths (Windows PowerShell):
Get-ChildItem -Recurse -File | ForEach-Object { $_.FullName.Replace($PWD.Path + "", "") } > rel_paths.txt
- Produce a JSON array of file objects (Python):
import os, json out=[] for dirpath, _, filenames in os.walk("."): for f in filenames: p=os.path.join(dirpath,f) s=os.stat(p) out.append({"path":p,"size":s.st_size,"mtime":int(s.st_mtime)}) with open("files.json","w",encoding="utf-8") as j: json.dump(out,j,ensure_ascii=False,indent=2)
9) Speed and performance tips
- Avoid spawning a new process per file when possible (use find -printf or Python os.walk).
- For very large trees, stream output to a file and avoid loading everything into memory.
- Use parallel or xargs -P for CPU-bound metadata operations but be cautious with disk I/O.
10) Troubleshooting
- “Permission denied”: run with rights or skip errors (find 2>/dev/null).
- Weird characters: ensure locale/terminal supports UTF-8.
- CRLF issues: normalize when transferring between Windows and Unix.
11) Quick reference table
Platform | Command (recursive) | Outputs |
---|---|---|
Windows (PowerShell) | Get-ChildItem -Recurse -File | Full paths, can export CSV |
Windows (cmd) | dir /b /s | Bare list or full listing |
macOS/Linux | find . -type f | Full relative paths, flexible printf |
Cross-platform | Python os.walk script | Custom CSV/JSON with metadata |
12) Final notes
- For one-off needs, built-in commands (dir/ls/find/PowerShell) are fastest. For repeatable, shareable outputs or metadata-rich lists use PowerShell Export-Csv or a small Python script.
- If you want, tell me your OS and exact requirements (recursive? extensions? CSV/JSON?) and I’ll give a ready-to-run command or script tuned to your case.
Leave a Reply