DEV Community

Beta Shorts
Beta Shorts

Posted on

2 1 1 1 1

Building a Simple Terminal-Based File Manager in Bash

A while ago, I found myself juggling between ls, cd, rm, and mv commands just to move and manage files across different directories. It felt inefficient—why not have a lightweight file manager right inside the terminal?

If you’ve ever wished for a simpler way to navigate and manipulate files without leaving the terminal, this guide will show you how to build a basic file manager in Bash—one that lets you browse, create, delete, and move files interactively.


Understanding the Core of a Terminal File Manager

A terminal-based file manager is essentially a Bash script that allows users to navigate directories and manage files using menus and keyboard inputs.

What It Should Do

Display a list of files and directories

Let users navigate between folders

Provide options to delete, move, or rename files

Offer a simple interface using the terminal

This can be accomplished using a combination of Bash commands like ls, cd, rm, and mv, along with interactive menus using select or read.


Step 1: Listing Files and Directories

The first step is displaying the current directory contents in a structured way. Instead of using a plain ls, let’s format the output to distinguish between files and directories.

for item in *; do
    if [ -d "$item" ]; then
        echo "[DIR]  $item"
    else
        echo "[FILE] $item"
    fi
done
Enter fullscreen mode Exit fullscreen mode

🔹 Why this matters:

  • Helps differentiate files from directories visually.
  • Avoids clutter when dealing with large folders.

Step 2: Navigating Between Directories

A simple file manager must allow navigation. Instead of typing cd folder, we can make the script prompt users for input.

echo "Enter a directory name to navigate into:"
read dir
if [ -d "$dir" ]; then
    cd "$dir"
else
    echo "Invalid directory!"
fi
Enter fullscreen mode Exit fullscreen mode

🔹 Why this matters:

  • Allows movement between folders without retyping commands.
  • Adds error handling for invalid inputs.

Step 3: Deleting Files Safely

Instead of manually typing rm filename, add a safety confirmation before deleting a file.

echo "Enter the filename to delete:"
read file
if [ -f "$file" ]; then
    echo "Are you sure? (y/n)"
    read confirm
    if [ "$confirm" == "y" ]; then
        rm "$file"
        echo "File deleted."
    fi
else
    echo "File does not exist!"
fi
Enter fullscreen mode Exit fullscreen mode

🔹 Why this matters:

  • Prevents accidental file deletions.
  • Adds a confirmation step for safety.

Step 4: Moving and Renaming Files

A file manager should allow renaming and moving files interactively.

echo "Enter the file to rename:"
read oldname
if [ -f "$oldname" ]; then
    echo "Enter new name:"
    read newname
    mv "$oldname" "$newname"
    echo "File renamed successfully."
else
    echo "File does not exist!"
fi
Enter fullscreen mode Exit fullscreen mode

🔹 Why this matters:

  • Simplifies renaming without manually typing mv old new.
  • Handles errors gracefully if the file doesn’t exist.

Step 5: Adding a Simple Menu Interface

Instead of running commands manually, let users pick an action.

echo "Select an option:"
echo "1) List files"
echo "2) Change directory"
echo "3) Delete a file"
echo "4) Rename a file"
echo "5) Exit"
read choice

case $choice in
    1) ls ;;
    2) echo "Enter directory:"; read dir; cd "$dir" ;;
    3) echo "Enter filename:"; read file; rm "$file" ;;
    4) echo "Enter old filename:"; read old; echo "Enter new filename:"; read new; mv "$old" "$new" ;;
    5) exit ;;
    *) echo "Invalid choice." ;;
esac
Enter fullscreen mode Exit fullscreen mode

🔹 Why this matters:

  • Creates an interactive experience instead of manually typing commands.
  • Makes the script feel like a real file manager.

Enhancing the File Manager

Once you have the basics working, here are ways to improve it:

Color-coded output: Highlight directories and files differently using ANSI colors.

Search functionality: Let users search for files by name.

Bulk operations: Allow multiple files to be selected and moved at once.

Terminal UI libraries: Use dialog or whiptail for a more user-friendly interface.


Final Thoughts: Build Your Own Bash Utility

This project is a great hands-on exercise for improving your Bash scripting skills. By building a basic terminal-based file manager, you get practical experience with:

Handling user input interactively

Working with files and directories efficiently

Creating scripts that automate real-world tasks

If you're new to Bash scripting, this project may feel a bit overwhelming. To get started faster with the basics, check out our Bash Scripting Cheat Book.


Want a Quick Reference for Bash Basics?

📌 If you’re just getting started with Bash and need a beginner-friendly guide, our Bash Scripting Cheat Book is designed to help you learn the fundamentals quickly.

👉 Get the Bash Scripting Cheat Book for just $3.99

📌 This book is NOT for advanced Bash topics like animations or UI enhancements—it’s a quick reference to help you understand Bash basics faster.


Discussion: How Would You Improve This File Manager?

Drop a comment below if you have ideas for additional features, or if you’ve built something similar in Bash!

Top comments (0)

👋 Kindness is contagious

Please consider leaving a ❤️ or a friendly comment if you found this post helpful!

Okay