Alex Kaloostian

Apple Certified Master Trainer | Systems Integrator | Video Editor | Motion Graphics Artist

Command Line Basics 6: time savers

1 Comment

I was planning on mentioning these tricks in a later post, but my good friend Hank made me see that this is the perfect time. Today I’ve got one problem, and three shortcuts for you.

Problem: spaces.

Ever notice that email addresses and web sites never have spaces in them? Spaces are a problem in Unix. Let’s say you had a folder called “Important stuff” and you wanted to look inside. You’d probably try typing

Lion-Apps:~ fmcadmin$ ls Important stuff

But you’d get an error:

Lion-Apps:~ fmcadmin$ ls Important stuff
ls: Important: No such file or directory
ls: stuff: No such file or directory

See what happened? It thought that Important and stuff were two different folders, and you wanted to look inside each. This is why “old schoolers” never use spaces in file and folder names. But all is not lost, it’s actually easy to deal with spaces in two different ways: just use quotes, or a black slash.

ls "Important stuff"

or

ls Important\ stuff

See what I did there? By putting the folder name in quotes, the command line knew that it was all one name. The second method is even quicker though, and more popular. Simply place a backslash before a space (but not instead of the space- you still need the space after the backslash). This is called escaping the space. Sounds like a late-night sic-fi show on cable.

Windows users out there will be used to using backslashes all the time for paths, but in the Unix command line, we use forward slashes for paths. The only time you need to use backslash is for a space in a  file name.

Shortcut #1: Drag and drop!

Let’s say you want to go to a folder, or look at a file, thats buried really deep in your hard drive, like four folders deep. And every time you try to type it you make an error and its driving you nuts. Well, just find the file in the Finder, and drag-and-drop it into the terminal window!

Lion-Apps:~ fmcadmin$ less /Users/fmcadmin/Important\ stuff
/More/data/Long\ complicated\ folder\ name\ that\ really\ 
shouldnt\ have\ been\ used/company\ data.txt

See what it did for me? It put in all those long folder names, and it even escaped the spaces for me.

What’s the less command do? That lets you read a text file right in the Terminal. We’ll work more with text files later.

Shortcut #2: Tab auto-complete

Okay, maybe you don’t have the file in the Finder, or can’t find it, or you are just too lazy to stop what you’re doing and switch to the Terminal to find it. You can also make typing long paths a lot easier with the tab key.

Just start typing the beginning of a file or folder name, and hit tab. The Terminal will automatically complete the name of the folder for you.

Lion-Apps:~ fmcadmin$ cd I

*tab*

Lion-Apps:~ fmcadmin$ cd Important\ stuff/

All I had to type was the first letter of the folder, and it read my mind! It could auto-complete the name of the folder because it was the only folder that started with I.

If there are multiple folders with the same letter, you’ll need to type a bit more. For example, in your home folder you probably have Desktop, Documents and Downloads. You can’t simply type

Lion-Apps:~ fmcadmin$ cd D

Because it won’t know which folder starting with D you want. However, if you press tab twice, it will list all the possibilities, which is kind of cool:

Lion-Apps:~ fmcadmin$ cd D
Desktop/ Documents/ Downloads/

But I could have avoided this completely, by typing just enough letters that the command line could figure it out. For example, three folders start with D, only two folders start with Do, and only one folder starts with Dow.

Lion-Apps:~ fmcadmin$ cd Dow
Lion-Apps:~ fmcadmin$ cd Downloads/

Shortcut #3: history

Ever type a long, long command, and screw up just one letter, and you have to type the whooooole thing again? Well no more. Just press the up arrow, and you can scroll through your history and se all the commands you’ve typed before. Find the one you want, change one letter, and BAM, you’re good to go.

If you hit up too many times and shoot past your command, just press down to go forward again. Imagine a long, long list of all the commands you’ve ever typed, and you can scroll up or down through them at will.

Some of you might also realize this has security implications – if you suspect someone was doing something naughty, you can scroll back through and see what they were up to! It’s only for the currently logged-in user, though. If someone else was doing it, you’d have to log in as them to see their history.

If you’re concerned, and you want to erase your tracks, the history is stored in an invisible file inside your home folder:

~/.bash_history

Remember the past few lessons? The ~ means your home folder, and the . means the file is invisible. We will talk about how to remove that file later.

Bonus

Okay, a couple more quick ones. Remember a few lessons ago, I said if you got stuck in a loop, or a command wouldn’t stop, you could escape with control-c? Thats kind of like your panic button. Control-c is also useful if you’re typing a command and then you decide you don’t actually want to do it, or you mistyped. Instead of pressing delete-delete-delete-delete, just press control-c and the prompt will clear.

Finally, if you want to clear the whole window, press control-l. That will clear your screen and give you a clean, empty Terminal window.

Till next time, true believers!

Advertisements

Author: alexkaloostian

I'm a video editor, motion graphics designer and Mac IT consultant in the Boston area.

One thought on “Command Line Basics 6: time savers

  1. Pingback: Command Line Basics 7 – review time | Alex Kaloostian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s