Four ways to quickly access a file or folder in the Terminal

NewTerminalIconXIf you are at least vaguely familiar with the Terminal, then you will know that performing actions on files and folders requires you specify their full paths, and often this means you need to change to a specific directory. While you can use the “cd” command and then type out the path to the desired directory, often the sheer length of the path makes this an impractical approach. However, in the Terminal, there are four approaches you can take to make accessing a desired file or folder quicker.

1. Tab completion

While not unique to OS X, tab completion can be a quick way to dive down a folder path in the Terminal. Start typing a few characters of your path and press Tab, and if the characters are recognized as a valid path then the Terminal will fill out the rest. This will only issue an error if the completion includes two or more possibilities, where you will then have to enter a few more characters to specify where to go, followed again by pressing tab to complete down to the desired path target.

2. Drag and Drop

Tab completion is great for terminal-only approaches to things (e.g., Single User mode, or when accessing a system remotely through SSH); however, for the local system you can simply drag and drop any file or folder to the Terminal window, to enter a full Unix-compliant path at the point of the cursor. This makes for an easy way to specify the target of a command that is partially entered. For instance, you can type “chmod 755” followed by a single space, and then drag your file to the Finder to type its full path before pressing Enter to execute “chmod 755” on the file.

Terminal drag and drop path variable

Once the variable for the file path has been set by drag-and-drop (first command), you can run subsequent commands on the variable to target the same file (second command).

Another approach to this is to create a variable for a specified file on which you want to perform multiple commands. To do this, type a desired variable name followed by an equals sign, like “THEFILE=”, and then drag your file or folder to the Terminal window. Then press Enter to save the variable (note that it will only be saved for the duration of the current Terminal session). Then you can run multiple commands on $THEFILE (using the dollar sign to bring out the value of the variable—the path itself), such as the following:

chmod 755 $THEFILE
chown username:group $THEFILE

3. Direct folder change

You can change to a folder’s location in the Terminal by using the “cd” command and then dragging the folder to the Terminal to enter its full path, and can do so for a file by similarly using “cd” and then dragging it to the window followed by deleting the file component of the path; however, this can be a little burdensome. Alternatively, you can simply hold the Command key when dragging either a file or a folder to the Terminal window, and the program will switch to the folder’s location, or to the parent folder of the dragged file. This is a very easy way to switch around in the Terminal to various locations that you currently have open in the Finder.

4. Finder service menu

New Terminal at folder contextual menu item

Once enabled, the contextual menu will contain this option, among other services, for handling a selected folder.

A last option is to enable the Terminal’s services in the Finder that allow you to directly open a selected folder in a new Terminal window or tab. To do this, go to the System Preferences > Keyboard settings and then click the Shortcuts tab. In here, select the Services category and locate the Files and Folders section, under which you will see options labeled “New Terminal at Folder” and “New Terminal Tab at Folder.” Enabling these will allow you to right-click a folder in the Finder and then use these options to open it in the Terminal. Note that unlike direct folder changing by Command-dragging an item to the Terminal window, this approach is limited to folders only; however, it is overall one of the more convenient options.

One thought on “Four ways to quickly access a file or folder in the Terminal

  1. Strod

    I didn’t know of the second variation of #3 (holding Command), nor of #4. Those will be useful and I thank Topher for pointing them out.

    Two brief comments:

    Regarding tab completion, Topher says: “This will only issue an error if the completion includes two or more possibilities”. True, but if at that point you press Tab again, it will list the file/folder names that match what you have so far, so it’s easy to proceed.

    Regarding drag-and-drop: if you want the path to a document you already have open, in most cases there will be a little icon next to the filename in the document window’s title bar. Press the mini-icon for two seconds, and when it gets dark you can drag it to the Terminal (or most other places that would accept the corresponding Finder icon).

Comments are closed.