Video: Setting command aliases

In this movie, we'll see how we can use command aliases to help us to work more efficiently in Unix. Now, I want to make sure that you understand that the aliases that we're talking about here are not the same thing as the Finder aliases, right? Remember that we can click on a file or folder, we can go to the File menu, we can pull down and select Make Alias. That's a Finder alias for files and folders. That's different than what we're doing here. What we're doing here is command aliases. Making the same kinds of shortcuts, but shortcuts that will execute different commands in bash. The first command that we should learn is just simply alias by itself.

Unix for Mac OS X Users unlocks the powerful capabilities of Unix that underlie Mac OS X, teaching how to use command-line syntax to perform common tasks such as file management, data entry, and text manipulation. The course teaches Unix from the ground up, starting with the basics of the command line and graduating to powerful, advanced tools like grep, sed, and xargs. The course shows how to enter commands in Terminal to create, move, copy, and delete files and folders; change file ownership and permissions; view and stop command and application processes; find and edit data within files; and use command-line shortcuts to speed up workflow. Exercise files accompany the course.

Setting command aliases

In this movie, we'll see how we can use command aliases to help us to work moreefficiently in Unix.Now, I want to make sure that you understand that the aliases that we're talkingabout here are not the same thing as the Finder aliases, right?Remember that we can click on a file or folder, we can go to the File menu,we can pull down and select Make Alias.That's a Finder alias for files and folders.That's different than what we're doing here.What we're doing here is command aliases.Making the same kinds of shortcuts, but shortcuts that will executedifferent commands in bash.The first command that we should learn is just simply alias by itself.

This will return a list of all the currently defined aliases.So right now I have none.You probably don't either.So to create our first one we just use the alias command again and then theshortcut that we want to use, essentially the alias name.So the first alias I'm going to create, I'm going to use ll.I'm going to say ll= and then in single quotes I'll put the command that I want it to equal.'ls -la'.So what I'm saying now essentially is, whenever I type ll on the command lineI'd like bash to execute ls -la for me.

You can see the savings that that offers. ll Return.There it does it.Now, instead of typing six characters, and I often mistype those six characters,instead now I can just type a very quick ll and I get that list back.ll is a very, very common one.In fact, a lot of Unix machines are preconfigured to offer you that alias.Now, we can create aliases with absolutely any name that we want to performany task that we want.You just want to be careful that you don't use an alias name that is for anexisting Unix command.We don't want to overwrite that.So mv obviously is a move command.

We don't want to create an alias for mv that would do something else, right?Because then suddenly mv would stop working for us.So you can use the man pages or the commands like which that we saw earlier tosee what the commands are, to make sure that you're not accidentallyoverwriting something.Let's create another one now.Let's do alias hello= and I'm going to have it echo "Hello World!"I wanted to show you this example partly to show you that you can use aliasesfor absolutely anything, but I also wanted to show you the pairings of quotes.It really doesn't matter whether we use single quotes or double quotes on theinside or the outside. The important part is the pairing of these.

We want to make sure that the alias command can tell what the entire string thatit's aliasing ought to be.And that's what those single quotes indicate.If we wanted to use double quotes in all cases, we would just need to escape thatstring by using the backslash in front of them.So I just want to make sure that I point that out to you.So now we have that.Now, if we type alias, you'll see that we get the list of aliases that includeshello and typing hello does exactly as advertised.It echoes back Hello World!If we want to remove an alias, then we just type unalias.unalias hello, and now it's gone.

alias and hello, command not found.So in addition to making sure that you're not overwriting a real Unix command,here is the catch about aliases though.Aliases only last for the current login.So ll, it's a valid command.Close the window and open a new one, just clear the screen. ll. Nope, command not found.So we typed it in the command line.It made an alias and that stuck around only for the current login.What we want is for those aliases to be defined for us every time that we log in to Bash.

So where do we put commands that we want to run every single time?Well, we do those is our configuration files, either in .bash_profile oreven better in .bashrc.So we'll write our alias commands in there.Whenever we log in to bash, whenever we open a new shell, what will happenis bash will read those commands, define those aliases for us, and they'llbe available to us.So let's do that. .bashrc, and we'll just drop-down here and we'll just call thealias command just like we did before.Remember we did the cal command, for example, before.

