Time is valuable, and sometimes it feels like there's never enough in each day.
Luckily, there are a number of quick shortcuts in Terminal that can be used to save time
when working with the command-line interface, or CLI.
Jeremy here, and I'll show you some keyboard shortcuts to help use Terminal more efficiently.
I'm going to change the directory to my Desktop and begin with the open command, which can
open files and directories, including apps.
For example, open acme-banner.png will open the image file on my Desktop using the default
app based on its extension - in this case, Preview.
The same goes for opening directories.
"open Jamf" will open the Jamf folder on my Desktop using the Finder.
But what if I want to open something that's not in the current working directory, which
is my Desktop?
I can still use the open command with the absolute path to a file or directory without
first changing directories.
Since absolute paths begin at the root level with a forward slash, I'll type open /System/Applications/TextEdit.app
and press Return to open the TextEdit app.
The open command can even specify which app to use when opening a file.
The hyphen a option specifies which app to use followed by what file to open.
Safari is capable of opening many different file types, so instead of opening the ACME
banner image file with Preview, I could specify Safari.
Type open hyphen a Safari to specify Safari as the app to be used and then the path to
the file to be opened.
Or, more appropriately, Safari can open web sites using the same syntax.
In this example, Safari is the app specified again and jamf.com is the file to open.
It's important to specify the https protocol when opening websites with this command; otherwise
Terminal would try to use Safari to open a file titled jamf.com in the current working
directory.
Pressing Return to execute this command opens the Jamf website in Safari.
Let's use this command to open a different website in a new tab.
Instead of typing out the whole command again, I can just press the Up Arrow key to repeat
the same command.
Continuing to press the Up or Down Arrow keys will cycle through the previously executed
commands.
The Left and Right Arrow keys move the insertion point one character at a time, or keyboard
shortcuts help speed this up.
Control-A moves the insertion point to the beginning of the command, or Control-E moves
it to the end.
Option-Left Arrow moves it backward one word at a time, or Option-Right arrow moves it
forward in the same manner.
If you want to move the insertion point to a specific character, press and hold the Option
key and click where you'd like to move it.
I'll change this to apple.com and now the command opens the Apple website in Safari.
Typos happen to the best of us, especially when you've got somebody watching over your
shoulder.
Thankfully, the Tab key is used to automatically complete directory or file names, commonly
referred to as tab completion, helping to speed up use of the command line while eliminating
human error.
I'll change back to my home directory to demonstrate how this works.
Now, when trying to change the directory again, pressing Tab displays a list of possible directories.
If I enter a capital D and press Tab again, only directories beginning with a capital
D are displayed.
Add a lowercase e and press Tab again, and the Desktop directory is completed.
Executing this command as is will change the directory to my Desktop; or adding a capital
J and pressing tab will auto-complete the command again.
Now the command changes the directory to the Jamf folder on my Desktop.
Another great way to eliminate human error is by dragging files or directories into Terminal
from the Finder.
For example, if I begin with the open command and a space character, then drag the ACME
banner image file from my Desktop to Terminal, the absolute path to the file is entered.
Press Return to execute the command, and the file is opened.
Watch what happens when I do the same with this PDF of the Jamf Pro Administrator's Guide.
Its file name contains space characters and an apostrophe, so each one of these characters
is preceded by something called an escape character.
When working with the command-line interface, space characters are used to separate commands,
options, and arguments.
A backslash is used as the escape character in order to avoid this separation and treat
a space character in a directory or file name literally.
Press Return, and the file is opened.
The other option for dealing with special characters is by placing the path in double
quotation marks.
If I type open, insert a double quotation mark, begin to type the file name, then press
Tab, the file name is completed and contained in double quotation marks.
Pressing Return will open the file again, or I can abort this command if I don't want
to execute it by pressing Control-C.
Some commands continue to run when executed unless an interval is specified.
A good example of this would be the ping command.
Ping is used to test network connectivity to another computer.
Let's ping the Google Public DNS by typing ping 8.8.8.8 and press Return.
The ping utility tests the connection every second, and will continue until aborted because
I didn't specify an interval.
Again, to abort the command, just press Control-C.
Lastly, let's take a look at executing commands with elevated privileges.
Think of this like when you're prompted for administrator credentials in the macOS interface.
If I try to create a new folder in the root Library Scripts folder, I'll be prompted for
my credentials in order to elevate my privileges because my user doesn't have privileges to
make new folders here.
In Terminal, executing a command to make the Jamf directory in the same location results
in a permission denied response.
Using sudo as a prefix will execute commands as root, or the superuser.
If I execute the same command with sudo at the beginning, I'm prompted for my password
to elevate my privileges and the Jamf directory is created.
Once a command has successfully been executed with sudo, I can continue to use sudo without
being prompted for my password for up to five minutes after each time it's used.
Similar to the result when trying to make the Jamf directory without sudo, if I were
to try to remove the Jamf directory without sudo, permission is denied.
Since I've already tried to execute the remove directory command once, there's a faster way
to try again using sudo.
In command-line terms, an exclamation point is read as "bang."
So I'll type sudo !! and when I press Return, the previous command is executed with sudo.
It's important to note only administrator users in macOS can execute commands with sudo
by default.
Try exploring some of these commands on your own, but remember that using sudo is very
powerful - and with great power, comes great responsibility.
That's all for our video on using Terminal more efficiently.
Thanks for watching, and be sure to check out our other videos on the command-line interface
and Terminal.