20

I want to develop an app to promote the experience when using terminal on Mac. And I want to get the current working directory (cwd) from terminal. How can I implement it?

I notice that the answer in Get terminal output after a command swift is really good but it seems that it still cannot resolve my problem perfectly. I learned Process().currentDirectoryPath from Apple's document https://developer.apple.com/reference/foundation/process Does it do the trick that I need? Can I use it like the following?

let path = Process().currentDirectoryPath

I am very new to swift and Xcode, please help me! Thanks!


Update: Thank you guys! It seems that both

let path = fileManager.default.currentDirectoryPath

and the Process() one temporarily work for me. Are there any differences between these two?

6
  • What exactly are you trying to achieve with this directory?
    – Alexander
    Commented May 26, 2017 at 3:30
  • @Alexander I intend to get the path and sketch all the elements in the directory out, like making a GUI. I am a student and just want to have a small project to practice.
    – Seaky Lone
    Commented May 26, 2017 at 3:36
  • 1
    How about FileManager.default.currentDirectoryPath?
    – Pang
    Commented May 26, 2017 at 3:38
  • @Pang Which one should I choose? The fileManager one or the process one? I just got my Macbook and I am still exploring how to use xcode.
    – Seaky Lone
    Commented May 26, 2017 at 3:45
  • Did you try the code you posted? What did you get?
    – Pang
    Commented May 26, 2017 at 3:48

1 Answer 1

35

I had this same question and went with the latter option:

let path = FileManager.default.currentDirectoryPath

To answer your updated question, the difference between the Process() and the FileManager approaches is that the Process().currentDirectoryPath approach prepares to create a new subprocess and then tells you the working directory that subprocess will use. The subprocess would have the same working directory as the parent process, of course, but it's wasteful to create a new Process instance just to find the working directory.

Definitely use the FileManager approach, as that returns the working directory that is used by the current process without creating any subprocesses or any other overhead.

Sources:
https://developer.apple.com/documentation/foundation/filemanager/1409234-default
https://developer.apple.com/documentation/foundation/filemanager/1410766-currentdirectorypath

4
  • Still seems to work completely as it should, yet it seems the answer got some downvote? Why the downvote? Commented Jul 10, 2018 at 14:07
  • 1
    For the sake of the argument, AFAIK simply declaring a Process var won't actually fork a new subprocess, that won't happen until you start() it. Nonetheless, you're right, the FileManager approach is the proper way to do it, even if Process() just wastes a temporary variable. Commented Apr 30, 2019 at 11:02
  • Good observation, Guillaume Laurent Commented Apr 30, 2019 at 15:32
  • 1
    FileManager.default.currentDirectoryPath does not work with sandboxed apps: stackoverflow.com/questions/31476313/…
    – Daniel
    Commented Nov 17, 2020 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.