5 useful Mac OS X command line tips you should know

Jun 29, 2008 - 9 Comments

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, so launch the Terminal and try them out! 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 OS X Command line utilities you might not know about for more.

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 slate will be clean.

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!

Clear the Terminal screen

If you have a screen full of nonsense, clearing the Terminal screen is very easy, you can either type clear or just hit Control-L and you’ll have a clean slate.

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.

Run the last executed command

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

9 Comments

» Comments RSS Feed

  1. 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 “&”.

  2. 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.

  3. 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.

  4. 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).

  5. 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 ^[.

  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. 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

  8. Eliza sahoo says:

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

  9. 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

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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