7 Handy Command Line Tips You Don’t Want to Miss

Oct 17, 2013 - Leave a Comment

Terminal icon in OS X Getting comfortable with the command line is often just a matter of learning a few command tricks and finding uses for them, and we’re going to offer six handy tricks that you’re almost certain to find some use out of regardless of your skill level in the Terminal.

Read on, you’ll be downloading files, using a better directory listing, killing processes quicker, re-running prior commands as root, finding past commands, and creating new files on the fly in no time.

1: Download a File from the Web & Watch Progress

Know the URL of a file that you need to download from the web? Use curl with the -O command to start downloading it:

curl -O url

Be sure to use the full URL. Also, remember to use the upper case ‘O’ and not the lowercase ‘o’ to keep the same file name on your local machine.

For example, the following command would download the iOS 7 IPSW file from Apple’s servers to the local Mac, maintaining the same file name as it appears on the remote server:

curl -O http://appldnld.apple.com/iOS7/091-9495.20130918.FuFu4/iPhone5,1_7.0_11A465_Restore.ipsw

We’ve covered this trick before quite some time ago and it really is useful. Because it shows download speeds and progress, it can also function as an alternative to the wget trick to test an internet connections speed from the command line.

2: List Directory Contents by Modification Date

Want to long list a directory, showing permissions, users, file size, and modification date, with the most recently modified files and folders appearing from the bottom up? Of course you do:

ls -thor

This is extremely useful, and also easy to remember because, well, the command flag is thor, and how could you ever forget about the mythological thor?

3: Search Spotlight with Live Results from the Command Line

The mdfind tool is a command line front-end to the excellent Spotlight search utility, typically accessible from the Finder. But at it’s default state, mdfind is different from Spotlight search in that it won’t update the results live as they are found. That’s what this trick is for, a simple flag will search spotlight from the command line with live updating results:

mdfind -time findme

This can go awfully quick depending on the specificity of the searched terms, but if you see a match hit Control+C to stop looking.

If you have Spotlight disabled or you find it’s not working, you can always fall back on the trusty ‘find’ command too.

4: Kill Processes Using Wildcards

Ever wished you could kill a ton of processes or commands at once using wildcards? Or maybe you just want to kill something faster without typing out the full process name or pid? The standard kill command won’t take wildcard input, but pkill does accept wildcards, making it the right choice for the job.

For example, to kill every active instance of the “SampleEnormousTaskNameWhyIsThisProcessNameSoLong” process at once, you could use the following:

pkill Sam*

Remember that wildcards are unforgiving, and pkill ends tasks without hesitation or requesting a save, so if you have any other closely matching process names they will get killed too. The easiest way around that is to just specify a slightly longer element of the task name to target.

You can also use pkill to target all of a specific users processes, which can be helpful for some situations on multi-user Macs.

5: Re-Run the Last Command as Root

Don’t you hate it when you go to execute a lengthy command and you find out after hitting enter that it requires super user to run? You know, like one of those defaults commands? Don’t retype the entire command string, use this simple trick instead:

sudo !!

This is an oldie-but-goodie trick that has been around for ages, and it’s sure to get a ton of use as you spend more time at the command line.

6: Get the Last Occurrence of a Command Without Executing It

Can’t remember the exact syntax you used the last time you ran a specific command? You can instantly find it without actually executing the command again by using this trick, where ‘searchterm’ is the command to match:

!searchterm:p

For example, to find the last full command that used the prefix “sudo” you would use:

!sudo:p

This will report something back like the following, giving you the full command syntax, but not running it again:

sudo vi /etc/motd

Again, this trick will only report back the last time a command was used based on any prefix. If you really need to go digging through your prior command list, you can search through your bash history with grep.

7: Instantly Create a Blank File or Multiple Files

The touch command makes quick work of creating blank files, either for space holders, testing, demonstration, or whatever else your plans may be. The secret is the ‘touch’ command and it’s simple to use:

touch filename

You can list out multiple names to create multiple files too. For example, this would create three files named index, gallery, and cv, each with the html extension:

touch index.html gallery.html cv.html

That one is particularly helpful for developers.

Want to learn more about Terminal and the command line? We have plenty more command line tricks for those interested.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

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