How to Save Terminal Commands on a Mac

Stop typing the same commands every time you need them

What to Know

  • Save a one-off command by copying and pasting it to a different document.
  • Create a script by saving the command in TextEdit and using the file extension .command.
  • Terminal automatically saves your recent history within the Terminal window.

This article teaches you different ways to save Terminal commands on a Mac so you don't need to retype them frequently and any caveats you need to know about the process.

How Do I Save in Terminal Mac?

If you want to save a quick command in Terminal for future reference or to put it somewhere else, there's a straightforward, if not particularly technical method, for doing so: the copy and paste commands. Here's how to do so via Terminal.

  1. In Terminal, type the command you need to save.

    Mac Terminal with a command typed in
  2. Drag your cursor to highlight the command.

    Mac Terminal with a command highlighted
  3. Right-click on it and click Copy.

    Mac Terminal with copy highlighted on a command
  4. The command you entered has now been saved to your clipboard and can be pasted elsewhere. 

How Do You Save a Command in Terminal?

If you regularly enter the same set of commands in Terminal, it can be helpful to save them as a script instead so you can initiate it at a click of a file. The process can be a little fiddly at first, but it's a time-saver. Here's how to do so.

Terminal commands can be very powerful. Make sure to enter the correct command, so you don't break anything.

  1. Open TextEdit on your Mac.

  2. Click File > New to create a new file.

  3. Enter the command you wish to create a script from.

    TextEdit on mac with a Terminal command typed in
  4. Click File > Save then enter the name as the name of the script followed by .command for the file extension.

    TextEdit on Mac with a file name followed by .command typed in
  5. Click Save.

  6. Click Use both.

  7. Find the file on your Mac, then press Enter on the file and remove the RTF part of the file name.

  8. Open Terminal and type chmod u+x followed by the name of the file location to give the script file permission to run correctly.

    Terminal with chmod u+x typed in and highlighted
  9. Double-click the script file to initiate it.

  10. The command will now run without you needing to type the command manually.

How Do You Save and Exit in Terminal?

If you've been entering commands in Terminal and want a quick way to save them for later reference, Terminal already has the functionality built into it. Here's what to do.

  1. Open Terminal.

  2. Enter the commands you need to use.

  3. Close Terminal.

  4. Reopen Terminal to find your previous work and commands remain on screen for future reference.

    Terminal with saved commands remaining

How Do I Save Changes in Terminal?

Your Terminal session will save anything you've recently entered on the screen, but it's also possible to save a record of everything. Here's how to do so.

  1. Open Terminal.

  2. Click Shell.

    Mac Terminal with Shell menu highlighted
  3. Click Export Text As...

    Mac Terminal with Export Text As highlighted
  4. The contents of your Terminal window have now been saved to your chosen location so that you can consult it at a later date.

FAQ
  • How do you open Terminal on a Mac?

    Select the Launchpad icon from the Dock to open Terminal on a Mac, then type Terminal in the search box. Select Terminal to open the application. Or, type Terminal into Spotlight Search.

  • How do you exit Terminal on a Mac?

    To exit Terminal, go to the top menu and select Terminal > Quit Terminal. Or, press the keyboard combination Command + Terminal to quit out of Terminal.

  • How do you navigate to a folder in Terminal on a Mac?

    To access another folder in Terminal, you'll use the cd command. By default, when you open a Terminal window, you're in your Home folder. Let's say you want to move to your Downloads folder. Type cd Downloads (with a space after Downloads) and press Return or Enter. You're now in your Downloads folder. Type ls and press Return or Enter to view the contents of your Downloads folder.

  • Can you find your administrator password in Terminal on a Mac?

    No, you can't find your admin password, but you can reset it using Terminal. Power down your Mac and then restart the Mac in Recovery Mode. Select Utilities > Terminal, then type reset password. Choose the drive with the administrator account, choose the account, then enter a new password. Enter a password hint, select Save, then power down your Mac and restart it again.

Was this page helpful?