It's no big deal to write our normal shell commands here.la, and I'm going to go ahead and add the h and g options to that.Those are h for humanize and g will just help to colorize the output.Now, as I said, you can do all sorts of variations on this.You might have something that is la, that does ls -a, or you might have lh that does ls -h.Any combination you want. You can come up with whatever suits your personal taste.I'm going to just show you some more that are common that a lot of people like to use.We have cd into our home directory as just simply home.

That will go to our user home.Alias up, a lot of people like this. Just cd to go into the parent directory.That will go to up one.And alias h, history, so that will just simply show your history.Now, again, you don't have to use these, but these are just ones that Ifrequently see other people use.Another nice use of aliases is to actually redefine some of the Unixcommands but with options.Let me show you what I mean.We say we wouldn't want to overwrite something like mv, move, because we need that, but we can overwrite it with options.

So whenever someone types mv, well, it's actually the same thing as saying mvwith the interactive option.Same thing for copy and remove, and basically that interactive will just pop-upand tell us before it actually overwrites a file or deletes a file.So that's a nice way to make mv, cp, and rm sort of idiot proof. They willalways prompt you and say, are you sure you want to do this before I destroy it?We can also on the df and du commands, we can provide the -h for humanize, andfor making a directory we can provide the -p option and that will then makesure that we create any parent directories that we need.

Now, if you don't want to use any of these, you can simply comment them out withthat hash or pound sign.Another common thing that a lot of Unix users like to do is sort of correcttheir own typos ahead of time.So if you find yourself that you frequently type pdw instead of pwd, well thenyou can make an alias.No sense in wasting time correcting your typo.If you find you do it often with sort of bumble fingers, well then, fix it.Make pdw equal to pwd, and you'll know what those frequent misspellings are, right,because they're going to be different for every person, but I just wanted toshow you that's an example of the kind of things you can use aliases for.

So now that we have all these in our .bashrc file, let's try it out.Ctrl+X. We'll save it into our .bashrc.Now let's try one of those out.Let's try that pdw that I just created. Oh, command not found.Now, why is that?Because it hasn't read in that .bashrc file yet, right?We've got to close this window and then it will open it. Otherwise we could alsotype source .bashrc and that would do the same thing.That reads in that bash file, now our pdw alias works.So again, either close that window or run source on that file so that it willactually read in the contents of that file to make those aliases available.

But now even when I close my window and open a new one, now I can do ll and Istill get my directory listing and I get all these items humanized and now myentries show up in color.So I think aliases become a really indispensable tool in working with Unix.For me personally, I've developed a sort of mini library of aliases that I liketo use to help me do things faster and I take those from machine to machine.When I move to a new Unix environment, I want to have my aliases available soI can do things like type ll and get those same results that I'm used to working with.

So I encourage you to experiment with aliases and really try and build up somealiases that work well for you.

Find answers to the most frequently asked questions about Unix for Mac OS X Users .

Here are the FAQs that matched your search "" :

Expand all | Collapse all

please wait ...

Q: The exercise files for the following movies appear to be broken:
07_02_files
07_03_files
07_04_files
07_05_files
08_03_files

Is there something wrong with them?

These exercises include one or more "dot files", whose file names start with a period. These files are normally hidden from view by the Finder. So that they would show up in the Finder, the period has been removed from the file names. Additionally, "_example" has been added at the end of the file name to make it clear that the file will not work as-is.
To make the dot files usable, either:

1) Open the file in a text editor to view its contents. Note that it may not be possible to double-click the file to open it because there is no file extension (such as .txt).
2) Resave the file under a new name (usually by choosing File > Save As), adding a "." to the beginning of the file name and removing "_example" from the end.

OR

1) Copy and rename the file from the Unix command line using the techniques discussed in this course. Rename the file by adding a "." to the start and removing "_example" from the end. Include the "-i" option to prevent overwriting an existing file unexpectedly.
Example: cp -i ~/Desktop/Exercise\ Files/Chapter_07/07_02_files/bashrc_example ~/.bashrc

Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.