Viewers: in countries Watching now:
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.
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.
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 the shortcut 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 line I'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 perform any task that we want. You just want to be careful that you don't use an alias name that is for an existing 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 to see what the commands are, to make sure that you're not accidentally overwriting 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 aliases for 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 the inside 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 that it'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 that string 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 includes hello 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 or even 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 happen is bash will read those commands, define those aliases for us, and they'll be available to us. So let's do that. .bashrc, and we'll just drop-down here and we'll just call the alias 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 I frequently see other people use. Another nice use of aliases is to actually redefine some of the Unix commands 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 mv with the interactive option. Same thing for copy and remove, and basically that interactive will just pop-up and 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 will always 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, and for making a directory we can provide the -p option and that will then make sure 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 with that hash or pound sign. Another common thing that a lot of Unix users like to do is sort of correct their own typos ahead of time. So if you find yourself that you frequently type pdw instead of pwd, well then you 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 to show 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 also type 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 will actually 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 I still get my directory listing and I get all these items humanized and now my entries 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 like to 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 so I 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 some aliases 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 "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.