I don't want to know every path of everything so I refuse to do anything like...
Most of us don't remember long and complex paths either, so this is very relatable. For example, there is an excellent command tool called "Airport Utility" (
airport) but it is not found in the PATH. Using
locate will get you an ungodly path of
This is impossible to remember, but it’s just one example (I use an alias for this command, by the way). There is no expectation that anyone will remember every directory path. However, we have a number of tools which we can use to make life just a bit simpler. Following is just a sampling of the tools that I use.
Drag and Drop from Finder to Terminal
If you already have the file/folder pulled up, you don't have to remember the entire path. Simply start your command as approprite (copying a folder in this example)
cp -R then drag the folder from Finder to Terminal and the path will be automatically populated.
Then continue the command as needed. Here I'm copying to my current directory so I use a period or dot
. but I could drag another file/folder from Finder as the destination.
Using Shell Builtins
We have several from which to use:
Tab Completion. This is availabe in both Bash (default for Mojave and earlier) and Zsh shells (default for Catalina and later). when you are entering a file or folder, simply start with the first few characters, press ⇨Tab and it will automatically complete the rest of the line for you. For example:
cp - R /Users/al<⇨Tab>
Will automatically fill in the rest of the folder until it needs more input from you to continue:
cp -R /Users/allan/Desk<⇨Tab> ← and so on and so on...
You can use this for both source and destination. Just tab complete your way through each directory
pwd → Outputs the present (current) working directory. It's also available as a variable, $PWD. For example, assuming you're deep into some obscure folder, and want to copy something like a single file or folder to a folder on your Desktop, just issue the command
cp -R $PWD/folder_to_copy ~/Desktop/destination_folder
~ Expansion → This is the shortcut to your home folder which is also conveniently referenced by the shell variable
$HOME. If you wanted to copy all of your Documents to a USB drive, you could simply issue the Terminal command:
cp -R $HOME/Documents /Volumes/My_Backup_USB/Documents
or equally valid
cp -R ~/Documents /Volumes/My_Backup_USB/Documents
dirs → Allow you to push directories onto, pop off of and list the contents of the directory stack that is LIFO or Last In/Fist Out. (Think of a stack like a stack of pancakes; you always put the newest on top and take the top one off when eating one).
dirs will list all of the directories on your stack (
dirs -p shows one entry per line). The first value is always the current working directory. This will remain so, until you "push" some entries onto the stack. Here's an example of a directory stack:
/Users/allan/Desktop/Email to Process
/Users/allan/Desktop/Saved Images to Process
/Users/allan/Desktop/eBooks to Process
Desktop is on there twice because I accidentally pushed it twice, but illustrates this function perfectly)
pushd will "push" the present directory or a directory you specify onto the stack. This is how I populated the stack for the previous bullet point. Examples:
popd will "pop off" the latest directory entry or the one you specify (can be determined with the
dirs command) For example, we can use some command expansion to get the 3rd directory in our stack with the command
popd +3 (3rd item from the top) or
popd -3 (for 3rd item from the bottom)
So, once you know where your directories are in the stack, you can use those with command expansion:
cp -R `popd` ~/Desktop ← Will copy whatever folder from top of stack to present working directory
cp -R `popd` `popd` ← Will copy the first folder on the stack to second folder on the stack
cp -R `popd +2` `popd -4` ← Will copy the 2nd from top to 4th from bottom
Please keep in mind that this is not a comprehensive tutorial on
dirs. There are many options (flags) that you can use with each to better work with the directory stack.