📘Display a List of Folder Names

If you want to display a list of folder (directory) names from a specific location — for example, /home — and output each name on a new line into a text file, follow the steps below.


1. List folder names into a file

Run the following command to list all items in /home and save the output to a file:

ls /home >> data.txt
  • This will append the list of folder names into data.txt.
  • If you want to overwrite instead of append, use > instead of >>:
ls /home > data.txt

2. Display the contents of the file

Once the list has been saved, you can display the results using:

cat data.txt

Each folder (or file) name will appear on a new line.


Optional: Filter only directories

If you want to list only folder names (excluding files), use this:

find /home -maxdepth 1 -type d -exec basename {} \; > data.txt

This will save only the names of directories in /home to data.txt, one per line.

Then view the output:

cat data.txt

Here’s an expanded guide including sorting and filtering options for folder names:


Display a List of Folder Names (with Sorting & Filtering)

1. List all folder names

Basic command to list folder names in /home:

find /home -maxdepth 1 -type d -exec basename {} \; > data.txt

Then display the contents:

cat data.txt

✳️ 2. Sort Folder Names Alphabetically

To sort the folder names alphabetically:

find /home -maxdepth 1 -type d -exec basename {} \; | sort > data.txt
cat data.txt

🔢 3. Sort Folder Names by Last Modified Date

If you want to sort the folders by last modified time (newest first):

ls -lt --group-directories-first /home | grep "^d" | awk '{print $NF}' > data.txt
cat data.txt

To sort from oldest to newest, use:

ls -ltr --group-directories-first /home | grep "^d" | awk '{print $NF}' > data.txt
cat data.txt

🔍 4. Filter Folders by Name

To list only folders that match a pattern, e.g., folders starting with user:

find /home -maxdepth 1 -type d -name 'user*' -exec basename {} \; > data.txt
cat data.txt

📏 5. Filter Folders by Size

To display folders over 1 GB in size:

du -sh /home/* | grep 'G' | awk '{print $2}' | xargs -n1 basename > data.txt
cat data.txt
Scroll to Top