Simplify the directory path (Unix like)

Given an absolute path for a file (Unix-style), simplify it. Note that absolute path always begin with ‘/’ ( root directory ), a dot in path represent current directory and double dot represents parent directory.

Examples:

"/a/./"   --> means stay at the current directory 'a'
"/a/b/.." --> means jump to the parent directory
              from 'b' to 'a'
"////"    --> consecutive multiple '/' are a  valid  
              path, they are equivalent to single "/".

Input : /home/
Output : /home

Input : /a/./b/../../c/
Output : /c

Input : /a/..
Output:/

Input : /a/../
Output : /

Input : /../../../../../a
Output : /a

Input : /a/./b/./c/./d/
Output : /a/b/c/d

Input : /a/../.././../../.
Output:/

Input : /a//b//c//////d
Output : /a/b/c/d

By looking at examples we can see that the above simplification process just behaves like a stack. Whenever we encounter any file’s name, we simply push it into the stack. when we come across ” . ” we do nothing. When we find “..” in our path, we simply pop the topmost element as we have to jump back to parent’s directory.
When we see multiple “////” we just ignore them as they are equivalent to one single “/”. After iterating through the whole string the elements remaining in the stack is our simplified absolute path. We have to create another stack to reverse the elements stored inside the original stack and then store the result inside a string.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

/* C++ program to simplify a Unix
   styled absolute path of a file */
#include <bits/stdc++.h>
using namespace std;
  
// function to simplify a Unix - styled
// absolute path
string simplify(string A)
{
    // stack to store the file's names.
    stack<string> st;
  
    // temporary string which stores the extracted
    // directory name or commands("." / "..")
    // Eg. "/a/b/../."
    // dir will contain "a", "b", "..", ".";
    string dir;
  
    // contains resultant simplifies string.
    string res;
  
    // every string starts from root directory.
    res.append("/");
  
    // stores length of input string.
    int len_A = A.length();
  
    for (int i = 0; i < len_A; i++) {
  
        // we will clear the temporary string
        // every time to accomodate new directory 
        // name or command.
        dir.clear();
  
        // skip all the multiple '/' Eg. "/////""
        while (A[i] == '/')
            i++;
  
        // stores directory's name("a", "b" etc.)
        // or commands("."/"..") into dir
        while (i < len_A && A[i] != '/') {
            dir.push_back(A[i]);
            i++;
        }
  
        // if dir has ".." just pop the topmost
        // element if the stack is not empty
        // otherwise ignore.
        if (dir.compare("..") == 0) {
            if (!st.empty()) 
                st.pop();            
        }
  
        // if dir has "." then simply continue
        // with the process.
        else if (dir.compare(".") == 0) 
            continue;
          
        // pushes if it encounters directory's 
        // name("a", "b").
        else if (dir.length() != 0) 
            st.push(dir);        
    }
  
    // a temporary stack  (st1) which will contain 
    // the reverse of original stack(st).
    stack<string> st1;
    while (!st.empty()) {
        st1.push(st.top());
        st.pop();
    }
  
    // the st1 will contain the actual res.
    while (!st1.empty()) {
        string temp = st1.top();
          
        // if it's the last element no need
        // to append "/"
        if (st1.size() != 1)
            res.append(temp + "/");
        else
            res.append(temp);
  
        st1.pop();
    }
  
    return res;
}
  
