A console-based in-memory file system built using core Data Structures like Tree, Trie, HashMap and Algorithms like String Parsing, Recursion, DFS.
Simulates Unix-like file operations such as mkdir, cd, ls, touch, etc. Without using the actual file system.
Design and implement a file system that:
- Maintains a virtual folder/file structure in memory.
- Allows users to create, delete, and navigate directories.
- Stores file metadata and simulates basic content handling.
- Lookup and Traversal should be fast.
- Allows Unix-Like file operations such as
mkdir,cd,cat,echo. - Built using OOP principle.
CommandInvoker.java: Implements the invoker part of the Command Pattern. It stores and executes commands provided by the user interface (like mkdir, cd, ls, etc.).
Command.java: Interface for all user commands in the system.MkdirCommand.java,CdCommand.java,LsCommand.java,etc: Concrete command classes implementing theCommand.javainterface. Each class represents a specific file system command and delegates the logic to the correspondingFileSystemOperation.java.
- core package
FileSystemState.java: Maintains the current state of the file system, including the root node, current working directory, undo/redo stacks, and Trie root. Acts as a shared context passed to all file system operations.
- helper package
Helper.java: Provides utility functions to resolve file paths (both absolute and relative), compute absolute paths, and insert entries into the Trie for auto-completion support.
- operations package
FileSystemOperation.java: Interface representing a file system operation.MkdirOperation.java,CdOperation.java,TouchOperation.java, etc: Each class implements theFileSystemOperation.javainterface to encapsulate a specific file system behavior (like creating directories, changing directories, file creation, content writing, deletion, etc.). These operations modify the file system state and support undo/redo functionality.FileSystem.java: Core class that maintains the reference to the current file system state. Provides methods to get/set current directory, root node, and manages undo/redo stacks. Executes operations via theFileSystemOperation.javainterface, acting as the central orchestrator of all file system changes.Action.java: Represents a reversible operation for supporting undo/redo functionality.TrieNode.java: Represents a node in a trie (prefix tree) structure used for efficient file/folder name search. Each node maintains its children and a flag to mark the end of a word. Supports auto-complete and fast lookup features within the file system.
Node.java: Core building block of the in-memory file system. Represents both files and folders using a unified structure. Each node stores metadata such as name, content (for files), child nodes (for directories), parent reference, and optional symbolic link support.
- Entry point of the application. Initializes the
FileSystem.java, reads user commands from input, and routes them through theCommandInvoker.javafor execution. Simulates a terminal-like interface for interacting with the in-memory file system.
- KMP (Knuth-Morris-Pratt) Pattern Matching
Used in thesearchcommand to efficiently match substrings inside file contents. Built from scratch with custom LPS (Longest Prefix Suffix) construction for optimal linear-time performance. - DFS (Depth-First Search)
Used in operations liketree,find, andsearchto recursively traverse the file system hierarchy, exploring all nested directories and files. - Trie (Prefix Tree)
Used to index file and folder names for fast autocomplete/search functionality (future extension ready). Trie insertion logic is handled during file/folder creation. - Stack (Undo/Redo Functionality)
Two stacks are used to implementundoandredooperations, allowing users to reverse or reapply changes with precision. - HashSet / HashMap
Used for visited tracking in DFS (to prevent infinite cycles) and fast lookups of child nodes in directories.
- Command Pattern
Every command (mkdir,cd,touch,echo, etc.) implements a common interface (Command) and delegates logic to operation classes likeMkdirOperation,CdOperation, etc., encapsulating request execution and promoting extensibility. - Separation of Concerns (SOC)
Clear separation between command parsing (CommandInvoker), execution logic (FileSystemOperationclasses), and state management (FileSystemState). - Single Responsibility Principle (SRP)
Each class has one focused responsibility. e.g.,Helperonly deals with path resolution and trie updates,Actiononly represents undo/redo state, etc. - Open-Closed Principle (OCP)
New commands can be added without modifying existing code – just create a newCommand+ correspondingOperationclass. - Immutability & Encapsulation
Classes likeNoderestrict direct external modification through proper getter/setter encapsulation.
- Prerequisites:
- Java 17 or later installed.
- VS Code (recommended) or any Java-supported IDE.
- Clone the Repository:
git clone https://github.com/Ansh2625/In-Memory-File-System
cd In-Memory-File-System- Import the Project:
- Open VS Code.
- Select
File > Openand choose the project's root folder (In-Memory-File-System).
- Build the Project:
- Open
Main.javafile inside src. - Click the Run button on top, or right-click and choose Run java.
- Run the Application:
- Right-click on
Main.java. - Alternatively, use the terminal:
javac -d bin src/Main.java
java -cp bin Main- Interact with the System:
The system simulates in-memory file system operations. Check console logs for actions like file/folder creation, navigation, content manipulation, search, and undo/redo execution.
- Input File: TestCase.txt
- Output File: SampleOutput.txt
- In-Memory File System supporting dynamic creation and manipulation of files and directories.
- Basic UNIX-like Shell Commands:
mkdir,cd,ls,touch,echo,cat,rm,tree,pwd, etc. - Symbolic Link (
ln) Support: Create and traverse symbolic links like in Linux. - Command Pattern Architecture: Each shell command is modular and follows the command design pattern for extensibility.
- Undo/Redo Functionality: Supports stepwise rollback or re-application of actions like mkdir, touch, rm, echo.
- Content Search using KMP: Efficient substring matching in file contents using the Knuth-Morris-Pratt algorithm.
- Auto-Complete Support: Simulates shell-like suggestions using Trie data structure.
- Recursive Tree Display: Visualize file structure like tree command in Linux.
- Persistence Layer: Integrate file saving/loading functionality using serialization to preserve state across sessions.
- User & Permissions System: Add support for multiple users, authentication, and Unix-style permission control (rwx).
- Disk Quota Simulation: Impose limits on storage size per folder/user to mimic real file system constraints.
- Timestamps & Metadata: Store and display creation/modification timestamps for files and directories.
This project is open-source and available under the MIT License.
Developed by Ansh Sharma. Contact, anshsharma2625@gmail.com.
