5 Useful Mac OS X Command Line Tips Everyone Should Know

Jun 29, 2008 - 12 Comments

Mac Terminal icon

Like many other power users, I’m addicted to the Mac OS X command line, any reason at all for me to launch the terminal I take as an opportunity to learn more about the powerful backend of our favorite operating system.

Here I have gathered 5 useful commands that will make your life easier when you’re working in the command line interface of OS X, so launch the Terminal and try them out on your Mac! If you have any others that you think should be added to this list, feel free to post them in the comments, and be sure to check out 10 Mac OS X Command line utilities you might not know about for more.

1: Delete an entire line of commands/text

Don’t repeatedly hit the delete key, all you need to do to clear your prompt is hit Control-U and your current prompt will be clean.

2: Create a nested directory structure immediately

If you need to create the directory structure /annoyingly/long/and/outrageous/directory/path/ , instead of typing mkdir annoyingly, cd annoyingly, mk long , etc etc, simply type the following:

mkdir -p /annoyingly/long/and/outrageous/directory/path/

And your nested directory structure will be created in full immediately!

3: Clear the entire Terminal screen

If you have a screen full of nonsense, clearing the Terminal screen is very easy, you can either type:

clear

Terminal in OS X Or you can just hit the command keystroke Control-Land you’ll have a clean slate to work with.

4: Run a process in the background

If you want to set a process to run in the background, simply put a & after it, the command will execute but you’ll still be in the same shell, allowing you to continue as usual.

For example:

./crazyscript.sh &

Would run that script in the background, and return you right back to your shell.

5: Run the last executed command

Need to re-run the last executed command? ! is the way to go, here are two ways to use it:

First, just typing:

!!

will run whatever the last command that was executed, typing

!l

will run the last command starting with the letter l, and so forth. Very useful, isn’t it?

terminal

Know of any super useful command line tricks that Mac users should know about? Be sure to let us know in the comments!

.

Related articles:

Posted by: Bill Ellis in Command Line, Mac OS, Tips & Tricks

12 Comments

» Comments RSS Feed

  1. xoxo says:

    @ant
    Jun 29, 2008 :D lol

    • the future says:

      The command line doesn’t change much, June 29 2022, September 13 2018, 2015, 2008, 1996, these are all relevant. Good old Unix, eh?

  2. ant says:

    * if you hit ”arrow up” key you get faster your las inserted command

  3. Natty says:

    I added the following to my .bashrc file so that my time-intensive commands wouldn’t die if I exited terminal.

    shopt -u huponexit
    ulimit -s unlimited

  4. Eliza sahoo says:

    Hi,
    Though i am not a very technical person.but i found your article very informative

  5. robig says:

    Expanding the possibilities of !! (last command):
    !ssh = last command starting with ssh

    ctrl+r = reverse search in last commands

    ctrl +d = logout (close terminal if enabled in the terminal options)

    ctrl +c = abort current command (also when still typing it)

    cd foo; .. do somtehing in foo …; cd $OLDPWD

    there are sooo many more….
    im switched from linux to mac btw ;)

    robig

  6. DrewMerkle says:

    The true hackers out there will think I’m explaining the obvious, but for me, these were some good discoveries when I learned them. This seems an appropriate place to add them as comments.

    The ampersand (“&”) at the end of a command is useful for executing that command in the background and returning control to your terminal, but if you log out (close the terminal window, and so on) that command will be killed. Frustrating for those new to the art of the command line.

    Sometimes the “nohup” command placed ahead of the rest of the command line is quite useful in this regard. “nohup” in this case immunizes a command from a “hang up”, which in the dawn of the computer age used to happen to a computer terminal connected via acoustic coupler modem to a mainframe… The ampersand can be combined with “nohup” to immediately send the immunized to the background.

    Don’t forget you can pipe the output to a file with the “> file.txt” sort of thing. Example:

    nohup command -option -option > output.txt &

    Even more fun is the “screen” command. “screen” allows you to create virtual text-mode screens which live on the system even when you log out and you can reconnect to them later to interact if needed. Very useful for starting something, going home, having a family life, and then checking back to see if your very long command has done anything useful.

    Type “man nohup” and “man screen” for more information.

  7. Ben Oakes says:

    Note that the control character sequences listed here (i.e. ^L, ^A) are for Emacs mode. If you’d rather use vi keybindings at the shell, you can run `set -o vi`. (To affect other programs, edit your .editrc and .inputrc.) You start off in insert mode and can enter normal mode using or ^[.

  8. Pedro says:

    I find the !! (execute last command) really helpful if I forget to type sudo before a command. You can just type sudo !! and the last command will be executed as root (assuming you’re in the sudoers file).

  9. Antonio says:

    So, all of this is “standard” shell use of many unix boxes… Many more things work also in the Mac OS X shell, like Ctrl-a/Crtl-e to move to the beginning/end of the line, etc

    A.

  10. Antonio says:

    So, all of this is “standard” shell use of many unix boxes… Many more things work also in the Mac OS X shell, like Ctrl-a/Crtl-e to move to the beginning/end of the line, etc

    A.

  11. Sean says:

    Hey,

    another way to run or rather put a started process in the background is by interrupting the current process with ctrl+z, then typing “bg” to re-activate it in the background. If you happen to need it in the foreground again, simply type “fg”. The latter one does also work for processes started in the background with “&”.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site