// Driver code.
int main()
{
    // absolute path which we have to simplify.
    string str("/a/./b/../../c/");
    string res = simplify(str);
    cout << res;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java program to simplify a Unix
styled absolute path of a file */
import java.io.*; 
import java.util.*; 
  
class GFG
{
    public static void main(String []args)
    {
        // absolute path which we have to simplify.
        String str = new String("/a/./b/../../c/");
        String res = simplify(str);
        System.out.println(res);
    }
  
    // function to simplify a Unix - styled
    // absolute path
    static String simplify(String A)
    {
        // Stack to store the file's names.
        Stack<String> st = new Stack<String>();
  
        // temporary String which stores the extracted
        // directory name or commands("." / "..")
        // Eg. "/a/b/../."
          
        // contains resultant simplifies String.
        String res = "";
  
        // every String starts from root directory.
        res += "/";
  
        // stores length of input String.
        int len_A = A.length();
  
        for (int i = 0; i < len_A; i++) 
        {
  
            // we will clear the temporary String
            // every time to accomodate new directory 
            // name or command.
            // dir will contain "a", "b", "..", ".";
            String dir = "";
  
            // skip all the multiple '/' Eg. "/////""
            while (i < len_A && A.charAt(i) == '/')
                i++;
  
            // stores directory's name("a", "b" etc.)
            // or commands("."/"..") into dir
            while (i < len_A && A.charAt(i) != '/'
            {
                dir += A.charAt(i);
                i++;
            }
  
            // if dir has ".." just pop the topmost
            // element if the Stack is not empty
            // otherwise ignore.
            if (dir.equals("..") == true
            {
                if (!st.empty()) 
                    st.pop();     
            }
  
            // if dir has "." then simply continue
            // with the process.
            else if (dir.equals(".") == true
                continue;
              
            // pushes if it encounters directory's 
            // name("a", "b").
            else if (dir.length() != 0
                st.push(dir); 
        }
  
        // a temporary Stack (st1) which will contain 
        // the reverse of original Stack(st).
        Stack<String> st1 = new Stack<String>();
        while (!st.empty()) 
        {
              
            st1.push(st.pop());
            // st.pop();
        }
          
  
        // the st1 will contain the actual res.
        while (!st1.empty())
        {
              
            // if it's the last element no need
            // to append "/"
            if (st1.size() != 1)
                res += (st1.pop() + "/");
            else
                res += st1.pop();
  
            // st1.pop();
        }
        return res;
    }
  
}
  
// This code is contributed by ankush_953

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to simplify a Unix
# styled absolute path of a file
  
# function to simplify a Unix - styled
# absolute path
  
  
def simplify(A):
    # stack to store the file's names.
    st = []
  
    # temporary string which stores the extracted
    # directory name or commands("." / "..")
    # Eg. "/a/b/../."
    # dir will contain "a", "b", "..", ".";
    dir = ""
  
    # contains resultant simplifies string.
    res = ""
  
    # every string starts from root directory.
    res += "/"
  
    # stores length of input string.
    len_A = len(A)
    i = 0
    while i < len_A:
  
        # we will clear the temporary string
        # every time to accomodate new directory
        # name or command.
        dir_str = ""
  
        # skip all the multiple '/' Eg. "##/""
        while (i < len_A and A[i] == '/'):
            i += 1
  
        # stores directory's name("a", "b" etc.)
        # or commands("."/"..") into dir
        while (i < len_A and A[i] != '/'):
            dir_str += A[i]
            i += 1
  
        # if dir has ".." just pop the topmost
        # element if the stack is not empty
        # otherwise ignore.
        if dir_str == "..":
            if len(st):
                st.pop()
  
        # if dir has "." then simply continue
        # with the process.
        elif dir_str == '.':
            continue
  
        # pushes if it encounters directory's
        # name("a", "b").
        elif len(dir_str) > 0:
            st.append(dir_str)
  
        i += 1
  
    # a temporary stack (st1) which will contain
    # the reverse of original stack(st).
    st1 = []
    while len(st):
        st1.append(st[-1])
        st.pop()
  
    # the st1 will contain the actual res.
    while len(st1):
        temp = st1[-1]
  
        # if it's the last element no need
        # to append "/"
        if (len(st1) != 1):
            res += (temp + "/")
        else:
            res += temp
        st1.pop()
  
    return res
  
  
# Driver code.
  
# absolute path which we have to simplify.
string = "/a/./b/../../c/"
res = simplify(string)
print(res)
  
# This code is contributed by ankush_953

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to simplify a Unix
// styled absolute path of a file
using System;
using System.Collections.Generic; 
  
class GFG
{
    public static void Main(String []args)
    {
        // absolute path which we have to simplify.
        String str = ("/a/./b/../../c/");
        String res = simplify(str);
        Console.WriteLine(res);
    }
  
    // function to simplify a Unix - styled
    // absolute path
    static String simplify(String A)
    {
        // Stack to store the file's names.
        Stack<String> st = new Stack<String>();
  
        // temporary String which stores the extracted
        // directory name or commands("." / "..")
        // Eg. "/a/b/../."
          
        // contains resultant simplifies String.
        String res = "";
  
        // every String starts from root directory.
        res += "/";
  
        // stores length of input String.
        int len_A = A.Length;
  
        for (int i = 0; i < len_A; i++) 
        {
  
            // we will clear the temporary String
            // every time to accomodate new directory 
            // name or command.
            // dir will contain "a", "b", "..", ".";
            String dir = "";
  
            // skip all the multiple '/' Eg. "/////""
            while (i < len_A && A[i] == '/')
                i++;
  
            // stores directory's name("a", "b" etc.)
            // or commands("."/"..") into dir
            while (i < len_A && A[i] != '/'
            {
                dir += A[i];
                i++;
            }
  
            // if dir has ".." just pop the topmost
            // element if the Stack is not empty
            // otherwise ignore.
            if (dir.Equals("..") == true
            {
                if (st.Count!=0) 
                    st.Pop();    
            }
  
            // if dir has "." then simply continue
            // with the process.
            else if (dir.Equals(".") == true
                continue;
              
            // pushes if it encounters directory's 
            // name("a", "b").
            else if (dir.Length != 0) 
                st.Push(dir); 
        }
  
        // a temporary Stack (st1) which will contain 
        // the reverse of original Stack(st).
        Stack<String> st1 = new Stack<String>();
        while (st.Count!=0) 
        {
              
            st1.Push(st.Pop());
            // st.pop();
        }
          
  
        // the st1 will contain the actual res.
        while (st1.Count!=0)
        {
              
            // if it's the last element no need
            // to append "/"
            if (st1.Count!= 1)
                res += (st1.Pop() + "/");
            else
                res += st1.Pop();
  
            // st1.pop();
        }
        return res;
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right



Output:

/c

Time Complexity O(length of string).

This article is contributed by arshpreet soodan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.