Mac Terminal is a powerful tool that lets you perform an endless number of tasks from the command line. Many times those tasks involve the need to know the path of a file. There are also instances where you need to know the path for an application.
In either case, Mac Terminal is a great method to use when you need to find this information. There are a few different scenarios in which you may find yourself when trying to determine a file path and in all of them, this can be done rather quickly using the Terminal app.
I’m Eric, a software engineer for nearly 25 years, I have used command-line interfaces on many different systems, and finding the path of a file is something I do regularly in my everyday job. Using Mac Terminal is no different and I can show you how.
Stick with me if you would like to find out more about this topic. If you are unfamiliar with what a path is, I will also give you a brief overview.
So, if you would like to learn something about file paths, read on and we will explore this topic.
Contents
Finding Your File Path
If you are a little unsure about the concept of a file path, no worries. We will explore that in more detail in the next section. For those who are knowledgeable about them let’s get right to it and see how you can quickly do this using your Mac Terminal.
While there are many uses and needs to have the path a file, there are really three situations you may be facing when trying to gather this information. Below, I will show you the method for each of them.
Situation 1: You’re Already There
In this first scenario, which in my view is the simplest, you are already in the directory that contains the file whose path you are looking for.
However you got there, you may not remember or may not have seen the path along the way, but there is a quick and simple command you can run to get it. Just type in the pwd command as shown below. It’s easy and does not have any parameters.
pwd
After you hit the return/enter key, you will see the output which will give you the path of your current location in the file system. As long as this is the location of your file, you will have the path of that file. See the example below.
After the command has been run, you can see the output /Users/ericwinkler/Test/ziptest2 which is the path to the ziptest.zip file I am looking for. From here I can select the text of the path, copy it, and paste it anywhere I need to use it.
Situation 2: Don’t Know The Location
In this situation, I do not happen to be in the directory where my file is located and I don’t actually know where it is located so I am going to need to find the file using the find command. The great thing about this is that the output of the find command will provide the path.
The find command can be a bit tricky sometimes and can produce a large amount of unwanted output from directories and files that you may not have permission to view.
There are many different ways to use and run the command and if run as shown below you should get the results you are looking for as long as the file exists on your file system. You can run this from anywhere on the file system that contains the file you are looking for.
find / -name <filename> 2>/dev/null
The 2>/dev/null tells the system to redirect any of the error or warning messages you might see from inadequate permissions so that they don’t clutter up your screen and you can see the real output you are looking to find.
Check out my example below where I am trying to find the ziptest.zip file that I used in the first example. You can see that the output of the command shows the path plus the file name. The path that is found will be the same as the one from our first example.
Depending on what you are using the path for, in some cases, you will want to include the file name with the path and in others, you will just need the path itself. You can select, copy and paste whatever part of it you need.
There are a couple of things you should notice about the above example. The first thing is the way that the find command is formed using the / path parameter. The / stands for and is the path to the root directory. So, this is telling it to start searching from the root.
If you know a little more about where the file is located, say under the Test directory, you could specify to start the search from /Users/eircwinkler/Test. The search will complete faster since it doesn’t have to go through the entire disk but it will only give you the remaining part of the path.
This remaining part is known as a relative path because it is relative to the portion you specified. If you want to form the full path, also known as the absolute path, you will need to add the search results to the part that you specified in the command to get the final result shown below.
/Users/eircwinkler/Test/ziptest2/
The other thing to notice about the output is that it found two files with the same name but slightly different paths. These are actually the same file and if you notice, the path from the second one is contained in the first one.
Either of these paths will work for the majority of commands and applications. They are just two different ways of getting there. The first one contains the entire system path while the second one just contains the path from where the disk volume is mounted to the system.
You can take a look at this information from Apple if you would like to know more about how a Mac file system is set up.
Situation 3: Just Drag and Drop
There is one more method that you can use to find and display the path in Mac Terminal and it is as simple as an ordinary drag and drop. If you have a terminal window open and have located the file in Finder, just drag it to the Terminal window and drop it there.
Mac Terminal will quickly display the path of the file. See the example below.
While you could just right-click on the file and see its path from the properties, this is an easy way to get the path into your terminal window if you need to do so.
What is a Path?
In the examples above we have seen a number of ways to find the path of a file in Mac Terminal which is great, but there may be some out there who are not familiar with the term and may still be wondering what a path actually is. Not to worry, let us take a quick look at what it means.
A path is just what the name indicates. It is a pathway or directions that you give to a command or an application that tells your computer how to navigate or traverse the hierarchy of directories to get to where you want to go in the file system. It also defines where a file is located.
Mac Terminal uses a UNIX/LINUX-based style of commands so paths are formed by putting the names of directories together with a / in between each directory name, with the root directory always being denoted as just the / by itself.
Using the directory structure shown above as an example, the path to the changerequests directory would be /Applications/oldprojects/changerequests.
Final Words
Using Mac Terminal, there are multiple ways you can find the path of a file. I have shown you three of the easiest above. I hope that the information I have provided can help you to determine the path of your files.
Let me know what methods you like to use to find a file’s path using the Terminal application. As always, I would love to hear your questions, comments, or suggestions.