As someone who has always been a little tentative when it comes to command line action I thought I’d share some of the commands I use regularly when I do foray into terminal-dom. Hopefully you’ll find them useful and they’ll help you become more productive from the command line.

crtl-u

My favourite (probably because I make lots of mistakes) is simply hitting ctrl-u to delete a whole line you’ve just entered. I use this countless times a day.

mkdir

mkdir is something I use on a regular basis to create directories. To make subdirectories I used to make the parent dir then cd into that, then make the next dir. It was an arduous process, until someone told me about mkdir -p

# mkdir -p for nested directory structure
mkdir -p parentdir/childdir/etc

!!

Another very useful command is !!. This runs the last command you ran so is particularly useful when a command fails because you don’t have the right permissions, just use sudo !!.

# this will fail unless run as sudo
chown -R User Directory
# so instead of typing it out again use
sudo !!

On top of that you can also use !{a letter}, this runs the last command you ran starting with that letter. E.g. !c to run the last chown command.

&, jobs, kill

Sometimes you run commands that produce lots of output, such as, a server. Often you want to leave these running and carry using the same shell. To do this you need to make the process run in the background. Putting a & after the command will do the trick.

# use & after the command to run in background
servedir &
# a number will be printed to the terminal
# e.g. [2] 9502, remember this
# use with the kill command to stop it running
kill -9 9502
# use the 'jobs' command to show background jobs
jobs
# to get the pids of running background tasks use
jobs -p

wget

wget is a command I love. How often do you need a resource from the web in your current directory? Well if you know its URI then all you need to do is use wget to download it.

# e.g. download the latest version of jquery
wget http://code.jquery.com/jquery.js
# or if you want to change the filename whilst downloading use the -O arg
wget -O jquery_latest.js http://code.jquery.com/jquery.js

That is the basic way to use wget but it can do a lot more. Have an explore, there are loads to wget tutorials out there with loads more info.

history

My trips to the command line are often made because I need to install something or set something up for a project. When doing this I’m usually following a set of instructions which is when I’ll notice new commands that people use. However when the time comes to use it I’ve more often than not forgotten how best to use it. Fear not that is when the history comes to the rescue. Use

# history -- returns a list of the commands you've used recently
# add a number to only return a set number of commands
history 25
# to look for a specific command pipe output to the search command grep
# e.g. history | grep 
history | grep git

See, the terminals not really that scary a place, if I can start to get to grips with it then everyone else will be able to. I think the key is throwing yourself in and looking to improve what you do there and become more efficient.

Anyway, hopefully that was useful for some people. Please share your favourite terminal tips/commands in the comments below…

16 Comments

  1. Pingback: Create New Directories and Subdirectories with One Terminal Command - IT Clips

  2. Pingback: Create New Directories and Subdirectories with One Terminal Command « Music RSS

  3. anonymous says:

    To execute multiple commands you can put two ampersands between then like: cd && ls. If the first command throws out an error, the second one wont run. You can also use this with growl-notify to send you a growl notification at the completion of a task.
    If we get tired of typing that all the time, we can edit our bashrc and create an alias for it. Just type “sudo nano /private/etc/bashrc” Then all the way on the bottom type: alias shortcut=command so we’d type “alias cdls=”cd && ls”".
    Anyway good luck with learning more about terminal!

  4. Colm says:

    Awesome! That is a great tip, thank you.

  5. Pingback: Wipe Out a Mistyped Terminal Command with Control + U | Binary Reveux

  6. Pingback: Wipe Out a Mistyped Terminal Command with Control + U « Music RSS

  7. Anderson Venturini says:

    Great tips! Very useful. Thanks

  8. Pingback: OS X: As much as we love the command line, because it can do so much more so much faster in many cas

  9. Pingback: Wipe Out a Mistyped Terminal Command with Control + U | The New Peoples Almanac

  10. Pingback: Type History to Review Your Recent Terminal Commands « Music RSS

  11. Pingback: Type History to Review Your Recent Terminal Commands

  12. Pingback: Type History to Review Your Recent Terminal Commands - IT Clips

  13. Pingback: Type History To Review Your Recent Terminal Commands | Lifehacker Australia

  14. Pingback: How To Clear The Mac Command Line With A Single Keystroke | Lifehacker Australia

  15. Bastian says:

    Hiya,

    I liked your tips and tricks for the terminal and I have a special question: I’m logging into a server via ssh and like a certain command to be executed regularly (every 15 minutes) but due to server restrictions, I can’t use the crontab. Is there a way to repeat my last command every 15 minutes without using crontab? The shell can be left opened all the time.

    Thanks
    Bastian

  16. Colm says:

    Hi Bastian, glad you liked them. Ooh that is a tricky one. I must admit I’m a bit of terminal novice but I’ll have a think and reply again if I think of anything. Also please let me know if you do find the answer, I’d love to know!

Leave a comment

Your email address will not be published. Required fields are marked *

*