If 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.
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.
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 stat $THEFILE …etc.
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
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.