Description
Your task is to implement a simple UNIX command parser and file system. Your program will have to implement a file system that can be modified via commands.
The starting directory of your file system is an empty root directory /
, with no subdirectories or files. Your program must be capable of handling the following commands:
Commands
cd <dirpath>
- Change the current directory
<dirpath>
will be a list of directory names or "..", seperated by "/"- e.g.
cd ../folder1/folder2
means to navigate up one directory level, then descend intofolder1
, then descend intofolder2
- If the directory path is not valid, then the command will do nothing
touch <filename>
- Create a new file
- e.g.
touch me.txt
creates a file calledme.txt
in the current directory - Filenames will only contain characters a-z and "."
- Filenames will contain "." at least once
- "." will never be the first or last character in a file name
- If a file already exists with the same name in the current directory, nothing happens
mkdir <dirname>
- Create a new directory
- e.g.
mkdir photos
creates a newphotos
directory in the current directory - Directory names will only contan characters a-z
- If a directory already exists with the same name in the current directory, nothing happens
rm [-r] <filename-or-dirname>
- Remove a file or directory
- e.g.
rm hello.mp3
removes a file namedhello.mp3
in the current directory - e.g.
rm -r documents
removes a folder nameddocuments
in the current directory, and all of its contents - If
rm
tries to delete a directory without the-r
flag, nothing will happen- However
rm
will delete a file even with the-r
flag
- However
- If the specified directory or file cannot be found, nothing happens
Tree output
Your program will output the following tree-like representation of the current file system using spaces as indentation. For example:
/
documents
document.docx
downloads
zippedfile
notavirus.exe
coolgoats.mp3
zippedfile.zip
pictures
myvacation.png
All directories names must end with a "/"no longer necessary- You may use any number of spaces to indent the tree (minimum 1)
- Directories must be listed before files in the same directory
Directories and files should be listed in lexographical alphabetical orderThe character "." lexographically comes before any alphabetical character
- You may output the contents of directories in any order you wish
Challenge
Create a program that accepts a series of commands, and outputs a tree-like representation of the current file system.
Input
The first line of input will be an integer N
. Following will be N
lines, each containing a command as described above.
You may feel free to omit the number N
from your input if it is not necessary
Slight variations are allowed (using commas to seperate commands, input as a list etc) as long as it's reasonable
Output
The contents of the current file system in a tree-like representation, as described above.
Test Cases
Input 1: Simple example from earlier
15
mkdir documents
cd documents
touch document.docx
cd ..
mkdir downloads
cd downloads
touch coolgoats.mp3
touch zippedfile.zip
mkdir zippedfile
cd zippedfile
touch notavirus.exe
cd ../..
mkdir pictures
cd pictures
touch myvacation.png
Output 1:
/
documents
document.docx
downloads
zippedfile
notavirus.exe
coolgoats.mp3
zippedfile.zip
pictures
myvacation.png
Input 2: Incorrect commands and edge cases
12
mkdir folder1
mkdir folder1
mkdir folder2
rm folder1
rm -r folder2
cd ..
cd ../folder1
cd folder1/folder2
touch file.txt
touch file.txt
touch file2.txt
rm -r file2.txt
Output 2:
/
folder1
file.txt
Input 3:
Alphabetical listing of directories and files no longer necessary
8
mkdir b
mkdir c
mkdir a
touch c.txt
touch aa.txt
touch b.txt
touch a.txt
touch ab.txt
Output 3:
/
a
b
c
a.txt
aa.txt
ab.txt
b.txt
c.txt
Input 4: Partially correct cd should not be parsed (Suggested by @Arnauld)
4
mkdir folder1
cd folder1
cd ../folder2
touch file.txt
Output 4:
/
folder1
file.txt
Input 5: Partially correct cd should not be parsed (Suggested by @Abigail)
3
mkdir foo
cd bar/../foo
touch file.txt
Output 5:
/
foo
file.txt
Standard loopholes and I/O rules apply.
This is code-golf, shortest code wins.
Edit: output requirements have been relaxed a little bit
mkdir foo
, doescd bar/../foo
descend intofoo
, or is this an illegal path and thus nothing happens? \$\endgroup\$touch
description -Filenames will contain "." at least once
so names for dirs and files will never overlap \$\endgroup\$