Unix for Mac OS X Users

Unix for Mac OS X Users

with Kevin Skoglund

 


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.
Topics include:
  • Moving around the file system
  • Creating and reading files
  • Copying, moving, renaming, and deleting files and directories
  • Creating hard links and symbolic links
  • Understanding user identity, file ownership, and sudo
  • Setting file permissions with alpha and octal notation
  • Changing the PATH variable
  • Using the command history
  • Directing input and output
  • Configuring the Unix working environment
  • Searching and replacing using grep and regular expressions
  • Manipulating text with tr, sed, and cut
  • Integrating with the Finder, Spotlight, and AppleScript

show more

author
Kevin Skoglund
subject
Developer, Web
software
Mac OS X 10.4, 10.5, 10.6, 10.7, Unix
level
Beginner
duration
6h 35m
released
Apr 29, 2011

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Introduction
00:04Welcome to Unix for Mac OS X users.
00:06My name is Kevin Skoglund and as a web developer I probably spend as much time
00:09working in Unix as I do writing code.
00:12In this course we are going to learn to use the powerful Unix operating system
00:15that's built into Mac OS X. I'll walk you through the fundamental concepts used
00:19in Unix, we will discover the Unix file system and learn to create, find, copy
00:24and delete files and directories all from the command line, and we'll see how
00:27user accounts function in a multi-user environment, and learn how to manage file
00:31ownership and user permissions.
00:33We will also cover dozens of useful Unix commands, everything from simple
00:37calculators to tools that monitor and manage every process running on your computer.
00:42I'll introduce you to some power user techniques that will allow you to find,
00:46alter, or capture almost any text or system information.
00:49Now most of what we cover will be useful in any Unix environment, whether it's
00:53Mac, Linux, or something else.
00:54But in the final chapter I'll demonstrate many useful Mac only commands and
00:58techniques that can speed up your workflow and give you more power and
01:01control over your Mac.
01:02It doesn't matter if you're a complete beginner or have some prior experience.
01:06We are going to cover all the fundamentals you need to become comfortable
01:09working from the Unix command line.
01:11So let's get started learning Unix.
Collapse this transcript
Using the exercise files
00:00If you are a premium member of the lynda.com Training Library or if you're
00:04watching this tutorial on a disc, you'll have access to the Exercise Files that
00:07are used throughout this title.
00:09The Exercise Files for this title are arranged by chapter and by movie, and you
00:14can find the Exercise Files that correspond movie that you're watching by first
00:18looking for the chapter number and then the movie number.
00:21You'll want to copy the contents of that folder into your user directory or to
00:24another convenient location.
00:26It's always a good idea to make a copy so that you still have the original to
00:29refer back to if you make changes.
00:31To do this on my Mac I open up a new window using Command+N, which by default
00:36opens in my user directory, and then I can simply Option+Drag over the folder
00:40contents from the Exercise Files to create a copy.
00:45Once you do that your files will be the same as mine at the start of that movie
00:48and you'd be able to follow right along with me from there.
00:52For some movies, the Exercise Files include one or more dot files.
00:56These filenames start with a period and are normally hidden from view by the Finder.
00:59So these files would be able to show up in the Exercise Files they've been renamed.
01:03We removed the period and we added an _example to the end of the filename, to help make it
01:08clear that the file will not work as is.
01:11In order to work with these files you have two options.
01:14The first is that you can open the file in a text editor to view and copy its contents.
01:18But take note that you may not be able to double-click the file to open it like
01:21I did, because there is no file extension at the end, like .txt.
01:26In that case you can drag the file onto the application's icon where you can
01:30select it using the application's Open File menu option.
01:33The other choice is that you can copy and rename the file from the Unix command line.
01:38You want to use the Copy command so that you can make sure you retain the
01:41original one and I recommend using the -i option to make sure that you don't
01:44accidentally overwrite an existing file.
01:47After that, to get the path to the file the shortcut is simply to drag the file
01:51into the Terminal and that will write the full path you need out and then you
01:55can put the location of where you want to put it.
01:57This case it will be in my user root directory, and I am going to name the file
02:00.bashrc, which is the name with a period in front of it, and removing at the end.
02:06Once you do that the file will be copied and you'll be ready to work right along with me.
02:10You can also just use the Exercise Files as a reference to check your work as you go along.
02:15Now you won't need any special software installed. Everything you need is
02:18already included with Mac OS X.
02:21If you're on a monthly or annual subscriber to lynda.com you will not have the
02:24Exercise Files that accompany this tutorial.
02:26But you can follow along with me.
02:28Everything that's in the Exercise Files we will create during the tutorials.
02:32So as long as you continue to work right along with me your files will exactly
02:36mirror what's in the Exercise Files, and remember that you can pause the video
02:40and rewind if you need more time to copy something down.
Collapse this transcript
1. Introduction to Unix
What is Unix?
00:00To start things off let's begin by getting an understanding of what Unix is and
00:04how Unix got to the point where it is today.
00:06Unix is an operating system.
00:08It was created during 1969 to 1971 by AT&T employees working at Bell Labs.
00:12A lot of these Bell Labs employees have been involved in designing a mainframe
00:17operating system called MULTICS, which was short for Multiplexed Information
00:23and Computing Service.
00:25Back then, mainframes were expensive so users were granted time-sharing slots
00:29to do their work and to use the power for the mainframe, often by logging in from
00:33a remote terminal that had far less computing power.
00:36MULTICS was designed to manage that remote login time-sharing process.
00:40But MULTICS became such a huge and complex project that Bell Labs decided to pull out of it.
00:45The Bell Labs employees who'd worked on MULTICS still like the project's
00:48goals and so, almost as a side project they worked on building a smaller and
00:52simpler version of it.
00:54Originally, their version only supported a single user, so that as a play on the
00:58name they called it UNICS,
01:00Uniplexed Information and Computing Service instead of Multiplexed.
01:04Very quickly though it became able to support multiple users and so they
01:08renamed it Unix with an X, which doesn't actually stand for anything anymore. It's just Unix.
01:13Now at this point Unix is just another mainframe operating system being used
01:17internally by Bell Labs.
01:18But two important events give Unix a big boost in popularity.
01:23First, in 1972 Unix was rewritten in the C programming language.
01:28Up until that point, operating systems were typically written in Assembly language.
01:32Assembly was a very low-level programming language that required the code to be
01:35written for specific computer hardware.
01:38It's very fast, but it's not portable.
01:40If you wanted to port an OS to a new computer type, then you have to rewrite
01:43major portions of your code to do it.
01:45The C programming language was a programming language originally designed for
01:49the Unix operating system to write programs for the Unix operating system.
01:53It's a low-level programming language but not as low-level as Assembly, and so C
01:58makes writing programs easier than Assembly and also makes them
02:01hardware-independent.
02:02So the important development here is that not only do they write programs in C,
02:07but they took the additional step and said, "Hey, what if we rewrote the entire
02:10operating system in C, so that then Unix becomes portable too?
02:14We don't have to rewrite the operating system whenever we want to take it to a
02:18new type of hardware."
02:20The next important event is the spread of Unix outside AT&T. Because of a
02:24court order in an antitrust case AT&T had been forbidden from entering the
02:29computer software business.
02:30So therefore they could not sell the Unix operating system. They can only give it away.
02:35So that's exactly what they did.
02:37If you wrote to them and asked them for a copy of Unix, they would give you a
02:40free license and send you all the source code for it.
02:42That made it very attractive to government agencies, universities, and
02:45corporations who had these mainframe computers because now they could get a free
02:50license and all the source code to be able to run on their mainframes.
02:54Perhaps the most important of those three is actually universities.
02:57Because if you think back, in 1975 this is a point when computer programming is
03:02really beginning to take off and there are a lot of people at universities who
03:04are learning about computers for the first time, getting access to mainframe
03:08computers for the first time, and what are they using? They are using Unix.
03:13So Unix becomes the first thing that these programmers learn.
03:16So they go on to find all these software companies throughout the 80s and 90s
03:20and the thing that they first started working with was Unix.
03:23So the first time release in 1975 is often referred to as System Five and that's
03:28kind of the first public version of Unix.
03:31But over time more branches and improvements are released and that keeps going even today.
03:36The most famous of these is probably the Berkeley Software Distribution, also
03:39just called BSD for short. That came out in 1977 and then there've been other
03:45ones that are either open source, closed source, or mixed source.
03:48Linux is open source. Closed source versions, each major software company
03:52seems to have their own.
03:53Sun Oracle has Solaris, IBM has AIX, and Hewlett-Packard has HP UX.
03:59And of course the mixed source version, mixture of open source software and
04:02closed source software, is Apple's Mac OS X.
04:06So it's gotten to the point now with all these branches and improvements that no
04:09one is really using Unix, the pure System Five Unix that originally came out.
04:14So Unix now really means a Unix-like system.
04:18There are all these different versions, they all have their quirks and
04:20peculiarities, they are a little bit different, but they all work according to
04:23the same principles and are very, very similar.
04:26So if you hear Unix now, it really can mean all of these things.
04:30In fact mobile devices now are Unix too.
04:32iPhone, the iPad and Android are all based on Unix. They are Unix like systems.
04:37Now of course the version that we're most interested in is going to be Mac OS X Unix.
04:42Mac OS X is based on BSD Unix, the Berkeley Software Distribution.
04:47But in addition to that there is code that was developed at NeXT.
04:50NeXT is a software company that Steve Jobs founded after leaving Apple and they
04:54created code that they called NeXTSTEP, which went on top of BSD Unix. When Apple
04:58bought NeXT they got all of that code and took it and added more improvements to
05:03it and called the combination of those three Darwin.
05:06So if you hear anyone refer to Darwin, that's what it is.
05:09It's BSD Unix plus NeXTSTEP's code plus Apple's continued improvements to it
05:14that make up Darwin, which is the Unix that sits underneath Mac OS X. Mac OS X
05:20includes a lot more than that.
05:21It's the Finder and all these other things as well.
05:23But Unix is under the hood.
05:25The Finder and system preferences interact with Unix.
05:28Now we can interact with Unix directly by using the Terminal application and
05:33we'll start doing that in the next movie.
05:35The command line is going to allow us to type the commands that we want Unix to
05:38do, instead of using the graphical user interface that you're familiar with, with
05:43your typical Mac operating system. So why do this?
05:46Why use Unix directly when you have this graphical user interface?
05:50Well there is a lot more power in the command line.
05:53Imagine this scenario and think about how you would do it in the Finder.
05:56Search your hard drive for every file whose name contains invoice and which was
06:00created after a certain date.
06:02Then remove the file extension from each filename and save that list to a text file.
06:07Now with Unix that type of data intensive task is very easy to do.
06:11It's not the answer to everything.
06:12You could never do in Unix what you do in Photoshop for example and it's nice to
06:16be able to just drag and drop files into folders with the mouse instead of
06:20having to type the command into the keyboard.
06:22But as a general rule when working with data, non-data elements can get in your way.
06:27I think of it as being very similar to the difference between car transmissions.
06:31If you have an automatic transmission you give up control for having the convenience.
06:35You don't have to worry about shifting gears.
06:37It just does it for you.
06:38But you're not necessarily getting the best shifting possible or exactly what you want.
06:43With a manual transmission you give up the convenience but instead you get control.
06:47So essentially what we are going to be doing in this training title is
06:50learning to drive stick.
06:52Now one final note before we do that.
06:55Most of all we cover in this course is going to apply to any flavor of Unix.
06:59All Linux distributions, Red Hat, Ubuntu, DBM, Solaris, AIX, each flavor of Unix
07:05may require a few modifications.
07:07Maybe the options to a command won't be exactly the same or maybe there are
07:11some new commands that exist on one of those distributions but not on another one.
07:14But you'll have enough knowledge that you should be able to quickly adapt to
07:17those systems and that makes this course useful not just for Mac OS X users,
07:21but also for web developers who plan to deploy their website to a remote LINUX server.
Collapse this transcript
The terminal application
00:00As I explained in the last movie, the way that we'll interact with Unix is going
00:04to be from the command line and the way that we'll do that on a Mac is going to
00:08be using the Terminal application.
00:10It's going to provide a command line access to Unix and it comes preinstalled
00:13with Mac OS X. So if you have Mac OS X, you have this application.
00:17It's going to be located inside your Applications folder, inside the folder
00:21called Utilities, and there you'll see the program Terminal.
00:25A very useful shortcut is that if you're in the Finder you can type
00:28Shift+Command+U and it will open the Utilities folder directly and there you'll see it.
00:32Just try that together and we can launch Terminal and see how it works.
00:36So here I am. You can see that I am inside my Finder. I'll hold down
00:38Shift+Command+U and it will open up my Utilities folder and right down here
00:43you'll see that I have Terminal.
00:44Now I could just double-click it to launch it, but because we're going to be
00:47using it a lot throughout this training title I am going to drag it into the
00:50dock down here so I'll have good easy access to it.
00:53So I close that window and now I can launch it and now what we're seeing is Unix
00:58or our command line access to Unix.
01:00You can see that gives us our last login information followed by a bunch of text
01:05which is our prompt, and then after that is our cursor where we can type. So we
01:09can just type hello for example and then I can hit the Delete key to erase that.
01:13So that's where we are going to type in our commands and then when we're done
01:16we'll hit Return after each command and then Unix will do whatever we've
01:19asked and respond to us.
01:21Let's take a look at a couple of things we can do to customize our Terminal
01:24environment before we move on.
01:26The first is that by holding down the Command key, either with or without the
01:29Shift key, and pressing Plus or Minus we can change the font size and the
01:34window size as well.
01:35So things will scroll up or down if you want something a little bigger to look at.
01:38An even better way to do this though I think is to go into the
01:42Terminal Preferences.
01:44Under Terminal Preferences we can change what Window settings we use to start up.
01:48Mine right now uses basic.
01:50If I click Settings we can see what those settings are for basic.
01:53I also have a number of other ones here, including one that I've created myself, OTL_Prefs.
01:58You can use the plus sign here to create a new one or you can actually
02:01duplicate settings from one if you want to make changes, just a few
02:05modifications to give it its own name.
02:07Here is mine for OTL_Prefs. What I've done here is I've changed my cursor from a
02:11block cursor, which we see here, to being just an underline.
02:15That's a little easier to see. When my cursor is on the top of the character you
02:18will be able to see the character underneath a little easier.
02:20I've also changed the text colors here and under Window I've changed the
02:24Background Color to black.
02:26I think that this color text on a black background is a little easier to read
02:30for me and also I think in the video it's easier for you to be able to read as well.
02:34So I've made those changes.
02:35The other real change I think I've made here is that under Advanced I've turned
02:39off the bell that sounds at times.
02:42It's sort of a warning bell.
02:43It bothers me. I don't like it.
02:45It's just a personal preference. I don't like having that audible bell.
02:48But you can play around with these things.
02:50Don't really mess with the Shell and the Keyboard, those are a little more
02:52advanced, but feel free to go ahead and make changes to Text, Window, and this
02:57Advanced window as well and see if you can get an environment that you like.
02:59You also see I am using a larger font size here as well.
03:02So I am going to make this my default now by clicking Default and I am going
03:06to go to Startup and make sure that that says New window will start up with OTL_Prefs.
03:10So there we are. If I close this now, I can open a new window by going to
03:15Shell > New Window or you can see I could just use the Command+N key to get a new
03:19window like you normally would a Finder window and it will open up now with my
03:23new color scheme, larger fonts and everything like that.
03:26So just wanted you to see how I arrived with this different look. Feel free to
03:29customize it to your needs.
03:31There is one more thing in addition to the plus/minus, which makes your
03:34font bigger/smaller.
03:35Another nice shortcut to know is Command +K, which you'll see from up here under
03:41the View, is Clear Scrollback.
03:44What that does is as I'm typing, I am just going to hit Return a whole bunch of
03:46times here, you will see that it keeps scrolling and we started getting a
03:50scrollbar here that let's us go back and review what we've done previously.
03:53That's called the scrollback, and if we use Clear Scrollback or Command+K,
03:58it clears that for us and we no longer have it available to us to scroll back up.
04:02So it's useful if you sort of want to get everything off your screen and get a
04:05clean environment again, you can use that Command+K trick and you may see me do
04:09that from time-to-time.
04:10There is no harm in leaving all those old commands there. You can actually go
04:13back and review them.
04:14You can even search them if you need to search for something that you did previously.
04:18So sometimes it can be really useful to have that scrollback.
04:20It's really up to you how you want to use it.
Collapse this transcript
Logging in and using the command prompt
00:00Now I want to just talk a little bit more about what's happening when we launch Terminal.
00:04Notice here this first line, Last login. That's telling us the time and date
00:08that we last logged into Unix.
00:09Every time we open a new window it logs us into Unix again as a new Unix session.
00:14We can have several of those open at once.
00:16I'll use Command+N to open a new window and now you can see that I have a
00:20different Last login time.
00:22There is no problem having several of these open once. Each one is a Unix
00:25session and each time I'm being logged in as Kevin.
00:28I'm being automatically logged in on the Mac.
00:32Other Unix systems don't automatically log you in.
00:34You'd put in your username and your password for it to know who you are.
00:38But your Mac already knows who you are.
00:40Now if you are using your Mac as a single-user environment, that may come as a
00:44bit of surprise to you because you may not even think about the fact that you
00:47are logged in as you.
00:49But if you go to Apple menu and down to System Preferences and into Accounts,
00:53you can see that we can manage different user accounts.
00:55Now you may only have one or you may have been taking advantage of this feature
00:59and you may have family members or co-workers or people like that who have
01:02accounts on the machine as well.
01:03But for a lot of people they just use a single-user account and under Login
01:07Options you may even have Automatic login set to True.
01:09In which case you don't even enter your username and password then.
01:13It remembers that it does it for you.
01:14So your Mac is completely turned off, you turn it on, and it automatically logs
01:19you in with your username and password, and voila, you are logged in as you.
01:24It's very much the same user experience that you would've had with OS 9 which
01:27was a single-user system.
01:29But once we moved OS X with this Unix underneath, Unix is fundamentally a
01:33multi-user environment.
01:35Even if you're only using it for just you, it still has all those multi-user
01:39features and that's going to have some applications later on that we'll need to talk about.
01:43So realize this: that when you first fire up your Mac you are being logged in as a user.
01:47Even if you don't explicitly put in your username and password, and therefore
01:52when you start your Unix session using the Terminal you're also being logged in as that user.
01:57So the next I want is to look at the second line and see what's going on here.
02:01What we have to begin with is what we call the command prompt.
02:04This is prompting us for some action, and we'll see how to configure this later on.
02:08Right now by default, it's going to be configured to the host name, the
02:12directory that we are in and then the user that we're logged in as and then this
02:16dollar sign here which is the prompt.
02:18Dollar sign space and then my cursor is here waiting for me to type something.
02:22It's prompting me to give it some commands.
02:25So I am going type a simple command, echo and then a single quote followed by
02:29Hello World and another single quote.
02:32I'll hit Return. That tells that all right, I'm done typing, take this command
02:36and execute it and it does.
02:38You'll see the response it gives me back and then I get the command prompt again
02:41saying, "Okay, I've done what you asked and now I'm ready for more input."
02:45Let me go ahead and just type a few more commands in here just so we have them.
02:48echo 'Hello', echo 'World' and then I want to show you that you can use the up
02:54arrows and down arrows to go back to previous commands.
02:57So you can access those previous commands using that and that's an
02:59indispensable tool.
03:00You are going to be using that all the time, up and down.
03:03You can even then use the forward and back arrows to go in and edit the line and make changes.
03:08So it can save you a lot of typing. You say, oops!
03:10You know what? I meant to do that command but I meant to have three
03:13exclamation points after it.
03:15Well you don't have to do the whole re- typing. You just go back, make the change
03:18you want and then hit Return again.
03:20There are some other useful shortcuts for working with command line that I want
03:24to just take a look at.
03:24But before we do I just want to show you that you can use Exit to exit a
03:28session or you can just simply close the window. That does the exact same thing; it exits you out.
03:33So if I hit exit, you'll see now it says logout and now it doesn't take any
03:36input from me anymore.
03:37I can then go up and close the window and whenever I want to log back in, I just
03:41open a new window and now it's ready to take my input again.
03:44I am logged back in.
03:45All right, let's look at some other shortcuts.
03:47As I said we've got the up and down arrows to review previous commands. Ctrl+A
03:52will move your cursor back to the start of a line.
03:55So if you've got a long line of 50 characters and you want to make a change at
03:58the beginning or the change you want is closer to the beginning, Ctrl+A will
04:02just shoot your cursor back to the beginning. Ctrl+E will go to the end.
04:05So I remember it by thinking A is the beginning of the alphabet, E stands for
04:09End and that will shoot me to the end of the line.
04:11On the Mac, inside Terminal, you can also Option+Click a point on a line and it
04:16will take your cursor to that point.
04:18I find that that can be a little buggy sometimes.
04:20It doesn't always work exactly like I expect, but it can be a really quick way to get there.
04:25So try that first.
04:26Option+Click the spot on the line where you wanted to go to and it should just
04:29move your cursor back along.
04:31Another very useful tool is Auto Complete.
04:33If you're in the middle of typing something and then you hit Tab it'll try to
04:36guess what the rest of it is, based on the available commands or the
04:40available filenames.
04:41So that's really useful if you want to type a whole filename. You don't have to
04:44type the whole thing; you can just type enough that it can tell the difference
04:47and hit Tab and it will finish it for you.
04:49Tab+Tab can be used whenever the Auto Complete doesn't work, because it's not
04:55sure what the matches are.
04:56So let's say here's five files that are all very similar.
04:59You type the first few letters and it can't tell from that which one you want.
05:03Tab+Tab will show you a list of what all of those possible matches are.
05:08And then the Command key plus the Tilde key will cycle between Terminal windows
05:12if you have several windows open and Command+K as I mentioned before will clear
05:16the screen and scroll back.
Collapse this transcript
Command structure
00:00Let's talk a little bit more about the structure of the command so that we make
00:03sure we always use them in the right way.
00:05There is a very definite structure in Unix.
00:06Commands are always going to be the command followed by the options followed by
00:11the arguments, always in that order. Command then options then arguments.
00:15You can't put the options after the arguments.
00:17They have to come in the middle.
00:19The command is always a single word, always.
00:22It's the thing we want to do.
00:24So in our case it was echo.
00:25That was the command that we were showing. echo (space) and then we didn't have any options.
00:30Options are optional.
00:32So options, if we have them, will modify the behavior of the command in some way.
00:36We'll see how to see what the available options are for each command.
00:38But for now just understand that it's command space, then the options, and then a
00:43space followed by whatever the arguments are.
00:46In our case the argument was a string, Hello World.
00:48It's the thing that we want the command to use when doing whatever that command does.
00:53So echo 'Hello World' echoes 'Hello World'.
00:57'Hello World' is the argument.
00:58Let's take a look at some examples and we'll be able then to see the different
01:01forms that options can also take.
01:05So as we saw we had just simple echo 'Hello World'.
01:07If we wanted to pass in an option, we could pass in the -n option that
01:11suppresses a new line Return after it outputs Hello World. So we don't get a
01:16new line at the end.
01:18It would just give us the command prompt immediately.
01:20So you can see there we divided them again with spaces, the command, then the
01:23options, then the argument.
01:26Notice also that the option has this hyphen in front of it.
01:29That's the other clue that we give to Unix that we're talking about an option
01:32here and not an argument.
01:34We don't always have to have an argument either, for example, we could have
01:38ruby -v.
01:40That will return the version of ruby that we have installed on our computer.
01:44-v is a very common option in Unix for returning the version of something, the
01:48software version, the command version.
01:50We also can do ruby --version.
01:54It does the exact same thing.
01:55It's just a different way of specifying the option.
01:57So you'll usually either have a single dash followed by a letter, or dash dsah
02:03followed by a keyword.
02:04It tends to not be a letter in that case.
02:06It tends to be a key word, but both of them work the same, -v or --version.
02:11We can also have a list of options ls -l -a -h and then the argument, the
02:19Desktop. That's actually a folder that we're talking about in that case, a directory.
02:23So we can have multiple options.
02:24Typically what you have in that case though is we smash them altogether. It has
02:28the exact same effect.
02:29We could mix and match these in any way we want. l, a and h together are the
02:33same as if we had broken them up.
02:35Notice that the spaces make it pretty easy to tell where the command and option arguments are.
02:42There is an exception to this, which is that sometimes an option wants an
02:46argument of its own.
02:47So for example the banner command -w specifies the width of the banner.
02:53It wants to know how wide you want it.
02:55So if we say 50, we need to pass that in as an argument to the w. So 50 is not
02:59an argument to banner; 50 is an argument to the w option.
03:03Hello World is actually the one and only argument for banner.
03:07To make that less confusing, a lot of times what you will see is people just
03:10eliminate that space. banner -w50 and that makes it clear that this 50 belongs
03:16to the w option followed by the string.
03:18We can also have multiple arguments. Just to show you that, we can have cat with
03:23the -n option or without it.
03:25-n option will number the lines and then cat will output the content of file1
03:30followed by the content of file2 and it will just use -n option to number those.
03:35So hopefully that gives you a feel for how these work. Let's try out a couple just so
03:38you can see how they work.
03:39So here I have a command prompt. So as we saw before, we could do echo 'Hello World'.
03:45Incidentally you can use double quotes instead of the single quotes.
03:48It works exactly the same.
03:49I just had opted for the single quotes.
03:52And then we can do the same thing, but we can back up here and put in the -n option
03:56and now we get the same thing without the new line return.
04:00Let's go ahead and get a new one here and let's type the ls command.
04:04It shows me a listing of the directories.
04:06ls -lah will give me a different listing of those directories.
04:12In the next chapter, we'll talk more about directories and these different
04:15listing options, but as you can see now, we get two different things based on
04:19the options that we put out.
04:20We can also do banner -w50 and let's do 'Hello World'. We'll just see
04:28what banner does for us. There it is.
04:29You can use the scroll, you can scroll back up here, and you can see that it
04:31outputs this big banner, 'Hello World.' It's horizontal, but that's suitable then
04:39for printing on your printer, if you wanted to have a big easy banner done that way.
04:42It is just sort of a fun little program.
04:43Another thing worth noting is that you can put semicolons between commands if
04:48you want to do several things at once. So echo 'Hello '; echo 'World'.
04:58Now it's going to do one command. Semicolon tells that now we're going to do a
05:01new command, and then we've got the next command right after it, so semicolon
05:05to break up commands.
05:06We can also of course put in that -n option in front of hello and we get it all
05:12on one line, Hello and World.
05:13That's actually two separate commands that's doing.
05:15So that's the fundamentals of command. Just remember that it is always the
05:18command followed by the options then followed by the arguments.
Collapse this transcript
Kernel and shells
00:00Before we dive in to actually using Unix, there's one last fundamental concept
00:04that I want to make sure that you understand, because we're going to be using
00:06it a few times later on and that is the difference between the kernel and the shells.
00:10So the kernel is the core of the operating system in Unix.
00:15It's what takes care of allocating time and memory to program,
00:18really doing sort of very fundamental root level management of how programs
00:22go about doing their thing.
00:23Mac OS X uses the Mach kernel inside Darwin to do that.
00:28So if you ever hear someone refer to Mach kernel,
00:30that's what they are referring to, is this very central part of the Unix
00:33operating system that really just takes care of how the operating system handles
00:37very basic tasks like memory management.
00:40Then outside of that there is the shell.
00:43The shell is the outer layer of the operating system.
00:44That's what we see when we open up a Terminal window.
00:47We're working in the shell.
00:49It interacts with the user and we can think of it as our working environment.
00:52The shell will send requests to the kernel.
00:54The kernel will then do its thing and results will then be returned back to the
00:59shell so that we can interact with them again.
01:01Mac OS X by default uses what's called the Bash shell, B-A-S-H, but it includes
01:07other choices as well.
01:08Let's talk about what some of those other choices are, because it also gives you
01:11a little bit more of the history of Unix as well.
01:14The popular shells are sh, which is the very first one.
01:18It's called the Thompson Shell and it was created in 1971 and that was the main
01:23working environment that people had for a long time in Unix.
01:26In 1977 something called the Bourne Shell was created and that was a replacement
01:31for the Thompson Shell.
01:32So it completely replaced it.
01:34It has the same name, sh, short for shell, and it just replaced the old one.
01:39Starting though in 1979, people said "Hey, instead of replacing shells all the
01:42time, let's get people choices.
01:44Let's have different shells and then if someone loves one shell or another, they
01:47can just switch between them."
01:48They started having different names at that point.
01:50We have csh for C Shell, a little bit of programmer humor there.
01:54Then we have the Tabbed C Shell in 1979, tcsh, the Korn Shell, ksh, then we have
02:02the Bourne-Again Shell, again another bit of programmer humor. Take off the
02:06Bourne Shell, this is the Bourne-Again Shell, which is bash, and that's the one
02:10that we have by default.
02:11And then the Z Shell.
02:13It's one of the most recent.
02:14There are a lot of others. They almost all end in sh.
02:17You can go some place like Wikipedia to get a full list of all of those, but
02:20these are the main ones and these are all installed on Mac OS X. So we have the choice.
02:25These are all there waiting for us to use. Actually I think that back.
02:28Thompson Shell has gone.
02:30It's been completely replaced by the Bourne Shell, but the other ones are all
02:33there on the Mac for us to choose from.
02:35For beginners, the difference between these shells is tiny.
02:38A lot of the difference between them is going to be the features that are
02:41available to high-end users for task like advanced shell scripting.
02:46For now, stick with bash to learn.
02:48You can always switch later, but I want you to understand that you are working
02:51in the bash shell when you log in.
02:54Now you may be tempted to think that a shell is just like an operating system,
02:59because we can pick between these different ones, that it somehow like the
03:02difference between Windows and Mac or something. It's not.
03:05It's just a working environment that we can choose and move between and in fact
03:09we can move from inside one shell into another shell without leaving the old one.
03:14We can nest them inside each other.
03:15It's just like moving into another working layer.
03:18Let's go to the Terminal and take a closer look.
03:21So here I am in my Terminal, and you see up at the top here it actually tells me
03:23that I'm in the Bash Shell.
03:25It's right there at the top.
03:26That's a nice giveaway.
03:27I can also find out what the login shell, the one that is going to put me in by
03:31default is, with echo $SHELL.
03:37That's like I can know it is an environment variable. It has the $ in all capitals.
03:41And it says "Oh, bin and bash.
03:43That's what I am going to start you up in when I first launch."
03:46That's actually changeable here in Terminal > Preferences, under Startup.
03:50Shells open with the default login shell or we can pick something else.
03:55So if we want to launch it with a different shell, we can do that there.
03:57Now again, that's the login shell that we're seeing. If we want to see the shell
04:00that we're in right now, it's echo $0.
04:06That's the shell that we are working in right now. Not the login shell, the
04:10shell that's going to be run when we launch this, but the shell that we're in.
04:13Why that's important is because we can go inside other shells.
04:16Let's take an example. Let's go into the Tabbed C Shell, tcsh. Just type it at
04:22the command line and boom! We changed shells.
04:24Now just to show you the difference, let's do echo $SHELL and you'll see that we still get bash.
04:29If we do echo $0, now you can see that we are inside the Tabbed C Shell.
04:34Just to show you again, let's try a few other. Let's go bash. Now I am inside bash.
04:38Let's do csh. Now I am inside the C Shell. sh, now I am inside the Bourne Shell.
04:44Now to get back out of each of these, you just type exit and you can see that
04:47the prompt changes slightly each time as I back out, until finally I get back to
04:52my original prompt and now I know that I am back at the Bash Shell.
04:57So as you can see, you can move between these.
04:59You are just moving into a new working layer, a new working environment.
05:02You won't need to do this; you can just be in bash all the time.
05:07But I just want you to know that that's where you are, that you are inside bash,
05:11and that as an advanced user you do have these other options.
05:14So if you hear people talking about, "Well, I know how to do this in the Z shell,
05:18but I don't know how to do it in bash," that's what they are talking about.
05:22They are talking about these different working environments.
Collapse this transcript
Unix manual pages
00:00This movie may very well be the most valuable movie in this entire training title,
00:04even though it's very simple.
00:06The Unix manual pages are an invaluable resource for helping you figure out what
00:10you want to when you're working in Unix.
00:12In fact, I learned 80% of the Unix I know from the manual pages.
00:16These are going to be your best friend during your journey.
00:19The manual pages, often simply refer to as just the man pages, is called that
00:23because the command to get to them is just man, and the man followed by whatever
00:28we want to look up in the manual.
00:30man echo will give us the manual pages for the echo command.
00:33Go ahead and hit that, you will see at the top BSD General Commands Manual.
00:37The name echo, what does it do?
00:39Write arguments to the standard output.
00:41Then we get a synopsis, then we get the description. You can see the option
00:45-n there, do not print the trailing new line character, and then at the
00:48bottom we get a colon.
00:50Now let's just know that there's more. We can see more.
00:52To see the next page, you can simply hit the Spacebar or you can use F and B to
00:57go forwards and back. F forward, B backwards.
01:00Q will exit for quit.
01:03Those are very common options in Unix.
01:05So let's go ahead and hit the Spacebar. We get down to the end.
01:07It says END. Now we have to hit Q and now the manual pages are gone.
01:11We're right back where we were.
01:12They don't stay there and crowd up our view.
01:14They just disappear.
01:15If we need them again, we can call them again. So that's it.
01:18That's how we find out how all these commands work and what all the options
01:20available to them are.
01:22It's extremely helpful.
01:23That way you don't have to refer to back to my videos every time you have a
01:26question about something. You just ask Unix, "Hey Unix!
01:28I need to refresher on what the options are I can pass into this."
01:31You will use it all the time.
01:33man actually has its own manual page too. man man and there you are, you can see
01:37the different options that are available for the manual.
01:40There is also an abbreviated version of that, man -h or man -help, and those will
01:46just give you a quick overview of what the options are that you can pass into to manual.
01:51The most interesting of these options is the k option and that says the k option
01:55is the same as apropos. What is apropos? Well, man apropos. Look it up, search
02:03the whatis database for strings.
02:05It searches a set database files containing short descriptions of system
02:08commands for keywords and displays the result on the standard output.
02:12Let's try it. man -k and then let's put in banner.
02:16That was the command that we used earlier. "Oh look," it says, "oh banner, I found that."
02:20Print large banner on printer.
02:22Now apropos works the same way, apropos. We just don't pass in the -k. apropos
02:29already looks for the keywords, things that match banner.
02:31Now just to show you the difference there, if we take away the first if you
02:35notice that just make the apropos ban, it's going to look for the keyword ban,
02:39and it found three this time. Not just banner, but it had a couple of others. bandwidth.
02:44That's where it found the word ban in this one and then down here abandon,
02:48it found ban there, so it pulled up three different possibilities and said "Maybe
02:51you mean one of these."
02:52So it could help us find the right command and find out what it does.
02:56Now when we looked at the manual pages for apropos, it said that it was looking
02:59in the whatis database.
03:01We also have the ability to say whatis banner, and it will tell us that exact
03:05same description. The one thing about whatis is it doesn't do keyword searching. Whatis ban
03:11comes up and says there is nothing there.
03:13So apropos or man with the -k for the keyword, look for the this keyword.
03:20Those are the ways to find out things if you aren't sure what the name of something is,
03:23you can look and say, "Hmm!
03:24Is there anything related to video, is there anything related to print," and you
03:29can find everything that's related to print.
03:30Now that might be a long list with thousands of entries, but it still might help
03:34you to find what you were looking for.
03:36But the most important tool is going to be man, so that you can find out what things do.
03:42man ls will then come up and tell us that ls lists our directory contents and
03:47give us all the options that we can pass in.
03:49You can see there's a very long list of options here, so there's no way you
03:52could remember what all these are.
03:54You might remember a few of the key ones, but you would want to refer to the
03:56manual pages to see the rest.
03:57So it is indispensable tool. Definitely if in doubt, try the manual pages first.
04:03man followed by whatever the command is.
Collapse this transcript
2. Filesystem Basics
The working directory
00:00In this chapter we're going to take a look at the Unix file system and how we
00:04can work with files and directories.
00:06I want to start that off by talking about the concept of the working directory.
00:09This is an important concept.
00:10It's the directory where we are right now.
00:13So when we issue commands, it's important to know which working directory we are in,
00:17because that's where those commands are going to happen.
00:19That's where they are going to take place.
00:21So if we say look for a file, it's going to look for a file in that directory,
00:25unless we tell it something.
00:26So it's important to know where we are.
00:29The best analogy is the graphical user interface of the Finder. Right here I am inside kevin.
00:35That's my user directory and you can see that I have all my different folders here.
00:39If I double-click on Public, I go inside the Public folder.
00:43I've changed my working directory from being in kevin to being in Public.
00:48I can go back and I've changed my working directory now back to kevin.
00:53It's the directory that we are seeing and we are working. It's very similar.
00:57Notice though that that only happens in the Finder when this sidebar is there.
01:01If I click this button over here and make the sidebar disappear, now I get
01:04a different behavior.
01:05I double-click on Public and I get a whole new window.
01:08That's not a good analogy, because that's not the way it works.
01:10We're not getting a new window at all.
01:12So this is the one that works for the analogy where we have moving around and
01:16this window is just shifting and changing into different directories as we go up and down.
01:21Now we can jump to different places.
01:23We can jump to our desktop.
01:25We can jump to our Applications folder.
01:28We don't have to move around in sort of a linear way.
01:30This will take me back to my user directory, but that's the analogy to think of.
01:35This is the working directory in the same way that we have a working directory here.
01:39So what working directory are we in here?
01:40pwd, that's the present working directory and there is my present working directory.
01:47It returns the path to me that describes what we're seeing in the window over here.
01:53If I actually hold down the Ctrl key while I click on kevin, you can see that
01:57it tells me, ah, Macintosh Hard Drive> Users and then Kevin. That's where I'm located.
02:04So in the root of the hard drive user is kevin, in the same way that this tells me
02:08the root of the hard drive.
02:09That's the first slash, /Users/Kevin. That's the path.
02:13If you use the Windows at all, it's the backslash on Windows, but on Mac and on
02:17Unix it's always the forward slash, That's what you use for a path separator in
02:21both Mac and Unix, is the forward slash.
02:23So in the next couple of movies we're going to learn more about seeing the
02:27different directories and how to move around between them, but keep in mind that
02:30you can always use this pwd to find out your present working directory and that
02:35you are always in a single directory when you're in Unix.
02:38So wherever you are at this moment that's the place that you are residing, much
02:42in the same way as if you had a window open like this and all of your commands
02:45were being typed relative to that window.
Collapse this transcript
Listing files and directories
00:00In the last movie we became familiar with the concept of the working directory
00:03and we saw how we could use pwd to show us our present working directory.
00:08Now what I want us to do is see how to see what files and folders are inside
00:12that present working directory, and we do that with the ls command.
00:16We got a peek at it earlier. ls on a line by itself will just list the contents
00:21of our present working directory. Desktop, Documents, Downloads, Library and so on.
00:25If we look over here that exactly matches what we see in the Finder window.
00:29Desktop, Documents, Downloads and so on.
00:32In addition we can pass in some options to ls.
00:35We could do man ls and see a list of all of those, but I am just going to show
00:39you the couple that I think are the most important.
00:41ls -l is going to give us a different kind of listing.
00:47ls -l option, now we get them in a vertical format.
00:52Notice the last column there still has the file names, but going top to bottom
00:57and then over to the left, all those other columns give us information about the file,
01:01the size of it, the permissions on it, the owner of it, the time, all
01:04those things we'll talk about lot later on, we'll get into those, but just ls -l
01:09will show you this different version of it.
01:12If we add another option to it, we can do it with or without the l, but I like
01:15doing it with it. ls -la and hit Return. Now we get that same list, but notice
01:22that we got a few extra files there.
01:24There is ., .., .CFUserTextEncoding, .DS_Store, .Trash and .bash_history.
01:31Now you may or may not have exactly those same ones. Don't worry about it.
01:35The important thing to understand is that the dot represents the current directory.
01:40That's all it is.
01:41It's that sort of symbol or a placeholder that means this directory that I'm in right now.
01:47Dot dot refers to the parent of this directory.
01:50It's the directory that's right above this, and in this case because our pwd
01:55returns users kevin, .. is a reference to Users.
01:59But we are always going to have dot and dot dot to refer to those two directories.
02:03The other four files there are what we call dot files, because they begin with
02:08the dot and dot files are invisible config files.
02:12Notice that they don't show up here.
02:14The Finder is set to hide dot files by default.
02:17We don't need to see them. They are just configuration files.
02:20So for example .Trash is files that are in the trash.
02:25That's where my trash is.
02:26If you ever wondered how does it know when files are in the trash, well, it
02:29moves them from let's say your desktop into this folder and it sits there
02:33waiting in this hidden folder until you throw them away, and then it empties out that folder.
02:38That's where those trash files are stored.
02:39Then we have DS_Store.
02:42That's actually for the Mac desktop, the Finder, to store different options
02:46about how we're viewing this folder.
02:48Whether it is in this sort of layout, the size of the window, the position of
02:51the icons, all that gets stored in this little configuration DS_Store.
02:56Bash_history we'll talk about later, but that's where we store history of those
02:59commands that we have been typing and then user text encoding is another just
03:03configuration file that the Mac uses.
03:06Notice that there is also another difference here.
03:08Some of these have Ds at the front of the line while some of them just
03:12simply have dashes.
03:13The dashes is a file. The d is a directory.
03:16That's what it's letting you know.
03:17That's your tip-off of whether something is a directory or not, is whether that
03:20line begins with the D.
03:21There is a third possibility that sometimes shows up there that we'll talk
03:24about later. Instead of D and dash you could have an L, which will be for a
03:28link or a shortcut.
03:29It's like an alias in Unix.
03:31So that's how we list the files.
03:32That's how we see the files that are there.
03:34We can pass in just ls, if we just want a very simple list.
03:37We can pass in ls -la if we want that longer list and actually one other thing I
03:42like to add as an option is ls -lah, and that returns the size of these
03:48different things in human readable format. 510 bytes, 15 kB, 1.1 k. That just
03:56gives us a nicer size that I think is a little more pleasant to read.
Collapse this transcript
Moving around the filesystem
00:00Over the last couple of movies we saw how we can use pwd to see our present
00:03working directory and ls either with or without options to see the files and
00:08folders that are in the present working directory.
00:10Now I want us to talk about how we can move around and change our working
00:14directory and to do that we use the cd command. So cd followed by the name of the
00:20directory that we want to change into.
00:22So for example library.
00:23Now our present working directory is changed.
00:26We are now inside the library.
00:27It's the same thing as if we go over here and we double-click on library.
00:31Now we have changed into the library.
00:32We can also hit the back arrow and change back.
00:36Here, do the same thing from the command line. We would type cd space and then dot, dot.
00:43Remember that I told you that I told you that dot, dot when we are seeing this
00:45listing is special file name.
00:48That references the parent of the current directory. dot is the current
00:52directory. dot, dot is the parent of this directory.
00:55You can see here that it actually says d in front of that.
00:57It treats it just like a directory and we can change into it.
01:00So it says, "I don't know what the name of the parent is, I don't care, I want
01:04to go backwards one."
01:05It's the same as hitting that back arrow.
01:07So now we are back our back in original directory.
01:10We also saw that these are file separators, the forward slash. We can use those to
01:15be able to move several directories at once.
01:17So cd Library/preferences will move us into the preferences folder. Now our pwd
01:25is into the library preferences, we moved two directories at once.
01:28It's very important that they start with library and not a slash in front of it.
01:33Not like this and I will explain that in a moment.
01:35We can also go backwards the same way cd../..
01:40Go to the parent of this directory and then to the parent of that directory.
01:44So we've now moved back two directories after we went in two directories.
01:47I also want to talk about a shortcut which is for auto completing file names.
01:52So if we say cd space and then just type an L and then hit the Tab key, you can
01:57see that it says, "Oh, I looked at the list of available files and folders in the
02:00current working directory and from that the only one I saw that starts with an
02:05L is Library, so that's probably what you mean" and it went ahead and filled it out for us.
02:09If it can't guess it, let's say we start typing Pref and then we hit Tab,
02:14it says Preference. Well actually no, we are looking for Preferences.
02:18It didn't put the full name.
02:19Hit Tab a second time and it comes up and says "Oh, I am trying to decide between
02:24Preference panes and Preferences, those are the two that I can't distinguish," so
02:27got to give me a little more to go on.
02:29So it finishes as much as it can and then it stops and waits for you to do it.
02:34So library preference on its own, if I hit Return, it says no such file or
02:38directory. Preferences that directory does exist.
02:42So I just want to make sure that you see how auto complete works, because
02:45that's a really helpful thing when you are moving around to be able to start
02:49typing a name and hit Tab and have it finish the rest of the directory or file name for you.
02:53Now I mentioned earlier that you did not want to put this beginning slash
02:57in front of it. That's because whenever something has a beginning slash it's
03:00an absolute path. That root cd/ that represents the very beginning of our hard drive.
03:07That's the root of the hard drive. cd/, now we are through to our hard drive.
03:12If we do cd users and then Kevin, we move forward back into users in Kevin.
03:19That's a relative path, relative from the root where we were, move into users
03:24and then into Kevin, but cd/users/kevin, that is an absolutely path that says
03:31"All right, I am going to give you the full path."
03:33So the one without the slash is starting with the working directory, I am going
03:37to give you the path, the one with the slash says, "I am going to give you the
03:40entire path starting from the root of the hard drive."
03:42You are not going to use anything relative to where you are now. It doesn't matter
03:46where you are. This path is the same if I am inside preferences or if I am
03:50inside library. cd /user/ Kevin is the same directory.
03:56That user home directory we keep moving into, right. We are going to do that a lot.
04:00That's our directory that's where lot of our files and folders are located.
04:03So there is actually a shortcut for it, cd, and then the twiddle or the Tilde key
04:09and that will take us into our user directory.
04:11So if I move into the preferences and then cd back, it takes me back to my user directory.
04:18There is one other thing that's good to know which is cd space and then just
04:23dash takes you back to the last directory where you were.
04:26So it just toggles between the current directory and the most recent directory.
04:32So cd- now I am back in my Preferences folder. cd- again now I am back in my users folder.
04:37So that's helpful if you need to just move between two directories. You want to
04:41switch to a directory, do a few things, and then switch back to where you were.
04:44That can be a really helpful tool.
04:46Now if you do any other cds while you are in there, then suddenly your directory
04:50will change and you won't be able to get back to where you were anymore, but if
04:53you just need to toggle between two, that cd- can be really helpful.
Collapse this transcript
Filesystem organization
00:00Now that we have gone over the basics of working with the file system, we know
00:03how to view the files and directories and our present working directory and how
00:06to move around the file system,
00:08I want to stop and take a look at the big picture for what the file system looks
00:11like and how it's organized, because there is a very standard way that Unix goes
00:15about organizing it and Mac OS X then has its own sort of version of that.
00:20So in a typical Unix organization, you have the root of the hard drive, we saw
00:23that with just simply the slash, and inside the root there is typically several
00:27folders. bin which is where the binaries or programs are stored.
00:31These are Unix programs, not programs like Photoshop and Microsoft Word.
00:37Those are Mac applications. These are Unix programs.
00:40Sbin is for system binary, system program that it uses. dev is where there are
00:45references and files for different devices like hard drives keyboard, mouse,
00:49etcetera. /etc which is also pronounced as etcetera or "easy" by some people.
00:56That's where system configurations go.
00:58Then there is home, which is where user home directories go. On most Unix systems
01:02once you log in, you will be placed into a folder inside the home. That's where
01:07your files and folders would live. Not on Mac but on most Unix systems.
01:11Lib is a place for storing libraries of code that need to be referred to various
01:15programs. tmp is temporary, that's for temporary files, things that you don't
01:21mind if someone comes in and wipes the file away. You won't miss it.
01:25That's where you would put in tmp.
01:27And then var is for various, mostly files that the system uses.
01:31Then there is usr, which is short for user and that's where the user would put
01:35programs tools and libraries, not their files. Their files would be in their
01:38home directory, but the programs that they would have installed just for them,
01:42they put those in the usr directory, not in the system bin.
01:46There would be things that are for the users in the usr bin.
01:48Most players of Unix are going to adhere to this basic structure.
01:52They may make small changes here and there and maybe add a folder, take away a
01:55folder, but it's basically the same file structure and for putting things in the right place.
02:01On the Mac though there are some additional files that you should know about.
02:05Most of them are probably familiar to you.
02:06There are Applications, Library, Network, System, Users, and Volumes and it's kind
02:10of self-explanatory what all of those are.
02:12Just notice that the Applications and Library are for Mac programs, not Unix
02:16programs, Mac programs and Library is for Mac libraries of code.
02:20We have a separate place for putting the libraries that Unix needs.
02:24These are just libraries that are used by Mac applications.
02:27System is where Mac OS X itself lives, and then Users is what it uses for
02:32its home directory.
02:33It does not use /home to put the user directories.
02:36It puts them all in /Users.
02:38Notice these are all capitalized. These are also all visible to you.
02:42If you go to the root of the hard drive in the Finder you will see them.
02:44The other ones are not.
02:46It's been told to hide them from you.
02:47Even though they are not .files names, it's still has said, these are some
02:51basic configuration files that Unix uses and the Mac user doesn't normally need to see those.
02:56If they need to see it from the command line, they will still see it, but from
02:59the Finder we are going to hide them.
03:00There are also these dot files we talked about before. .DS_Store holds folder
03:05view options and icon positions that exists in a lot of your directories.
03:10You will see that in lot of places.
03:11Sometimes you will see .MacOSX. That's just the directory for Mac OS X to store
03:15some options. .Trash we talked about is the user trash and then two that you
03:21might see pop up here and there which are .hotfiles.btree and .Spotlight-V100.
03:26Leave those alone if you see them. These are used by Spotlight or Mac OS X to
03:32quickly organize things.
03:33It's to better index and find files, things that it's going to need often.
03:37So that gives you sort of the big picture. You also have both of these in
03:40your Mac OS X. You have the Unix one and then you also have the Mac one
03:43sitting side by side.
03:44So if you want to surf around, you can go in the command line, you can use cd to
03:48move around, poke about, see what's in all these different directories.
03:51You will see that the Mac one does make some customizations to the original one.
03:56There are few things that are different but for the most part this is sort of
03:59what you can expect.
04:00You can expect that the Unix one is going to exist on most Unix systems and Mac
04:04is going to add these other files in there.
04:06So hopefully that will just demystify it for you a little bit and get you a
04:09little bit familiar with where different code is going to live.
Collapse this transcript
3. Working with Files and Directories
Naming files
00:00In this movie, we are going to be working with files and directories in Unix.
00:04Before we see how to create files in Unix, I want to just first talk about the
00:07way we name files, to make sure that we follow the standard Unix rules and
00:11conventions, and that's because they are a little bit different than what you are
00:13used to in working with Mac OS X's Finder.
00:16The first rule is that there is a maximum for 255 characters.
00:19That's the same as you are used and 255 should be more than enough.
00:23The second rule is that you want to avoid most symbols.
00:26I have listed off the main ones there, but you really want to stay away from
00:30almost all symbols and the reason why is that in Unix a lot of these from the
00:34command line have special meaning.
00:36In Unix, as it's trying to parse the line and figure out what you mean, may
00:39confuse the symbol on a file name for being a command or an operator that it normally uses.
00:44Now in some of these cases, Unix will allow you to use the symbol but then
00:49whenever you want to reference that file name and do something with it, you will
00:52have to escape the symbol. You have to put a backslash in front of it.
00:55So you end up typing a lot of backslashes to escape the characters, which is just
00:59more needless typing.
01:00So we just typically want to stay away from them altogether.
01:02What you do want to use are letters, numbers, and the period, underscore, and hyphen.
01:09Remember period has a special meaning. If it's the first letter of the file name,
01:13it becomes a dot file and it gets treated a little bit differently and it won't
01:17show up in the Mac OS X Finder.
01:19You also don't want to put a hyphen as the first character of a name because if
01:22you remember when we talked about options, options begin with a hyphen.
01:26So ls -la, that hyphen in front of la indicates that l and a are options.
01:32So we don't want to put hyphen as the first name of our file so that it never
01:35gets confused with being options.
01:37Typically, even though we can use uppercase letters, you want to stick to
01:42lower case letters in Unix and the reason why is because Unix typically is case sensitive.
01:48In other words, MyFile with capital M and F is different from myfile with
01:53lowercase m and f. They would be considered two different files in Unix.
01:58This applies to most Unix systems. Mac OS X is a special case because the
02:03default formatting for a hard drive is to use a case insensitive formatting for
02:08your hard drive and that means that then Unix on a Mac becomes case insensitive.
02:14Those myfile and MyFile would refer to the same thing.
02:18That's different from most Unix systems and it's important to know the
02:21difference because it can cause problems if you are dragging files over from
02:25another Unix system on to your Mac, because one of them is case sensitive and
02:29one of them is case insensitive.
02:31If you have two files, myfile and MyFile, on one system they are allowed to
02:35co-exist, but on your Mac they are not allowed to co-exist. They are
02:38considered the same file name.
02:39so keep that in mind, we would typically just stick with lowercase for all file
02:43names in Unix and avoid the issue altogether.
02:45The other thing is different from working the Finder is that underscores
02:49are better than spaces.
02:50It's no big deal to put spaces in your filename inside the Finder.
02:54You can certainly use them in your filename in Unix but remember spaces are how
02:59Unix can tell the difference between commands, options, and then all of the
03:03different arguments.
03:04Spaces are what help break that up and delineate the different parts.
03:08So if we have spaces in our file ame, Unix can't tell that that should be one
03:12continuous filename.
03:13So every time we typed a name you have to put a backslash in front of the space
03:17to escape it, just like we have to escape those symbols.
03:21So instead of having to do that all the time, it's better just to avoid them.
03:24The other thing you can do is put quotes around names that have spaces in them
03:28and then it will know ah, this is one complete unit.
03:30I don't need to break that up.
03:32The last thing is that you want to use file endings whenever possible. They are
03:35not required but they are very helpful, .txt for text files, .html for HTML fille, and so on.
03:42It really helps you when you are looking at your listings to differentiate to
03:45what are files from commands and directories.
03:48Now there are a few other rules.
03:50For example, you can't name your file dot, or dot, dot, because obviously those
03:54have special meaning we have already talked about, either the current
03:57directory or the parent directory.
03:59You wouldn't want to name a file the same name as a Unix command. Now if you put
04:03a file ending at the end of it that won't be a problem, but you just don't want
04:06to call something a file echo, because echo is one of the commands that we're
04:09going to use and you want to make sure that Unix can tell the difference.
04:13Before we go onto creating files, let me just show you what I mean about this
04:15spaces inside the filename, just so you get an idea of that.
04:18Okay, so here I am in my Terminal and I am in my home directory. I will write
04:22cd and I am in my user directory and from here there is my library folder cd, library.
04:29Inside my library, if we do ls, you will see that there is Application Support
04:34that has a space in it.
04:35That's one of the folders that Apple created and Apple put a space in it.
04:38Perfectly legal to have a space in it, nothing wrong with it, but it creates a
04:42problem because now watch when I could do cd into Application, I will hit the Tab,
04:48you will see what the auto complete had to do. The auto complete put this
04:51backslash right here in front of the space to indicate this is all one thing.
04:57cd, don't take this as being two separate things.
05:00Look what happens if I go back here and take those out, cd Applications Support.
05:04Well you can look at it and tell what's going to happen.
05:06It's going to say "cd into Application? That's the folder? And then I don't know
05:11what I am supposed to do with Support," but it's going to try and just go in
05:14Applications and say there is no such file or directory.
05:16So it really does have to have that backslash in front of it.
05:19Application, hit Return, and you can see I went in there.
05:23You also, as I said, can put quotes around it.
05:25"Application Support" and it gets me that the same way.
05:29So those are the two ways that we have to make sure that Unix knows this
05:32one complete thing.
05:33It's one file name or one directory name.
05:36It's not supposed to be two separate things. Disregard that space.
Collapse this transcript
Creating files
00:00Now that we understand about file naming, we are actually ready to start
00:03creating some files and there are three main ways to create files in Unix.
00:08The first is using a text editor and Unix has its own text editor.
00:11We are not going to be using the text editors that you have on your Mac like
00:14Microsoft Word or whatever it might be.
00:16We are going to be using Unix's text editors and we are going to see how to do
00:19that in the next movie.
00:20There is also a technique where we can direct output from a command and put that
00:25in a file and we will see that in a future chapter.
00:28Then there is the simplest of all, the one that we are going to look at right
00:31now, which is just touch.
00:33Let's go back to our command line and see how the touch command works.
00:36Back in my home directory, we can just see what's in there right now, ls -la.
00:41We will see the full list. Before we do touch, let's take a look at what touch actually does.
00:45Let's look in the manual for touch.
00:48Change file access and modification times.
00:51Well that doesn't sound like it has anything to do with creating files.
00:55But here is the thing. touch is a very simple command that Unix users use all
00:58the time and what it does is it just reaches out to a file and if it exists it
01:04touches it and updates its access time.
01:06If it doesn't exist, it creates it for us and it's that second part that is
01:10what I want us to see.
01:11So let's just do touch somefile.txt and now let's do a listing again and there it is.
01:18It's the last one there.
01:19It's a zero file. There is nothing inside of it. You can see that the size of the file here.
01:24All I did was reach out and say, "Oh you want me to touch that file? Well it
01:27doesn't exist. I will create it for you."
01:28Now the other way of using touch would just be to say, let's say,
01:32touch .bash_history and see my history file right there.
01:35You can see the last access time for it was 15:59.
01:37I will touch the bash_history and then let's go up, ls -la, now we can see that
01:43the time has been updated. Now it's 16:12.
01:45Now let me be the more conventional way of using touch, but the thing is that
01:49Unix users use touch to create files all the time.
01:52It's a really, really simple thing to do. You want to make sure that the file
01:55exists, so you touch it.
01:57If it exists already, fine.
01:59It's just going to update its access time and it's there, but if not, it will
02:02create it for us so that we can be assured that that file is there whenever we
02:06do a future operation.
02:07So just keep it in mind as one of the tools.
02:10Now the file it creates is empty and in most cases what's more useful is
02:14actually to be able to put content in the file.
02:16So for that we are going to need to use a text editor.
Collapse this transcript
Unix text editors
00:00In this movie, we are going to take a look at the text editors that
00:02are available for Unix.
00:04If you remember back to our discussion about the history of Unix, Unix has been
00:07around a really long time and it pre- dates all of the graphical user interfaces
00:11in the modern day text editors that we are used to.
00:14So what did they use in those really early days, when Unix had first been invented?
00:17Well, they used a text editor called ed, for edit text, and it was really simple
00:23and is not at all user friendly.
00:24So it's not something that we even want to think about now, but in the early
00:27days that was the very first thing.
00:28Well, then over time people said, "It would be great if we had something that was
00:32more of a visual text editor. We can see the page that we are working with."
00:36And so they came with something called vi.
00:38That's visual editing mode and later that was improved and replaced with vim.
00:43So you can still type vi, but chances are it will take you into vim, because vim
00:46has pretty much replaced it.
00:48It's just vi with some improvements.
00:50vim is very much still in use. There are a lot of Unix users that love it.
00:55The reason why they love it is because your fingers get to stay in the center of your keyboard.
00:59Its' a modal editor, so you switch modes. You hit a special key.
01:04It switches you into a different mode and then now your keystrokes on those main
01:09letter keys do something different, let's say move your cursor around.
01:13Then you switch into editing mode. Now your keystrokes are actually editing
01:17the text. Then you switch back out of editing mode into move the cursor around mode and so on.
01:22So they like it because their fingers really stay in the center of the
01:24keyboard and it's very fast.
01:26It's tough for beginners, because you have get used to switching between these
01:29different modes and sometimes you might hit a key thinking you are going to get
01:33the letter t, but in fact you have actually triggered a different action because
01:37you were in a different mode.
01:38Another very popular editor in the Unix community is Emacs.
01:42It stands for editor macros.
01:44Its power really comes from the fact that it has lots and lots of macros that
01:47allow you to do complex task, so it really allows you to do all sort of
01:51things besides just simply edit the text. You can think of it as being a Swiss army knife.
01:55Again not that great for beginners because you have to learn what all these macros are.
01:59It has all these power there but it requires you to really learn how it works,
02:03the same way that vim does.
02:05A better choice for beginners is pico which later became nano.
02:09pico stands for pine composer. pine was a very simple email program that will
02:14allow you to send emails from the command line.
02:17So you would compose your simple email message using this pine composer and then
02:21send the message up, very user friendly.
02:23pico became an editor on its own separate from the mail program.
02:27Later that was improved and replaced with nano and nano is a little bit of take
02:31off of pico because pico can be prefixed to a size or a number.
02:36nano is 1000 times larger than pico and nano is now what we have and what we will be using.
02:42If you type pico on your Mac, you will just get put into nano.
02:45It has completely replaced it.
02:46It has basic features. It is very easy to use.
02:49So the easiest way to use nano is just to type the nano command. nano boom!
02:54Now I am inside nano.
02:56This is my text editor. I can use the Return key to go down, use the arrow key to
03:01go up, and I can just type. Hello!
03:04Whenever you want to exit the program you will see at the bottom it gives
03:07you hints about things it's going to do. You see that there is a little up arrow there.
03:11That's the equivalent of the Ctrl key.
03:13So Ctrl+X, for example, will allow me to exist. Ctrl+G would get help and show me
03:18lots of help information.
03:19Let's go ahead and exit for now.
03:21Ctrl+X. It comes up and says Save modified buffer?
03:25So do I want to save the changes that I made.
03:27That's what it's asking.
03:28It's the same kind of pop-up window that you get if you try and close a
03:31Microsoft Word file without saving your changes first.
03:34So do we want to save these changes. If I say No, just type it in, you will see I
03:39come right back here.
03:40If I had said Yes, then it would prompt me for a file name.
03:42There is another way that you can use nano. We can use nano on an existing file
03:47or nano on a new file.
03:48If I type nano followed by newfile.txt then it will go ahead and have that name
03:56waiting for me when I am done.
03:57Let's go ahead and try it a real quick. nano newfile and This is a new file and
04:03then we will do Ctrl+X, save changes, this time I am going to hit Y for yes, and
04:08it says File Name to Write to.
04:10Do you want to write it to newfile.txt?
04:12So it has already got the name that I gave it there waiting. I just hit the
04:16Return key and there I am. Now if you do ls -la, you will see that that new file is there.
04:22You can also use it on an existing file. Let's take the somefile.txt that we
04:26created before. W can just do Nano somefile.txt. There we are, there are no
04:33lines in there but you can see that I am inside this file and I will just type
04:36changes, and then Ctr+X, Y, voila!
04:40There I am out of there and now we can see that that file actually has those
04:43changes. nano somefile. There is my changes inside there.
04:48Now as I said before, you just use the arrow keys to move around. You can hit
04:52the Delete key to backspace, to erase thing,s and then type replacement
04:56characters just at the cursor, exactly like you would expect.
04:59The other main features that you are going to use from this are all down here
05:02at the bottom. You can search through a document using the W, where is, and
05:06that will search the document for the next occurrence of whatever it is you are looking for.
05:10Previous page and next page. If you have a long document, you can use the Ctrl
05:14key with either Y or V to go forwards and backwards and then you might also
05:18frequently use K to cut text.
05:20So you can cut text sort of to cut and then uncut. It is the same thing is paste.
05:24We will cut a line of text and then you will paste it somewhere else.
05:28There are lots and lots of other options. You can use the help menu to find out
05:31a lot of things that you can do here.
05:33You can also go to http://www. nanoeditor.org and that will also give you
05:42more information there.
05:43So www.nano-editor.org and it's sort of a user's manual for all the things that
05:48you can do with nano.
05:49But this is really what we are going to be using for. It's just these real
05:52basics,. Just sort of open it up, edit some text and exit back out of it.
05:55Now you may be wondering, well, what about other text editors things that you
05:58are most familiar with, can I use those?
06:00Absolutely. If you have something like TextMate, things that just write plain
06:05text files, those are fine to use.
06:07Something like Microsoft Word though, you would not want to use because
06:10Microsoft Word saves in its file not just text but also its formatting
06:15information that has to be interpreted by Microsoft Word.
06:18So we couldn't open it up in nano or we would see all sorts of characters that
06:22don't mean anything to nano, which do have a special meaning to Microsoft Word.
06:25So plain text editors are fine, but using something like Microsoft Word, you want
06:29to make sure that you save your files as plain text.
06:32Since we are really trying to focus on Unix, let's try and stay inside the
06:35command line and use the tools that Unix gives us.
Collapse this transcript
Reading files
00:00Now we will learn how to read the contents of a file.
00:03Now you may be thinking, why not to use Nano to open and read the contents of a file?
00:07Well, you certainly can. In fact I do it all the time.
00:10But what we are interested in here are tools which are designed just for reading files.
00:14This difference will become important later because we'll learn that to read a
00:17file really just means to output the text to your screen and we'll learn to
00:21direct that output that places besides our screen and even to other commands.
00:25A tool like nano which depends on user interaction would do too much for us
00:29and it would get in the way.
00:30Now, we have a couple of options for reading files. The first is cat.
00:35cat is the simplest and the one that you would use most often.
00:38It has kind of a funny name, because cat is short for concatenate.
00:42You see, cat doesn't just read a file.
00:44It will concatenate or join several files together.
00:48You can think of it as read and output the first file, then immediately read and
00:51output the second file, and then the next file and so on.
00:54That's its classic and intended use.
00:57But if we give cat only one file as an argument, well then it just reads
01:00that one file and quits.
01:02There's no second file to ever go on to.
01:04So it has a bit of a quirky name, because its name implies using more than one file,
01:08while we probably are going to use cat more often on just single files.
01:13But we can't use it for both cases.
01:15But there's one big problem with using cat when we want to review a long
01:19file on the screen.
01:20With cat all the output is red and displayed at once.
01:24That leaves you scrolling up through the output to find the start of your very
01:27long file and then scrolling down again as you read.
01:31So, for this case when we have a long file,
01:33we have another command that's better called more.
01:36more gives you paginated output.
01:38It outputs one page of text and then waits for you to hit the spacebar to move
01:42to the next page. Much easier but more also had a problem.
01:46more doesn't allow you to go backwards to the pages.
01:49You could move through the pages forward, but if you want it to go back, well,
01:52then you had to exit out of it and start again from the beginning.
01:55That wasn't very convenient, especially if you just wanted to go back one page
01:58for just a quick second before you moved on again.
02:01So as an improvement on more, we have less.
02:04Now, obviously the name less is a play off of more and less offers the ability
02:09to scroll backwards.
02:10That also has a little bit memory use. Where more would load the entire document
02:14memory, less will just load one page into memory at a time.
02:17It's a bit of a Unix joke that since less was an improvement on more, that you
02:21can say less is greater than more.
02:23Now, on the Mac you can actually say that less is more, because more has been
02:27completely replaced by less.
02:29You can still type more, but the command that actually will execute is less.
02:34The man pages use less.
02:35So we have already seen them in action and we have gotten a bit familiar with it.
02:38We can use F to go forward, B to go backward, and Q to quit and return to
02:43the command prompt.
02:44Let's try them out.
02:45So in Terminal, I am in my user directory, and I'll just show you that I have
02:49got a couple of new files in there.
02:50The first one is called lorem_ipsum.txt and with that is a very long file filled
02:55with vague Latin text.
02:57Then I also have this file short_file.txt, which is also including the exercise
03:02files but it's a very simple file. You couldn't come up with any short file.
03:04I just wanted to have something to contrast against the very long lorem_ipsum.
03:09So let's start trying these out.
03:10So I've got cat and then I can say cat short_file.txt. There you see it.
03:16It just output the contents of the file.
03:19Now, as I said, what cat is really for is for concatenating together.
03:22So if we had cat short_file.txt and then space followed by newfile.txt.
03:28That's just a simple one line file we created in the last movie. There they are.
03:32It puts those together.
03:34So we've got now the short_file followed by the newfile, one immediately after the other.
03:38So that's what concatenate is really designed for.
03:41Even though most of the time, you'll probably use it in the first usage just
03:44with a single file name. So let's try.
03:46I am going to clear the screen using Command+K. Just cat that lorem_ipsum.
03:53Here we go. Ready? Hit Return. Boom!
03:54It output all of this text and now look at this scrolled back.
03:57Right, I would have to scroll all the way up here and find what I was looking for,
04:01not really that useful.
04:03So instead what you would want to use is more or less.
04:07So on the Mac less is more, so we don't need to bother playing with more at all.
04:10We'll just go straight to less and let's try out with lorem_ipsum again and I am
04:15going to clear the screen just so we have that old stuff out of there again. And there we are.
04:19Now look at the bottom. It's waiting.
04:22It shows me just one page.
04:23I am going to hit Space. Now I get the next page. Hit space again. I get the next page.
04:28And as I said, F and B allow us to go forward and backwards.
04:31So we can toggle between those and then when we are finally done we can hit
04:35Q to get out of it.
04:36There's one other really useful command in there I want to show you, which is
04:40that you can G to go to the beginning or the end.
04:44G by itself goes to start of the document. Uppercase G goes to the end of the
04:49document, so Shift+G. So let's try it. Let's do Shift+G and we jump to the end.
04:54You could see it says END down there, and I do just a regular g, lowercase g.
04:59It pops you back up to the top of the document.
05:01So that's a good and useful to know.
05:03There are also a couple of nice options that you can add in.
05:06You can do less with a capital M for lorem_ ipsum and it gives you a little bit better prompt.
05:11So now you can see at the bottom it actually tells me which lines I'm viewing
05:14and how far I am through the document. I get a percentage. There is another one.
05:18Let's go back here and do n. That shows line numbers.
05:22So it numbers all those lines for me and there's lots more options that you
05:27can use with less. The man pages for it are very long.
05:29So you can definitely go in there and see all the different things that you can
05:32do with it, but that's will get you started with it.
Collapse this transcript
Reading portions of files
00:00In this movie, we'll look at two other useful commands for reading files.
00:04They are head and tail.
00:06The difference is with both of these commands, they will read just a portion of the file.
00:10head displays the lines from the beginning of the file. This is useful for just
00:14peeking at the start of the file, let's say viewing the first ten lines.
00:18tail does the same thing but in reverse.
00:20It displays lines from the end of a file.
00:22This comes in very handy, when you want to look at files like access logs or
00:25error logs where the latest entries in the file will be added to the end.
00:30So for example, you can view the tail of a web server's access log to see the
00:33most recent requests made to the web server.
00:36tail also has a very useful option, the f option which allows us to follow the file.
00:42It reads the tail of the file normally, but then instead of exiting back to the
00:45command prompt, it will watch for changes to the file.
00:48Whenever a new line is added, it will show us that line. We are following the
00:53file waiting for updates.
00:54It is a lot like following a news story for the latest updates.
00:57Let's see how we can use these. head lorem_ipsum. Voila, you see we just see
01:03those first ten lines I guess.
01:04We can change one of the configurations is to be able to change how many lines
01:08we see, but I am seeing the default there.
01:10So you can see we can peek at the beginning of the file, and tail does the same
01:13thing but looking at the bottom of the file.
01:15I'll just clear the screen so we can see the difference.
01:18So there we are seeing the very bottom of file.
01:20Now, as I said, one of the places the tail is really useful is being able to follow a file.
01:25Let's try that real quick.
01:26Lets type tail -f and that will allow us to follow the file and let's
01:32try it with this newfile.txt that we created in the last movie.
01:35Just a nice simple file. There it is, there is the output.
01:39And you can see that it's just sitting there waiting.
01:41We didn't get our command prompt back.
01:43That's because it's following the file. The process is still going on.
01:46To get out of it, in case you feel stuck, it's Control+C. That's one of the key
01:51combinations that's really common on Unix to be able to get out of a program to
01:54exit out, we'll be seeing that a little later on but for now, but for now just
01:58Control+C will get you back out into your prompt.
02:00Let's do it one more time so that we can see what it actually does.
02:03I am going to open up a new window with Command+N and let's just edit that with nano.
02:07So newfile.txt, there it is. I'll go down here. I'll make a new line here.
02:12I'll say This is a new line in this file.
02:17I'll hit Return. Now exit out of it and save those changes to newfile and look
02:23over here, look what happened.
02:24It went ahead and actually echoed this line a second time.
02:27I think that's because of the line return that I put in, but this is a new line of this file.
02:32So it just followed the progress of the file. As new entries get made into there,
02:35we'll see what those are.
02:36So that's a really useful tool.
02:38And then Control+C to get out of there.
02:40If you want to try that out with tailing a file,
02:43one place that you can try that is to tail your system logs and that's stored in
02:47var/log/system.log. That's where that's contained.
02:52I am not going to show you mine, but you can take a look at your system and
02:54see what's in there.
02:55If you have a web server running, you can do that on a Mac at least.
02:59Those are stored in apache2/access_log and you can watch requests come into the web server.
03:05Or I believe its error_log is where you would usually find the errors that come in.
03:11So you could tail both of those files and try that out.
03:13Now just as a quick summary again, you want to use cat for small files,
03:19you want to use less for large files, and you want to use head and tail when you
03:23wanted to take a peek at the beginning or the end of a file.
Collapse this transcript
Creating directories
00:00I want us to see how we can go back creating directories that we could put our files into.
00:04Directories are essentially folders, but in Unix you are always going to
00:07call them directories.
00:09So start getting use to that terminology.
00:10Instead of calling it a folder, call it a directory.
00:13Making a directory is really simple.
00:15We just say mkdir, space, and then the name of the directory we want.
00:20So I am going to create one called testdir. Hit Return and it created it.
00:25It didn't give me any output there. If I do ls-la you'll see that it created.
00:30If we come down here at the very last entry, you can see there's a directory
00:33because it has this d at the front and that differentiates it from just a
00:36standard file which has a dash.
00:38Now if you were to try it again and create the same directory again, you'll get
00:42an error letting you know that file exists.
00:44It says File exists but it's actually letting us know that the directory exists
00:48and then makes sure we don't accidentally overwrite something we don't mean to.
00:52Now, if we want to create a directory inside a directory, we can just pass in a path,
00:55 mkdir testdir/test1.
00:56That will create the test1 directory inside testdir.
01:05It's just a path, nice and simple. So that will work.
01:08What if I want to go two deep though?
01:11If we do test1/test2, now I am saying in addition to going inside the test
01:16directory I want you to create two directories.
01:18The test1 directory that doesn't exist followed by the test2 to directory that
01:21doesn't exist. This won't work.
01:23If we try this, it will come back and say, "Oops! Sorry, test1 doesn't exist."
01:28In order to make it work, we need to pass in the -p option.
01:32p is parents, the parent directories.
01:34So create all parent directories as needed until you get to the final
01:39directory that I've got.
01:40That's very helpful if we want to create a large directory structure. We can say
01:43all right, not just this folder but also these folder is deeper down, create all
01:47of them all at once.
01:48That's nice handy trick.
01:50And if we did ls-la,and we'll type in the directory, testdir/, it will give us
01:57the contents of that directory.
01:58The other thing I think is kind of nice is the -v option.
02:02So we say mkdir -v is for verbose, let's use that p option again, testdir/test1,
02:10and let's create another one called test3 that will sit right next to test2.
02:14So there you see it gives me some feedback and lets me know which directories it created.
02:18If it created four directories, we would have four entries there, telling us
02:21each one that it created.
02:23So that's really all to creating directories.
02:25Just make sure that you create a directory with the name that doesn't already
02:28exist and if you can pass in a full path to the directory that you want, but if
02:33some of those directories in that path don't already exist, you will want to
02:36also pass in the -p options so it will create them as well.
Collapse this transcript
Moving and renaming files and directories
00:00In this movie we'll talk about moving and renaming files and directories.
00:04I'll start out by just doing ls -la to see what's in our user directory.
00:08A couple files that I have added here that are in the exercise files.
00:11overwrite_tests1 and overwrite_test2, those are just real simple files.
00:15Just so you can see what's in them, just overwrite_test1. I just put a bit of text in there.
00:20We'll be using that a little later on to talk about overwriting.
00:23Everything else are things we have been working on before and should be
00:26available in exercise files.
00:28So we want to move files around. All we need is the mv command.
00:33mv, the file that we want to move, let's say we want to move newfile.txt, and then
00:39a space and then the path where we want to move it to. This can either be a
00:43relative or an absolute path.
00:45In this case, I'll just say a relative path. testdir/newfile.txt. There it is.
00:54It moved it into that directory.
00:56If we do ls -la testdir, you can see now that that's where the file exists and
01:02it's no longer in this directory. So it did move it.
01:05It moved it into that deeper directory.
01:07We can move it back. I am going to actually change directories into the
01:10testdir to do it, just to show you that we can use newfile.txt with
01:16../newfile.txt and that works.
01:20That's the parent directory.
01:22So its saying go back one directory, that this file and send it back one
01:26directory. That's what it did.
01:28We can take a look in this folder and see that it's not there anymore and if we
01:31go backwards one directory,
01:33let's do ls -la, and we can now see that its back here again.
01:37Now the move command is also smart.
01:39If we say move newfile.txt testdir and nothing after it, we could put the slash
01:47or not, it will move the file into that directory with the same name.
01:50It assumes the rest of it for us because testdir is an existing directory.
01:55That's the key part. It says, "Oh! I see this existing directory.
01:59I am guessing that you want to move this file into that directory."
02:02So you can save yourself a little bit of typing.
02:04So move it into there, and same thing. Let's change directories into the test
02:09directory and you'll see that there it is.
02:13Now, we can move it backwards the same way. mv newfile.txt ..
02:20We are saying parent directory and this is saying "Oh!
02:22the parent directory exists, so I am guessing you want to take this file and put
02:25it back there," and it throws it back into that folder.
02:28That's a nice shorthand.
02:29And directories move the exact same way as files.
02:32Let's say for example that from here, I am inside the test directory.
02:35Let me show you we have got inside test directory, we have got test1 and that
02:39has two folders in it.
02:41test2 and test3 that we created.
02:42Lets say we want to move test folder 3 into test folder 2.
02:46We would do that with mv and we would say inside test folder 1 you'll find
02:53test folder 3. Where do we want to move it, the path? Well, that would be inside
02:57test1 inside test2, and again we can either specify it by putting test3 and
03:04being completely clear about it or we can let it assume, and because test2
03:09exists, it'll say," I am guessing you want to put it inside that directory not
03:13replace that directory."
03:15That's key. Because that directory exists already, it's not going to replace it.
03:19It will put it inside of that directory.
03:21Now, we can just check that out by doing ls test1/test2 and we can see that
03:27test3 is inside there.
03:28In addition to moving files, we can also rename files.
03:33If you use the man pages for man rename, you would be like "Oh, seems to be obvious.
03:39That's the way that you go about renaming files."
03:41No, it doesn't work.
03:43It doesn't work on the Mac and it doesn't work on a lot of systems.
03:45It's very unreliable.
03:47Most Unix people use mv for renaming. It's how you go about renaming.
03:51So let's back up a directory here.
03:54I am back in my user directory again and let's try renaming that new file and we
03:59can rename it while we move it. Rename newfile.txt. I could rename it in place.
04:05I could just call it new_file.txt and look at what that would do.
04:10It would move this file and there's not something existing, there's not a
04:13directory called new_file.txt, so it says,
04:16"Oh, you must want to rename it to that."
04:19I can do it at the same time. I can say all right, let's move it into the test
04:22directory and rename it all at the same time.
04:25Take a newfile, move it in test directory and give it this new name. Let's just
04:30take a look at that and you can see there it is.
04:33So it gave you that new name. You can see I added the underscore is the difference.
04:37We can do the same thing with directories.
04:38So lets change the name of that testdir that we have been working with and let's
04:41start calling it unix_files.
04:45So now, I am saying move this test directory and move it to the same place I am
04:49now but with this new file name.
04:51Again, notice that the important difference here is that this directory
04:55doesn't already exist.
04:57So it's not going to move it into a folder.
05:00If the name that I gave it is the second argument was a folder that exists,
05:03then it wouldn't rename it.
05:05It would just move it into it.
05:06So notice that difference.
05:07So there we are. So now, let's rename it. Let's do ls -la one more time.
05:13And you'll see that now it's been renamed to unix_files.
05:15Now, let's take a look at some of the options that we can use with mv, because
05:19they are pretty important.
05:20There are four main options and these are important options because they are
05:23used for a lot of things having to do with files and directories.
05:27The -n option says no overwriting. Don't accidentally overwrite a file.
05:32When I move this file, don't replace it, okay.
05:35-f says force that overwriting to happen.
05:38It says all right, when I move this file, if something is already in it's way,
05:41blow it out of the way and put yourself in it's place. It's destructive.
05:44Interactive gives us a choice.
05:47It just pops up it says, oh, wait a minute.
05:49You want to move this file into that directory with this file name?
05:53But there's actually already something in there. What do you want to do?
05:56And then verbose is very similar with what we were working with directories.
05:59It just kind of gives you some reporting information as it's going about doing
06:03the move so that you know that it succeeded. -n, -f, -i and -v.
06:07Now, here is the super important thing to know. By default move will overwrite files.
06:13The -f option is turned on by default.
06:16So you would want to use -n or -i to make sure that you don't
06:21accidentally overwrite files.
06:23Let's see this in action.
06:25As I said at the beginning, we have these files overwrite_test1
06:28and overwrite_test2.
06:29That's so that we can play around with these and have some files that we
06:31can safely overwrite.
06:33So if I do mv -n, that's the no overwrite option, and I tell it to
06:39overwrite_test1 on top of overwrite_test2,
06:43I am telling it essentially rename this file.
06:46The thing is that there's already a file with that name.
06:48The -n option, it doesn't do it.
06:50Do ls -la, you see that they're both still there.
06:54It did not actually do it.
06:56If we do it with the v option, see if we get any more information back from it.
07:02Not overwritten, so verbose comes up and tells us.
07:05Now let's try the same thing, but this time let's do it with interactive.
07:08I am going to use Ctrl+A to shoot to the front of the line and then from
07:13there I can just put in the i. So that will try and rename it, but then when it
07:17pops up it says, oh, wait, do you want to overwrite that? Yes or no?
07:20You may hit n for no, hit Return, and then it will not overwrite it.
07:25So that's the best one I think.
07:27I really like the i option.
07:28And then the f option is the default, so I will just do it without any options.
07:33Let me just remove that so you can see what happens.
07:36This would be the default behavior. We just think we are innocently renaming the
07:39file, but in fact we overwrote it.
07:43So now if we cat overwrite_test2, you see that it actually has the content
07:49that was in test1. Something to be very careful about.
07:52And in the movie about aliases later on, we are going to talk about how to
07:56change that default behavior so that you can set it so that it'll always does
08:01either no overwriting or interactive overwriting.
08:04Now as an exercise on your own, go ahead and make sure that you have this
08:07directory called unix_files.
08:08Create a new directory if you need to and move all these files that we
08:11have been working in,
08:13like lorem-ipsum for example, move those into unix_files.
08:17Let's go ahead and put all of the files that we have worked with in there
08:20and start to have one place for those where we can work with them in that directory.
08:23And take a little time. Try moving around, try creating new directories, try
08:26creating new files, we know now how to do that, and get use to moving around
08:30and see what happens.
Collapse this transcript
Copying files and directories
00:00Now, I would like to talk about how to copy them.
00:01Let's take a look. First of all, we'll do ls -la.
00:05Notice that I'm inside my unix_files directory now, which is inside my user root.
00:10At the end of the last movie we moved everything into this folder.
00:13So you can see I have got all of my unix_files just in this folder.
00:16Now, I'll start by doing cat new_file.txt.
00:21It's just a very simple text file.
00:23Let's copy that. We use cp to copy and we start by saying copy these source.
00:30Our source for this is going to be file.txt.
00:33Again, we can provide a full path to that source.
00:36It doesn't have to be something that's in this current directory.
00:38It can be an absolute path or a relative path. And then the destination, the target.
00:43And again, absolute or relative path.
00:46In this case, we are going to call it newer_file.txt. It did it.
00:51Say ls -la,. We can now see the newer file there and cat newer_file and we see
00:57that it copied the content as well. So it's that easy.
01:00It really is just using the cp command to do a copy.
01:04The options that we can pass in for copy are the same as they were for move.
01:06n is for no overwriting, f is for force overwriting, i is for interactive -- ask me
01:12before deciding whether to overwrite or not and v is for verbose, just to give
01:16you a little more information about what it's trying to do.
01:19And also like move, force overwriting is the default. Let's try that.
01:23So I just created this newer_file.txt. Now let's try copying short_file.txt and
01:30we'll also put it at newer_file.txt.
01:32So I am basically saying copy short_file and its target should be newer_file.txt.
01:38It did it.
01:39It didn't object at all and if we say now cat newer_file and see what's in it.
01:46You can see it has that short_file text in it. The same thing as short_file,
01:49not the same thing as newer_file.
01:51So it replaced it destructively.
01:54So we would want to use n for don't replace or i for interactive to ask me
01:58before we actually do it.
01:59Again, when we get to the aliases chapter, we'll talk about how we can change
02:03that default all the time.
02:04And the directories work pretty much the same way with one important change.
02:08I'll just clear this.
02:10So cp test1, that's the directory that's in the folder where I am now.
02:14Let's just call it test1_copy.
02:19It's a directory so it was not copied.
02:21It does that just to make sure that you know, hey, this has a whole lot of stuff.
02:24If I make a copy of it, I may be here for a while.
02:27That may be copying up lots of stuff, taking a lot of hard drive space.
02:30Make sure that really want copy it directly.
02:32To do that, we do cp -R for recursive copy.
02:38Keep copying down the line recursively for every single folder that's inside
02:43this directory and that will allow us to copy test1 to test1_copy. Take a look, there it is.
02:51So that -R option, is going to come a lot.
02:53R is really the option. Generally lowercase r works as well.
02:58So, you don't have to worry too much about remembering which one it is, but
03:01technically it should be the capital R. That's really all there is to
03:05being able to copy files.
Collapse this transcript
Deleting files and directories
00:00Now that we have seen how to create, move, rename and copy files, we are ready to
00:05see how to go about deleting files and directories.
00:08Removing is very simple. Before I show you how, let me also just make the caveat,
00:13this is not the same thing as in the Finder when you delete something and it
00:16moves into your trash.
00:18If your remember ls -la, our user folder, and you'll see that there's this special
00:24folder called Trash and that is where things go.
00:28That's what's actually down here. When we put files in the trash that's where they exist.
00:33When we empty the trash, it empties them out.
00:35What happens when you delete a file in the Finder is it's actually moving it.
00:39It's not to empty the trash.
00:41That it actually deletes it.
00:42So there is a sort of buffer time where you can recover a file.
00:45Delete in Unix doesn't work that way.
00:47When you remove something, it is gone.
00:49It is gone right away.
00:51It doesn't move int to the trash.
00:52You can move things to your trash to if you want. No problem you can do that.
00:56You can move a file into the trash and it would sit there and wait until you
00:59actually have decided to empty your trash.
01:01But that's not what we are talking about here.
01:03So let's just notice I am inside my unix_files folder in my user directory.
01:08Lets take a look at the files that are in there.
01:09Let's start by deleting this somefile.txt that we created a little while ago.
01:14To delete it, all we use is rm for remove, nice and simple.
01:18rm somefile.txt and now it's gone completely.
01:24So again, use it carefully. The file is completely gone, not recoverable,
01:28we can't get it back.
01:29So use that very wisely.
01:31What about with directories?
01:32Well let's try that. Let's try making a new directory.
01:35We'll call it delete_me. Now we have made a directory and now let's try removing it.
01:40remove delete_me. It doesn't work.
01:43It comes and says ah, this is a directory.
01:45To remove directories, we have two options and there's a difference between them.
01:49The first is we can say rmdir. Just like we had make directory, now we have the
01:55reverse of that, remove directory. delete_me. Voila, deleted it. It's gone now.
02:01Now, the one thing about remove directory is it will only remove
02:05directories that are empty.
02:07Let's try and remove directory on test1 copy that we created earlier.
02:12Directory not empty.
02:14So therefore, we have to go inside that directory and empty it out.
02:18Inside that directory, there may be more things and more things and more things.
02:21It could take a lot of emptying things out.
02:24In that case, what we have is actually the same thing as we had in copy recursive.
02:28Remember we had cp -R and that's how he created the copy of test1 to test1_copy.
02:34Well, we have the same thing remove, with the capital R, test1_copy.
02:40So again, use this very carefully.
02:42It will remove everything that's in test1_copy recursively.
02:48So it doesn't care whether they are empty or not.
02:49It will remove non-empty directories.
02:52There it is. Now it's gone, take a look.
02:55Now test1_copy is gone too.
02:56So to review rm only removes files.
03:00rmdir only remove empty directories.
03:05It's up to us to go inside and empty them out, and there's no way to override that.
03:09There's no option you can pass in. rmdir is always safe that way.
03:14That makes it good.
03:15It's a good choice to use most of the time.
03:17rm -R is really powerful.
03:20That's the one that will remove files and directories recursively and it doesn't
03:25care if the directories are empty by default.
03:27So just be careful with that one.
03:28That's the really powerful one.
03:30That's a great tool to have in your toolbox.
03:32But just be careful, as you are cutting away things.
03:34Make sure you don't accidentally cut away something you don't need to.
Collapse this transcript
Finder aliases in Unix
00:00Over the course of the next several movies I want to take a look at how to
00:03work with links in Unix.
00:05Conceptually, links are similar to file aliases that you create in the Mac OS X Finder.
00:10You may already be familiar with those, but they're not the same thing and it's
00:13important for us to understand the differences between them.
00:16We'll start by taking a look at the way that the Mac OS X Finder aliases work.
00:20And to begin with, let's just create a simple file.
00:23So notice that I am already inside my unix_files directory and I'll create a new file.
00:27I'll call it linkedfile.txt and in it, I'll just put some sample text, Link test.
00:34Ctrl+X to exit, yes to save the changes, and return to accept the name that
00:39it wanted to give me.
00:40Now if we do ls -la, we can see that file we just created right here.
00:44Notice that the size of the file is 10.
00:47That's because there are just ten characters in it.
00:49That's what it's storing and keeping track of, and it's just a normal regular file here.
00:53That's what this dash at the beginning indicates.
00:55Pay attention to those as we look at all these example, because they will change
00:58between the different options that we are going to look at.
01:00Now let's create an aliases to it in the Finder. This is not in Unix, this is
01:04in the Mac OS X Finder.
01:05We find the file inside unix_file, here is linkedfile.txt, and then we can
01:09create an alias in a couple of ways.
01:11From the File menu, we can select Make Alias or we can use the Command+L
01:15shortcut that you see there, or we can actually Option+Command+Drag the file and
01:19it'll create an alias that way.
01:20I am just going to choose the File menu item and I am going to rename this file
01:24as well to be alias_to_linkedfile, okay.
01:30Notice that it has an arrow on the icon to indicate that's it's an alias.
01:34That's an indication in the Finder that if we were to double-click on this file
01:37it would act as if it were opening the original file.
01:41It would find that original and open that file up.
01:44Now, there's a couple of important points about the way that the Finder aliases work.
01:48First if the file or the alias moves it still points to the file.
01:52The alias still points to the file, no matter where we relocate the two of
01:55them in the file system.
01:57The Finder will help make sure that the two can always find each other.
02:00And if the file is deleted, then the alias will break, because the alias will
02:05still be sitting there but when we double-click on it, it will try go and
02:08find the original file.
02:09But the original file is now gone so it can't open it up.
02:12So the alias essentially still sits there as a dead end.
02:14We can also make aliases of folders as well.
02:18So if we click on test1 and we pick Make Alias, it will make and alias for
02:22that as well, and I'll just rename that alias_ to_test1, and I will pop back up to the top.
02:28Now, lets take a look at these in Unix.
02:31So ls -la, I'll just clear my screen first.
02:34So take a look at each of these. Notice that both of them have much larger file sizes.
02:40This one right here is significantly larger.
02:42They both also are just regular files. Notice the dash at the beginning.
02:45So that just lets us know that they're just regular files that point to these other ones.
02:50So what's in these files that makes them be so large?
02:53Well, that's the information that the Finder is using to be able to keep
02:57track of these things.
02:58So that as the two move around in the file system, it has all the information it
03:01needs to be able to locate that file.
03:03That's what's inside there.
03:05So on the Finder side, if we were to double-click on alias_to_test1,
03:10it opens test1 for us.
03:11If we click alias_to_linkedfile, you see that it opens up for us.
03:17But on the Unix side, that's not the case.
03:19Lets try opening up the file in our text editor.
03:21nano alias_to_linkedfile.
03:25Well, look what we get back.
03:27All sorts of information that Finder is using, but it did not actually use the file.
03:33It did not alias the original file.
03:36And if we do cd alias_to_test1 and try and change directory, it says oops,
03:43that's not a directory.
03:44We can't change directory into that. That's a file.
03:46So these files are very useful to the Finder and for the Finder to do its thing,
03:52but they are useless to us outside of the Finder.
03:56They are really for the Finder and they do their job very well, for the Finder.
03:59But in Unix, these kind of aliases are useless.
04:03Instead what we are going to need to use in Unix is Unix version of aliases
04:08which are called links.
Collapse this transcript
Hard links
00:00In Unix we are going to want to make use of links.
00:03And the first kind of links that we are going to look at in Unix are hard links.
00:07The way that we make a hard link is just simply to have ln, short for link,
00:11space and then the file that we want to link. That could either just be a file
00:16name or could be a full path to a file, if the file is located somewhere else.
00:20Followed by a space and then followed by the name of the hard link or if we
00:25want to put that link somewhere else, the path where we want to locate it, as
00:28if we were creating a file.
00:30But if we are in a single directory, than we would just put the file that we
00:34want to link and then the hard link that we want to make to that file.
00:37What this will do is make a reference to a file in the file system, the same
00:41way that making a Finder alias would do.
00:44And like the Finder, it will not break if the file is moved.
00:47But there's one important difference about the way that hard links work.
00:51Remember in the Finder, if we threw away the original file, the alias still is there,
00:56but it's broken.
00:57It doesn't point anything anymore.
00:58It was just an alias to that original file.
01:02Well, that's not the case with hard links.
01:04With hard links, they don't break if the file is deleted.
01:07Now that could be a little weird to work with it first, until you get used to it and
01:11understand what they are really doing.
01:13But when you think about it, every time we see a file listed for us, all it is
01:18is a reference to a file in the file system.
01:21So making a hard link is really no different.
01:23It's just making another reference to that same file in this file system.
01:28Both file names that we see on the screen are pointing at the same spot on our
01:32hard drive at that same file storage space.
01:36So what we are doing with the hard link is allowing two different names to
01:40point to that same thing.
01:41That's different than the way the Mac OS X Finder works.
01:44The Mac OS X Finder there's definitely an actual file that points to that
01:48space on the hard drive and then there's this other thing called an alias
01:53which points to that original file, not to the spot on the hard drive but to that other file.
01:58Let's try that out in Unix so you can get the hang of it.
02:01So notice that here I am inside my Unix files,and in the last movie, ls -la,
02:07you can see that I have my linkedfile.txt that I'll be linking to and then I
02:11also had these other aliases here.
02:13Now these aliases are not included in the exercise files because they don't
02:17travel well. They really are designed for making references on my hard drive, so
02:20you won't find them there.
02:22But it's easy enough to create these aliases in the Finder.
02:24What we want to focus on though is this linkedfile.txt and so we want to make a hard link to it.
02:29Well, we use ln and then a space and then the name of the file we want to link.
02:34So linkedfile.txt followed by a space and then the name of the link that
02:40we want to give it.
02:41I am going to call this one hardlink.
02:43So it's always in that format: the link, followed by the source, the thing we are
02:47targeting, and then the name of the link is the last item.
02:51Now let's do ls -la again, and let's take a look at what's there now.
02:55So here's my hardline. Notice that the size of this is much, much more smaller
03:00than the size of the aliases I created.
03:02It is exactly the same size as linkedfile.txt and that makes sense.
03:07These are two files pointing at the same spot on the hard drive.
03:12And if we open it up, let's do cat hardlink, now you see it has the exact
03:18same text inside of it.
03:19Let's take a look over here in the Finder and just see it here. In the Finder it
03:24didn't' give it any kind of icon here because it didn't have .txt or anything
03:27after it, so it just gave it a generic file icon.
03:30But you can see that it does not have the little arrow that the Finder uses to
03:34indicate the special alias files that it creates.
03:37So as far as the Finder is concerned, this is just another file.
03:40Let's create another directory that we can move this into.
03:43I'll make a directory. I'll just call it linkdir and then let's move the
03:49original file, linkedfile.txt, let's move that file into linkdir.
03:55So now we have ls -la, we can see we have our hardline, and then we have
04:00linkdir and if we look inside linkdir, you can see that we have our linked file.
04:06Now, let's try opening up that hardlink again.
04:08So same thing, hardlink, yup!
04:10It's still got that same data on it.
04:12It was able to still keep track of it, even though we moved it.
04:15That's to be expected.
04:17Here's the weird part. If we delete on one of these, and it doesn't matter which
04:21one we delete, if we delete hardlink or we delete linkedfile.txt, the other one
04:26will still contain the data.
04:28It will not remove it from our hard drive. Let's try that.
04:30What I am going to do is I am going to remove the original.
04:34I'll do rm inside linkdir and we'll get rid of linkedfile.txt.
04:41So now, the file is removed. Let's just take a look there and see, yup,
04:44it's actually gone.
04:45Now let's take a look at our hardlink again.
04:48See, it still contains that information, even though we threw away the original.
04:53The hardlink is exactly equivalent to that file.
04:57So there are times when having a hardlink can be useful.
05:00But it can be a little bit weird, because you can think that you are throwing
05:02away a file, but actually that file continues to exist.
05:05And in a shared Unix environment, that can be useful because several users could
05:09all have a hardlink to a file and if some makes a change to the file,
05:13the change happens for all users. They share this file.
05:17But if one of them throws it away, well, it doesn't throw it away for everyone else.
05:20They still have access to it.
05:22So in that context it does make sense.
05:24But because of how we are use to working with Apple aliases or Window
05:27shortcuts, it feels a bit odd.
Collapse this transcript
Symbolic links
00:00In this movie we are going to look at what's called symbolic links.
00:03These are also called sym links for short.
00:06So you'll often hear me just say sym link.
00:08We create them the same way as we do the hard links. We just use ln, short for
00:12link, but we use the -s option to indicate that it's a symbolic link.
00:17So ln -s and then the file that we want to link or a path to the file that we
00:22want to link, followed by the name of the link or a path with the name at the end,
00:27just like we did for hard links.
00:29We have our target followed by the name of the link.
00:32Where symbolic links are very different from hard links is that symbolic links
00:36reference the path to the file.
00:38That's what they keep track of, is the path to get to that file.
00:41Not the file itself.
00:43So hard links keep track of the file on the hard drive and point to that file;
00:47instead symbolic links keep track of the fact, which directory is this file embedded in.
00:54As a consequence that means that they break if the file is moved.
00:58The file is no longer at that path, well, then when we try and do something with
01:02this links its going to go to that path and it's going to say, "Oh, wait I was
01:05expecting a file to be there," and it's not and you'll get an error.
01:09And of course, if the file is deleted then, the file will definitely not be at
01:13that path any more and so will also break which is different from the way hard links works.
01:17Let's try working with symbolic links.
01:19Notice that I am inside Unix files and in here we have some files already created.
01:25You can see that I have hard link that I created in the last movie.
01:28We also have linkdir, which is just an empty directory that we'll be using again
01:32in this movie, and then I have a couple of aliases that I created in the Finder.
01:36Those aren't going to be included in the exercise file, just because they don't travel well.
01:40They really do reference things on my file system.
01:42So you won't have those in exercise files, but they are easy for you to create.
01:46At the end of the hard link movie, we deleted our original file to see how that
01:50worked and see how it kept the data around.
01:52So what I want to do now is I want to recreate that file.
01:55So the first thing we want to do is nano, and let's create linkedfile.txt and
02:01this time just give it any kind of different text from what we had before.
02:04This is Another link test.
02:07So different than the text we had before and we'll safe that.
02:11And the reason that I want to show you that it's different is just so that you
02:13see that this hard link still has a size of 10, while our new one has the size of
02:1718 and if we take a look at that hard link it's still pointing at the old file.
02:22So I don't want you to think this is in any way related to this new file that we just created.
02:26So now let's see how we go back creating sym links.
02:28The way we do that is with ln and then the -s option, the path to the target
02:35file that we want to make a link to,
02:36so in our case that's going to be linkedfile.txt, and then the name of the link
02:42that we want to create.
02:42I am going to call it symlink.
02:44Let's take a look at that and you can see the file and notice that it's very different.
02:49Notice right of the bat it says symlink and then we have this arrow and then
02:54the path to the file.
02:55We can't really see the path there because it's in the same directory.
02:58But if it were listed somewhere else on the file system we would have the full
03:03path that it would take to get to that from here.
03:06Notice also that at the beginning it's not a regular file. It's an l file.
03:11We have seen the d and we have seen the dash. This one has an l to let us
03:14know that it's a link.
03:16The hard link doesn't do that. The hard link is just like a regular file.
03:20If we look at it in the Finder, you'll see that in the Finder it does give it
03:24this little icon to make it look like its alias just like Finder, but it's not.
03:29Looh at the difference here. Look at the size of this alias we created earlier
03:33and look at this size.
03:34Here is the difference.
03:36You see this path right here. Count up those characters.
03:39There are fourteen characters in that path.
03:42That's the fourteen characters that are being stored inside this file.
03:45So, this special type of link file and what's inside of it?
03:49Just the path to find this other thing.
03:51So when we try and do something to the symlink, like cat symlink, what Unix does
03:57is it goes to the symlink and it says, "Oh, wait, this isn't a normal file.
04:00this is a link file.
04:02I need to read the path that's in here, go to that path, and try and find the
04:06file that's indicated and do this action to that file."
04:10The way that an alias normally works, it's more like the way the Finder does it.
04:13When we do something in the Finder, it's not pointing to the same file on the file system.
04:18It's pointing to the file itself.
04:20It's an alias to that file and if the file goes way the link breaks.
04:25The difference is that our symlink, we can't move the file around or it breaks,
04:30because it just keeps track of this path.
04:32It's the simplest possible implementation of this.
04:35Whereas the Finder version keeps a lot more data around, so that it can
04:40keep track of this file.
04:42That's why its so much bigger is because it has this added ability that it
04:45doesn't break as you move things around.
04:47You drop something into a folder? Then no problem, your alias keeps working.
04:51But if you move something into a folder here with your symlink, let's move
04:55linkedfile into linker,
04:59now cat our sym link and it says, "Oops no such follow directory". Because it's
05:04still pointing at that same place, but when it gets there the file is not there anymore.
05:09But in the same way, if we move it back, move linkdir linkedfile.txt, move it
05:15into this directory,
05:18now we can cat our symlink again and it works again, because it went to that
05:23path and the file was there again.
05:24It suddenly magically worked.
05:26So, just references the path.
05:27Now of course if we threw this file away, then of course it would break because
05:31the file wouldn't be at that location anymore, just the same as when we moved it.
05:34So there's really three different approaches to this and you can see why Apple
05:37came up with their alias approach.
05:39It sort of tries to marry the best aspect of hard link and symlinks together
05:44into something new.
05:45But there's three different ways of doing it and you need to understand them all to
05:48be able to choose which one you want.
05:50Now the short version, 99% of the time, what you are going to want is symlinks.
05:56That's usually what you mean.
05:57When you are working in Unix what you are usually trying to do is give yourself
06:00a shortcut to say from this folder, boy, I really wish I had a shortcut to just
06:05shoot over to my web access logs.
06:07My access logs aren't going to move. Chances are they are going to stay in the same place.
06:11So I'll make a symlink from here and then I can just see the end of that
06:15directory and boom, I'll be inside my web log.
06:18The other nice thing about symlinks is that they do work in the Finder.
06:21So here in the Finder, if I double- click on this, it opens it up, exactly the
06:25same as these other ones do.
06:27To the Finder they seem the same, the only difference being that symlinks are
06:32brittle and they do break if files move around.
Collapse this transcript
Searching for files and directories
00:00Now we've covered most of the fundamentals of working with files and
00:03directories, we want us to see how we can go about searching files and
00:06directories to find certain files.
00:08The Finder offers a really powerful search tool already.
00:11It's called Spotlight.
00:12You can access it from the upper right-hand corner of the Finder or most Finder
00:16windows also have a field that where you can type in what you are looking for
00:20and it will help you find all the files that match that criteria and it's very
00:23powerful and it's pretty fast.
00:24However, we also need to learn to do it the Unix way.
00:26First of all, you may be on another Unix system at some point.
00:29Maybe you're even logged in remotely to a Unix web server and want to search for things there.
00:33Well, Spotlight won't do you any good on that remote server and also sometimes
00:38it will be useful to be able to just do it from Unix without switching out and
00:41in some cases, it will actually be faster.
00:42You will be able to find exactly the results you want by doing it this way.
00:46So the way we'll execute a Unix search is using the Find command.
00:50Find, space, and the first argument is the path that we want to search in so we
00:54can limit the search results to certain folders, and then the expression and the
00:58expression could be a lot of different things, a lot of complexity can go there,
01:02but that's going to tell it what it should try and find.
01:04So to just give you a very simple example, we could have find inside the user
01:09Documents folder anything that had name "someimage.jpg".
01:14Those quotes are optional.
01:15You don't have to have them there. Notice that name,
01:17even though it has the dash in front of it, is not an option in the typical
01:21sense of being a command option.
01:22It is part of the expression.
01:24name and someimage are the expression that we are using here.
01:27That's why the option comes after the first argument, not in the traditional
01:31place where options come between the command and the arguments.
01:34It is an argument itself.
01:36It is not an option to find.
01:38Find can have some options to it.
01:39You can use the man pages to look those up.
01:42Now that will return anything that's in Documents that has exactly the
01:46name someimage.jpg.
01:48Well, that's great if we know exactly the name of the file we are looking for,
01:51but most times we don't.
01:52Most times we are looking for something that's a little fuzzier than that.
01:55Maybe we are looking for everything that has the word image in it or
01:57everything that ends in .jpg.
01:59When we were to execute those kinds of finds, we need to employ
02:02wildcard characters.
02:03So we'll use wildcard characters to hold the place of where we want to say
02:08well, anything could potentially go here.
02:11So if we use an asterisk that means that zero or more characters can be
02:15represented by that.
02:16It's often called the glob.
02:18If we use the question mark, it represents any one character.
02:22It can hold that place and with square brackets any of the characters in the
02:25brackets could have that position.
02:27If we try these out, I think you'll get the hang of the wildcards pretty quick.
02:30For this movie, I am actually not inside my Unix files.
02:33I am just inside the root of my User folder so you can use cd ~ to get in the same place.
02:39So from here, most of you should have a Sites folder and inside there should be
02:43something called index.html. ls ~/Sites/.
02:48You can see I have something called index.html.
02:50That's a default web page that Apple gives you on your Mac.
02:53If you don't have it, then you can pick any other file.
02:55I just wanted to pick something that I thought everyone would probably have.
02:58So if we find inside our Sites folder something with the name index.html, it
03:05comes back and it says "Ah, here is what I found for you."
03:08If we pick something else, let's say, index2 .html, it says "Nope, didn't find anything."
03:13Okay, so you see the difference in behavior whether it finds something or not.
03:16It lists everything that it finds.
03:18For the wildcards, what we do is we would just say all right, you know what, I
03:22am looking for not just anything that is index, but anything that has index dot
03:28and four characters after it.
03:30I don't care what four they are, has to be four, but those four can be anything.
03:34That's what I mean by a wildcard.
03:35It's sort of like a joker being wild in the card game.
03:39So it still returns the same result.
03:41Let's take away one of those.
03:42You'll see that comes back and it says no.
03:44If we don't know how many characters, you might have two, might have three,
03:47might have four, well then we would use the glob, that asterisk, and that says
03:51yeah, I recognize that.
03:52So let's say for example we wanted everything in that folder that ends in .html.
03:58You might have a circumstance where you have several index files.
04:02We might have index and we might have index1, index2, index3 and we want to get
04:07all of them, well, then we would say index(123) and now it would return anything
04:12to us that had index 1, 2 or 3.
04:15Now obviously we don't have those files, so it didn't return anything but I
04:18wanted to show you how those square brackets work.
04:19It basically says this character position can contain any of the things that's
04:24inside the set, 1, 2, or 3.
04:26So that's how the wildcards work.
04:28Let's try a couple more examples.
04:29Let's do find anything inside our user directory. I'll just clear the screen so
04:34you can see this little better. And where the name ends in .plist.
04:39That's the file extension that Apple gives to a lot of its preferences.
04:44So that will be anything in my User preferences folder that is an application
04:48preference or something like that and a whole lot of these scroll bar and now
04:51you see I have got some for Safari, I have got some for QuickTime, and so on.
04:54So that's sort of a classic use case for how you would do this.
04:57Now so far, examples have just included showing name, but you can also read the
05:02man pages and see that you can put in path, the last modified time, the size of
05:07the file, things that match certain regular expressions.
05:10You can do all of that and you can read the man pages to find out exactly how to
05:13do what you are looking for.
05:15Find can be pretty complex.
05:16Let me show you one other thing though that I think is pretty cool, which is that
05:19we can also provide some modifier options to the expression.
05:23So for example, I had find everything that was a plist, but let's say I don't
05:27want to have the ones that were QuickTime.
05:28There were whole lot of those that were QuickTime. Find everything and not path
05:35and then with quotes or without we can do QuickTime.
05:38Notice I have the asterisk at the beginning and at the end.
05:42There may be stuff before it, there maybe stuff after it. I don't care.
05:46I am looking for anything whose path contains this string, QuickTime.
05:50I don't want to see those.
05:52So you see how you use and, not, and then path. Path is the same as name, but the
05:57operators and, not, and or are all things that you could use. So let's try that.
06:02Now you'll see I get another list slightly different.
06:04Now we can say okay, but what about everything that's not in our Preferences file?
06:08Let's say -and -not -path * Preferences. I get a narrower list, dropped
06:17everything up that had preferences.
06:18So you can see how it starts to become very powerful.
06:20You can start to hone in on exactly what you're looking for.
06:23As I said, find is a really powerful tool.
06:25It goes much deeper than this.
06:27I just want to make sure that I gave you a good introduction to it so that it at
06:29least gets you started with the basics.
Collapse this transcript
4. Ownership and Permissions
Who am I?
00:00In this chapter, we're going to talk about file ownership and privileges.
00:03Let's begin by making sure we can answer the question "who am I?" In chapter 1, we
00:07talked about how Unix is fundamentally designed to be a multiuser environment,
00:11and we've talked about how we log in each time we open a Terminal window.
00:15Now in Unix, a user is someone who has an account on the system and each user
00:20on the system wants to keep their private files private and only share certain files with others.
00:25Otherwise, if everyone has access to everything all the time ,then what's the
00:28point of even having logins and user accounts?
00:31So fundamental aspect of this multiuser environment is that a user has total
00:36access to their own files, limited access to other people's files, and so that
00:41we can have collaboration, we have the ability to grant permission to certain
00:45files to certain people.
00:46Before we can do that though, we need to understand who we are and in Unix,= we
00:50can just simply type whoamI and it will tell you who you are.
00:54Now that may seem like a really sort of obvious answer because you're on your Mac.
00:58It's just you, you know you're logged in as you, but when you're working in
01:01Unix, it isn't always completely clear.
01:04You can actually switch users.
01:06We'll see how to do that little later.
01:07You may be logged into a remote Unix server like a web server. There are Web servers
01:11where I can log in as a specific user or I could login as root, and then it's
01:17important to know who am I right now.
01:19If you can't do something that you think you ought to be able to do. If you
01:22can't open and read a file that you think you ought to have permission to read,
01:26well you can just type whoamI as a sort of sanity check to be like oh right,
01:31when I started this whole process, I logged in as a different user.
01:34I need to exit out and log back in as that other user or switch users so that
01:39then I have permission to do the thing that I want do.
01:42So it is an important concept in Unix.
01:44To keep most of the user's private files, each user gets a home directory.
01:48We saw that earlier.
01:49We used cd and then the twiddle or tilde as a shortcut and that's the shortcut
01:55to get to my home directory for kevin's home directory.
01:59Whoever I'm logged in as that's where it's going to send me.
02:02Incidentally, that value is stored in home.
02:05So $home, that's where my home directory is.
02:08Each user has a different value when they logged in for home.
02:11For OS X those are located inside capital users and then the username.
02:17On a lot of other Unix systems, it will be home/ and then whatever the username is.
02:22So it will be something like that.
02:23So just to show you it's not there on the Mac, but that's where it'd live
02:27on the Unix systems.
02:28So now that we're clear on who we are right now, where we should be putting most
02:31of our private files, we are set up well to talk about the idea of file
02:35ownership and permission and how we go about changing those.
02:38One thing that we touched on earlier that I want to make sure I mention again is
02:40that you can create other users.
02:42So you can go to your Apple menu > System Preferences and then from Accounts, you
02:48can click lock here to make changes and then click the plus sign and then you will be able
02:52to create new users.
02:53If you then want to switch and log out as a user, it'll quit all your
02:57programs and everything.
02:58So anything you have been working on will be gone, but what you'll do is from
03:01the Apple menu, you pick Log Out as that user.
03:05You don't have to shut down, you don't have to restart, you can just log out.
03:08Your Mac is still running. Unix is still running. You've just logged out as one
03:12user and it gives you the opportunity to log back in as a different user.
03:16So you can try it out.
03:17You can create another user and you can try out a lot of the things that
03:19we're going to do here and you'll be able to see how the ownership and
03:22permissions affect things.
03:23Now you may not want to do that a lot because you are going to have to quit out
03:26of everything and then relaunch it all again, but it might be worth doing at
03:30some point during the exercises just to watch several of the movies in this
03:33chapter and then log out as one user, log back in as a different user, and see
03:38what you have the ability to do.
03:40Now some of you out there may be thinking "Well, I am the only user on my Mac and
03:43I'm always going to be the only user on my Mac and I am not going to work on any
03:46remote Unix servers or anything.
03:48So I can skip all this. I don't need to know it."
03:50You can't. You really do need to still understand the idea of ownership and
03:54permissions because it's such a fundamental part of the way Unix works that
03:58you can't work in Unix without understanding how it works and how to
04:01manipulate them.
Collapse this transcript
Unix groups
00:00In addition to having users on a system, there is also this idea of groups in Unix.
00:05A group is a set of users and each user belongs to at least one group, a primary group,
00:11and it can belong to any number of other groups as well.
00:14Groups are useful for associating a group of users with a file.
00:17So, file's permissions can be set to allow group members to access files,
00:21directories or commands, while making sure that people who are not members of
00:24that group can not.
00:25So for example in a large corporation you might have a group called Human
00:28Resources, Legal, or Tech-Support and those users would be granted access to
00:34certain files and then by adding a user to one of those groups, they would
00:37automatically gain access to all of those files and you wouldn't have to manage
00:41access user by user or file by file.
00:44However, setting up managing groups really applies to shared servers and
00:48falls under Unix system administration more than Unix usage, and on the Mac you
00:53really don't need groups that often.
00:55You don't really use it in a multi-user environment that much.
00:58So groups really don't apply that often either.
01:02But even though you probably won't need to use it,
01:04I think it's still a good idea for you at least know about the concept because one
01:07day you may very well find yourself working on a well-managed server that does
01:10use groups effectively and you will want to know who can access which files.
01:14So for now, I just want to let you know that you can type groups and you can see
01:18the groups that you belong to.
01:19So these are the groups that Apple has us belong to and Apple then makes these
01:23users able to do certain things that have certain permissions.
01:26As I said, you'll almost never need to worry about which group you're in.
01:30Your Mac manages that for you.
01:31It puts you in the right groups.
01:33If you ought to be an admin, who has the admin privileges, well, then you're
01:36in the Admin group.
01:37All the other groups that you are part of may depend on what different
01:40software you have installed or what different processes you have running, don't
01:43worry about that, okay.
01:44I just want you to see that you are a part of a group because groups is going
01:47to come up when we talk about file and directory ownership, which we're going to
01:51do in the next movie.
Collapse this transcript
File and directory ownership
00:00Now that we've talked about users and we've talked about groups, we're ready to
00:03talk about file and directory ownership.
00:05Ownership is an essential part of working in a multiuser environment.
00:08It is how Unix can tell which files belong to you, which ones you can access
00:12and which ones you can't. You can see the ownership of files and directories
00:16whenever we do ls -la.
00:19It's the second and third columns that you see there.
00:21So the owner of all of these files and directories except for one is going to be kevin.
00:26The one that you see as root is the parent directory of that.
00:30That's the /Users directory.
00:33That's going to be the parent directory and it's owned by root.
00:36But my User folder is owned by me and all the things that are inside that
00:40folder are owned by me.
00:41The group that owns it, or you can say the group that's assigned to it, is staff.
00:47Now we don't need to worry too much about groups on Mac, but I just want you to
00:50see it so that you have the conceptual understanding.
00:52For contrast, let's take a look at the other user that I've set up on my
00:55computer, ls -la /Users/lynda/.
00:59So we can take a look at her directory and her files and we can see that the
01:03owner of all of these files is lynda.
01:06The group is the same, the group is staff. But hers is lynda where mine is
01:10kevin, so we have different ownerships.
01:12You can now see that.
01:13You can also see that the owners in the group are maintained separately.
01:16So when we talk about permissions, we'll be able to set permissions based on the
01:20owner and on the group.
01:22We can do it separately. And so therefore files that I'm the owner of, I can
01:25have one set of permissions, but files that are in my group, I can potentially share with Lynda.
01:30I could have files that are readable by anyone in my group and then Lynda and I
01:34could both read those files.
01:36Even though we don't own the file, we could share it and have those group settings.
01:40So what I'd like us to do is I am going to change into my Unix files directory
01:45that's in my User home directory.
01:46Let's clear my screen so we can see those files.
01:49I am going to create a new file in here.
01:51I will just do it using touch and I'll call it ownership.txt.
01:55You can really create this anywhere. It's just to have a file that we can work with.
01:58So let's do ls -la again.
02:00I'll clear the screen.
02:01So now we can see that ownership is there and any new file that you create is
02:06going to be owned by you. That makes sense, right?
02:09It's also going to get your default group.
02:10Everyone has a default group and that's what's going to get assigned to it by default.
02:14How do we go about changing that?
02:15Well, we do it with the command called chown or chown, depending how you want to
02:20say it, but it stands for change ownership.
02:22That's where it comes from, change ownership, and so we use chown, space, and
02:27then we put in the user:, the group.
02:31So we could say, for example, kevin:staff
02:33and then the name of the file or directory and that would change the
02:38privileges and in this case, I'm changing it from kevin:staff to the kevin:staff.
02:43It's redundant, but you can see that it does it.
02:46We also can specify those by just putting in chown kevin ownership.txt, like that.
02:51That will change just the owner. Or we can change just the group by doing it with
02:57the colon and the group.
02:58The colon stays with the group and that lets it know all right, I am
03:01not changing the owner.
03:02I am only changing the group for this.
03:04So we also can do the same thing for directories just so you see.
03:07Let's do-- we have test1 as a directory there. Go ahead and just we're not
03:13actually changing them to anything different.
03:15We are just changing the same one and that will change the owner of the directory.
03:19Directories have owners too. But it doesn't change all the contents that are
03:22inside the directory.
03:23They all keep whatever ownership they already had.
03:25If we want to change everything inside of it then we use the R option, -R.
03:32Just like we had for copy and remove, we have this R which stands for recursive to
03:37tell it okay, not just this top directory, but everything in it, recursively
03:41move through all of the contents of this directory and change all of those ownerships.
03:45Okay, so that's how you do is with -R.
03:47Just for an example, let's try chown lynda:staff ownership.txt.
03:55Comes up and it says Operation not permitted.
03:57Well, you may be thinking "Well, geez what uses this chown if I can only change
04:01it to myself, to the person that already belongs to it?"
04:04Well, that's true and there's actually a way around it which I'll show you in just a moment.
04:07The reason why you can't do it is because we don't want to be able to just give
04:11files to other users for security reasons.
04:14We wouldn't want to, for example, be able to launch a process and have some kind
04:18of program running and then be able to stop and change its ownership permissions
04:22to belong to someone else.
04:23That's a security concern.
04:25So we are not able to do that.
04:27The way that you can do it though is if you are an administrator on the
04:30machine. On the Mac that's something that could be managed in those System
04:33Preferences for Accounts.
04:35If we do that, we can use sudo.
04:37We'll be talking about sudo later in this chapter, but I just want to show you
04:40for now that you can do it. That if we say sudo chown lynda:staff ownership.txt,
04:47it overrides that and it says okay, sure.
04:49It wants to know your password to make sure that you are allowed to do this and
04:53I put in my user password that I use to log in as kevin, so it's just
04:56re-authenticating me to be able to do this.
04:59Now ls -la and now you can see that it's owned by lynda. Now let me do it.
05:04So sudo in front of it lets us do chown and so chown is really not that useful
05:09without that sudo, but I wanted just to go ahead and see it now even though we
05:12are not going to talk about sudo for a couple of movies.
05:14Let's just change it back. Let's make it my file again, kevin, there we are and ls -la.
05:23Now you can see that it changed it back.
05:26You may have noticed that it didn't ask me for a password the second time.
05:29It remembered that I had recently entered it.
05:30But we'll talk more about how that works when we cover sudo in depth.
05:33Now before we end and move on to talk about permissions, I just want to give you
05:36a anecdote of a real world example where I ran into this that it was not sort of
05:40web development related, which is the place where I normally end up dealing with
05:44ownership and permissions.
05:45I bought music from iTunes and I wanted to share it with my wife who is
05:49authorized to use my music.
05:50So I took it out of iTunes by dragging it on to my desktop and then over our
05:54local home network I put them on her Mac.
05:56When it got to her Mac though, she couldn't open them up and she tends to me
05:59saying "Hey, this isn't working. I don't know why not."
06:01Well, it turns out that the owner of the file had not changed.
06:04It still was being owned by me.
06:06So even though the files were now on her computer, she didn't have ownership of
06:11those files and wasn't able to open them.
06:13So I just went on her computer, opened up Terminal, changed the ownership
06:17recursively for the folder, and boom!
06:19Suddenly she had the ability.
06:20She now owned the files and she was able then to play the music.
06:24So that's the case we're really knowing what's going on under the hood in Mac OS X
06:27and being able to work with Unix can really help you out in real-world
06:31non-command line scenarios.
06:34Now that we know about the file ownership, we are ready to talk about file and
06:37directory permissions, which is what we'll do in the next movie.
Collapse this transcript
File and directory permissions
00:00Now that we understand file and directory ownership, we are ready to look at
00:03file and directory permissions.
00:05The way that we see permissions is using the ls -la, just like we did for owner,
00:09to show us the full listing.
00:11Notice that I am already inside my user directory inside the unix_files folder.
00:15In this listing, we just got through talking about the owner column,
00:18the column where it says kevin over and over and then the one where it says staff
00:22that's the group column.
00:23What I want to talk about now is that very first block there, all the rws and
00:27dashes that are there.
00:29The very first character that you see we already said was an indicator of
00:33whether or not it's a directory, a file, or a link.
00:35d for directory, dash for file, l for symbolic link.
00:38The next nine characters after that is a notation that indicates the permissions
00:44for each of these files and directories.
00:46So before we can go about changing the permissions, we need to understand what
00:49these symbols are trying to tell us about the current permissions.
00:52So we need to know how to decode them.
00:54We refer to this system as being alpha notation because we are using the
00:58alphabet to describe the different permissions.
01:00Imagine that we have three categories.
01:02We have our user. which is our owner category.
01:04We are going to call it user.
01:06That's the first category.
01:07The second one is group, everyone who belongs to the group, and then the
01:10third category is other.
01:11That's everyone else who might have access to this file.
01:14So user, group, and other are our three categories.
01:17And for each of them we can set three kinds of permissions.
01:20We can set read permissions, whether or not you can read the contents of a
01:23file or a directory.
01:24write, whether or not we can actually make changes to a file or make changes to a directory.
01:30And then execute, which for a file would mean that we could run it like a
01:33program or a script.
01:35For a directory, it means that we can search inside of it.
01:38That's what it means to be able to execute on a directory.
01:40Notice in this table that I've got yes's and no's, indicating which permissions
01:44I'd like to give each of these three categories.
01:47So I want the owner of the file to be able to read, write, and execute a file.
01:51The group, I'd like to just be able to read and write it.
01:54They can't execute it.
01:56For everyone else, they can only read it.
01:58I don't want them to make changes.
01:59So the only people should be able to make changes are the user and the group.
02:03So what we do for each of those, read, write, and execute, is we use the letter r,
02:07w, or x to indicate it and then we essentially add them up, so that what we end
02:12up with for the user is r, w, and x and they can do all three things.
02:16The group is r and w, but not an x and we put a placeholder dash in place of it.
02:23For everyone else, they can only read. So they get r and two dashes.
02:27Notice now we have nine characters.
02:28You take all of those, you smash them together, and that is the nine-character
02:32permission string that we are seeing in our directory listing.
02:35So for example, if we take this file lorem_ipsum.txt, you can see that its
02:40permissions allow me, the owner, to read and write to the file but not
02:44execute it and that's fine.
02:46Frequently we don't have execute turned on for files because it's not a script. It's a text file.
02:50We don't need to run this.
02:51We are just going to be reading and writing to it.
02:54For the group, which would be everyone in the group staff, which you'll
02:57remember included the other user I created, lynda, has the ability to read the
03:01file, but not to write to it or execute it, and then everyone else,
03:04everyone else who might ever come in contact with this file, is able to read it,
03:08but not to write and execute to it.
03:10Now you can see that, for example, that there is different permissions down here
03:13for the test directory.
03:15By default, directories are given the x so that they are searchable.
03:18It's essentially the same thing as being able to read, so we're able to search
03:21inside of it if we can read what's in it.
03:23So, by default, when you create a directory, it will include that as well.
03:26But you can see that the group staff and everyone else can't write to this directory.
03:29They can just read it and search it.
03:31So let's see how this prevents people from having access.
03:34Let's take a look as a contrast.
03:36I have the other user on here, which is lynda, and now we can see the contents of her directory.
03:42Here is the directory. Remember this dot represents the current directory.
03:45So this is the directory we are looking at.
03:47We as part of her group have read and execute permissions on this directory.
03:51That's why we are able to see this listing.
03:53Let's try now to do the same thing, but let's add pictures to begin.
03:57We are going to try and look at her vacation photos.
03:59Notice what the permissions are down here.
04:01She has read, write, and execute privileges, but that's it.
04:04No one else has any privileges.
04:06So if we hit Return, it says "Oops, permission denied."
04:08If we do cd into that folder, you will see it says "Nope sorry, you can't get in
04:15there, permission denied."
04:16So you see how it works.
04:17You see how these keep us from getting into other people's stuff.
04:20So we can see her user directory, but we're not able to go any deeper into her
04:23documents or her movies or her pictures or anything like that.
Collapse this transcript
Setting permissions using alpha notation
00:00Now that we understand how permissions work, how do we go about changing them?
00:04Well, you remember with ownership, we used chown, change ownership.
00:08We are going to use chmod to change permissions.
00:11It stands for change mode and that's what Unix refers to the mode of these files
00:15is being what it calls to permissions.
00:17So chmod and then we are going to put in whatever the mode is.
00:20I am just going to write the word mode as placeholder, we will talk about what
00:23the mode is, followed by the file name and so we will just put file name. That's the format.
00:28chmod, whatever changes to the mode we want, and then the file name.
00:32So let's look at some example of what that mode might look like.
00:35So first, remember that we have three categories.
00:38We have user, group, and other.
00:40Notice the first letters of those.
00:41u, g, and o. We are going to be using those first letters to indicate where we
00:46want to make changes to, which category should get changes.
00:49Okay, so u, g, and o, r, w, and, x.
00:51So the first one I want to look at is going to be changing user, group, and
00:57other and setting them all equal to something.
01:00All right, so it can provide whatever three letters I want there. I am going to
01:03say r, w, and x. This is going to give all of them all three of those.
01:07Now most of the time you don't want to do that. Most of the time you are trying
01:10to set different permissions for different people.
01:12So another way to write that out would be to say u= and give it whatever
01:16permission is for u, followed by a comma then the g= whatever permissions it
01:21should have and then o for other.
01:23User, group, and other.
01:24Commas separating them.
01:26That's a long thing to type and I don't know anyone who really types it out that way.
01:30Typically when you are changing the permissions, you know what the current
01:33permissions something are.
01:35what you want to do is change it to something slightly different.
01:38You are like, oh, you know what, I wish that the user and group both had write
01:41permissions to this file.
01:42Well, to do that, you can just use the plus sign.
01:46Keep the existing permissions exactly like they are. All I am interested in is
01:49for user and group add the w permission.
01:53We can also do the same thing subtractively.
01:55We can say all right, I want to take away the write permission from other.
01:59So plus and minus, it's typically the way people work.
02:01Instead of spelling it all out for each one, typically you just say all right
02:04here are the changes that I want to make.
02:08Now as I said, u, g, and o are how we define the three categories that we are
02:10targeting. There is one other shortcut I want to show you which is, is if you
02:13are trying to do something to all three of them and you are using ugo, you can
02:16also use a, so that's a shortcut. Those last two do the exact same thing.
02:21a means u, g, and o altogether.
02:23Let's try it out, so you can get the hang of it.
02:25Okay, I am going erase this line, because that's not going to do anything for us.
02:28Let's do ls -la so we can see the current permissions and I am going to really
02:33target everything on this ownership.txt.
02:35You can use any file you want. You can just create a new file.
02:39You can see that the current permissions are read and write for the owner and
02:43just read for the group and other categories.
02:45So to begin, let's do chmod and let's do user group and other equals, so we can
02:51see what that looks like and let's say r and x.
02:54And we will do it to ownership.txt.
02:57So that's typically what a chmod statement looks like. chmod, then what we want
03:01to do to it, followed by the file name.
03:02So we are going to add essentially an x to the last two.
03:06ls -la and now you will see that the x has been added to those two.
03:11You see that actually took the w away from the owner as well, because I set it to equals.
03:15So now let's try it slightly different.
03:17let's do chmod u=rwx and let's do group = rw and other.
03:25It's going to be equal to just r for ownership.txt. ls -la again.
03:31Now you can see how we changed it using that.
03:34All right so we broke it up using commas. We can also add to it, for example.
03:38Then we can say chmod user group and other, plus x for ownership, ls -la.
03:45There we go, you see that it added the x to it and let's just do chmod a-x so
03:52you can see what that does. ls -la again.
03:56You can see that it took back away the x.
03:58So now that you have the hang of how we specify these, let's see what the actual
04:02practical affects are of these.
04:03Let's try chmod and let's do user group and other. Let's take away their read privileges.
04:10Okay, so now we don't have the ability to read anymore.
04:12We just clear the screen so you can see it. So there we are.
04:15We don't have read privileges anymore.
04:17Let's try nano ownership.txt. And notice that it pop opened nano but look at the
04:22message that it gives us at the bottom, permission denied.
04:25So I am inside nano, but I can't do anything.
04:27I can type here, but I am not actually inside that file and when I say save,
04:32it's going to save to a new file.
04:34It's not actually reading the contents that file into the buffer for me to change.
04:38I am just essentially opening nano.
04:40So let's try now changing the ownership so that, say chmod, let's say user
04:47group and other ought to be equal to read for ownership.
04:50All right, so now we only have read permissions. We don't have write permissions.
04:55Once again, let's try opening up nano ownership.txt.
04:58So it opened it up.
05:00It read it fine. There is nothing inside this file to begin with, but it did
05:04successfully open it.
05:05Let's say hello and now let's do exit, save buffer, yes. File name to write.
05:11Now I have the opportunity to provide a different file name but I am essentially
05:14now about to write on to this file, and I hit Return.
05:17It says, "Sorry, permission denied."
05:20So we can't write to it.
05:22We can write to a different file name if we want, we still have the ability
05:24to save our changes, but we can't overwrite ownership.txt because we don't
05:29have write privileges.
05:30So I will just say no to saving those changes and let's go ahead and just set
05:33it back to chmod and we will make user gain its write privileges back on ownership.txt.
05:41Let's take one last look at it and there we go.
05:44The other thing I just want to mention to you is that in addition to doing
05:47changes like this, if we have a directory and we want to change this to the
05:50directory, let's say that we want to do chmod, let's add the write attribute for
05:56group, and we will write that for test.1 for the test directory.
06:01If we do this right now, it will change it just for that top level one, same as the ownership did.
06:05If we wanted to change it to everything that's inside of there, we have to also
06:08include the recursive flag.
06:10So that's the option -R and that will recursively change everything
06:15that's inside of there,
06:16directories and files all the way down the line.
06:19So I just want to make sure you know we have that recursive option available to us.
06:22That's important if you are trying to change everything that's inside of that
06:24directory to have the same permissions.
06:26That's really all you need to being able to manage permissions in Unix.
06:29You now understand what they are, you understand how to read the notation
06:33that it gives you there on the directory listing, and you now know how to
06:36manipulate them, how to enable and disable permissions for those three
06:39categories one way or another.
06:40But before we move on, I wanted to take a look at how we can also specify
06:44these permissions using something called octal notation and we will do that in
06:47the next movie.
Collapse this transcript
Setting permissions using octal notation
00:00In the last movie we learned how to work with file permissions and to use chmod
00:04to change the permissions that we wanted for user, group, and other, and to do
00:07that we used what we called alpha notation.
00:09That's because we're using the alphabet, u, g, and o for user, group, and other
00:13and r, w, and x for read, write, and execute.
00:16That's everything you need to know to change file permissions. That's it.
00:19You could get by do everything with that.
00:21However, I want you to also learn about octal notation, because it's extremely popular.
00:26It's the way that I change file permissions and I think a lot of other Unix
00:29users do and you're bound to run into it.
00:31So I want you to at least have some exposure to it.
00:34So remember here we have u, g, and o, r, w, and x and we use that to come up
00:39with those nine digits rwx, rw-, r--, right?
00:44Now imagine that for r, w, and x that we establish a conversion. Each one of
00:48those letters is instead going to be represented by a number, 4, 2, and 1.
00:53r=4, w=2, x=1. So in place of the yes's and no's for each one of those, let's
01:00drop in those numbers.
01:02So for the user, the user can read, write, and execute, so they get 4, 2, and 1.
01:07The group can read and write, so they get 4, 2 and 0, and for other, they can
01:12only read, so they get 4 and 0 and 0.
01:15Take all those numbers and add them up. You get 7, 6 and 4.
01:20And those three digits are the octal notation representation of rwxrw-r--.
01:27So we're not adding permissions or taking away permissions; we're defining
01:31all nine of those just using these three numbers.
01:34We're saying each one of these numbers will represent the state of read, write,
01:39and execute for each one of those.
01:40Let me show you some examples.
01:42So for example, let's say we want to say that a file ought to have all the
01:47permissions. Well, read, write, and execute, all added together, 4, 2 and
01:511 those add up to 7.
01:53That's the highest number we can have.
01:54So 777 means give all the permissions wide open, so everybody can do everything.
02:00When we're just looking at, we had 764.
02:02That means that the user can read, write, and execute. For the group they have a 6.
02:08That's made up of 4 and 2.
02:10That's the only way we can come up with that number is with 4 and 2, therefore
02:14they have read and write privileges, not execute.
02:17And then for other, they have only read privileges. That's the 4.
02:21A very common one that you will see is 755.
02:24That means that I as the user have read, write, and execute. Everyone else just
02:29has read and execute privileges. They don't have write privileges.
02:33And then of course, if they didn't have any of them, it will be 000.
02:36That would essentially take away all privileges from everybody.
02:39More commonly what you would see is something like 700, which would mean I get
02:43all privileges, but everyone else gets none.
02:457 represents read, write, and execute. So that's it.
02:48That is all that is there to it.
02:50It's just a really compact concise way to spell out exactly what you want to
02:54have happen with those permissions.
02:56Like I said, you can still do it the other way, but this is a really powerful
02:59way once you get the hang of it.
03:00What I like you to do now is just pause the movie with this alpha to octal
03:05conversion up, r, w, and x, just to remind you, and try a few of these.
03:10Just try setting the permissions on a test file. Go ahead and just try 777, see what it does.
03:15Try 755, see what it does.
03:18Remember that the numbers will always have to be combinations between 0 and 7.
03:22They can't be any higher than that.
03:23So, you can just play around and see what it does, see if you can get the hang of it.
03:26Maybe even if you start out using alpha notation, just every now and then just
03:30try the octal one and see, and if you're going to do all of them wide open
03:33anyway, just use 777.
03:35It's certainly a lot faster, instead of typing out user, group, and other equals
03:39read, write, and execute, right?
03:40It's a lot more characters to type.
03:42Once you feel like you have to hang of it, we'll move on to talking about how we
03:45can switch to a different user identity.
Collapse this transcript
The root user
00:00So far in this chapter we've been talking about user accounts and user privileges.
00:04The things that a user is or is not allowed to access on the Unix file system.
00:08Now I like us to talk about a special kind of Unix account, which is the root user.
00:13The root user is a super user account that can do absolutely anything on the Unix system.
00:18It can open any file, it can run any program, it can change any permissions.
00:22It is an all-powerful user that's not bound by the normal user permissions that
00:27we've been discussing so far.
00:28So the root user can see Lynda's photos and they can read Kevin's documents.
00:33In fact, they can even delete Kevin and Lynda's accounts.
00:35When Unix is first installed on a computer, it's this root user that exists
00:39before you've even created the user accounts.
00:41So as the root user, you would first create Kevin's account, give him a
00:45password, and then from then on we can log in as Kevin.
00:47Now in Mac OS X, you don't see that process happen, because the Mac OS X
00:51installer does it for you.
00:52As you're installing Mac OS X, it comes up and says, hey, what information do
00:56you want for your first user account?
00:58You fill it out, you provide the username and a password, and then acting as
01:02the root user, it creates that first user for you and then it actually disables the root user.
01:08So the root user is disabled by default in Mac OS X.
01:11Now the root user can be re-enabled, but you don't need to and I really don't advise it.
01:16So why talk about it then?
01:18Well, it's an important Unix concept and it's going to have implications in a
01:21lot of the things that we're going to do.
01:23And you may also read or hear references to it, either in the man pages as you
01:27research information about different commands or on blogs as you go out and
01:32continue learning about Unix after you leave this tutorial.
01:35You may even hear in movies or TV shows where hackers talk about getting root, right?
01:39Or having root access.
01:41That's what they are talking about.
01:41They're talking about becoming this all-powerful root user.
01:44And lastly, it's going to be important when we talk about sudo.
01:47sudo is essentially a command that lets us temporarily take on those
01:51abilities of the root user.
01:54And we'll see that how to do that in the next movie.
Collapse this transcript
sudo and sudoers
00:00In the previous movie I told you that the root user is disabled on Mac OS X by default.
00:05That's okay. We don't need it to be enabled.
00:07Because as admin users on the Mac, we can do everything that the root user can do.
00:11We just have to do it by using the sudo command.
00:15That stands for substitute user and do.
00:18Some people mistakenly think that it stands for superuser do, because the root
00:21user is a superuser.
00:23But what it's actually doing is substituting in a different user identity and
00:27I'll show you how you can pick a different user besides root in a moment.
00:30But sudo is a command that runs other commands.
00:32So it's essentially like prefixing a line with sudo when you wanted to do it as root.
00:38So just as an example, sudo ls -la. The command is exactly like we would
00:43normally type. We're just saying, hey, don't do this as Kevin, do this as root.
00:47Let me show you what happens.
00:48We hit Return. It comes up and it wants a password.
00:51Now that's not root's password, because root is disabled, what it wants is in
00:55this case it wants my password, Kevin's password again.
00:59What it wants to do is make sure that we really aren't admin before we do admin like things.
01:04It's a security precaution.
01:05So I am going to type in my main user password that I use to log into this
01:08system, and now it does what I asked to do.
01:11Obviously this is something that I could have done without being root, but that
01:15should just give you a feel for how we prefix our normal commands with sudo and
01:19get the same results.
01:20We got to a peek at this earlier when we were changing ownership
01:22permissions, right?
01:23We had to do sudo and then chown lynda and then let's say
01:28unix_files/ownership.txt, and then that changes the ownership permissions.
01:33Now notice when I typed it the second time it did not ask me for a password again.
01:37That's because this password authentication stays valid for a little while.
01:41So that we potentially could be entering several commands.
01:43It would be a pain if every single thing that we did, we had to keep
01:46entering that password.
01:47So it remembers it and it stays valid for about five minutes.
01:50That can be configured on different Unix machines.
01:52But it's typically five minutes.
01:54Incidentally, you can type sudo -k, and that will expire it right now.
01:59So that then when we try and do something again, let's change this back to
02:02Kevin, now it's going to ask me for my password again.
02:07Okay, so let me just clear the screen and let me just give you a couple of more
02:10examples to show how this work.
02:11So let's say I have whom, Right?
02:13I am Kevin.
02:15Let's do ls -la and let's try and take a look at Lynda's pictures.
02:19It's on here and I don't have permission to access it, so it comes up and
02:22says permission denied.
02:24If I now do the same command with sudo in front of it, Users/lynda/Pictures, now
02:30it's going to make me into root before doing it, and remember the root has the
02:33ability to do absolutely anything on here, including look at Lynda's pictures.
02:38So using sudo I can now see what's in there.
02:40Now as I said at the beginning of the movie, root is not the only user you can
02:43become. Let me just show you if you do sudo whoami.
02:47It comes up and says, "Oh, I am root."
02:49If we use sudo and we use -u, and then the username that we want a substitute,
02:54now whoami, we become lynda.
02:57So we are substituting the user identity before we execute the command.
03:01So now we could do the same sort of thing, we say all right, well, as Lynda,
03:04I'd like to now open this file or look at this directory.
03:07So we can become a different user and take on their privileges and their role
03:11just as easily as we can take on root.
03:14Most of the time you'll just use sudo without any options, because what you
03:17essentially are saying is I don't have enough privileges to do what I want
03:20right now, like changing ownership.
03:22So therefore, I'm temporarily become root to accomplish what I'm trying to accomplish.
03:27Not everyone can do sudo. Only admins, and that's something that you set up in
03:32the Account Preferences, System Preferences under Accounts, and you can see it
03:37says Admin underneath, right?
03:39If you make someone who's not an admin account, they won't have the ability to sudo.
03:43That is what an admin is.
03:45Now let me show you why that's true in Unix terms.
03:48In Unix there's something called a sudoers file.
03:54So that is stored in cat/etc/sudoers, that's where it lives, and this is a file
04:00that keeps track of the configuration for sudo and especially who ought to be
04:05allowed to execute this command.
04:07Because we don't want let everyone do it. Otherwise again, what's the point
04:10in having privileges.
04:11So only admins on the system get these special privileges.
04:14We can't view that file, because we wouldn't want everyone to be able to view it
04:19unless we put sudo in front of it.
04:22As you can see the security is very well thought-out.
04:24So now let's take a look at it as sudo. There is all sorts of default settings
04:29and everything, but the most important thing are these lines right here.
04:33You don't need to ever change these yourself.
04:35The system preferences are going to handle this for you.
04:38What these lines are telling you is that the root user ought to have the
04:42ability to do everything.
04:44That's what those three ALLs mean.
04:46The group admin also ought to be able to do it.
04:50So what it does is it adds you to the group admin and once you're part of the
04:53group admin, well, then you have the same privileges that root does.
04:57You have the ability to become root.
04:59Now again, you don't ever need to actually edit this sudoers file, but I just
05:03want you to see what's happening, because on other Unix systems this is the way
05:06that you manage who has sudo access, is via the suedoers file.
05:10The Mac manages it for you.
05:11The Mac gives you this nice convenient interface and it just drops you into the
05:15admin group and once you are part of the admin group, well, then you're
05:17automatically added to the sudoers file.
Collapse this transcript
5. Commands and Programs
Command basics
00:00In this chapter we are going to be taking a look at some Unix commands and
00:03programs and seeing lots of useful things that we can do.
00:05I want to start out by just talking about some of the basics of commands and
00:09this is really a follow-up to the discussion we had back in Chapter 1, where we
00:12saw the basic structure of a command.
00:14Remember it was command, then the options, then the arguments.
00:17I want to go a little bit deeper here and take a closer look at commands.
00:20Let's start out with just the simplest command that we've seen so far, echo.
00:23So echo 'Hello World,' just echoes back that string to us, right.
00:28That's a classic command.
00:29Now a command is actually a program.
00:31It's not a big program like Photoshop or Excel, but it is still a program.
00:35We typically just call it a command.
00:37The other thing you need to know is that commands are not somehow magic.
00:40They're just files that you are executing.
00:42That's all they are.
00:43They are files that are executable and when we execute them, they do the
00:47business of the command.
00:48You remember when we talked about where files are located in the Unix file system,
00:52I told you that /bin is a place where we store a lot of these files and
00:56that's where echo is.
00:57So /bin/echo is where the file is located, and all we are doing is executing it,
01:02and to execute a file, you just type the file name. That's it.
01:05That's all there is to executing a file and then we'll pass in the
01:08argument, 'Hello World'.
01:10So when we type echo, it's just a shortcut for saying go to /bin/echo and run
01:17that file with these arguments, and so commands are essentially a shorthand for
01:23going to these files and executing them.
01:24We'll talk a bit more about how it knows where to find these in the next movie.
01:28For now, let's look at whereas. whereis will tell you where that file is
01:32located, or you can use which, which will also tell you where it's located.
01:37We can also use whatis to tell us the simple information about the command.
01:41If we do it for echo, we'll get a lot of page of information as it tries to find
01:44everything that references echo.
01:45I am going to instead show you with banner, just so you can see a nice
01:48simple one line listing.
01:49So the banner tells us, ah, what it does?
01:52It prints a large banner on a printer.
01:53There are few other helpful things to know about working with commands.
01:57First is that it's very common that you can pass in the -v, the --version or
02:03--help options to a command to find out more.
02:05A lot of times it will just have the command itself followed by the option.
02:09Now that's not always true. A lot of commands don't respond to these.
02:12But if you're trying to figure out how to work a program,and man pages don't
02:16have anything, you might try help. If you are trying to figure out what version
02:18of Ruby you have installed, or you might try passing in the --v or --version.
02:23So those are just common idioms that are frequently implemented.
02:25If you want to exit out of a program and you can't figure out how to do it,
02:29Typically Q, X, Ctrl+Q, Ctrl+X or the Escape key will get you out of it.
02:34We saw a Q when we were looking at the man pages.
02:36Remember man pages use less.
02:38That's the command that it's using to display those pages, and Q exits out of that.
02:43When we were working in nano, we saw how Ctrl+X is what gets us out of nano.
02:47I would say probably Q is the most frequently used. Escape is probably the least
02:50frequently used, but you might try all of those.
02:53If can't figure how to get out of a program.
02:54If you're really stuck then you can force quit and you can do that with Ctrl+C.
02:58That's the Unix way to completely cancel the process that we're working on.
03:03So Ctrl+C will just tell Unix, hey, stop what you're doing.
03:06You also can close the window, the Terminal window on a Mac.
03:09But be advised that if you do that, the process may keep running. You haven't
03:13told Unix to quit it.
03:14You've just said, hey, I want to abandon whatever I'm doing.
03:18The process will keep running.
03:19When it finishes, it will just stop at that point.
03:22But it doesn't actually stop the process itself. Ctrl+C is what does that.
03:26So be advised about that difference.
03:28The other thing you should know is that you can put semicolons between commands.
03:32So we can execute one command, semicolon, followed immediately by another command.
03:36That can be really useful if we are trying to do two things really fast.
03:39Let's say we want to move a log file to a different name and then we want to
03:43create an empty log file back in its place.
03:46So in one command we can essentially do both really fast, within a matter of
03:50milliseconds, and as I already mentioned we have whereis, which, and whatis
03:53available to us to find out more information about these commands.
03:56I told you that Unix knows to go look in that bin directory in order to find the echo command.
04:00In the next movie I want to talk about how it goes about doing that.
Collapse this transcript
The PATH variable
00:00In the last movie, we saw how commands and programs are really just files that
00:04are being executed, and we saw that we type echo 'Hello world' that what it's
00:08actually doing is executing a file that is located in bin/echo.
00:11I write the argument.
00:13Those are doing exact same thing.
00:14This is just a shortcut for finding this file and executing it.
00:19So how does Unix do that?
00:20How does Unix know to find echo in this location or to put it another way,
00:24if I create another file named echo, how does Unix know not to use that one
00:28instead, or what if I wanted it to use that one instead?
00:31How would I go about doing that?
00:32Well, the way that Unix manages all of this is with a variable called PATH.
00:38$PATH will show you the current value of your path and the path is always a
00:43colon-separated list of file paths and this is the list that Unix will use when
00:48trying to locate commands to execute.
00:50The early entries take precedence over the later ones.
00:53So what it does is it starts at the left.
00:54When we type echo, it says, "Oh! I'll look in user bin.
00:57Do I find echo there? No.
00:59All right, I'll look in bin. Do I find echo there?"
01:01Well, in this case, it does and so it runs it.
01:04If it didn't though, then it would look in usr/sbin, sbin, usr/local/bin, and
01:08finally user/X11/bin.
01:10If it didn't find it in any of those places, then it would say "Oh, the command is not found."
01:14You can see that behavior.
01:15If you just type some junk on the screen and hit Return, it comes up and
01:18says command not found.
01:20What it actually did was checked all of those directories in order and then said, "Nope,
01:24it wasn't there. It must not be a command that's available to me."
01:27Now, you can change this path in bash. We do that with just typing PATH, no
01:32dollar sign in front of it, equals, and then the path that we want it to be,
01:36Different shells choose a slightly different technique for setting the path.
01:39I am just going to show you the bash way.
01:42Let's just grab this string here and I'll use Command+C and then Command+V to
01:46paste it and that will give me everything except that very last path.
01:50So I am just leaving off that last path and the colon that's before it.
01:53Now, if we go up and do echo $PATH, you can see my path has changed and I've
01:58omitted that last directory.
01:59What that means is that any commands or programs that were in that last
02:02directory, Unix will no longer look there.
02:05It won't be able to find them.
02:06I have essentially taken them out or uninstalled them.
02:09We can do the same thing by adding our own directories.
02:11If we have other places we want it to check, we can add that in there.
02:15Frequently, when we do web development, we want to add a MySQL path so it'll
02:19look inside our MySQL folder to find all those MySQL commands.
02:23That's a very common use of this.
02:24Let me show you an extreme example.
02:26If we set our path equal to just simply usr/ local/bin and let's echo our path, that's it!
02:33It's just going to check this one place.
02:35We could use echo, we could try that as an example, but echo is such a
02:39fundamental and that it's actually really built into the system.
02:41The system will find it anyway.
02:43As a different example, let me use ls.
02:45That's another pretty fundamental one but if we try ls now, it says oops. Command not found.
02:50It didn't find the ls command anymore.
02:52Now, you could change your path back and just paste that back in, or I want to
02:56show you that if you close the window and you reopen it now, now if we say ls,
03:02we get our directory listing.
03:03That's because setting the path in the way that we did only lasts for the current session.
03:08It doesn't stick around.
03:09In Chapter 7, we are going to talk about configuring your environment and
03:12you'll be able to set a path there that will stick.
03:15So if you want to know how to do that, Chapter 7 will cover how to make a path last.
03:19But I want to make sure that I show you here is the concept of the path so that
03:23you will understand how it's translating these commands into executing the files
03:27that may be located in different locations.
03:30There is a last thing.
03:31Let me also just mention that the command that we learned in the last movie,
03:34which is the which command,
03:36so if we say which ls, it tells us which version of ls we are using.
03:40What that means is there may be several versions here, but I'm using your PATH
03:44variable and I am telling you which one I'm going to give you.
03:47So that's important. which is slightly different than whereis because which
03:50uses our path to say this is the one that's active.
03:53So it's a really good command to know.
03:54So for example, maybe we have several different versions of Ruby installed.
03:58Well then we could say which ruby, and it will come back and tell us the version
04:02of Ruby that we will get if we were to type a command that started with Ruby.
04:06Okay, now that we have those fundamental concepts of how commands and
04:08programs work, we are ready to actually start diving in and learning some
04:12useful commands.
Collapse this transcript
System information commands
00:00In this chapter, we are going to be looking at useful commands that we can
00:03use in Unix, and I want to start us off by looking at useful system
00:06information commands.
00:07The first one we'll look at it is probably the simplest, which is just simply
00:10date and that tells you the current date.
00:12That's a nice handy thing.
00:13If you want to know what the current date is, you just type date and it tells you.
00:17Keep in mind that this value is the date that the computer has been set to,
00:21which is not necessarily correct.
00:23Hopefully, we did set our Mac up to have the correct date, but it's essentially
00:26the same date that you would see up here on your Mac if you had a clock display.
00:30You would set this value via the system preferences and the date and time settings.
00:34So that's what it's telling you.
00:35This is the date the system thinks it is.
00:37We also have uptime.
00:39That's going to report to us the time that it's been turned on.
00:42You can see that I've been up for three hours.
00:44So for three hours I've had Unix up and running.
00:47That's the time since I booted my computer.
00:49It tells you also the time here.
00:51Don't need to worry about the load averages here.
00:53That's letting you know just sort of how well the system is performing.
00:56You won't need to worry about that.
00:58Notice that it says 2 users.
00:59You may be thinking, well, wait a minute!
01:002 users? Why are there two users?
01:02Well, we can actually use a couple of other commands.
01:05Let's use users and that shows you just one user.
01:09So wait a minute, so that doesn't seem to match. Or who. who shows us a
01:13list of all users and what they're doing and it doesn't de-dupe it. users de-duped the 2.
01:18There is actually two Kevins logged in.
01:20One of them is the Terminal program that I am using now when I open up a new bash shell.
01:24But keep in mind that I also have the Finder running here, which is also
01:29interfacing with Unix.
01:30So it has to be logged in as Kevin as well.
01:32So those are the two users, the one that's sort of outside here in the Finder
01:36and the one that's inside this window.
01:38I am going to actually open up a new Terminal window and then from here you will
01:42see now we have a second one. We can type who.
01:45You can see now we have three.
01:46There is ttys000, which is this other one over here and the other one has
01:50been called ttys001.
01:51So that's how it keeps them straight.
01:54So who shows you each of these occurrences every time that I log in, whereas if
01:58you just type users, it just shows you the one user, Kevin.
02:01It doesn't care the fact that there are several of them.
02:03So those are useful commands.
02:05uptime, users, and who.
02:07We also have uname, which returns the operating system name.
02:12Now, you may be like "Wait a minute!
02:13Darwin? I thought I was using Mac OS X." Remember when we talked about back, when
02:17we said what is Unix?
02:18Darwin is the name that Apple gives to their version of Unix with all the extra
02:23code that they've added in on top of it. All the proprietary stuff as well as
02:26the open-source stuff altogether is called Darwin.
02:28So that is the version of Unix that we are running.
02:30OS X is running on top of that.
02:33We are talking about the Unix name.
02:35That's the u in front of it. uname is Darwin.
02:37We also can pass in some options here.
02:40uname, dash, and the options are mnrsv and p and that will return all sorts of
02:47information to us, not just the operating system but you can see it tells me
02:5110.6 is what I am running.
02:52It gives me real specific information about the release that I'm running.
02:56It tells me my processor, the hardware that I'm running on, and actually a
03:00shortcut for all of those. For everything except the P is the A option.
03:04That does mnrs and v altogether.
03:07P is the processor, and that's the second.
03:09You can see that those are actually the same, the last two entries there but
03:11that will do the same thing for you.
03:13You can check the man pages to see what all those are but that just gives you a
03:15little more information about the system that it's running on.
03:18We also can use hostname and domainname. Those are more useful when you're in a
03:22networked environment.
03:24The hostname would be the host that we are on and domainname for me is not set
03:28to anything but on a web server or some kind of a shared Unix work server, those
03:33might return something more useful to you.
03:35So together those commands make up some tools that we have to find out some
03:38basic system information.
Collapse this transcript
Disk information commands
00:00In this movie we're going to be looking at some commands to give us some hard
00:03drive information, or as Unix typically refers to it a disk.
00:07The two commands we're going to be looking at are df and du.
00:10df stands for disk free space, so it's going to display the amount of disk free
00:15space available to us.
00:16If I type a line by itself and hit Return, it'll come up with the report
00:19telling you all of the volumes that it can see, the hard drives, and how much
00:23space is being used and how much is available on each of them.
00:26Notice that it has a couple of devices here that it's calling volumes that are
00:30just sort of utility devices that it's using.
00:32You can basically ignore those.
00:34The one we care about are the ones with the big numbers.
00:37Main hard drive is listed here as root.
00:39That's where it's mounted.
00:40If we had other hard drives, maybe you have your hard drive partitioned or you
00:43have external drives plugged in, you would see those under /volumes/ and then
00:48the name of the hard drive.
00:49We would see reports for those as well.
00:51Now I don't know about you but I find these numbers a bit hard to read.
00:54So one of the options we can use with df is the -h option for humanize.
00:59This will humanize those numbers so now we say, oh!
01:0239 gigabytes used, 16 gigabytes available, 71% of it has been used, okay.
01:08much easier to read.
01:10However, there is a caveat that comes with those numbers.
01:12There are two different ways that one can calculate gigabytes.
01:15You can calculate it using Base-2 or Base-10.
01:18It's a bit of a technical point.
01:20You may remember in the news there was some discussion about the fact that hard
01:23drive manufacturers were selling hard drives
01:25that would say, well, this is a 10 gigabyte drive and then people would get it
01:29home and it wasn't a 10 gigabyte drive once they started putting information on it.
01:33It's because of the way that the gigabytes are calculated were different.
01:36So we also have df -H which uses Base-10 to calculate it.
01:42These give us those higher numbers, the ones that the hard drive manufacturers
01:46were using, whereas the lowercase h is giving us the actual amount of bytes that
01:51we could put on there when we're talking about the data.
01:53Now I think you want to stick with the lowercase h. I recommend that you just
01:57use that all the time.
01:58It's more widely supported, as we'll see in a moment.
02:00But that capital H, I believe, is the one that now is used in Snow Leopard.
02:04So starting in Snow Leopard, Apple started using capital H when it looks at
02:07sizes for hard drives instead of the lowercase h. It's really less of a
02:11technical point and more of an issue of customer confusion over the marketing.
02:15The second command that we're going to use is going to be du, which stands for disk usage.
02:20And with that we'll provide it the path that we wanted to look at and tell us
02:24about the disk usage of that path.
02:26We wouldn't want to do this at the root of our hard drive because then it would
02:28tell us every single file and folder on the entire hard drive.
02:31If you accidentally do that, hit Ctrl+C to exit out of it.
02:35Instead we want to target it a little more at a direct path. disk usage for in
02:40my user folder unix_files.
02:42that's where I've got just a few files set aside.
02:45And it comes up and it tells me for each and every one of the directories that
02:48it sees, what the size is.
02:50Now just like the other one we have a -h option that we can pass in.
02:54That's a lowercase h. unix_files, and now it gives me a human readable version.
02:59It has a -H option but it's completely unrelated to human readable output, so
03:05you wouldn't want to use it here.
03:06That's why I said that lowercase h is more widely supported as being humanized.
03:10So stick with lowercase h.
03:11Now you may notice it's only showing me the directories here.
03:14If we want it to also show us the files, we can put the -a option in front of it.
03:19Now you can see it tells us all of the files and the directories and
03:23their sizes, all right?
03:24So we get a list of all those. Fo for -a, think all.
03:27Now this is sort of similar to what you get in the Finder if you do Get Info, right?
03:32And it calculates the sizes.
03:33But what it does is it actually summarizes the sizes.
03:36The way that we can do that is by using the depth.
03:39So for example we say du, we'll just do the directories.
03:43We'll leave out the files. Humanize the directory with the depth of 1, so that's
03:47the depth option and then the argument to the option is 1, and then we'll tell it
03:52to look at unix_files.
03:54And you'll see it just goes one directory deep.
03:56It shows me the current directory plus one.
03:58If I did the same thing with the 0 option then it says, "Okay, just for the
04:04current directory, right?"
04:06Show me just this one directory, summarize the size of that.
04:09So a depth option can be really useful if you don't want all of this output.
04:12You really what you want to know is just this directory, to summarize and tell
04:16me how large that is.
04:17Now there's another important point that we need to talk about with disk usage.
04:21What it's reporting to us here is the amount of size that has been set aside
04:25for these files on the hard drive. That's different than the size that the files
04:30are actually using.
04:31You notice here that all of these files report 4K as their value, right?
04:36But they're not all the same size files.
04:38In fact, we can see that.
04:39If we do ls -lah, we'll get a listing. Oops!
04:42Let's actually do that for the unix_files.
04:45-lah and unix_files and we can see those files and we see that some of them
04:51take up just 55 bytes.
04:53It's because the file system sets aside blocks of space for files.
04:57Even if they don't take up the whole space, it goes ahead and gives them a whole
05:00block on the hard drive.
05:02So there's a minimum size for those.
05:04Disk usage is returning the space taken up by the blocks, the space allocated to
05:09the files, not the actual file size.
05:13So I want you ti understand the difference and understand that the two aren't the same.
05:15ls is reporting the actual size of the file, whereas du is reporting the
05:21allocation of space for that file, even though it may not be using it.
05:25You may have come across this before if you ever defragmented your hard drive.
05:28There are tools out there that lets you to defragment your hard drive to regain space.
05:33One of the things that it's doing is it's taking the files and by moving them
05:37all together so that they're not fragmented, well, then the block allocation can
05:41really be best utilized.
05:42And we can really make sure that we're using only the exact number of blocks
05:46that we need, whereas if they're fragmented and part of the file is spread out
05:49at different places on the hard drive, you may end up with lots of blocks that
05:52are partially used.
05:53They've been allocated for the file but not completely used.
05:57And that's how you're able to reclaim a lot of that space when you defragment.
06:00So again, the two main commands for finding out disk information are df for
06:04disk free and du for disk usage.
Collapse this transcript
Viewing processes
00:00Over the course of the next few movies I want to take a look at some Unix
00:02commands for managing processes.
00:05Think back to Chapter 1 where we talked about the difference between the
00:07kernel and the shell.
00:08In our case the shell is bash.
00:10Whenever we run a command inside our shell, a file executes and it
00:14communicates with the kernel.
00:15And essentially it says to the kernel, "Hey kernel!
00:17There are some things I need to accomplish here.
00:19Can you help me out?"
00:19The kernel sets aside some memory space and starts a process running in it.
00:23Then whenever there's output from that process, it returns it back to the
00:27shell for us to see.
00:28And whenever the process is finally done, the kernel then closes it out and
00:32reclaims that memory space so that it can be used by other processes.
00:35That's what the kernel does.
00:36it manages the processes for us.
00:38Now we can have processes that are really short, like when we run the echo command.
00:42That's a really, really short process.
00:44It starts, it does its thing, and then it's over.
00:46We could also have longer running processes.
00:48Let's say that we're going to print five pages to a printer.
00:50That'll start a process and that process will just feed information to the printer.
00:54After it submits page 1, it'll go to page 2, and so on.
00:57And if the printer runs out of paper, the printer will communicate that back to
01:01the process, which will then let us know that the printer has run out of paper.
01:04That's an example of a long-running process.
01:06We can also have background processes.
01:08For example, if we have a database server like MySQL running, it'll just sit and
01:12run in the background waiting for connections, waiting for someone to make a
01:16request to it which it'll then respond to.
01:18So it just sits in the background waiting to do its thing.
01:20Now what we want to do now is be able to see those processes.
01:24And the easiest way to do that is with the ps command.
01:26So ps stands for process status, and it's essentially giving us a snapshot of
01:30the processes that are running.
01:32Now there are lots of processes running on my machine.
01:34By default what ps is showing me are processes that are owned by me, the user
01:38kevin, and also a process which have a controlling terminal.
01:42That is, I am in control of them.
01:44They're not background processes.
01:46If we want to see processes that are owned by other users as well, we can say ps -a.
01:51Still I am not seeing background processes but now I see processes that are not owned by me.
01:55Notice that the third line there lists the process they I just ran. ps -a.
01:59It's a process that's not owned by me.
02:01I own the bash process.
02:03Bash started the ps -a running, but it's actually a process that's owned by root.
02:09And when it's done doing its thing, it returns the results back to me.
02:12So it's a bit of a technical point but we don't necessarily own all the
02:15processes that get started.
02:17A lot of times process gets started on our behalf by the kernel, which is acting as root.
02:22Now there's one big quirk about ps that you need to know about and that is
02:25that because ps has been around a long time and it has been modified in
02:29different versions of Unix, there's been a real effort to maintain some
02:32backwards compatibility here.
02:34So in addition to ps -a, we also can use ps a, without the dash. One is
02:40the classic option passed into the command that we've been seeing.
02:44The other one is using more like an argument that's being passed in.
02:47And you'll see that it gives us very similar results, not exactly the same but very similar.
02:51What we're essentially seeing here are two different implementations of ps.
02:55One from one version of Unix and one from another.
02:57The reason I mention this is because the most classic way to use ps is ps with aux.
03:05Those are the options that we pass in and we typically don't put the hyphen in front it.
03:08It's a bit of an odd bird because it doesn't match the options that we're
03:12normally used to using with the dash. We can actually do a and x together but
03:17the problem is u, because u actually has a different meaning between the two
03:21versions, so therefore we can't do it that way.
03:23So we're going to do it this way.
03:25a means show me all processes regardless of who they're owned by, u says include
03:30a column showing me the user that owns the process, and x says show me the
03:35background processes too.
03:37That's what those three options mean.
03:38Let's take a look at it. ps -aux, and there's a list of all the processes that I
03:42have running right now.
03:43Let's scroll up the top and take look at the header information.
03:46You can see that first column is the User, then is the Process ID.
03:49Every process gets assigned a unique ID number to help us keep track of it.
03:53It shows us the percentage of the CPU that's being used, the percentage of the
03:56memory that's being used.
03:57This is the amount of virtual memory that's taking up.
04:00This is showing us the Terminal.
04:02In most cases it's a question mark. That often means that Mac OS X launched it.
04:05If we scroll down to the bottom you'll see that these processes that we've been
04:08working with have 000.
04:10That's for our current Terminal.
04:11If we own up a new window and ran commands there it would be 001.
04:15So let's go back to the top again. We have the status.
04:18You don't need to worry about those codes. The time that it started, the amount
04:22of time that it's been running, and then the command itself, which is a path to
04:25the file where this command got started.
04:28So that gives you some information to browse over and you can see what's
04:30going on in your Mac.
04:31Now if you want to use other options, the man pages can give you a lot more of
04:35those information about them.
04:36But the most common way that you'll see it use is ps aux, and that should
04:41return a list like this.
04:42It'll show you a snapshot of what's happening with different processes at
04:45this moment in time.
04:46Now the problem with that is that it is a snapshot, and things change.
04:50things are constantly changing.
04:51And it'll be great if we had something where we can actually watch things like
04:54CPU and memory usage as they change over time.
04:57And to do that, we've got to use the top command which we'll see in the next movie.
Collapse this transcript
Monitoring processes
00:00In the last movie we saw how we can use the ps command to be able to view a list
00:04of the Unix processes that are running.
00:06But the problem with it is that sometimes we don't just want a snapshot in a
00:09moment in time. We actually want to be able to watch the processes.
00:13We want to be able to monitor them as the CPU and memory usage goes up and down,
00:17whereas processes start and stop.
00:19So to do that we need something that's a little more interactive that will keep
00:23refreshing and showing us the state of things as they change.
00:26The tool for doing that is called top.
00:27So if you simply type top and hit Return, you will get a list of the top processes.
00:32Now, top processes depends on what order you've sorted it in.
00:35By default, it's sorted by the Process ID.
00:38So the most recent processes are at the top.
00:40You will see above that that we actually have a list of some summary
00:43information, the CPU usage, the memory usage, that kind of thing that's going on,
00:47and then we get that information for each one of these as well.
00:50So we can see how much memory usage it's using up and how much of our RAM it's using up.
00:54Now perhaps the most important thing you need to know about top is that you can
00:57hit the Q key to get back out of it.
00:59So Q will exit out and take you back to your command line.
01:02So there are some options that we can pass in when we launch top. We can say top
01:06-n to specify the number of processes to show.
01:09By default, it shows however big the window is.
01:11But we can say, all right,
01:12we only want to see the top 10. We can specify the sort order.
01:16There are some special keys that we used to say the sort order and I can show
01:19you those in the moment.
01:20For now let's just say CPU.
01:21We're going to sort it by CPU usage and we can set the refresh rate.
01:25We will use the -s for seconds.
01:27By default, it's set to 1 second refresh. Let's set it to 3.
01:30So every 3 seconds it will refresh and then we can also pass in a capital U for
01:35user showing filter by user.
01:37So we are going to see only the user that we specify, Kevin in this case.
01:40So we will see the top 10 processes of Kevins, sorted by CPU, refreshing every 3 seconds.
01:47So there you go!
01:49Now, every 3 seconds it will re-update, but you can see the thing that's using
01:52the most CPU is Snapz Pro, which is the program I'm using to record this video.
01:57We have some interactive commands that we can issue as well, the most important
02:00of which is the question mark.
02:01That's the same thing as sort of help. It displays this Help screen.
02:04Notice here that it gives you sort orders.
02:06Right now, I have it sorted by CPU.
02:08That's what it's telling you here. The current state.
02:10We can pick a different thing, we can sort by time, we can sort by size.
02:14That's the resident memory size, which refers to the RAM that it's using up, and
02:18we can sort by user and so on.
02:19The man pages will detail what each of these actually does for you.
02:23We can also change the delay just by using s followed by the delay. So let's try that.
02:27Hit any key to continue.
02:29Now, we are back here again.
02:29Let's change our delay back to 1.
02:30I will hit s and 1, hit Return, and now it updates every 1 second.
02:35If we want to see all the users, we type a capital U and then I can just hit
02:39Return and that will show me all users now, not just Kevin.
02:42But one thing that we can't do from here is change the In value, the number of
02:47processes that it's showing.
02:48That's not available.
02:49But the o, s, and capital U options are all still available from here.
02:54Now, there are some differences between top and different Unix versions.
02:57This is the way it works on the Mac.
02:58The Mac is a little bit limited in the interactive options that you can type.
03:02Something like Linux the version of top actually has a lot more options and
03:05shortcuts that you can use from that interactive page.
03:07So check the man pages, check that help page, and it will help you out with all
03:11the different commands that you can use.
03:13Let's launch it one last time and I just want to show you if I go ahead and I
03:17launch Firefox, and we can see it pop up and we can see the memory that it
03:21takes up and so on, all right?
03:23You can see it up here and then we come back to Firefox and we quit out of it again.
03:27You can see that it goes away.
03:27So this is a nice little tool.
03:29You can just leave it open and watch the processes come and go.
03:31In the next movie we will actually take a look at how we can stop these
03:35processes from running.
Collapse this transcript
Stopping processes
00:00Over the course of the last two movies we've been looking at how we can view and
00:03monitor the different processes that are running in Unix.
00:05In this movie I want us to see how we can stop processes.
00:08Now, the number one best way to stop a process is with Ctrl+C. That assumes that
00:13you're running the process inside your Terminal window. You started it.
00:17It's just taking too long or maybe you changed your mind in the middle of it.
00:20Ctrl+C tells Unix, hey!
00:23Stop that process, interrupt it, let's go back to the command line.
00:27So that's the best thing, but sometimes we can't use that.
00:30Sometimes there are processes that are running in the background, or maybe
00:33there's a process that we did have control of but somehow we've lost control.
00:37So we can still see it, we can see it's running there, and we really wish it would go away.
00:41Well, to do that, we use the kill command.
00:44In order to show you how we would do it, let's do ps aux. We see a listing
00:48there and you will see I have a listing on here for s000 bash.
00:53That's my current bash program.
00:54Now I am going to open up a new window.
00:56Essentially what I want here is a process that I am not afraid to kill off,
00:59because I don't have an errant process running right now and I don't want to use
01:03sort of advanced tricks to get one running.
01:06Instead, I am just going to create this new Terminal here.
01:09Now when I do ps aux you'll see that I see another bash here and this bash is owned by 0001.
01:17Let's clear the screen just so we can see it nice and well.
01:19Here is bash down here and if we scroll up here, it should be this other bash up here.
01:25Now notice 000 is the one that I am on.
01:27That's the one I don't want to get rid of.
01:29The one I want to get rid of is 001.
01:33That's the other window that's going on and that's the process I am going to kill.
01:36I am going to forcibly just stop that bash from running.
01:40What I need is the process ID.
01:42That's how we tell Unix which process we want.
01:45Every single process has a unique ID, so it's the way that we can reference it.
01:49What we use is kill 1837.
01:53That's the process ID.
01:55We have killed it, except that we haven't.
01:57Some processes can't be killed by using kill on its own like this.
02:01So always try kill first.
02:03It'll do its best to kill it off.
02:04But there are cases where it'll say, you know what, I think you probably
02:07don't want to do that.
02:09This is one of those cases, because it's running bash in a Terminal window.
02:11So it says, you know what, I could kill that off but I think that's probably not a great idea.
02:16So it didn't do it.
02:17Let's just take a look here. ps aux, I will clear the screen, just so we can see
02:21it is still running.
02:22Right here it is 001, bash, same process ID.
02:26But if we pass it the -9 option, that says, yeah!
02:30I know better than you.
02:31It means really, really, really kill this thing off. Forcibly kill it.
02:36So kill -9 and then the process ID number.
02:39Now, I killed it off.
02:40Let's take ps aux again. Take a look.
02:43Notice that it's not there.
02:44The only bash that I have is 000 and come over here to the other Terminal and
02:49look, process completed.
02:51The process is done.
02:51If I hit anything, I don't get anything in that window anymore.
02:54I killed off that process.
02:55But you can do this anytime you've got a process that's taking up tons of
02:59memory, tons of CPU usage, something you don't have control over.
03:03Obviously you want to be careful. A lot of these processes your system needs.
03:06You don't want to just start killing off processes willy-nilly.
03:09But it is a technique if there is a process that you know needs to be killed.
03:13This is how you would do it.
03:14First, try kill and then kill with the - 9 option as well.
Collapse this transcript
Text file helpers
00:00In this movie we will take a look at some Unix commands that can help us when
00:03working with text files.
00:04Now when I say text file, I am really talking about a file that has nothing in it but text.
00:09That's very different from a Microsoft Word document which contains both
00:12formatting information and text.
00:15These commands would not help us in those cases.
00:17For example, if we try to do a word count of a Microsoft Word document,
00:20it would try and count all of the formatting information as well as the actual
00:25text of the document.
00:26So in all these cases we are really talking about working with pure text files.
00:29So the three that we are going to be looking at are wc, which is short for word
00:32count, sort for sorting lines, and unique, uniq. There is no ue on the end of it
00:38and that's for filtering in and out repeated lines.
00:40Let's take a look at all three and see how they work.
00:43So in Terminal, notice that I am already inside my user directory and I'm inside
00:46my unix_files directory. I can do ls-la on them and we will see that I've added
00:51a new file here called fruit.txt.
00:54Take a look at the contents of that file.
00:55It's a very simple file. It just contains a list of fruit.
00:58Notice a couple of things about it though.
01:00Notice that they are not sorted. Notice that there are repeats in there
01:04and notice in particular that strawberry is repeated immediately one after the other.
01:08So you can create your own. You can pause the movie and copy this list down if
01:11you want. They're also included in the Exercise Files.
01:14The important thing is to make sure that it's not sorted, that it contains
01:16duplicates, and at least one of those duplicates are one immediately following another.
01:20So let's try this out now.
01:22Let's look at our first one. We have wc.
01:24That's for word count and all we do is say the file name that we want. wc fruit.
01:28It comes up and it gives me 3 numbers followed by the name of the file.
01:32The first number is the number of lines in the file, the second number is the
01:36number of words in the file, and a word is a set of characters with spaces
01:40on either side of it.
01:41That's what it considers a word. And then the third value is the characters in
01:45the file, the number of letters that have been used.
01:47So again, if we take a look at the file, cat fruit, you can see that there are 13
01:51lines, each line has one word, so there's also 13 words, and if we add it all
01:56up, there are 99 characters in that file.
01:58Let's try it with something a little more useful. Remember we have this lorem
02:01ipsum text, which is just fake Latin text, just a long document.
02:05Let's do wc lorem_ipsum.txt.
02:08You will see that it comes up and it tells me that that file has 523 lines,
02:135,289 words, and 36,232 characters.
02:19Now I just want to point out something here, if we do head lorem_ipsum.
02:22We can take a peek at the beginning. Each one of these lines actually has a
02:25line return after it.
02:26I did that on purpose when I created this file. There is a line return at the
02:30end of each of these lines.
02:31Now, with a lot of paragraphs, that would not be true, right?
02:34They would not be wrapped like this, they would just continue, and you wouldn't
02:37have a line return, you would just keep going.
02:39So every paragraph would be considered one line.
02:42So keep that in mind.
02:43When we are talking about lines. We're not talking about the lines that you
02:46see on your screen necessarily; we are talking about a line up until it gets to a line return.
02:51So that's really all there is to doing word count.
02:53It's a very simple utility that can be very useful.
02:56Let's try out working with the next one which is sort, and all we do for sort is
02:59we say sort the lines in a file. sort fruit.txt.
03:03So now it gives us that same list back but it's been sorted.
03:07Now notice it did not actually change the file itself.
03:09If I do, the original file is untouched.
03:12What it did was just take that file, sort it, and output the results to me on the screen.
03:16Now I could copy and paste it or, as we will see in the next chapter, I could
03:19then send that to a file.
03:21We will see how to do that.
03:22I want to make sure you realize that it's not actually sorting the contents of
03:25the text file itself. All right!
03:26Let's try that with our lorem_ipsum.
03:28Let's try sort lorem_ipsum.txt.
03:31Again, each one of these has a line return after it, so each one is considered a line.
03:36If they were paragraphs, then this would not behave the same way.
03:39Notice that we are seeing all of the Vs down here at the bottom.
03:41I just want to scroll up a bit through the alphabet, so we get up here to the As.
03:45Here we have the As. Now, notice this.
03:47Lowercase a and then we start over with capital V. Capital letters and lowercase
03:53letters are treated differently by sort by default.
03:56Now we can pass-in the f option and we can change that.
03:59So let's do -f. I will clear the screen, just so we have everything gone from before.
04:04And now look. You see that they are mixed up, the uppercase and lowercase.
04:07That's the f option.
04:08There is also a reverse sort.
04:11Let's take out the f option and do an r. That's for reverse.
04:15Now we see a lot of blank lines here.
04:18If we scroll up you'll see the blank lines count as lines.
04:21They get sorted before the As.
04:22So there is the As, we keep going up.
04:24Here is the Cs, and the Ds, and so on.
04:26So that's reverse sort and it also helps you to see that these blank lines are
04:30still included as lines and then the last thing let's do is as if we sort, sort
04:35fruit reverse first, just so you can see that it reverse sorts those.
04:38That's nice and easy to see. And let's do sort -u and that's the option
04:42for sorted and uniq.
04:45So we get rid of all those duplicates at the same time.
04:47sorted and unique. Now each entry just occurs once.
04:51Notice what it did there.
04:52It sorted them, and it made sure we only had each one, one time.
04:54So sort has its built in uniq option.
04:57There are a couple of other options. You can look at the man pages to see what
05:00those are, but sort is a really, really useful tool.
05:02It actually works well with other commands.
05:05We'll see that later.
05:05We'll see how to use two commands together.
05:07The sort is really useful for that.
05:09Another one that works well with other commands is uniq.
05:12Uniq allows you to filter in or out repeated lines in the file.
05:16The default is to de-dupe the repeated lines.
05:18Let me show you by using it on our fruit file and it's easy to see.
05:23Notice that Apple is still in there twice.
05:26It did not de-dupe that, but strawberry is only in there once because
05:30strawberry is in there immediately after.
05:32So it does not sort them and de-dupe them.
05:35It only goes down the line and says, ah,
05:36is this line the same as the next line?
05:39If it is, reduce it down to one, de- dupe it till we only have one line.
05:43But it does not jump ahead and look around to other ones.
05:46We would have to sort it if we wanted that behavior.
05:48But one of the nice features of uniq is that it has a couple of other options we can pass in.
05:51uniq-d fruit will return the lines that are repeated. So it tells me oh,
05:59there was one line that was repeated in here. It was strawberry.
06:01That's a nice handy tool.
06:02There is also uniq-u and this just shows me the unduplicated lines.
06:08That's why we have the u. So now I am seeing everything, but strawberry,
06:12strawberry has been completely taken out.
06:14Not just de-duped, but actually removed.
06:16So those are complementary of each other.
06:18Between unique -d and uniq -u you get the same thing that you get if you did the
06:23total list from uniq.
06:25Having no option shows you the entire list both duplicated and non-duplicated
06:29but with no duplicates immediately following each other.
06:32So again, if your goal is really to completely de-dupe it, then you either need
06:36to use sort with the uniq option or you will need to use uniq in combination with
06:41other commands which we will learn how to do in a little bit later.
06:44So these are all three really simple tools, but they can be very powerful when
06:47you're working with lots of data in text files.
Collapse this transcript
Utility programs
00:00In this movie, I want to show you several utility programs that Unix provides
00:04that I think can be really useful to you.
00:05We are going to look at cal and also a variation on that called ncal, which is
00:09a calendar program.
00:11bc, which is a calculator program.
00:13bc stands for bench calculator.
00:15Then we have expire, which is an expression evaluator.
00:18essentially it's a simple one line calculator.
00:21We are just evaluating a mathematical expression. And then units which allows us
00:25to do units of measure conversion.
00:27So for example, from feet to meters.
00:29Let's try all of them out.
00:31We will just start with the simplest one, which is calendar.
00:33Just type cal and look what you got.
00:35You get the current month's calendar. That's it.
00:38That's all it does.
00:38It is very simple, shows you by default the current month's calendar.
00:41So let's type in the month 12, followed by 2020. So we are going to see what
00:46December of 2020 looks like.
00:48As an exercise, plug in your month and year that you were born and you can see
00:53what day of the week that you were born on.
00:54Now in addition we also have the ability to take a look at a full year by
00:58using the y option.
01:00So this will be the year 2000. We can see what that full-year of calendar looks like.
01:04Now, of course, we could do the same thing for this current year as well. There we go.
01:10That's what the current year looks like and then let me just show you the
01:13variation on this, which is ncal.
01:14It has some slightly different options to it.
01:17It's actually a different program but what it mainly does is it rotates the
01:20calendar so that the days of the week run down the left side instead of
01:24running across the top.
01:25You can look at the man pages to see what's some of those additional options are.
01:28Personally, I prefer the standard cal most of the time, but I just want you to
01:31know that this is there as well.
01:32Okay, let's move on to look at the next one, which is bc for bench calculator.
01:37If we just type bc and hit Return, it drops us into this calculator.
01:41It's a program that runs and it actually has a pretty rich programming language.
01:45So there is a lot of things that you can do in here.
01:46We are just going to use it for the most basic.
01:49I am not going to try and teach you everything there is to know about it.
01:51I just want you to know it's there and get an idea of how it works.
01:54So just 1+1 and it reports the results once we hit Return. That's what it does for us.
01:59To get out, we type quit and we exit back out again.
02:02So let's go back in there and take a look at a couple of other things about it.
02:06So let's try some expressions. 3+4 times 210.3 and there it gives us the result.
02:15Let's try another one. 1000 divide by 9.
02:19Now wait a minute.
02:20I think about the math here for a second.
02:22That doesn't seem quite right, because 9 times 111 actually equals only 999.
02:28We are missing some decimal places here.
02:31There is an important point about this, is that the precision that it reports the
02:36results is arbitrary.
02:38We can change the precision that it reports those results and the way we do that
02:41is what scale equals and then whatever scale we want.
02:45That's how many decimal places we are looking at.
02:47So now when we try 1009, you can use the up arrow to get there as well, just
02:52like you normally can.
02:53Now it says okay, it's actually equal to 111. and
02:57then 10 decimal places of 1s because that's the precision than I told it to report.
03:02You can change the scale to be 5 and of course it will only give you 5 back.
03:07So that's an important point, that you do have to tell it how much precision you
03:12wanted to return to you.
03:13Now again, there is lots more that you can do with the bench calculator, but at
03:16its simplest, this is what it is.
03:18Just a calculator where you can type in expressions and they will return results
03:22and then wait for you to do another calculation.
03:24Let's quit out of that.
03:25If you really just want to calculate a really simple expression then you have
03:29expr available to you.
03:31Expr says all right, calculate the expression and I am going to just show you
03:34what won't work first. 1+1.
03:37It doesn't realize that it needs to break that up.
03:40You have to put spaces in here.
03:41Let me just show you if I try it first.
03:44It just comes back and says, oh, the value of that word, that thing, that unit is 1+1.
03:51It returns it to you.
03:52What we need to do is put spaces, so it says, ah! I recognize.
03:55The first thing is a 1, the next thing is what you want to do to it and the
03:59next thing is the 1.
04:00That's because we are in the Unix command line and Unix is taking each one
04:04of these as arguments.
04:07Even though it's an expression when we are in bc, we don't have that
04:10Unix command line issue.
04:11But here we really need an expression to see ah, there are three arguments here.
04:16So we need those spaces.
04:17Also because we are on the command line, if we wanted to do something
04:20like multiplication, 1122 times 3344, this won't work because this has
04:29special meaning in Unix. It's a wildcard.
04:32Remember that?
04:33So if I hit Return, comes up as oops,
04:35I don't know what do you mean.
04:36We have to escape that first. Just like we had to escape spaces in the
04:40file names, we need to escape it to let the command line know, hey, I
04:43mean the literal asterisk.
04:45I'm not using this as a wildcard. And then it works properly.
04:48Okay, so that's the things you need to know.
04:49Make sure that you put spaces there, make sure that you escape characters that
04:53the command line is going to interpret as something different.
04:56But you're going to see that's probably the simpler calculator then
04:59actually dropping into bc.
05:01bc is really designed to switch into it, to do lots of calculations over and
05:05over, to sort of leave it running while you're working at your workbench, which is
05:09why it's a bench calculator, whereas expression really just does a real simple
05:13expression evaluation.
05:14Okay, let's look at units.
05:16units is another one that drops you into a mode.
05:19It comes up and tells me how many units it has in its library and then it asks
05:24you have what, and that you can put in a value here.
05:28Let's say I have 1 foot, all right.
05:30We are talking about a unit of measure 1 foot and what I want is meters and then
05:36it comes back and it tells me that is equal to 0.3048 meters. Then it gives me the
05:44second line here which is the reverse calculation.
05:48That's what it would take to go the opposite way.
05:50Most times you can just ignore that.
05:51It gives it to you because there are times when the precision is actually better
05:56in that second line than it is in the first line.
05:58Most of the time you can just ignore that other line.
06:01Then it pops up and it says all right, you have what? And it asks again.
06:04So let's just try a few more.
06:05Let's say 1 inch into centimeters.
06:091 inch is equal to 2.54 centimeters.
06:125 gallons is equal to liters.
06:18There we go and let's try 3 furlongs into miles.
06:23Let's try 72 degF, degrees Fahrenheit, followed by degrees Celsius and it turns
06:33up and tells what 72 degrees Fahrenheit is equal to in Celsius.
06:36So when we finally want to get out of this, there's no graceful
06:38way, unfortunately.
06:39We can't just type quit, which just seems a shame.
06:42Instead you would hit Ctrl+C to exit out of it, just like you would force quit a program.
06:46There is also a usage of units from the command line if we put it in quotes.
06:50So '2 liters' into 'quarts' and that comes back and just returns it and then
06:56it comes back to the command line, more like the expression evaluator that we were just using.
07:00That's a really handy tool.
07:01It's nice to be able to just very quickly drop into units and be able to figure
07:06out the conversion that you're working with.
07:07So you have this tool available to you on your Mac all the time.
07:10Just open up Terminal, type units and voila, you can do all the conversions that
07:14you are trying to figure out.
07:15So I think all four of these utility programs can be really useful.
07:18So try and make yourself a mental note.
07:19The next time I want to do some calculations and the next time I want to do
07:23some unit conversions, to switch into Unix, and actually try them out and get
07:26used to using these.
Collapse this transcript
Using the command history
00:00In this movie, we are going to learn how to utilize the Unix command history.
00:04Early on in this training, we learned how to use the up arrow to go back and
00:07review commands that we had issued previously.
00:09We can then either reissue those commands or make edits to them.
00:11But what you may not have thought about is how does Unix remember those commands?
00:15Because even if we close the Terminal window, even if we shut down our Mac and
00:19reboot it, Unix still remembers what our previous commands are.
00:22So it must be keeping track of them somewhere, and it is.
00:25It does it inside a file and that file is inside our user directory, ls -la.
00:30You can see it here.
00:32It's a dot file, bash_history.
00:34And this is where the bash shell stores its history of commands.
00:38We will take a look at the contents of that file.
00:40You will see that it lists our previous commands.
00:42It's just one line per command.
00:44Now yours is probably a lot longer than mine.
00:45I actually went in and edited mine to make it a lot shorter.
00:48I want you to notice something.
00:49If we hit our up arrow now, our last command was actually to look at the bash
00:54history and the command before that was to see what's in the directory and the
00:57command before that was to change to our user directory, but none of those are
01:00being reflected in this bash history file.
01:02That's because bash writes to the bash history file whenever it quits.
01:06So once we quit the sessions, close out this window, it'll write our reset
01:11commands to that bash history file, and the next time we come in, bash will
01:14read the bash history to reload those commands and make sure that it has them
01:18in memory ready to go.
01:19Until then, until we quit, it just keeps them and holds on to the main memory.
01:22So, essentially these commands that we see here in this list are the commands
01:26that were issued prior to our current login.
01:29If we want to the full list of commands, then we need to issue the history
01:33command. Just simply history and then we will see a list.
01:36What we are seeing here is a list of all previous commands, those that were in
01:40the bash history, plus the more recent ones.
01:42Notice that the list is numbered. We can use these reference numbers to do
01:46that command again.
01:47This is much easier if we have done a whole lot of things than hitting the up
01:50arrow over and over and over till you find the command that you did a long time ago.
01:54Then chances are if you're like me, you still will end up using that up arrow a lot of the time.
01:58There is something that was done a long time ago, you know,
02:0050 commands ago, then just go to your history and look for it this way.
02:04So how do we reference these numbers?
02:05Well, there are some shortcut commands to work with the history and you can
02:09recognize them all easily, because they all start with the exclamation point.
02:12Exclamation point followed by the number of the command that we want to execute.
02:16So for example, if we want to execute the calendar command, it's just
02:19!1, and there it is.
02:21It runs it. Notice that it first tells us what command it's actually doing and
02:25then it goes ahead and does it.
02:26Now, also I want you to notice, if you hit the up arrow, that we don't see the !1 anymore.
02:32That was just a shortcut, right. What we actually see is the command itself,
02:36which is really useful, because then, for example, we can edit this.
02:39So we wouldn't be able to edit it if we didn't have the actual command itself.
02:43So I can make an edit here.
02:44I am just going to hit the down arrow without hitting Return.
02:47Okay, it's very important, because I want to show you something.
02:49So I have made a change to this line. Hit the down arrow again.
02:52Now let's type history one more time.
02:53Notice now that it does show that command that I just executed a moment ago, but it changed it.
02:59Now it says Cal 6 2008. So hitting the up arrow, making edits to command, it
03:04does change them in the history.
03:05And it includes an asterisk next to the number to let you know that it was edited.
03:09So even though that command never ran, it's in our history that way.
03:12All right, let me show you the next shortcut, which is to use the exclamation
03:15point and then a minus sign and then the number of commands back that we want to go.
03:21So for example, if I want to run a command that was two commands ago,
03:24that one command back would be my history.
03:27Two commands back could be that cal command.
03:29I am going to hit Return and you will see what it does.
03:31It actually did this command, Cal 6 2008.
03:34Even though that's the amended command, it still does it.
03:39But the minus 2, you can do minus 3, minus 4 whatever, it's always relative
03:42to where you are now.
03:43If I do minus 2 now, it won't give me that cal again, right?
03:47It's a moving target.
03:48Now two commands back would be the history command.
03:51So it's relative to where you are, how many commands back, but if you have done
03:54something, you do one command, you do two commands, you do three commands, you
03:57do the fourth command, and say, I want to do that, first one again, well, it's
04:01real easy just to say all right, I want to go back four commands and do that
04:04command that I did a moment ago.
04:06In most cases it's probably going to be easier to use the absolute id value
04:10instead of this relative one.
04:11Another nice shortcut is that we can actually use the exclamation point followed
04:16by alphanumeric characters and it will go back and try and find the command
04:21that begins with those characters.
04:22So for example, you see up here that I have command number 10 is to
04:26evaluate this expression.
04:28If I type expr and then hit Return, that was the most recent command I did
04:33that began with expr.
04:34Now I didn't actually have to type the whole command. I could just type ex and
04:37it would have done the same thing.
04:39It again tells me what command it's issuing and then gives me the results of that command.
04:43That's a nice handy trick.
04:44So let's say that you added a file using nano and then you do a few other
04:48things and then you say oh you know what, I needed to edit that file again,
04:51well, if you haven't done another nano since then, you could just type
04:53!nano and you will be re-added in that file again.
04:57Keep in mind of course, if you move around and change directories, then that
05:01command may not work the way it used to, because it's always being executed from
05:05where you are in the file system.
05:06So you might have to modify the path to the file that you want to edit in order to get it.
05:10There's another very useful shortcut that I use all the time and that's to use
05:14exclamation point, exclamation point.
05:17This is the same thing as doing exclamation point minus 1. Where it's most
05:21useful is let's say we were doing a chown and we are going to chown on unix files.
05:27Inside there we had a file called ownership.txt.
05:31That in the ownership movie, we were talking about changing ownership of that file,
05:33but remember, operation not permitted. I can't do that.
05:37I need to do it with sudo in front of it.
05:39So it's a classic use case. We say sudo!!.
05:43Right? Saying no, I really mean it.
05:46Do the same command, but this time prefix it with sudo.
05:49See how that works?
05:51So it doesn't just execute the command.
05:52It's actually a reference to the command.
05:54I will use the up arrow, so I can change that ownership back.
05:57Notice that once again the command itself has changed so that now sudo is in front of it.
06:03We are actually seeing the actual command itself, not sudo!!,
06:08which is perfect for a use case like this, where I want to go
06:10back and edit the line and re-execute it again.
06:13Another one that's really handy is exclamation point followed by a dollar sign.
06:17What that does is it references the arguments from the previous command, so
06:21let's say for example that I do nano and let's go in to my unix files and I am
06:27going to edit the file fruit.txt. Here is my file.
06:31I go in, I make some changes.
06:33When I am finally done, Ctrl+X and I exit out.
06:36Then let's say I go, you know what, did I make change correctly?
06:39I want to look at my changes.
06:41I want to cat that file.
06:42Well, I could type the whole thing again, but I also can use exclamation point
06:46dollar sign and what that references is the arguments to the previous command.
06:51So, not the nano part, but the unixfile/fruit.txt. And there it is.
06:56See, here is the command that it actually executed.
06:58It shows you that first.
07:01That's what it did.
07:01it grabbed just this last part here and used it with the new command.
07:06That's super handy because a lot of times, you want to have a file and you want
07:10to do several things to that file, right?
07:12You might want to move it and then you might want to copy it and then you might
07:15want to edit it. You can just keep using this to keep referring to that same
07:19file without typing it over and over again.
07:21You can read the man pages on history to see some of the other things you can do with it.
07:24I just want to give you a couple of options that are helpful.
07:26Let's say we have history and let's say that I want to get rid of a line.
07:30Let's say that this line 27 that's here.
07:32We have it twice. I want to delete that out of my history,. You can do that with
07:36-d and then the number, and now I want to do history again.
07:40You will see that it's gone and everything shifted up one.
07:43So the numbers are not absolute.
07:45Each command is not completely bound to these numbers, but if we delete it, they
07:48shift up so that it doesn't leave a space.
07:50And if we want to clear all of our history, you can use history-C.
07:53That will clear it. Or you can just get rid of that bash history file. You can
07:57just remove the file.
07:59So just so you have a quick reference, here is a chart that shows where all of
08:02those shortcuts are, just so you can see them and refer to them.
08:05Make sure that you understand what they all do, and try and get used to using
08:08them, because they will make life easier.
08:10You may feel a little odd at first, but if you get used to using them, I think
08:13you will find that they really speed things up for you a lot.
Collapse this transcript
6. Directing Input and Output
Standard input and standard output
00:00In this chapter we're going to be talking about how we can modify the input and
00:04output that Unix uses and before we use do that we first need to understand the
00:07concept of standard input and standard output and you may already be able to
00:11guess what these standard input and output are if you think about it.
00:14The input is the keyboard.
00:15That's its main way of getting input into Unix.
00:18It's the things that you type on the keyboard and the way of getting information out,
00:22standard output, is typically the text Terminal, the window, the screen that
00:26you're seeing inside Terminal.
00:28So that's where it outputs its results to.
00:30So when we type a command, we type in the command, we hit Return, that's the input.
00:34It does its process and at the end of it, it outputs the results to the screen
00:38so that we can see it.
00:39What we're going to be doing is changing each of those.
00:41I also want to mention the common abbreviations for standard input are just
00:46stdin and for output its stdout.
00:48So if you see that, that's what it is referring to, is standard in and standard out,
00:52and there are actually sort of special files on Unix that correspond to
00:56these and that's inside that dev folder. dev is where information about
00:59different devices stored.
01:01So inside dev there is a stdin and stdout, which are files that are really sort
01:06of references to these two things, to the keyboard and to the Terminal.
01:10What we want to do is figure out how we can work with commands so that we can
01:14change the input or the output.
01:17So let's start by looking in the next movie at how we can direct the output not
01:20to the screen, but instead to go into a file.
Collapse this transcript
Directing output to a file
00:00In the last movie I told you that the standard output in Unix is going to be to
00:04the screen and we can see that.
00:06Notice that I'm inside my user directory, inside Unix files and I'm just
00:10going to type sort and fruit.txt. This is the command we did before, just sorts
00:14this list of fruit.
00:15Now I didn't actually change the file itself, all right. Sort just took it and
00:21the output of that command was to my screen.
00:23Not back to that file. Just simply output it to the screen and that's what
00:26we're seeing there.
00:27What we want do now is change the output.
00:30We want it to go somewhere else.
00:31The way we do that as we say sort fruit.txt and then after that we use the
00:38greater than sign or an arrow pointing to the right. Essentially that's the way
00:42we're directing the output and we want to direct it to a filename and the file
00:47doesn't have to exist already.
00:48We're going to create a new file, sorted_fruit.txt.
00:52So that's it. That's all there there is to it.
00:54We simply say our instead of sending this information to the screen direct it
00:58to sorted fruit.txt. Hit Return, and I don't get any output because the output
01:03didn't come to my screen anymore but if I do ls -la we now see that there's
01:08this sorted_fruit file and if we take a look at that sorted_fruit file there it
01:12is, there is my output.
01:13So extremely useful, especially for something like sort like that. Then we
01:17actually have the sorted version of the file and now we can do something like
01:20call unique on that file right.
01:23Before we saw the unique didn't work unless the results were sorted . Now we have
01:27the ability to do that.
01:28Now we could save this unique version if we wanted, right.
01:30Go ahead and try that your own. Save it to something called unique sorted_fruit.
01:35So any command that we typically get output back to the screen we can actually
01:39have be directed to a file. So for example let's say "Hello World" right.
01:43echo, and instead echo coming back to me let's put that in the file, right, hello_world.txt.
01:50It did it. We did do ls -lah, right. We saw that command before. Normally that
01:56returns our directory listing.
01:57Let's save that to our file, content.txt.
02:00Let's try history. We just saw how to work with history. Do recent history.txt.
02:06So you can see when I just do ls -la you can see that all these files have now
02:10been created and you could do a cat on any of them and you'd see the content
02:14and it's exactly what would've come your screen but now it's going to a file instead.
02:18Let me show you another one. We did the banner command earlier. Let's do banner
02:22"Hello World" and let's save that also to hello_world.txt.
02:28Now notice we already have an existing file called hello_world.txt, which just
02:33contains the text hello world.
02:35So what do you think what we'll get when we take a look at that file now? It's very long.
02:39Well let's scroll up to the top of it just to show you that it completely
02:42replaced what was there, right.
02:44It is destructive, so it does overwrite. So you're essentially saying make this output
02:51completely take over this file.
02:53All right, we're going to learn how to append to our file in the next movie, to
02:56add more content to it.
02:58What this does is replaces it, okay. So very important, make sure that you
03:02understand that otherwise you might accidentally target a file that you don't mean to.
03:06Let me just show you a classic use case for this which is, if we do, let's say
03:10we have two files here and I have a file called new file.txt.
03:15It just has a couple lines in it, and I have a another version of it called
03:18newer_file.txt, right. Different text to that. Notice the lines that are in each one.
03:24Cat you remember concatenates, right.
03:26So new_file.txt concatenated with newer_file.txt and let's put the output
03:32to joined.txt, okay. So there it is.
03:37It outputs those two concatenated together and directed the output to this
03:41file called joined.
03:42Now if we take a look at joined, you can see that we get both of those
03:45together in one file.
03:47And as I noted before, of course if we go up and we change the order of those,
03:51let's edit this so that we take new and newer and reverse them.
03:56Now if we do cat joined you'll see that it flipped the order of them and
04:01replaced the old one. The old one is completely gone. So again it overwrote that one destructively.
04:06If we instead want to append the file, to add to it, we need to do something else
04:10and that's what we'll see how to do in the next movie.
Collapse this transcript
Appending to a file
00:00In the last movie we saw how we can redirect outputs, so that instead of being
00:04output to standard out which is our screen,
00:06instead it's directed to a file.
00:08In the process of doing that we saw that it actually overwrites the contents of the file.
00:12So we completely destroy whatever was in the file by doing it that way.
00:16What we're going to see now is how we can append to the end of the file.
00:20So we are still directing the output to a file, but we're just doing it without
00:24destroying what was already there.
00:26Notice that I'm inside my user directory and inside Unix files, and if I do ls
00:30-la you'll see the files that are there now. A lot of those are files that we just
00:34created in the last movie.
00:35Let's begin by creating a list, nano people.txt. Let's just put a list of
00:41people's names in here, Lynda, Bob, Susan, Larry, and Anne.
00:48I'm going to hit Return. I want to show you something that nano does here. If I
00:51hit the down arrow you'll see it actually gives me one extra line.
00:54So there is an automatic sort of implied extra line there. I'm going to remove that, okay.
00:58So just make sure that we have only one line return after Anne then I'll hit
01:02Ctrl+X. Yes save the changes to people.txt.
01:06So let's take a look, people.txt, and there is the list of people.
01:10Now let's say we want to add Claire to the end of this list. Well we can do echo
01:15"Claire" and if we do it this way, we know what it will do.
01:18it'll completely erase our old list.
01:20What we want instead is to append it to the end of the list.
01:23It's very easy. We just use two of those greater than signs.
01:27Append it to the end of the list instead of getting rid of it.
01:30So now it echoes that line there. If we now say cat people.txt, now we see Claire
01:35at the end of the list.
01:36I want to add John to the list, same thing, John, and add it to people.txt.
01:42Now John is added to the list.
01:43That's it, that's all there is to it.
01:45Let's just try another example. We were working with a new file.txt and we were
01:51concatenating it before with newer_file and putting in a joined.
01:55I'm just going to put new file in there, so that replaces whatever was there.
01:59Now let's do it again, but this time let's do newer file and append it. Notice
02:06the difference between those two. I'm taking a different file and I'm appending
02:09it to the end what was there.
02:11That's the same thing as if I concatenated those together. And then just let's go
02:15ahead and do new_file in there one more time, okay.
02:18So you can probably guess what we'd expect to be in there. If we take a look at
02:22that file, we have new_file then newer_file, then new_file, okay.
02:27So it's really all there is to it. It's pretty easy as far as the concept goes.
02:30The hard part is just making sure that you have the discipline to remember to
02:34use the greater than sign, just one of them, when you want to completely replace
02:38the content and two of them when you want to append it.
Collapse this transcript
Directing input from a file
00:00Now that we've seen how to change the standard output to go to a file, I'd like
00:04to see how to direct input from a file that is instead of using our standard
00:08input which would be our keyboard, the things that we type into the command line,
00:12we are going to use input from a file.
00:15Notice that I'm inside my User directory and I'm inside unix_files.
00:19These files are in the Exercise Files as well.
00:21The one I'm going to be using to start with is this fruit file.
00:24You will remember what's in there.
00:25It's just a list of fruit, all right, a bunch of unordered fruit with
00:29duplicates in there as well.
00:30So what I'm going to do is I'm going to use the sort and we saw that we could do fruit.txt.
00:35Essentially what this is doing when we call that is providing as argument
00:40fruit.txt which the sort function then uses as its standard input.
00:45So it's already doing what we're talking about for us, but I want to show you
00:49the way that we can do it all the time, sort of universally for any command
00:53that takes input, even if the input is just from the keyboard. We can then use
00:57this less than sign.
00:59That's the opposite of what we used for output. It's facing the other direction,
01:02pointing to the left.
01:03What we are saying is take this fruit.txt file, use that as your input, and send
01:09it to the sort command.
01:11Use it as if we had actually typed those commands in there. And you will see that
01:15it gives us back through the results.
01:16Now those results are still coming to our screen. We are not redirecting those right now.
01:20We are focused just on directing the input.
01:22Let's try another one, wc < lorem_ipsum.txt. So there you see.
01:27Now it's using the contents of that and passing it into wc to count it.
01:32Notice that it doesn't show the file name anymore, which it did before when we
01:35called it the other way, right.
01:37When we call it this way, it actually uses that file name.
01:41So this is providing the file as an argument.
01:44This is providing that file as input that's being passed to this command.
01:49It is a subtle difference.
01:50Let me show you one way that maybe a little more obvious.
01:52Let's say that we echo, a mathematical expression, (3*4) + (11*37).
02:01Okay, so I've just got a simple expression and I'm going to output that to a file.
02:04I'll call it calculation.txt.
02:06We know how to redirect our output.
02:08Now I'm going to use bc, the calculator that we used in the last chapter.
02:12Before we used it from the command line, but instead I'm going to say hey,
02:16instead of using what I type, instead take the contents of this file and pass
02:21those into bc, and bc happily takes those and returns a result to us.
02:26So I think it's a pretty straightforward thing, but there are two important
02:28points that I want to make sure you understand.
02:30One is that we can both redirect the input and the output.
02:33So let's say for example, if I do sort < fruit.txt, the output right now is
02:39going to come to my screen.
02:40If instead I want it to go somewhere else, well then I need to direct it as well,
02:44sorted_fruit.txt.
02:47Okay, see what it's going to do?
02:50The input is the fruit.txt; the output is the sorted_fruit.txt.
02:55It's the same thing as if we actually had sort and we piped in fruit.txt)
03:03> sorted_fruit.txt.
03:05All right, that works exactly the same.
03:06It has the exact same results.
03:08The parentheses aren't necessary though because we always put the input first.
03:13Right, it's always the input, then the output.
03:16So I just wanted to see that you can use them together and also that the input
03:19always needs to come before the output.
03:21I also want to make another point here that the arguments to these always have to be files.
03:26Okay, it's very important.
03:27We're working with files when we talk about this.
03:30You can't for example do sort < cat fruit.txt, right.
03:36That's going to read the file?
03:37No. We want the file itself, not what we get when we read the file piped into it.
03:41It says no such file or directory is cat.
03:43It's expecting a file or a directory to be passed in there.
03:46Same thing if we say lorem_ipsum.txt piped into wc as output, right. This won't
03:56work either because in this case it expects it to have output and the file on
04:00its own doesn't have any output.
04:01We have to cat the file to have output and then we need to pipe it into a file.
04:07The receiver has to be a file.
04:08If I just do wc, not word count, that's actually a file called wc and it happily
04:13does it. ls -la and there we see we have a file called wc at the bottom.
04:17Okay, so I just want to make sure that you understand the thing going in using
04:20the less than sign needs to be a file and the thing that's coming out of the
04:24greater than sign needs to also be a file.
04:26All right, so let's go ahead and remove that.
04:28The last thing, let's just show you if we use unique case an example.
04:32Let's take our sorted_fruit. txt now and let's output it to
04:35unique_sorted_fruit.txt.
04:39Okay, so that works and it should have sorted it. cat unique_sorted_fruit.txt.
04:44So we went through several steps to get that sorted_fruit doing it this way, right.
04:48And we know that there is an option for sort that would let us do all in
04:52one step and make it unique. Certainly could use that.
04:54But what I am interested in really making point about is the way that we sort of
04:58use these files as intermediaries, right.
05:01We took a file, we sorted it, we saved it to a file.
05:04Then we take that sorted file, we passed that into unique, and we saved that
05:07off as another file.
05:08It would be great if instead we could take the output from one command and send
05:13it straight to another command without a file, right.
05:16We can't do that with these operators because these operators are expecting this
05:20and this to both be files.
05:22Instead we need another tool to do that.
05:24We call that the pipe.
05:25That's what we will look at in the next movie.
Collapse this transcript
Piping output to input
00:00Up until now we've been seeing how to direct input and output, but we've been
00:04doing it to files and from files.
00:07So we have to have these files as sort of the intermediary step between them.
00:11What would be great as we could actually take the output from a command and use that
00:15as the input into another command, without having to go to a file in between.
00:19It's something that you are going to use all the time in Unix.
00:22In fact you'll probably use it more often that you will either of those file redirects.
00:27To do it, we use something that we called the pipe.
00:29I'm just going to type one just so you see what it looks like.
00:31We hold down the Shift key and then the Backslash.
00:34That's the key that is above the Return key.
00:37You get this upright pipe.
00:38So that's what I refer to whenever you hear me say pipe and typically we say we
00:42pipe something into something, right?
00:45We use it as a verb as well.
00:47So if we have something like echo "Hello World", we know that would
00:51normally output to our Terminal, but if instead we use the pipe, we can redirect
00:56that into another Unix command.
00:59Don't use a file here.
01:01A file is used with those other operators.
01:03Here we can use another command.
01:04Let's just do wc as a simple one.
01:05So Word Count comes up and it reports to me
01:08there is one line, two words, 12 characters.
01:11You see what it did?
01:12So it took the output from one command, piped it into the other one, so that it
01:16received it as input and then that's what it uses.
01:19Let's take another example, let's say echo, let's put in our mathematical
01:22expression we used before, (3*4) + (11*37), and let's pipe that into bc, right.
01:32Before, remember I saved it to a file and then I took that file as input to bc.
01:37Now I'm just saying all right, take this directly and put it into bc, right.
01:41It gives me back a result.
01:42I piped this output from echo into bc.
01:46If we take our fruit file, remember I'm inside my unix_files directory, right
01:51here, ls -la. I have this file fruit.txt that we've worked with a few times.
01:55Let's cat that fruit file.
01:58It's just a list of fruit in an unsorted order and let's pipe that into sort.
02:03Notice before I was taking the file and sending it to sort. Now I'm actually
02:07outputting it, right. This is generating output and that output is what's being
02:11sit in their, okay. Ot's a subtle difference, but it is different and you see we
02:15get back this result.
02:16Let's not take that and let's pipe that through unique.
02:19So now I get the unique version.
02:21Now we've seen a couple of different ways that we can do this, that we can sort
02:24them and make them unique.
02:25Sort has an option that does it.
02:26We can take it from sort into a file and then a file back into unique.
02:30I wanted you to see what we're doing here.
02:32We're piping it along from one command to another, so we get a string of commands.
02:36The first one is catting the file, then we're sorting it, then we're doing
02:39unique and at the end of all that, we could actually output it. Let's say
02:43unique_sorted_fruit.txt.
02:45I already have this file, so it's going to overwrite it. There it goes.
02:48So now all of that went through all those commands and at the end of it then I
02:52directed the output to a file.
02:54See how that works?
02:55It's really, really useful.
02:56You could also do something similar to what we did before where we could say
02:58well, sort should get its input from fruit.txt, all right.
03:03We know what that's going to do, right. That does that and then we could take
03:06that and we could pipe that into unique.
03:09We know what that does, right, and then we could take that and we can send that
03:13out to unique_sorted_fruit, but that's a little less readable.
03:16If you just compare the two, I think it's a lot easier to understand
03:19what's happening here.
03:20We're reading the file, sending it to the sort, sending it to unique, then it
03:23ends up in the file.
03:24The other one, you have to jump back and forth to read a little bit.
03:27So, both of them accomplish the exact same thing. I just think that the second
03:31one is perhaps a little more readable.
03:33Another place where this is really useful is we have a long file like our
03:37lorem-ipsum file, right. It's very long.
03:39We can pipe it into less. There we go.
03:42Now we are in less.
03:43We can go page by page.
03:45Now of course that's the same thing. We could do less lorem-ipsum.txt and that
03:48gives us the same results. This takes a file as its argument, but for something
03:53like ps aux, now that doesn't take a file as an argument.
03:57That's the processor output that we saw.
03:59So we are seeing a list of all our processes piped through less.
04:03Guess what? Now we get pagination.
04:04We can go through them page by page and we can use forward and back to look at those.
04:08All right, there is a very common usage of it is to take something that we're
04:11working with and pipe it into less.
04:14We will be using this type a lot especially when we get to the chapter on the
04:17Unix Power Tools where we look at a lot of the advanced features.
04:19We will be able to use this pipe to great effect.
04:22So the main thing to remember with pipe though is that unlike the other
04:25operators, we're piping it into a command, not into a file, into a command and
04:30the output from it should be the output from a command, not from a file.
04:35Even here this is actually output of a command, not a file.
Collapse this transcript
Suppressing output
00:00Now that we've talked about the basics of redirecting input and output,
00:03there is one last redirection technique that you should know about.
00:07Instead of redirecting output to a file or into a command, what if we want to
00:10have no output at all?
00:12This is especially useful when you become advanced enough that you're able to
00:15setup scripts on a server that run automatically at 4 a.m. even when there's not
00:19anyone logged in to view the output.
00:21At that point you don't need the output, right. You just want it to just do this
00:25thing and the output should just go away, and there may also be occasions where
00:29you want to run a program or you just don't want to see the output. You just
00:32want the program to do its thing and get its results.
00:35Well we could just put the output to a file and then throw away the file, but
00:39there is an even easier way to do it. We can redirect the output to a special file
00:43that referred to as the null device.
00:46It's also sometimes called the bit bucket or the black hole, and it lives at /dev/null.
00:49Do you remember I told you that there were the special files for input and
00:53output that were represented at dev /standard in and dev/standard out?
00:57Well, dev/null is another one that's in that same folder.
01:00It's a special device where anything that's directed there Unix just discards.
01:05It just like a black hole. We just send data there and Unix says "Oh, yeah okay,
01:09never mind." It's gone.
01:11It's not kept in memory.
01:12It's not kept in the file.
01:13It just disappears.
01:14And the way you send things to dev/null is the same way that you send them to any file.
01:19So for example let's just take a simple listing right, ls -la. Let's send that
01:23to dev/null, always at the root, all right,. Always we have to make sure we
01:28specify that so it knows the absolute path dev/null. We get no output.
01:33cat lorem_ipsum, let's send that to dev/ null. All right, there is nothing there.
01:38If we want to bring in nothing, let's bring in nothing from dev/null, all right.
01:43It echoes back nothing to us.
01:46So it works just like a file.
01:48It's just like a special file, but it has this unique property of always staying empty.
01:53Now in truth you may not have need for this very often but it is an important
01:57concept I think for you to know, and so I want you to recognize it whenever you
02:00come across it in your future Unix endeavors, so that you know how you can have
02:04something that has absolutely no output.
02:06The way you suppress output is you direct the output to dev/null.
Collapse this transcript
7. Configuring Your Working Environment
Profile, login, and resource files
00:01In this chapter, we're going to take a look at how we can configure our working
00:04environment in Unix, or put another way, how can you customize bash to your
00:07personal preferences?
00:08Well, the way we'll do that is providing commands inside bash resource files.
00:13Let's take a look at those files and understand how bash works with them.
00:16First, let's talk about the initialization that bash goes through whenever we first log in.
00:21Now on a Mac, logging into bash is the same thing as opening up a Terminal window.
00:26You open up a new Terminal window and it logs us into the bash shell, right?
00:30It auto logs you in as the user that you are in the Finder.
00:33Well, when it does that, the first thing that bash does upon login is it reads
00:36any commands that are inside the directory etc inside the file profile.
00:41Those are master default commands that every bash user will get.
00:45We don't want to mess with those. Leave those alone.
00:47Those are default settings.
00:48Let Apple take care of managing those for you, all right.
00:51That's the defaults all users get.
00:53Instead, we want to take care of just personal customizations that apply only to us.
00:57So because these are our personal customizations, they are going to live
01:00inside our user directory.
01:01So notice that I've got four file paths there.
01:04Each one begins with that tilde indicate that it's in the user directory.
01:07Also notice that each of them begins with a period.
01:10These are dot files.
01:11They are not visible in the Finder and the dot is to indicate that it's really
01:14a configuration file.
01:16So we have four choices.
01:17We have .bash_profile, .bash_login, .profile, and .login, and what bash does
01:21is after finishes reading that main profile, it goes to our user directory and
01:25it sees which one of these four files it can find in the order that I have them listed.
01:30When it finds one, it reads the commands that are in there and then it
01:33ignores the rest of those.
01:35Okay, so it just goes finds one and only executes one file, so there is
01:39no point in having both .bash_profile and .bash_login.
01:43Any commands that are in .bash _login will just get ignored.
01:45So I'm going to recommend that we put everything into .bash_profile.
01:49.bash_profile and .bash_login are really specific for bash. .profile and .login
01:53are there for historical and backwards compatibility reasons and they also
01:57would potentially apply to working in other shells as well.
02:00But the things that we put in these configuration files are often very shell specific.
02:05So the things we put in there, the syntax that we use, might only apply to bash.
02:09So if we use other shells, we're really better off using their profiles instead.
02:13So let's put everything inside .bash_profile.
02:16Now that only covers when we login to a new shell by opening a new Terminal window.
02:20If from the command line we type the word bash, it opens up a new bash shell,
02:26but it's not a login shell.
02:28There is a distinction between that.
02:30The login shell is the first time that we come to it when we open up that
02:33Terminal window. Typing bash on the command line opens up a sub-shell, and
02:37whenever we start a new bash sub-shell then it executes the commands that
02:41are inside .bashrc.
02:42RC is to indicate resource.
02:46So here is the thing.
02:47Anything that we put in .bash_profile doesn't become available to us in the sub-shell.
02:53Anything we put into .bashrc isn't available to us until we go into a sub-shell.
02:58So we're going to want to have a way so that we can have one set of
03:00commands that are available to us in both cases, right, because we want the
03:03same configuration regardless of whether in this master shell or whether
03:07inside a sub-shell.
03:08Now you may not be in a sub-shell very often, but it's still going to be a good
03:12idea to set it up the way that I'm going to show you.
03:15Before we do that, let me also just mention that there is one last file that
03:18is the logout file.
03:20Whenever we log out, it will run any commands that are inside .bash_logout.
03:26Okay, so those are going to be the logout files.
03:28If we want to perform specific actions as we're logging out, we can do them here.
03:32Now as I said, this is the load order for the bash shell, which is what we're working in.
03:36If you want to work in other shells, you can go to Wikipedia.
03:39They have a nice explanation of the load order for all those different shells.
03:42So you'll be able to figure out exactly where you have to put your
03:45configurations for those shells.
03:46Okay, so now how do we handle this problem between the login shell and the sub-shell?
03:50Well, it's a really easy solution.
03:52What we'll have to do is just add a bit of code to our .bash_profile that says
03:56if you can find this .bashrc file, well, then load everything that's in it.
04:01This is really the only bash scripting that we're going to do.
04:03But I'm going to just give it to you, Say if the file exists,--that's what's
04:07inside those square brackets is a shortcut for whether the file exists or not.
04:11Then read in the .bashrc file. That's it.
04:15That source is just saying all right.
04:16Let's execute whatever is in there.
04:18So we'll put this in our .bash_profile and now all of our commands and all our
04:22configuration can go in .bashrc.
04:24Whenever we log in, it will run that .bashrc file.
04:27Whenever we switch to a new shell, it will run that .bashrc file.
04:31Okay, it's a very simple solution that allows us to have the same configuration
04:35applied to both cases. Let's try it out.
04:38So here I am in my main user directory. Make sure you're in the same place. ls-la.
04:42You'll see that I have my bash history file, but I don't have any of those other
04:47configuration files that we just talked about.
04:50So the first thing let's do, let's create our .bashrc file. All right!
04:54We can just do that with touch.bashrc.
04:57Notice that there is no underscore and its just bashrc.
04:59Now we have that file.
05:01It is empty, but the file exists.
05:03Now let's create our .bash_profile.
05:04nano .bash_profile.
05:08There it is and inside nano what we're going to do is just paste in that code
05:12that I just gave you.
05:13So pause the movie if you want to copy it down. Make sure that you get the punctuation
05:16and everything correct.
05:18Notice also that this if statement ends with fi.
05:21That's how we close an if statement, right?
05:23So it starts with if and it ends with fi.
05:26So make sure that you got that right.
05:28That's not a mistake.
05:29Then when you're done, we're going to save that file and we'll save it to .bash_profile.
05:34Now we can use that trick that we learned in the history section just to take
05:37a look at the file, see what's in there, and we can also do the same thing for .bashrc.
05:41You'll see that there is nothing inside that file. All right!
05:45But both of them exist.
05:46Now we see both of them there.
05:47Okay, so we're set up now.
05:49Now we've set things up so that the .bashrc file is our master file.
05:53Now that doesn't mean we can't still put things in the .bash_profile. Let's try this.
05:57.bash_profile and before I load in this .bashrc file, I'm just going to up above
06:05it paste in some more commands. All right! TO HERE
06:07Now you can put whatever you want here.
06:08These are all things we've covered before.
06:10I've got echo in a new line.
06:12then echo without making a new line.
06:14Welcome to Unix on Mac OS X. and then a semicolon for a new command, whoami,
06:20so that I put my name.
06:21and then another new line.
06:23and then another blank line followed by Today is the date and with the date,
06:27I've provided a format that's just string from Time formats, you can look
06:31those up if you need reference for what all the symbols mean, we'll see it in just a second.
06:35And then I've got an echo.
06:36and then I'm using Cal to display a calendar that will show the current month. and then Uptime.
06:41All right!
06:42So, all basic things that we've learned before, Ctrl+X, and yes, all
06:46right, there we are.
06:47Now let's quit this window and let's open up a new one, all right?
06:50So I'm going to just close this window.
06:52I'll click up here in the red dot, and now Command +N will open up a New window. Look what it did.
06:59It came up and gave me all those commands upon login.
07:02If I type bash, I don't get them. All right!
07:05I am in a new shell, now but I'm not in my login shell.
07:08They are only executed one time.
07:09Let's just see the difference.
07:11Let's go into .bashrc and let's put in just the last part, that Uptime, right?
07:17So whenever we login to a new shell, we'll just report the Uptime to us followed
07:21by a blank line and then keep going with whatever it was doing. All right!
07:23So let's exit out of that. Let's now go bash.
07:27All right! There it is.
07:29Now executes what's in that .bashrc file.
07:31Let's go back and exit out of this again.
07:33Let's close this window.
07:35Let's open up a new one now.
07:37It says yes, we do want to close it, and here we go.
07:41Notice now it gives me the Uptime twice.
07:43We know why that is.
07:45We do nano .bash_profile.
07:49Now you can see that it's doing the Uptime here and then it's doing it again
07:53when it reads in the file here.
07:54Okay, so that should help you understand how these two things are working.
07:58Incidentally, in these configuration files a Pound sign indicates a comment. All right!
08:03So that comments it out.
08:05I can make all sort of comments here, and I can say for example, at the top of
08:08this, I put a comment that says, This only runs on user login, and then I can
08:18say, I'll just actually delete these lines.
08:20Let's go ahead and just remove those and say, This loads in the configuration in
08:30.bashrc, put all configuration there.
08:36So that's how we can do comments by just using those Pound signs or hash.
08:40So let's Ctrl+X out of there, and now we can save that file.
08:44Once again, we'll close that window and open a new one and now you can see only
08:47reports Uptime to us.
08:48One time, I type bash, we get a new shell.
08:51It just does the Uptime for us again.
08:53Okay, now you can put whatever you want in there.
08:55I'm not trying to tell you have to it exactly my way.
08:57I just want to give you some ideas on what's possible, and mostly to show you
09:01which files to put your things in and the sort of interplay that happens
09:05between those two files.
09:06Now that we understand where we should locate our changes, let's look at what
09:09other configurations are possible.
Collapse this transcript
Setting command aliases
00:00In this movie, we'll see how we can use command aliases to help us to work more
00:04efficiently in Unix.
00:05Now, I want to make sure that you understand that the aliases that we're talking
00:07about here are not the same thing as the Finder aliases, right?
00:10Remember that we can click on a file or folder, we can go to the File menu,
00:15we can pull down and select Make Alias.
00:17That's a Finder alias for files and folders.
00:19That's different than what we're doing here.
00:20What we're doing here is command aliases.
00:23Making the same kinds of shortcuts, but shortcuts that will execute
00:26different commands in bash.
00:27The first command that we should learn is just simply alias by itself.
00:31This will return a list of all the currently defined aliases.
00:34So right now I have none.
00:35You probably don't either.
00:36So to create our first one we just use the alias command again and then the
00:41shortcut that we want to use, essentially the alias name.
00:44So the first alias I'm going to create, I'm going to use ll.
00:45I'm going to say ll= and then in single quotes I'll put the command that I want it to equal.
00:52'ls -la'.
00:53So what I'm saying now essentially is, whenever I type ll on the command line
00:57I'd like bash to execute ls -la for me.
01:02You can see the savings that that offers. ll Return.
01:04There it does it.
01:05Now, instead of typing six characters, and I often mistype those six characters,
01:10instead now I can just type a very quick ll and I get that list back.
01:14ll is a very, very common one.
01:16In fact, a lot of Unix machines are preconfigured to offer you that alias.
01:19Now, we can create aliases with absolutely any name that we want to perform
01:23any task that we want.
01:24You just want to be careful that you don't use an alias name that is for an
01:28existing Unix command.
01:30We don't want to overwrite that.
01:31So mv obviously is a move command.
01:35We don't want to create an alias for mv that would do something else, right?
01:38Because then suddenly mv would stop working for us.
01:41So you can use the man pages or the commands like which that we saw earlier to
01:45see what the commands are, to make sure that you're not accidentally
01:47overwriting something.
01:48Let's create another one now.
01:49Let's do alias hello= and I'm going to have it echo "Hello World!"
01:57I wanted to show you this example partly to show you that you can use aliases
01:59for absolutely anything, but I also wanted to show you the pairings of quotes.
02:03It really doesn't matter whether we use single quotes or double quotes on the
02:06inside or the outside. The important part is the pairing of these.
02:09We want to make sure that the alias command can tell what the entire string that
02:14it's aliasing ought to be.
02:15And that's what those single quotes indicate.
02:17If we wanted to use double quotes in all cases, we would just need to escape that
02:20string by using the backslash in front of them.
02:22So I just want to make sure that I point that out to you.
02:24So now we have that.
02:25Now, if we type alias, you'll see that we get the list of aliases that includes
02:29hello and typing hello does exactly as advertised.
02:32It echoes back Hello World!
02:34If we want to remove an alias, then we just type unalias.
02:37unalias hello, and now it's gone.
02:40alias and hello, command not found.
02:44So in addition to making sure that you're not overwriting a real Unix command,
02:47here is the catch about aliases though.
02:49Aliases only last for the current login.
02:52So ll, it's a valid command.
02:54Close the window and open a new one, just clear the screen. ll. Nope, command not found.
03:00So we typed it in the command line.
03:02It made an alias and that stuck around only for the current login.
03:05What we want is for those aliases to be defined for us every time that we log in to Bash.
03:11So where do we put commands that we want to run every single time?
03:14Well, we do those is our configuration files, either in .bash_profile or
03:18even better in .bashrc.
03:20So we'll write our alias commands in there.
03:23Whenever we log in to bash, whenever we open a new shell, what will happen
03:27is bash will read those commands, define those aliases for us, and they'll
03:31be available to us.
03:32So let's do that. .bashrc, and we'll just drop-down here and we'll just call the
03:37alias command just like we did before.
03:38Remember we did the cal command, for example, before.
03:41It's no big deal to write our normal shell commands here.
03:45la, and I'm going to go ahead and add the h and g options to that.
03:48Those are h for humanize and g will just help to colorize the output.
03:52Now, as I said, you can do all sorts of variations on this.
03:54You might have something that is la, that does ls -a, or you might have lh that does ls -h.
04:02Any combination you want. You can come up with whatever suits your personal taste.
04:06I'm going to just show you some more that are common that a lot of people like to use.
04:10We have cd into our home directory as just simply home.
04:14That will go to our user home.
04:16Alias up, a lot of people like this. Just cd to go into the parent directory.
04:20That will go to up one.
04:21And alias h, history, so that will just simply show your history.
04:27Now, again, you don't have to use these, but these are just ones that I
04:29frequently see other people use.
04:32Another nice use of aliases is to actually redefine some of the Unix
04:36commands but with options.
04:38Let me show you what I mean.
04:39We say we wouldn't want to overwrite something like mv, move, because we need that,
04:43but we can overwrite it with options.
04:45So whenever someone types mv, well, it's actually the same thing as saying mv
04:49with the interactive option.
04:51Same thing for copy and remove, and basically that interactive will just pop-up
04:54and tell us before it actually overwrites a file or deletes a file.
04:57So that's a nice way to make mv, cp, and rm sort of idiot proof. They will
05:01always prompt you and say, are you sure you want to do this before I destroy it?
05:04We can also on the df and du commands, we can provide the -h for humanize, and
05:10for making a directory we can provide the -p option and that will then make
05:14sure that we create any parent directories that we need.
05:16Now, if you don't want to use any of these, you can simply comment them out with
05:20that hash or pound sign.
05:22Another common thing that a lot of Unix users like to do is sort of correct
05:26their own typos ahead of time.
05:27So if you find yourself that you frequently type pdw instead of pwd, well then
05:31you can make an alias.
05:33No sense in wasting time correcting your typo.
05:35If you find you do it often with sort of bumble fingers, well then, fix it.
05:38Make pdw equal to pwd, and you'll know what those frequent misspellings are, right,
05:42because they're going to be different for every person, but I just wanted to
05:44show you that's an example of the kind of things you can use aliases for.
05:47So now that we have all these in our .bashrc file, let's try it out.
05:50Ctrl+X. We'll save it into our .bashrc.
05:54Now let's try one of those out.
05:55Let's try that pdw that I just created. Oh, command not found.
05:59Now, why is that?
06:01Because it hasn't read in that .bashrc file yet, right?
06:04We've got to close this window and then it will open it. Otherwise we could also
06:08type source .bashrc and that would do the same thing.
06:12That reads in that bash file, now our pdw alias works.
06:15So again, either close that window or run source on that file so that it will
06:20actually read in the contents of that file to make those aliases available.
06:23But now even when I close my window and open a new one, now I can do ll and I
06:27still get my directory listing and I get all these items humanized and now my
06:31entries show up in color.
06:32So I think aliases become a really indispensable tool in working with Unix.
06:37For me personally, I've developed a sort of mini library of aliases that I like
06:41to use to help me do things faster and I take those from machine to machine.
06:45When I move to a new Unix environment, I want to have my aliases available so
06:49I can do things like type ll and get those same results that I'm used to working with.
06:53So I encourage you to experiment with aliases and really try and build up some
06:57aliases that work well for you.
Collapse this transcript
Setting and exporting environment variables
00:01In this movie we're going to learn more about environment variables which are
00:04also known as shell variables.
00:05We're going to do that so that we can begin to use environment variables to
00:08configure our working environment.
00:11We've touched on shell variables already.
00:13For example, we saw that we can have echo $SHELL and that will return the
00:17default login shell for the current user.
00:20The shell variable in this case is called SHELL and it's in all capitals.
00:24The dollar sign is actually not part of the shell variable name.
00:26The dollar sign is there to serve as an indication to Unix that we want it to
00:30return the value that's stored in the shell variable.
00:33We can also define our own shell variables as well.
00:35For example, I can have MYNAME= and then inside quotes put your name.
00:41Then I could echo that back the same way.
00:44echo $MYNAME, and it returns the value that we've set inside that variable.
00:48Notice that I did not use the dollar sign when I set the value of MYNAME. I only
00:52use it when I wanted to retrieve the value.
00:55Just like the command aliases that we were creating in the last movie,
00:57variables that we set during the current bash session will disappear once we
01:01log out of that session.
01:03And like aliases if we want those to be defined all the time, well they need
01:06to be stored in either our .bash_profile or our .bashrc file.
01:11As I said before, I think the .bashrc file is the better choice.
01:15So let's put those in there.
01:16Notice I am inside my main user directory. Open up that file.
01:21You can put it really anywhere in here.
01:22I am going to put it right here near the top.
01:24So I'll just say MYNAME= and then I'll put in Kevin Skoglund.
01:30Now we'll exit. I'll save the file.
01:32Now if we first just try echo $MYNAME, we still have it saved from before.
01:38To really test this, what we need to do is actually close this window and open a
01:42new one and then we can try it again. echo $MYNAME, and there it is.
01:47Now you can see that it's set all the time.
01:48Every time we open up a bash session, this variable will be defined for us.
01:53So that takes care making it available the bash.
01:55But there's one more thing that we need to do.
01:57As I've shown it here, MYNAME won't be available to the child processes
02:02that bash starts for us.
02:04It'll only be available in bash itself.
02:06To indicate to bash it should also pass along these variables or export them to
02:12other commands, programs, and scripts,
02:14well then we need to use the export command.
02:16Let's go up to nano .bashrc again, and then now we can just say export MYNAME.
02:26And now bash knows that it also should export it to all of the programs that it runs as well.
02:32So every time it launches a program, it'll make this MYNAME variable available as well.
02:36Now you can see here I did it in two lines.
02:38I set it in one line and then I exported it in the other.
02:40You can actually do both in the same line, and I'll show you that in just a moment.
02:44Now while it is moderately useful to define your own variables, it's even more
02:48useful to use variables to set some configuration options for our Unix
02:51environment and the programs that we use.
02:53For example, if you read the man pages for the command less, you'll learn that
02:57there's an environment variable that can be used to configure the default
03:00settings that less will use when it starts up.
03:02So to do that we'll just use export LESS= and then inside quotes we'll put
03:09whatever options we want LESS to use.
03:12Notice first of all that I also did the exporting and the setting all on one line.
03:16Before I did it on two lines.
03:17Here I am doing it in one.
03:18export LESS= and then we can use options.
03:21So we could use the m, the lowercase m. That will give us a medium verbose
03:26prompt every time we start up LESS or we could use capital M. That would be the
03:29maximum verbose prompt, right, the longest prompt possible.
03:33We can put capital N. That would give us some line numbers.
03:37You can go through the man pages for LESS and decide what options you want.
03:40I am just going to use the -M option here.
03:42So I am going to use Ctrl+X to exit, and I am already exporting it so that
03:47means it will be available not just in bash but inside LESS, because LESS is a
03:51program that bash starts.
03:53If you want you can play with it.
03:54You can try taking away the export and you'll see that it doesn't get passed along.
03:58Now if we just do right here echo $LESS, we don't have a value.
04:02That's because bashrc has not been run since we made that change.
04:06To do that we either need to close the window and reopen it, or we can
04:09call source .bashrc.
04:12That will actually execute the program.
04:15And now if we go up and say echo $LESS again, you see that value is there.
04:19Let's test it out. Let's actually call less on inside of our unix_files. We have
04:24that lorem_ipsum.txt file.
04:26Let's call it on that.
04:28And you'll see now I get that maximum verbose prompt at the bottom.
04:32So that's the default option being used every time I call less.
04:36So this is the way that you do it.
04:37You can interact with different programs that you might run by setting these
04:40values, but make sure that you export them so that they're used not just in
04:43bash, but actually pushed out to the program that gets launched as well and
04:48made available there.
04:49In the next couple of movies I want to show you some more useful environment
04:51variables that we can set.
Collapse this transcript
Setting the PATH variable
00:01We first took a look at the path variable back in Chapter 5.
00:03You can jump back there if you need a quick refresher.
00:05Path is a colon delimited list of file paths that Unix uses when it's trying to
00:10locate a command that you want it to run.
00:12And we can take a look at that with echo $PATH.
00:15There you see each of the paths that it's going to check in.
00:18For example, /usr/bin and then a colon followed by the next path /bin and then a
00:23colon and so on down the line.
00:24And Unix will look in each of those directories, trying to locate a command in that order.
00:28So for example, if we type less unix_ files/lorem_ipsum.txt, then the process
00:34that Unix went through when trying to run the LESS command was to first look
00:38for it in this directory and then if it didn't find it, to look for it in this
00:41directory, and so on all the way down the line.
00:44In this movie I want to still learn to modify our path variable using what we
00:47just learned in the last movie about setting environment variables.
00:51To begin with, let's just do it here from the command line. PATH= and we'll
00:55just use an empty string.
00:56So now we're telling it don't look in any directories when trying to find these commands.
01:00We're essentially unsetting the path.
01:03Now let's try that same command again. less unix_files/lorem_ipsum.txt.
01:07It comes and it says "Oops! less?
01:10I've never heard of it. I can't find it."
01:12Now less is still a functioning program.
01:13We didn't disable less.
01:15What we did was we took away Unix's ability to find the command less.
01:20We can still run it if we did /usr/bin less and then unix_files/lorem_ipsum.txt,
01:28and now it still works.
01:29We just had to go ahead and specify the full path to find the command instead of
01:34having it built into our path that Unix would automatically use to look for it.
01:39So you see what the path does and you also see how you can screw yourself up if
01:43you get your path wrong.
01:45Now luckily all we have to do is reset this environment variable back to this
01:48default string and we will be all set, or as we saw in the last movie we can
01:52actually just close our window and reopen a new one, and now echo $PATH will
01:58return the original string again, because it wasn't saved between bash sessions.
02:03Now, of course most of the time we probably do want our path variable setting to
02:07stick around, so we're going to want to put this in our bashrc file.
02:10nano .bashrc, and then in here we can do export PATH= and then whatever we want
02:17to set that value to.
02:19Obviously, we don't want to unset it, so instead let's just type in the current
02:23setting and we can use that as a starting point for making modifications.
02:27Now what kind of modifications might you want to make to the path?
02:29Well, you might want to take something out of the path.
02:32You might say, "Oh you know what?
02:33I don't want it to look in this directory anymore.
02:35I want to just take that out of the equation."
02:37So you can make that change if you wanted.
02:40More often what we want to do is we want to control the order in which the path happens.
02:44So for example, a very common one is to say all right, let's take usr/local/bin.
02:47I'll actually copy that and use Option to shoot to the beginning of the line
02:53here, and we'll want to put that at the beginning. So now it'll look for
02:57usr/local/bin before it starts looking in those other directories.
03:01That gives me, the user, the opportunity to put a file in there. Maybe I want to
03:06run my own version of less, right?
03:08So I put my own version in usr/local/ bin and it'll get to my version before it
03:12gets to the version that's inside usr/bin.
03:15Now we could do it this way by just declaring a new string for the path, but a
03:18more common way to do it is just to amend the existing variable by adding to it.
03:24Let me show you how we do that.
03:25Let me just use Option and I'll shoot down here to the end.
03:28Let me just erase a bit. There we are.
03:30And let's just take all of this away, all the way back to here, and then let's use $PATH.
03:40So what I am doing is I am calling the existing value of path, which is that
03:44string we just had, and I am going to use that inside my new string but put my
03:48new path at the beginning, and set that whole thing then equal to my new version of path.
03:53It's no problem to use path both here and here.
03:55However, what is a problem is the single quotes.
03:59We have to use double quotes whenever we want it to pull in the value of this
04:02dollar sign variable.
04:04If we just use single quotes then it will take it as a literal string.
04:07it'll think we want it to look inside a directory called $PATH, which doesn't exist.
04:13What we want is we've to grab that variable, so the double quotes are very important.
04:16So we can also put our new code at the end, if we wanted it to look in
04:20usr/local/bin, after its normal places. We can put it at the end.
04:25But I want to try and get in front of it.
04:27I want to say all right, I am not trying to add to the path to the additional
04:30places that you might check.
04:31I am trying to supersede whatever is in your additional path.
04:34A very common setting to use and one that I use in a lot of my trainings is usr/local/bin:
04:38followed by usr/local/ and then sbin and then colon and then if you have MySQL
04:47installed, usr/local/mysql/bin.
04:50So that's only if you have MySQL installed
04:52you want to do that last one but what I am saying is all right, basically
04:55checking my three main local directories where I might have put things.
04:59And then if you can't find something there, then go look in all the additional
05:02places where you might normally look for commands.
05:04Now you may realize that actually usr/ local/bin is defined inside PATH, right?
05:12So it's actually going to be in the string twice.
05:14That's not a big deal.
05:15Don't worry about that.
05:16It's really not a major concern that it puts both of those in the path.
05:19If you really cared about it, you could put the full string or edit it out somehow.
05:23But it's perfectly acceptable to do it this way and just have it in the path twice.
05:27Let's use Ctrl+X to exit.
05:28we'll save the changes to our bashrc file.
05:31If we just do echo $PATH now, we won't have that value because we have to run
05:37the bashrc file again or we have to close our window and open a new one.
05:41Now we try echo $PATH and now you can see that it made my changes at the beginning.
05:45So here's my directories followed by the regular path.
05:50Now at this beginner level it might not be really obvious to you why you
05:53would want to do this.
05:54But once you start actually working in Unix a lot and you start having your own
05:57programs that you start putting in these folders, you'll understand that it's an
06:00essential thing to be able to control this path variable.
06:03Probably the most important environment variable that you can set.
06:06So it's important that you understand how it works and how to set it.
Collapse this transcript
Configuring history with variables
00:01Environment variables can be very helpful in allowing us to customize how
00:04the Unix history works.
00:05There are five different variables that we can configure.
00:08Before we start looking at them, let's first start by just using history -c to
00:13clear out our existing history so that everything we work with we'll be able to
00:16easily identify what's new and what's been added to the history, and then let's
00:20do nano .bashrc and right below where we set our path I am just going to paste
00:26in the lines for the history and then we can talk to them and that way you don't
00:30have to sit and watch me type.
00:32So notice that on each of them I'm exporting it and here is the variable name.
00:36HISTSIZE, HIST being short for history, HISTSIZE= and then a number.
00:41This is the number of commands that history will remember.
00:44By default it will remember 500.
00:45If you want more or less, well this is how you change it. You set the variable here.
00:50Once we get to that maximum number then the oldest command will drop off the top
00:55and it will still just add the newest command at the bottom.
00:57So essentially what we are looking at would be the 500 most recent commands.
01:02In my case the 10,000 most recent commands.
01:05I have given a very large number so it just keeps growing and growing and
01:08growing until I manually clear it.
01:09We can also set the maximum size that the history file has allowed to
01:12become, using HISTFILESIZE.
01:15Here I've said that it can be one million k that's a very large number.
01:18But you could restrict it to something smaller if you wanted.
01:21If you said I never want that file to get larger than this certain amount,
01:24once it gets to that amount well then it will truncate the oldest entries in
01:28the file and leave the newest ones, so that it gets back under that file limit again.
01:33The next one is HISTTIME format.
01:36What this one does is it provides a timestamp next to each of your history
01:40entries, letting you know when they were added.
01:42It does that by using a string here and a set of special format codes.
01:46These are known as the strftime format.
01:51I call that string from time.
01:52Other people may pronounce it as string f time.
01:54I have given you a reasonable example here, but you can also go out on the Internet
01:58and look up these formatting codes, if you want to use something different.
02:02The next one is HISTCONTROL and you can see the value for that is ignoreboth.
02:06Well what does that mean ignoreboth?
02:08Where there are actually three values that we could put there.
02:11We could put ignoredups, ignorespace or ignoreboth and I'm telling it I wanted
02:16to do both, both dups and space.
02:18ignoredups means that we don't want history to record the same line multiple times.
02:23Try to ignore the duplicates when you can.
02:25ignorespace tells history not to record any line that begins with a space.
02:31Why would we need that?
02:32Well it's a little trick that a lot of people like to use.
02:35Let's say that there is a single Unix command that you're about to enter, but it
02:38has something in it like a password that you don't want to be saved in your
02:41history where someone might see the password later.
02:44Well if you just start the line with a space and then type the command, Unix
02:48will still interpret the command as normal but history will ignore it and it
02:52won't show up in your history file.
02:54So it's a very convenient way just be able to hide a single line from your history.
02:57We also can ignore certain commands all the time and we do that with HISTIGNORE.
03:03Here we provide a colon-separated list of all the commands that we want history
03:07to ignore all the time.
03:08For example history. I don't need to record in my history the fact that I typed history.
03:14Another common one is PWD.
03:16Every time I've checked to see what directory I am in I don't need to have that
03:20in my history, so I can leave that out.
03:22You typically want to leave the things to get ignored to be a very simple list.
03:25Either put nothing in there at all or keep it very very simple.
03:28I've put mostly here informational things.
03:31Things that are just sort of I might check you know just to see where I am at,
03:34to see what's going on.
03:36I don't want to record those in my histories.
03:37But things like copying and moving and stuff, I want to have those in my history
03:41because I may want to go back and use them again or edit them or to see the fact
03:45that I took those actions.
03:46All right remember that you can pause the movie to copy all of this down.
03:49Once you've get it in there, let's use Ctrl+X to exit, save the changes, and
03:55let's also then run, using source .bashrc will read in those changes as well.
04:01So now all those variables have been defined and let's just type a few
04:04different commands.
04:05Let's try echo "Hello" and let's try ls.
04:10Let's try echo "Hello" again and one more time and let's just space echo, we'll do
04:17'secret', let's say we have a secret password in there, ls -la, and last of all pwd.
04:24Okay, now that we have done all those, let's clear the screen. Let's type
04:27history and let's see what's in there.
04:30My nano that I did to edit the bashrc, the source where I ran those changes, and
04:34the first echo made it in there and that's it.
04:38That's because ls, ls -a, pwd and history were all hidden those are all things
04:44that I told them that they should ignore those commands.
04:47The duplicates were kept out, right, so I only saw echo "Hello" one time and
04:52also the echo 'secret' that I did with a space at the beginning got left out as well.
04:55So you can see how we are able to leave those out.
04:57You also can see that now I have this time format here at the beginning, telling
05:01me when each of these different commands was entered. I have a timestamp there.
05:04As far as the history size and history file size, you'll just have to sort of
05:08take my word for it or you can set those to really low numbers so that you can
05:12actually see how those work as well.
05:14Now I want to show you want other thing though.
05:15Let's try ls -al, right.
05:19I added ls -la in that ignore list, but now let's type our history and notice
05:25that it did get added there.
05:27So the things that we type under HISTIGNORE have to be exact, even the options.
05:32For example if I do history, pipe it through tail -8, now I see the last 8
05:38commands that were run including this history command that I just ran, right.
05:42So history by itself doesn't get added, but history piped through this other
05:46thing does get added.
05:48So again all of these changes are really just about configuring your working
05:52environment to be the way that you like it to be.
05:54We have now seen a few of the most useful configurations that you can make with
05:57the environment variables, but beyond these I think it's more important for you
06:00to understand the overall concept of how you go about setting environment
06:04variables and making them available every time that you log in, by putting them
06:08in your bashrc file.
06:09Other Unix programs you encounter in the future are also going to make use of
06:13shell variables and when they do, now you know how to set them.
Collapse this transcript
Customizing the command prompt
00:00In this movie we're going to learn how to customize the Unix command prompt.
00:04And we talked about long time ago the prompt is this bit of text right here that
00:08occurs before every line, right, to prompt you to enter something.
00:10I don't know why, but it's very, very satisfying to customize that to
00:14something that you like.
00:15Maybe it's because it's the part of Unix that you see most often, but it's
00:19really useful to be able to customize that and make it look exactly the way that you want.
00:22The way that we're going to do that is with a shell variable. And we saw shell
00:25variables in the last movie.
00:27The shell variable we want here is PS1.
00:30That's for the main command prompt.
00:32There's also a PS2, 3 and 4.
00:34those are for other prompts that occur in other contexts and we don't need
00:37to worry about those.
00:38PS1 is the one we want.
00:39PS1= and then we can make it equal to whatever we want, so -->.
00:45It's always a good idea to put a space after it just so that it separates the
00:48prompt from what you're about to type.
00:50Notice the space that it gave me.
00:52We could try it without the space and see the difference.
00:54See now when I type it's right up against that.
00:57So if you like that, you certainly can do it.
00:58I prefer to have the space there. So that's it.
01:01You can see that it took effect right- away but like the shell variables that
01:04we were setting before, if we close this window and log out, it won't be set anymore.
01:09In order to have shell variables set so that they're there all the time, we need
01:12to put those in either our .bash_profile or our .bashrc file.
01:16If we define them there, then as soon as we log in, then our prompt will be
01:20defined the way we want it.
01:21So we're going to do that.
01:22We're going to move it over there, but this is a great way to play with it.
01:24Just play with it here, get it to something that you like, and then we'll move
01:28over to .bashrc and we'll actually plug it in there permanently.
01:31So let's try a few more.
01:32You can have it be whatever you want.
01:33You're going to be What now?, space, and that's what you'll get every time, or
01:38you can have it just be something like your username, all right. kevin.
01:42Now that's the string kevin.
01:44That's not my username kevin.
01:46In order to get the username I have to use some special formatting codes.
01:49\u will be for my username, so that's my actual username.
01:53The other one was just a string of characters.
01:55It could have just as easily been bob.
01:57It wouldn't have mattered.
01:58\u is actually pulling my username from Unix.
02:01Let's look at what these other formatting codes are so that you'll be able to
02:03use them when customizing your prompt.
02:06So as we just saw there's u for username. We also can use a lowercase s for the
02:10current shell that we're in. bash in this case.
02:13There's lower case w and upper W which will show the working directory.
02:16The difference is that w shows you the full path to get to the working directory
02:21whereas capital W just shows you essentially the name of the folder that you're in,
02:24just that current directory only.
02:26w is the same thing as if you typed pwd.
02:29Then there's \d which will show you the date.
02:32There's \D which allows you to then provide curly braces with a format, a
02:36string from time format.
02:38You can look up with those formatting codes or other places.
02:40I've just given you a real simple one to show you the year, the month, and the day.
02:43Then we have four different ones that'll show the time, depending on the format you want.
02:47There's capital A, lowercase t, there's the at sign, and then there's the
02:52capital T. There's capital H for hostname, lowercase h for just the first
02:56part of the hostname.
02:57There's exclamation point which will be the history number of this command.
03:01If you use your history a lot, that might be useful.
03:04And then there's the special dollar sign. If you're logged in as root, that
03:08will then display a pound sign; otherwise it's going to display a dollar sign .
03:12So essentially it gives you a nice visual cue for whether or not you're logged
03:16in as root at the moment.
03:17And then \\ would be a literal backslash, if you want to use the actual backslash
03:22character instead of using it for one of these formatting codes.
03:25So before we launch into a lot of these let me just show you what the Mac default.
03:29The Mac default is the \h:\W \u$, space, and then we close the quotes.
03:39So that's the default setting.
03:41If you decide oh, you know what, I liked most of that.
03:42I just wish it didn't show me the hostname at the beginning.
03:45Well then, no problem.
03:46You can take that out.
03:46Let's try some other ones.
03:48So let's say we have t that'll show us the time. We have capital T was another
03:54one, shows the time. Capital A. We have a at sign that we can plug in there.
03:59So you see the different versions the time that it'll give you.
04:04Let's do a lowercase d for the date.
04:06If we want capital D then we need to provide some formatting as well.
04:10Capital D and then inside the curly braces we'll use %Y-%m-%d. The percents are
04:19the special characters.
04:20These dashes could just as easily be swapped in with something like this if
04:23you like that better.
04:24So let's just do the dashed version for now. There we go.
04:26And you can combine these of course.
04:28So if you want that, then you also wanted to have a space followed by a slash.
04:33Let's pick the at sign. there you go.
04:36So now you get the date and time.
04:37Now I just want to show you the difference between the w version.
04:41Go up here and let's pick out w, lowercase w. Notice that in my current
04:46directory it's just the tilde. If I switch into the unix_files directory into
04:50test1, now I get that full path, right?
04:53Exactly the same thing as if I did pwd, except it abbreviates it for the fact
04:58that I am in my user directory.
04:59So it tries to shorten it when it can, but it does still show me the full path.
05:03If I instead put the prompt with a capital W, then now I get just the current
05:07directory that I am in.
05:09As I move backwards I see just the current directory.
05:12So again, you can customize it to be absolutely anything you want.
05:15This is actually one of the ones that I find to be most useful.
05:17So I am just going to copy that using Command+C and then I will back up into my
05:23user directory, nano .bashrc, and then we'll just put that in there.
05:27Let's drop it right below all the exports we do here.
05:31We'll do export PS1.
05:34Now remember export we need to do to make sure that it's available all the
05:36time, so go ahead and make sure you add that. export PS1 and then whatever
05:41prompt you decided you liked.
05:42Ctrl+X. save it, and then just to make sure that it takes effect, you want to
05:47say source .bashrc and that'll read that file then.
05:51So now that's my prompt and as I move around we'll just see the current
05:54directory that I am in. unix_files.
05:57That's the file we'll see.
05:58So on your own, play around with those different format codes, come up with a
06:01prompt that works well for you, and then put that in your .bashrc file.
Collapse this transcript
Logout file
00:00So far all of our configurations for our Unix working environment have been
00:04done in either the .bash_profile configuration file, or in .bashrc and that's
00:09typically the two places that you'll locate them, because those are the files
00:12that get read in when you first start a new session and they set things up the way you like.
00:16But there is one other configuration file that I don't want us to forget about,
00:19which is the logout file. That's in bash_logout, and anything that's in there will
00:24get executed whenever we log out of Unix. Let's take a look at how it works.
00:27Let's create a new file. You see I don't have a bash_logout file right now,
00:32So let's do nano .bash_logout and in it let's just put echo "See ya later!",
00:40which is nice and simple.
00:41That's the command that we get run whenever we log out.
00:44Now on a Mac, let me just show you first of all that there is the file .bash_logout.
00:50Now on a Mac, it's not that useful because if we close this window, if we just
00:55go up here and close it, well then we'll never see that echo statement.
00:58If we do it this way, exit, then you see it run, "See ya later!"
01:02Now you may be thinking, "Well, if I normally just close the window, why have this file at all?"
01:06Well, immediately you won't use it very much, but I just want you to know that
01:09that's there. You could use it.
01:10It could alert you if there's some unfinished business before you log out.
01:14It could start a database backup as soon as you logout.
01:17It could notify friends who are on the same server that you've left the server.
01:21It could record some things in a log file or maybe clear out some temporary
01:25files that you left behind.
01:27All of those types of things could be put into a bash_logout file and even
01:31though you aren't seeing them, you certainly won't see anything that's echoed,
01:35it does get executed.
01:36So for example, if you had a clearing out some temporary files, Unix would
01:40gracefully close out, run everything in the bash_logout file as the window was closing.
01:45So again, it's certainly the least useful of the configuration files, but it's
01:49still important not to forget that it's there.
Collapse this transcript
8. Unix Power Tools
grep: Searching for matching expressions
00:01In this chapter we're going to take a look at some of the most useful and
00:04powerful Unix commands.
00:05Up until now we've really been covering the fundamentals and getting familiar
00:08with Unix, but now we are at the point where we can start doing some serious
00:11work, and the most powerful tool in the Unix toolbox by far is grep.
00:16Grep is a powerful way for us to search for text which matches patterns that we
00:20specify, and we aren't just talking about searching for simple text strings.
00:24We can define complex matching patterns by using regular expressions, and regular
00:28expression are actually part of how grep gets its name.
00:31Grep stands for Global Regular Expression Print.
00:34If you think back to the movie where we talked about Unix text editors, I told
00:38you that the earliest Unix text editor was called ed or Ed, and if you were
00:42working inside the ed text editor and you wanted to search for something, the
00:45way you did it was by typing a g, then a forward slash and then a regular
00:49expression that described what you were going to search for, another forward
00:53slash and then p, for print.
00:55And G stood for global, meaning globally search for it, and p for print meant
00:59to output the results, and the shorthand that described that pattern, global
01:04regular expression print, become shortened to just be rep and it is these
01:10regular expressions often just called regex or regexp for short
01:13that give grep its power.
01:15Grep and regular expressions have been around since the earliest days of Unix.
01:18You might have guessed that since it was available in this early text editor,
01:21ed, but they were actually created for Unix.
01:24By now though, grep and regular expression was spread to most
01:26programming languages, into many text editors as well, so they're useful
01:30in a lot of context.
01:31It's really worth trying to learn how they work.
01:33We'll focus on regular expressions in the next movie.
01:35First, let's get familiar with grep by using simple text strings. So to begin
01:39with, notice that I'm in my user directory and I'll just change into my Unix
01:43files directory and let's take a look at what's in there.
01:46File that we're going to start out working with is fruits.txt.
01:50That's just a simple list of fruit that we created earlier and what we're going
01:54to do be doing is search inside this file for an expression.
01:57We're going to start with just searching by a simple text string to begin with.
02:00And what you also might say is that you're going to grep that file.
02:04It is perfectly acceptable to use grep as a verb.
02:06So we're going to grep this file for text string.
02:08The way that we will do that is with grep, space, then the expression that we
02:13want to search for, we're going to search for Apple.
02:16Now this is still an expression, even though it's the simplest
02:19expression possible.
02:20It's the literal characters, a-p-p-l-e, but it is still a regular expression
02:24that it's passing in.
02:25So grep for Apple inside the file fruit.txt.
02:30That's all there it is to it.
02:31We hit Return and it comes back and what it returns to us are the results.
02:35The results that we're seeing are the three lines that contain a match.
02:40Not just the three words, the three lines. What I mean by that is in our case
02:45each of our lines only had one word on it, but if that first line had been Tom
02:49likes apples and the second line was Mary likes pineapples, then it would
02:54return the entire line, not just the single word apple.
02:57It would have returned the line that had a match, so don't be confused by that.
03:00We're actually seeing the entire line that gets returned.
03:03It's also a case-sensitive match.
03:06So for example, if we search for apple with a capital A, well, then it comes
03:10back and it says no matches, because it checked for case-sensitive search and
03:14looked for apple exactly as we specified it and it wasn't there.
03:17If we wanted to be insensitive, we can pass an option for that, I. That's a case
03:22insensitive search and now it doesn't care.
03:25It doesn't care if I have upper or lower case a in my expression and it doesn't
03:29care whether it's upper or lowercase a in the results.
03:31It will still match.
03:33I is probably the single most common option that you would use with grep, but
03:36there are a number of others.
03:37You can use the man pages to look through all of them.
03:39Let me show you a few of the more useful ones.
03:42Notice for example that it matched both Apple and pineapple. If we were to use the -w option,
03:47that will match only on whole words.
03:49I'm going to take away the capital A to make sure that that still matches.
03:52Now notice it didn't find the pineapple.
03:54It only found the two whole word matches apple and apple and it's smart
03:58about how it finds words.
03:59It uses spaces and punctuation and line returns to know where the word breaks are.
04:04Now we also could write a more complex regular expression that would do the
04:07same thing and we'll that little bit later, but this is sort of the quick and easy way.
04:11Another nice one is the V option. So with the V, we'll get our lines that don't match.
04:16I is the reverse, the inversion of our regular search.
04:21So notice now apple and pineapple are missing from the list.
04:24We've essentially filtered them out.
04:26So all lines that do not match is what we're looking for.
04:29We could do the in option.
04:31In option is nice because it gives us line numbers. So it numbers the line, so
04:36it says, all right, I found a match in line 5 and here's the line.
04:40I found the match in line 6 and here's the line.
04:42A match in line 13 and here's the one.
04:45Now you may not actually be interested in seeing those results. Maybe what
04:48you're interested in is actually just counting them and you can do that with
04:52the C option. grep c, it comes back and says ah, I found it three times.
04:56An example of a place that where that might be more useful, if we do grep inside
05:01lorem_ipsum, let's search for lore. So it's our long fake Latin text.
05:06It comes back and it says, ah, the word lorem occurs in there 44 times.
05:10Now notice also how fast that was.
05:13It's really, really fast about being able to search in there looking for this text.
05:16That's part of the power of Unix, is being able to do these things and do them
05:19in a really, really fast way.
Collapse this transcript
grep: Multiple files, other input
00:00What if we want to grep from multiple files or search inside directories?
00:04There are a couple of ways to do it.
00:05One is we could just say well, grep for Apple inside and then provide the file
00:10path, a directory in this case.
00:12So let's just say we're going to use the current path.
00:14The dot represents the current path.
00:16We could just as easily type Users/Kevin /unix_files, same thing. It comes back and says
00:22I didn't find anything.
00:24It's because we've given it a directory.
00:27If we want it to do it in this way then we need to put the -R option in front
00:32of it for recursive.
00:33Recursively look inside here, meaning check all of the files that are in this
00:38directory and then go into the directories below that and look inside those and so on.
00:42The same option that we used with copy, move, and remove earlier. So grep
00:47recursively for apple inside this directory.
00:50So whenever we use a directory, we want to use -R just like we did when we
00:53were doing copying.
00:54So now it tells me not only the matches it made but the file where it occurs.
00:59We could also provide the -n option after that and now it gives me the line
01:04numbers in each of those files.
01:05Notice that it does this by default. It says ah,
01:08now because I'm searching multiple files I'm not just going to give you the
01:10results. I'm going to tell where I found them as well.
01:13You can suppress that. Let's just erase all of this and I'll just use the dot
01:18from now on just because it's a little bit shorter and instead of the n option
01:22let's use the h option.
01:24h option will suppress the file name and still show you just the line itself
01:29or if you wanted the other way around and you wanted just the file name, you
01:32can use the l option.
01:33Now it tells me ah,
01:34I've found apple in three files.
01:36Fruit, sorted fruit, and unique sorted fruit.
01:38That can be really handy.
01:39All right you could say all right, here are the relevant files I found them, and
01:43now we could presently pipe those results into something else, right.
01:46Maybe we want to now concatenate these three files.
01:49After we find them then we could potentially concatenate them together.
01:52If we want to do the opposite of that and find the files that don't have a match
01:56it's the capital L.
01:57So that returns all files that did not match.
02:00This is very similar to the -v option that we did earlier but that returned the
02:04lines that didn't match.
02:05This is returning whole files that don't match.
02:08So it's similar but not the same.
02:09Now this recursive approach using this R option is searching all files in all directories.
02:16Don't forget that bash also lets us use wildcards to trend pick more specific
02:20files and that might speed up our searches.
02:22For example, just using ls we can do ls*.txt.
02:27And we can see just the text files that are in the current folder. Or even more
02:31specific, we can say all right let's check just the fruit files.
02:33All right, so now I'm saying just the fruit files. That wildcard lets us do that.
02:37That's handy with grep because now for example we can say we'll grep for
02:42apple inside just the files that end in fruit.txt. It's the same results that we
02:47got but it's just searching these three files instead of searching every file and directory.
02:53If you have a lot of files and directories this can be a lot faster.
02:55But the main point is don't just choose recursive by default. Think about what
02:59you're really trying to accomplish and try and find the best way to do that.
03:02Now these are working with files and directories. We also can change our standard input.
03:06We can use piped input.
03:08We could for example do cat fruit.txt and pipe that into grep for Apple.
03:13That's still just working with a file though, right.
03:15It's just as easy to do it the other way.
03:17Well it's really more useful as with something like ps aux. You remember that
03:20that returns a list of all of our currently running processes. Well, we can grep
03:24that list. Let's grep for Terminal.
03:26There is my entry for Terminal.
03:28I can now see what its ID number is.
03:31I can see how long it's been running.
03:32I can see how much memory it's taking up, all of that information just by
03:36grepping for only the line that contains Terminal.
03:38Or we could say all right, show me everything that has applications in it.
03:44So now all of my Mac applications will show up and I can see just those and not
03:47have to sort to that whole list of running processes.
03:51Another place it is useful with the history.
03:53So for example we'd say history and let's grep that for Unix files.
03:58Every time that I've made reference to the Unix files directory, we also could
04:02then use this as shortcuts to be able to run those commands.
04:05We saw how to do that when working with our history.
04:07Let's try history and let's pipe that through grep nano and then pipe that into less.
04:15Now I only have a few but if you have a lot of these we now would have a
04:18paginated list of all the times that we've ever used nano.
04:21So piper history, filter out so that we get only lines that have nano in it, and
04:25then use less pagination.
Collapse this transcript
grep: Coloring matched text
00:00You can see how grep is a really power full tool.
00:03There is one more thing that I want to show you.
00:04Let me just do grep for lorem ipsum again. lorem inside lorem ipsum.
00:10Now here's the content there. So you see that this line has lorem in it and
00:15this line over here has lorem in it and here it says lorem again, so I'm seeing
00:18each of those lines.
00:19But it's a little hard to find those matches.
00:22Grep also offers an option called color which is really useful.
00:26Color and now you can see that it colors those.
00:29Color also can have a couple of variations. color equals auto, color equals
00:34always, or color equals never.
00:37Always of course will always provide the coloring. Never will not do it. What
00:42auto does is auto says I want you to color it if you're showing it to me on
00:46the Terminal, but not when you're sending it somewhere else like to a file or a pipe.
00:50And the reason why it is, there is actually some special characters around the
00:53word lorem there every time so that it knows to display it.
00:56Otherwise how would it find those?
00:57What we don't want is those special characters to accidentally slip into a file.
01:01So auto is the option that'll take care of that for us.
01:04Now by default this color is going to be red, but if we want to change that we
01:08can do it using a shell variable.
01:10We saw how to work with shell variables. Let's just go back into our user
01:13directory and we'll open up our bashrc file and I'm just going to scroll down
01:19here to the bottom of these exports that I've done before and I'm just going to
01:23paste in some text that we will then take a look at.
01:25Let me just scroll up here so you can see the top of it.
01:28So these are the grep color codes.
01:29That's the name of the environment variable, grep_color, and I've given you the
01:33different numbers, so you know what they all mean.
01:35But here's the command down here, export GREP_COLOR and then in quotes put the
01:41colors that you want separated by semicolons. So you can pick different
01:44attributes. You want to pick only one text color and only one background, but you
01:48can pick several attributes.
01:49So for example this is 34 blue. 47 is white.
01:53So it's going to be blue text on a white background and that's how it will show me those.
01:57We'll just take a look at that in a second.
01:59There's also another one down here which is export GREP_OPTIONS and then you can
02:04provide options that you want grep to use all the time.
02:06So if you always want to do a case insensitive search, well you can put the -I option here.
02:11I've put --color equals auto and there is my grep option.
02:14That's probably the one people use most often.
02:16So feel free to use these or don't use them.
02:18But I just wanted you to see them and know how they work, because a lot of
02:21people really like them.
02:22So let's just save that file and one last time I'll go up and let's rep.
02:27I actually have to specify it's inside the Unix files directory now.
02:31Oh! It didn't work.
02:32I need to run that file source.bashrc.
02:35All right, and now that I've loaded that file or if you close the window
02:40and reopen it, now that bashrc file is run and now you can see the results of the coloring.
02:44So that's the basics of working with grep.
02:47As you can see it's really complex tool.
02:49So far we've just been using a regular string.
02:52Where it really gets powerful is when we start working with regular expressions
02:55and that's what we'll do in the next movie.
Collapse this transcript
Introduction to regular expressions
00:00In the previous movie we learned to use grep to search by using a simple text
00:04string as our search expression.
00:06Grep becomes even more powerful once we learn to use regular expressions.
00:10Now there is a lot to learn about regular expressions.
00:12We can have an entire course on them, and we won't too in-depth here, but in
00:15this movie we will cover the most essential concepts so that you can apply them
00:19and use them with grep.
00:20To begin with notice that in the Terminal I'm inside my user directory and I'll
00:24just change it to my unix_files directory.
00:26And the file that we're going to start by working with is the same fruit.txt
00:29file that we're working with in the last movie.
00:31It's just a simple list of fruit that we can search inside of and see how
00:34regular expressions work.
00:36To begin with we saw that we can grep for apple inside fruit.txt.
00:40First thing I want to tell you about regular expressions is that it's a good
00:43idea to put quotes around it.
00:45Now they are not strictly required, you saw it worked just fine without them,
00:49but it's a good idea.
00:50And the reason why is because when we start working with regular expressions
00:53we're going to be using some special regular expression symbols.
00:56And some of those regular expression symbols also have a meaning to the Unix
00:59command line and by putting it in quotes we're helping the Unix command line to see ah,
01:04this is the expression.
01:06The symbols that I see in there they are not for me to interpret and for me to
01:09do things with. They're meant to be part of the regular expression.
01:12So the single quotes are good habit to be in.
01:16Now the string that we've been searching for, apple, that is a
01:19regular expression.
01:20It's the simplest regular expressions that you can have, because it matches only
01:24the literal characters apple.
01:27Instead what we want to do is be able to provide some variation to that.
01:30So for example, we can replace those p's with periods.
01:34The period is a wild card character for regular expressions.
01:37It says any character could match here.
01:39So now we're not just looking for apple we're looking for a followed by any
01:43character, followed by any other character, followed by l and e. It has to be
01:472 character. It can't be 1, but 2 characters, but we don't care what those characters are.
01:51So now it matches not just apple, but it matches ankle.
01:54Now it's not that useless in this case, because it really just does match apple
01:57just the same, but let's try another example.
01:59Let's do grep that file for .a.a.a inside fruit.
02:05Do you know what it's going to return?
02:08Now we get back both banana and papaya.
02:11The reason why is because each of them matches the pattern that we've described.
02:15Any character followed by an a, any character followed by an a, and then any
02:19character followed by an a, and both of them match this pattern.
02:21And that's really what we're doing with regular expressions. We're defining
02:24patterns that we want to use for searching.
02:27So you can think of that.
02:27You can think of regular expressions as ways of defining patterns that we
02:31want to search for.
02:32Let's try another one let's just grep the file for e and a together, all right
02:36so, anytime that we have ea together.
02:38So it returned pear, peach, pineapple, and pear.
02:43Now if we wanted to just get pear and peach, we can just put a p at
02:46the beginning, right?
02:47And that would rule out pineapple. That would no longer match our pattern, but if
02:51we wanted to get peach and pineapple and omit pear how we go about doing that?
02:56Well, we can say for example that we want the third character after the e and a
03:01to match either c or p, but not the r. Now you can see it returns peach and it
03:07returns pineapple, but it didn't return pear.
03:10We're going to talk more about the square brackets, but I want you to just
03:12understand the concept that what we're doing here is we're defining a pattern
03:16that we wanted to match and both eac and eap match our pattern.
Collapse this transcript
Regular expressions: Basic syntax
00:01Now I'm not going to go through all of these item by item, but you can pause the
00:04movie if you want to take a look.
00:05Really get a feel for what they do.
00:07First a simple period, that's a wild card. The character set, so that's any
00:11character listed inside those square brackets.
00:13So in the example on the far right you can see that that would match both grey
00:16and gray, both spellings of gray could get matched.
00:21Then we have a negative character set. We'd have the same square brackets but we
00:25put a caret as the first character inside of them and that would say any
00:28character that's not included there.
00:30So for example, if we wanted to match everything that was not an A, E, I, O or U,
00:33then we would use that negative character set.
00:37Next we have the range indicator which is a minus sign and that's typically just
00:41used inside a character set to indicate a range of characters.
00:44You don't have to actually spell out every single character you mean. You can
00:47just say capital A to capital Z, A dash Z, lowercase a to z, 0 to 9, and those
00:53are the typically the cases you would use them with is with the alphanumeric
00:56characters and with digits.
00:58The next three items the asterisk, the plus, and the question mark all have to do
01:02with how often element repeats.
01:04Does it occur zero or more times, one or more time, or zero or one time?
01:08So to take the first one as an example, file_name, that underscore may or may not exist.
01:15If it's not there it still matches.
01:16If it exist one time it still matches. If it exists two times, three times, it
01:20doesn't matter. It still matches.
01:22Below those we have the alternation operator.
01:24This is the upright pipe that we've already seen we were piping commands.
01:28Notice that this is one of the symbols that has a meaning to Unix that's
01:31different than the meaning in regular expressions.
01:33Here it's the OR operator.
01:35So what it means is for example I asked for JPG or GIF or PNG. That's very common
01:42if you want all three file types to be matched.
01:45Next we have the start and end of line anchors.
01:47The start of line anchor is the caret symbol.
01:49That's the same thing we used up on the negative character set, but it's in a
01:52different context here. Don't get them confused.
01:54If it's inside the square brackets, it's the negative characters set.
01:57Otherwise, we're really talking about this start of line anchor and that means
02:01that a line beginning with hello would match, but if hello was somewhere in
02:06the line, not at the beginning, it would not match. Same thing for the end of line anchor.
02:11If you need an actual literal character and you want to escape one of these
02:13regular expression characters, you would use the backslash and then there's a
02:17number of them at the bottom that I won't go over in detail, but they match
02:20either any digit, anything not a digit, anything that's an alphanumeric
02:24character or not, anything that's white space or not and so on.
02:28Notice though that three of those I've marked with an asterisk.
02:31These are extended regular expression syntax.
02:33We'll talk more about that.
02:34But for now, just notice that there are a couple of these that are part of the
02:38extended set and some of them that are part of the basic set.
02:41There're also some predefined character classes that can help you to find
02:44certain types of things easier.
02:46The symbols for these are a little bit longer.
02:48All of them have square brackets and colons on either side of a keyword.
02:52So for example, alpha would match any of those alphabetic characters.
02:56Digit would match any numeric characters.
02:59Alnum would match both, alphabetic or numeric, and so on.
03:02Now you could just as easily use those other symbols to write these out
03:06yourself. These just provide useful shortcuts.
03:08Notice here that the square bracket is not indicating a character set.
03:11This is talking about a single character.
03:14If you want to say this is the character set, we put another set of square
03:17brackets around that.
Collapse this transcript
Using regular expressions with grep
00:01So we've already gotten a look at the wildcard, the period, and we've also seen
00:04character sets when we were working with finding both peach and pineapple.
00:08Let's try the beginning Of line and end Of line anchors.
00:11So for example grep and the beginning of line anchor followed by P inside fruit
00:17will find every line that begins with P. Notice it did not match apple.
00:21Apple has a P in it, but it's not at the beginning of the line.
00:24I am going to do the same thing.
00:25We can find everything with berry at the end and that will return every line
00:30that has berry at the end.
00:31Now in this case actually all occurrences of berry were also at the end of the line,
00:35but it would find it if it were only at the end of the line.
00:39Let me show you that.
00:40One way that you can work with grep that's really useful is instead of working
00:43with a file, let's say we have berry bush, we can use pipes from echo, so
00:48we'll pipe-in the string, and then we'll grep for 'berry$'.
00:53Now, it didn't find it, because it's not at the end of the line.
00:56If we instead we're looking for berry bush berry, now it does find it.
01:01It matched the last one, not the first one.
01:03You can see I have my colorization turned on, so it just colorized the last one.
01:07If we did the same thing but we take away that end Of line anchor, now it
01:10finds it both times.
01:11Let's try another one.
01:13Let's do echo, and let's do AaBbCcDdEe. There we go!
01:21And let's pipe that into grep, and this time I'm going to make sure that color is on.
01:25If you don't have your color on, this will turn your color on.
01:28Let's search for just upper.
01:31We're looking for uppercase.
01:32We're going to use that character class.
01:35Notice what happened here.
01:36It didn't match the uppercase letter that you might have expected it to, because
01:41it's not interpreting this as being a character class.
01:44It's interpreting this as being a character set.
01:47It thinks that we want to match anything that is in this character set.
01:51So E is in this character set, so it got matched.
01:54There is no U, P, and R. That's why the lowercase E got matched.
01:58If we instead put double-brackets around this, and we actually should probably
02:02put quotes around it too.
02:04It's always a good practice, remember.
02:05Now, it returns what we expect it to match.
02:08So I just point that out to you to make sure that you see with the single
02:11brackets we're actually referring to the class of characters.
02:14But if we wanted to actually work inside grep, we use the double ones so that
02:18it's saying a character set made up of the character class.
02:23I'm just going to paste-in another example, so you don't have to watch me type it.
02:27I've just got a bunch of punctuation here and I'm going to search that to find
02:29all the punctuation.
02:30I think that gives you the basics for regular expressions.
02:33Again, it's a very deep subject, and this is really only the surface of it.
02:36There are even sites that catalog regular expressions.
02:39So if you're looking for a regular expression that will match every phone number
02:43or every email address, all those different combinations for how those might be
02:47formatted, those exist.
02:48People have written them and they've shared them on different web sites and you
02:51can make use of them without having to reinvent the wheel.
02:53But what I do want to show you is there are a couple of things to watch out for
02:57when using regular expressions with grep.
03:00The first of these is that if we were to grep for 'ap*le' inside *fruit.txt,
03:09that the two asterisks here have different meanings.
03:11This is the regular expression asterisk.
03:13It means that the P is repeated zero or more times.
03:16This would match ALE, APLE, APPLE, APPLE, and so on.
03:22This one is a wildcard for the file system, totally different meaning.
03:27So I want to make sure that you see those and realize the difference.
03:29That's part of why we want to make sure that we put these inside quotes, is to
03:32help keep that separate.
03:34The second is I'll take away the asterisk here and I'll run the command and
03:37you'll see that it does return the results that we would expect.
03:40If we run this other version with the plus sign instead, that's the operator
03:46that means one or more times.
03:48That would match APLE and APPLE, but not ALE.
03:52It has to occur at least once.
03:54That's what the plus symbol means.
03:56If we run it now, we get nothing back. The reason why is it's taking that plus
03:59to be the literal plus sign.
04:01This is what I was talking about with the basic and extended
04:04regular expressions.
04:05So it's an important sort of gotcha when working with regular expressions, that
04:09there are the basic ones that work all the time, and then there is this extended
04:13set of few things that only work in some cases.
04:16If we want to use the extended set in grep, we need to use the -E option. Now it works!
04:23Now, it finds it exactly as we would expect.
04:25So if you want to use a few of these extra features, you're going to need to use that -E.
04:30There are more of them than just the three I showed you.
04:32Those are the most basic and the ones that come up and cause the problem most often.
04:36Could you use -E all the time? Absolutely.
04:38Another spot where this basic and extended regular expressions causes problems
04:42is when we're working with that OR operator as well.
04:44So for example, if we grep for Apple or Pear inside fruit.txt, so it feels like
04:51a very simple operator.
04:52It feels like something we ought to be able to do.
04:54It says oops, it's not there.
04:55All we've got to do is put that -E in front of it and now it does find every
05:01occurrence of either Apple or pear.
05:02So regular expressions, especially when combined with rep, is really great for
05:06finding exactly what you want.
05:08But what it doesn't do is it doesn't allow you to change or manipulate anything.
05:12All it does is allow you to find it.
05:14So in order to make changes to manipulate the data, we're going to need to take
05:17a look at a few more Unix tools.
Collapse this transcript
tr: Translating characters
00:01In this movie, we're going to take a look at another simple but powerful
00:03Unix tool called TR.
00:05TR is short for Translate and what it does is it copies from the input it
00:09receives to output, but with the substitution of selected characters, according
00:15to translation rules that we give it.
00:17So at its simplest we would have echo a, b, c, and we'll pipe that in to tr, and
00:23the first argument of tr is going to be the thing we want to search for. Our search string.
00:27So we're going to search for all of the commas, and the second argument will be
00:30the replacement string.
00:31So we'll replace it all with dashes. So as you might guess, it takes all the
00:35commas and it translates them into being dashes.
00:38Now this is a totally valid use.
00:40But translating one character to one character doesn't really reveal how TR works.
00:45So let's try another better example.
00:47Let's say we have echo and for this string let's use the numbers 1 to 6, so
00:51you can just peck away through your keyboard, just pick a random set of
00:54numbers, but not going any higher than 6. And then we're going to pipe that
00:57into tr, and this time we're going to say that our search string is the numbers
01:01in sequence 1 to 6.
01:021, 2, 3, 4, 5, 6, and the replacement string that we'll use for that, I am
01:07going to use the letters E, B, G, D, A, E. Those map to the tunings on a six string guitar.
01:13When we hit Return, now notice what it did.
01:16It took all of the 1s and it turned them into Es and it took all of the 2s and
01:21it turned them into Bs and all the 3s became Gs and so on.
01:25Everything that it found in the search string, not only did it find it, but then
01:28it said "Oh, what position are you in the search string?
01:31Let me map that to the replacement string and find the item that's in the
01:35same position there."
01:36It's a mapping, right?
01:38So the number 6 gets mapped to E, number 4 gets mapped to D, right?
01:43Do you see how that works?
01:44Notice also that if we had another number, let's put in a 9 or any
01:48other character really,
01:49it wouldn't get matched, right?
01:52It doesn't get translated, because it's not in our translation string.
01:54Therefore it gets left alone.
01:56It's only those selected characters and those selected characters get
02:00translated based on their position in the search string and in the replacement
02:05string, so position does matter.
02:07This translation is a lot like the code breaking that you might have done as a kid.
02:11In order to find out a secret message, you'd have to swap all the As for another
02:14letter and all the Bs for another letter.
02:16When you were finally done, you would have decoded the message by doing this
02:19simple alphabetic translation.
02:21We can actually do this with TR.
02:23A simple version would be to encode a string using something called ROT-13.
02:26ROT-13 is just rotating all the characters 13 places.
02:31So the A becomes N, B becomes O, C becomes P, and so on. Let's try it.
02:37echo 'This is ROT-13 encrypted.' A single quote and we'll pipe that into tr and
02:46now for tr's search string, we're actually going to use character sets.
02:49We could write out A, B, C, D, and all that, but we can shorthand that with A-Z,
02:55all the letters A to Z in that order, followed by the lowercase letters, a-z in
02:59that order, because it is case-sensitive.
03:02Then in the replacement string, what we want to do is rotate all of those 13 positions.
03:06So N-Z would be take care of letters A through M, and then A-M capital M would
03:14be the second half of that.
03:16Same thing for the lowercase letters.
03:17n-z, a-m, and we translate it.
03:20Now what we get is something that is ROT -13 encoded. And we can just take that
03:24and we can decode it by just rotating it 13 places again.
03:28Since there are 26 letters in the alphabet we can do that, and now we can decode
03:33the message just as well.
03:34So that's probably the simplest kind of encoding and encryption there is.
03:37It is just that simple rotation cipher.
03:40But hopefully now you start to see the idea of how translate does its thing.
03:43Just to make it really clear, let me show you what it's not for.
03:46If we have for example something like already daytime and we want to swap out
03:52tr 'day' for 'night',
03:56it doesn't replace the word day with the word night.
03:59That's not what it's doing.
04:00What it's doing is it translates the d into n and the a into I and the y into g, right?
04:06Do you see that?
04:08So already also got affected by that.
04:10We're translating each of those characters that we find, not doing a Find &
04:14Replace like you might do with the word processor.
04:17Now we don't have to provide a simple one-to-one replacement.
04:21For example, let me just paste in an example here.
04:23I have a long string and then have as my search string bedf and then the
04:28numbers 5 through 9 in that order.
04:30I'm replacing them all with x, just by itself. See what it did?
04:34It found those specific items and replaced them all with x.
04:38To see what it's actually doing, a good way to do that is just let's put another
04:41character there. Let's say z. Now notice that the b got replaced with the x,
04:46but everything else got replaced by the z. That's because it's repeating the z.
04:51It's the same thing as if I've done something like that, right.
04:54Whatever the last item is, it just gets repeated multiple times until we have
04:58enough characters to match our search string.
05:01So if the replacement set is smaller, then the last item repeats.
05:05If the replacement set were bigger than the search set, well then those items
05:09will just never get reached.
05:10There would be nothing that ever would map to those remaining characters.
05:13So now that you understand what it is and how it works, what are some real-world
05:17use cases where you would use this?
05:19Well, one example might be, if you remember we have a file here called people.txt.
05:24I am in unix_files folder already.
05:27It just has names of people.
05:29Well, what if I wanted to make all of those lowercase? We could do that.
05:32Let's say tr and we'll do capital A-Z should be translated into a-z and we
05:41would not say people.txt, right?
05:44There are only two arguments allowed. Let's try that.
05:47You'll see that it comes up and says no, I only want two arguments.
05:49Redirecting our input from the file, and remember how we did that.
05:53So we're now taking the contents of the file and passing those in as input to tr
05:58and then it gives us the output.
05:59It now made those all lowercase.
06:01You also can use those same regular expression classes that we just saw in the
06:06regular expression movie.
06:08upper and then here would be lower, right?
06:17Does the exact same thing.
06:18So we can use upper and lower because those are well ordered sets.
06:22Using some of those other classes like punctuation, they would work but the
06:26order might not be obvious to you, what order those characters come in.
06:30Another example where it might be useful is let's imagine that we have something
06:33that's in a foreign language, right, and has lots of accented characters in it.
06:36We need to get all those accents out of there.
06:38For whatever reason, we aren't going to be able to print those accents so we
06:41need to just take all of the accented Es and turn them into regular Es.
06:44That will do it for us.
06:44It will strip them out and turn them into its equivalent.
06:49Another really useful case is let's imagine we have a file. I have a file here,
06:53a new file I've added.
06:54It's us_presidents.csv.
06:57CSV is for Comma Separated Values.
07:00If we take a look at the head of that file, us_presidents, you'll see that I've
07:04got values here that are separated by commas. so George Washington, 1789, and so on.
07:10That's comma-separated Values.
07:12It might actually be a little easier to understand the data by looking at it in a table format.
07:16So I've essentially got the number of the President, their name, the year they
07:20started, and finished their term, their party, their state, and then where
07:23their Wikipedia entry is.
07:25So that's what I've got.
07:26I've just got all these values not in a table, but separated by comments, right,
07:30and then I could import them into things and that kind of thing.
07:32Well, in addition to comma-separated values, a very common format is to have tab-
07:36separated values, and you put tabs in between each of them.
07:39So using tr, that's a cinch, right?
07:41We just say tr, take all of the commas and let's replace those with tabs.
07:46Tabs, the special character is this /t, and so we'll pipe in our
07:50us_presidents.csv and you can see the difference.
07:54Now you see those tabs and we can just take that same thing and let's output
07:57it now to us_presidents, and we'll do TSV for tab-separated values. So there we go.
08:05Now I have my comma-separated values and I have a tab-separated version with
08:08just one simple command.
08:09So it's the basics if I had to work with tr.
08:12There are a couple of options to give you some nice features.
08:14So let's look at those in the next movie.
Collapse this transcript
tr: Deleting and squeezing characters
00:00In the previous movie we saw how to use tr by looking at its standard translate usage.
00:05The tr has several options that we can specify that will allow us to work
00:08with it in other ways.
00:10Instead of translating, it can also allow you to delete or filter out characters
00:14into dedupe repeating characters, a process that it refers to as squeezing.
00:18The three options we are going to be looking out are the -d, -s and -c options.
00:21-d will delete characters in the set that we specify.
00:25So it'd just simply remove them. Instead of translating them that will just get removed.
00:28The -s option will squeeze repeats that are in the listed set.
00:32So it won't necessarily squeeze everything, but the things that we've told it to,
00:35it will compress down.
00:36If we had for example five Xs in a row, it would turn it into one X instead, and
00:42then the -c option says Use the complementary set. It's the opposite.
00:45So you would use this option either with either the -d or the -s option.
00:50With the -d option I mean delete characters that are not in the listed set, right.
00:53It's the opposite of that, the reverse.
00:55It will become clear once we actually look at some examples.
00:58So let's say that I have a simple string.
00:59I have "abc1233deee567f".
01:04The threes and the Es are repeated.
01:06I am going to take that string.
01:07I am going to pipe it into tr, I am going to use the -d option, and I am going
01:10to say the set that I want to specify, the thing I'm looking for, is this class called digit.
01:14So what I am doing is deleting all of the digits and you can see the output
01:18there to the far right.
01:19It would then just give me the letters.
01:20If use the -c that's the complementary set.
01:23So now I'm saying delete everything that is not a digit.
01:26So what I'm left with is just the digits.
01:28Now that's not just stripping out the letters.
01:31That's also stripping out tabs, line returns, anything else that might be in
01:35that file at all, anything that's not a digit.
01:37So a lot of times it's easier to specify not the thing you're looking for,
01:40the thing that you don't want, right.
01:42We are filtering out the opposite version.
01:44It can be very handy.
01:45Notice that in both of those cases there is only one argument tr now. Before we had to.
01:50We were saying translate from a to b; now we are just saying we want to delete
01:54from set a. We don't need to translate to the other set.
01:57So there is no reason to have a second argument.
01:59Squeeze works the same way.
02:00We just have one argument.
02:02This time I have tr -s digit and that squeezes all the digits.
02:06So you notice in the result there to the far right that now the threes have
02:10just become a single 3. It's just 1, 2, 3.
02:13If we use the complementary set, well then it's the opposite. Squeeze everything
02:17that's not a digit, so that squeezes the es.
02:20So now there's only one e.
02:21There are still two 3s in there. It's 1233.
02:22It's the es that got compressed.
02:25Now again that's the complementary set, so everything that's not a digit.
02:28That would be line returns, that would be tabs, anything else that's in that file
02:32that's not a digit would get squeezed.
02:35We can use these two options together.
02:36So for example, if I have tr -ds, the first argument you are going to specify is
02:41going to be the thing you want to delete.
02:43The second argument is going to be the thing that you want to squeeze.
02:46So in the first example I have there, it's going to delete all the digits and
02:49then it's going to squeeze all of the letters, and then we come up with abcdef.
02:54Now if we use the -c option with that as well, it's important to note that the
02:57-c option only applies to the delete.
03:00So what the example I have there will do is it will delete everything that's not
03:04a digit and then squeeze the digits.
03:07It's not squeezing the opposite of the digits, right.
03:09-c just applies to the first argument to the -d.
03:12So that's how it works, but you may be thinking well why in the world would I
03:15need something like this?
03:16I mean if you have something like a file that has an essay in it, why would you
03:20want to transform your letters so that the word sweet became swet?
03:24That's not that useful.
03:26Well, admittedly you won't use it that often, but it is a good tool to have it
03:29in your toolbox for a couple of special cases.
03:32Let's say for example that you want to remove certain characters from the file.
03:36We can remove all no-nprintable characters from the file for example, right.
03:39So remove everything that's not a printable character out of the file and then
03:43presumably I'd be able to print the file, right?
03:46You could also remove other things.
03:47You can remove all of the tabs from a file.
03:49You can remove all the line returns from a file.
03:51You can remove all the punctuation, right.
03:53Any of those things are possibilities, but essentially we are saying go through
03:56the file and remove the things that I don't want in there.
03:59One common use case that I want to give you just so that you have it and can
04:02refer to it is that you can remove the surplus carriage returns and end of file
04:06characters that Windows files often have.
04:08For the end of line in a Windows file it has both a carriage return and the line
04:13feed, but on Mac and Unix, that's just a line feed character.
04:17So what we want to do is get rid of extra carriage return character and then
04:20Windows must also have an end of file character to indicate the end of the file,
04:25which Unix sometimes doesn't like as well.
04:26So we can strip those out and essentially take something that was a Windows file
04:30and make it a happy Unix file by stripping out the hex codes that I have there. That /015/032?
04:37There is no way you would know this.
04:38You would have to look those up to know what they are, but I wanted to give them
04:41to you because this is a handy tool to have in your toolbox.
04:43And an example where you might you squeeze? Well, you might want to remove all the
04:47double spaces from your file, right.
04:49So every time you hit a period, if you accidentally hit the spacebar twice, you
04:53really wish you'd only done it once, well, we can squeeze all those spaces out
04:56and not just double spaces, but if you accidentally hit triple spaces or
04:59however many more there was, this would squeeze them all down, so that they would only be one space.
05:04There are all sorts of variations on these. There are ways that you can combine
05:07these. You could for example remove all characters that are not printable and
05:10squeeze the spaces in the file, do those together.
05:13The main thing I want you to see is that tr doesn't just translate.
05:17It also has these other special features which almost could be programs on their
05:20own, but they're really just options to tr.
05:22So tr -d and tr -s.
05:24So make sure that you have tr -d and tr -s in your toolbox.
Collapse this transcript
sed: Stream editor
00:00In this movie, we will take a look at a Unix program called sed.
00:04sed is short for Stream Editor.
00:05What that means is that sed modifies a stream of input according to a list of
00:09commands before passing it on to the output.
00:11It's very similar to the way that tr works, but with sed, we can modify the
00:15stream of input in many more ways.
00:16sed is a complex tool that offers several modes of working and mini features.
00:21In this movie, we are just going to focus on the basics and get to see the most common usage.
00:25It's most common usage is for doing substitution and so what we'll be doing is
00:29sed, space, and then providing an expression for substituting.
00:34That will always begin with the letter s inside the quotes to indicate that it's
00:38going to be doing substitution.
00:39Then we have got some delimiters, the forward slash in my example, and then we've
00:43got patterns of searching and replacing, just like we had with tr.
00:47So we are going to search for whatever is in a and replace it with whatever is in b.
00:51Notice that the big difference between this and tr though is that tr actually
00:55had two different arguments.
00:58It had tr, then the first argument, and then the second argument.
01:01Here, they're all in one argument and they are inside those delimiters.
01:05sed is really based on this one expression.
01:07So everything that we wanted to do, the sort of command that we are sending to
01:10sed, is going to be inside those quotes.
01:13Let's take a look at some examples.
01:15So, for example, let's take some input, echo upstream, and we are going to
01:20pipe that into sed.
01:22So then what we needed here is an expression inside quotes for substitution.
01:27I am going to go ahead and put my delimiters.
01:29Now, inside each of these, we are going to need to fill in what we are searching for.
01:33I am going to search for up and we want to replace it with down.
01:37So, upstream becomes downstream, which is appropriate since we are doing
01:41stream modification.
01:43Upstream, it was upstream.
01:44Once it gets downstream, it's downstream. In between is sed doing the stream editing.
01:49Now, notice how this is different from what we were doing with tr.
01:52tr was translating each of those.
01:54What we are doing here is we are searching for one thing and we are replacing it
01:59with something else.
02:00It's much more like the find and replace that you might do in a word
02:02processing program.
02:03Now there is one important thing I need to show you about sed, which is that by default
02:07it doesn't search globally.
02:09I will show you what I mean.
02:10So let's say we have upstream and upward and then let's just hit Return.
02:14Notice now it changed the first occurrence of up, not both of them, just the
02:19first one got changed.
02:20That's different from what we were doing with grep.
02:22When we were greping for something, it was finding all occurrences of it and
02:26that's because in grep, the g stands for global.
02:29It's globally searching.
02:30So, just like grep, we need to provide a g modifier here to say that we want
02:34to do this globally.
02:36So after this pattern of s and then a replace string, then we put a g to say
02:42that we would like to do that globally. Downstream and downward now does both of them.
02:46Now, the delimiters that we are using here, these forward slashes, actually are modifiable.
02:51It doesn't actually matter which one we have.
02:53Let's do, for example, colons. What sed does is it says, all right, what's the
02:59first thing that comes after the s?
03:00All right, I see the s, I know we are going to do a substitution, what
03:03delimiters should I use? And whatever we have is that next character is what it
03:07will use as the delimiter.
03:08So, we can use pipes like that. It still works.
03:13I will just paste in an example to show you why you might want to do this.
03:17The forward slashes are really the most traditional usage of it.
03:20But let's say, for example, that I had a phrase like MacOS/Unix:
03:22awesome and I wanted to change that.
03:27Then the search string that I'm looking for has a forward slash and a colon in it in it,
03:32so I don't wan to use either of those first two choices. Therefore I'm using the upright pipes.
03:37Now, I could still use the forward slashes. I will just have to escape each of those values.
03:42Every time it occurred, I would have to put a backslash before the forward
03:45slash to escape it.
03:47By switching to a different delimiter, I can just make it a little cleaner
03:49and easier to read.
03:50So far, the stream that I've been using has been texted that I'm piping into
03:54sed, but sed also works with files.
03:56So, let's say we have sed s and we will use pear to mango inside our fruit file.
04:03Now, I am already inside my Unix files here.
04:05Inside my user directory. We have a file of just fruit there.
04:09Now, notice that I did not put the pipe here. I certainly can.
04:13That still works too.
04:14This is different from tr.
04:15In tr, you had to put this there.
04:17with sed, you don't have to.
04:19It will take a second argument, which is the file that you want it to do, and
04:23then we can take that and we can pipe that and we can have something like mango_fruit.txt.
04:28Now, we've redirected the output into this other file.
04:31Now, notice here that we did not use the g modifier.
04:35We did that, we didn't use the g modifier, but at the same time it replaced pear
04:38here and it replaced pear down here, two times. Why is that?
04:43It was because each line gets treated as a stream.
04:47Let me show you an example where you can really see this in action. s/a/x and we
04:51will do that with the fruit file.
04:54Now, notice here when we had, for example, banana, it only changed it the first time.
05:00It didn't change it the second time but it did do it on the next line.
05:02It found the first occurrence and then the first occurrence and so on.
05:05But then it got on papaya.
05:07It only used the first one. We didn't do it globally.
05:09If we switch that and you do the g in front of our global, now notice the
05:13papaya all got changed.
05:16So, it's line by line.
05:17So when we say global, we are saying per stream and each one of these lines is
05:21being treated as a new stream that's coming in.
05:23Now, you can search for just about anything inside a file and replace it.
05:27It's useful for things like let's say we had a file that was written in Britain.
05:32So it has the word colour in it, spelled with o-u-r.
05:33We want to change that to color, the American spelling without the u. Then we
05:39put that global and whatever file we want to work with.
05:41Now, obviously, I don't have the word color in there.
05:43That's the kind of thing you could do globally to a file and sed would make it easy.
05:48You can also provide multiple sed commands.
05:50Let's say for example that I have a phrase like echo.
05:53During day time, we have sunlight and let's take that and we'll do sed.
06:03We want to first do s for day and night.
06:09That will simply replace the day with nights and that says during nighttime, we have sunlight.
06:13If we also wanted to replace sun with moon, we can provide a second command and
06:18the way we do that is we put -e.
06:22It's important that it's lowercase and that says that we are going to
06:24have multiple commands.
06:25We are essentially going to build them up.
06:26So, -e in the first one, -e, and then next sed command, s/sun/moon.
06:30There we go and now it does both of them.
06:35So, all you have to do is prefix it with that -e option and it says "Oh, what
06:40comes next is an argument to -e.
06:43Therefore, I am going to take that as one of my sed arguments and I am going to
06:46wait until I get another -e."
06:47And I will take that and just sort of build up the set of filters that we want to apply.
06:52So, it's nice. You can actually put together a five or six of these and change a
06:55whole bunch of things all at one time.
06:57And you can remember that it's the e because e is for edit.
07:00So we are providing the edits.
07:02So each one of these is an edit that we want for our stream editor to use.
07:06Now, we won't go over it, but if you have a lot of sed commands that you want to
07:09run on a single input stream, what you can do is you can put all these edit
07:12commands into a file and you can use the f option to run all of them at once and
07:17that will run every sed command that's in the file.
07:19So you can have a set of regular things that you want to change, sort of filters
07:23you want to apply. Save them to a file and they're available to you.
07:27Anytime you want them, you can just call up that sed file and say hey, run all
07:31of these commands on this file for me.
07:32Now, these will give you some examples of just the simplest substitution that
07:35you can do just by using literal characters.
07:38Where sed really becomes powerful was when we start working with regular
07:41expressions and that's what we will look at in the next movie.
Collapse this transcript
sed: Regular expressions and back-references
00:00In the last movie, we got familiar with the syntax of sed, but all of our
00:04searching so far has been with literal text strings.
00:07Now we're going to learn to use regular expressions with sed.
00:10It may seem like sed is really similar to grep.
00:12That's because it is.
00:13All sed is, is grep and then substitute.
00:16So put another way, anything that you can find with grep, you can change with
00:21sed, and that includes making good use of regular expressions.
00:24So as a simple example of this, let's just have echo "Who needs vowels?" and
00:31we'll pipe that into a sed expression where we will look for anything that is in
00:37a, e, i, o, or u, inside a character set and we'll replace that with an
00:41underscore. We'll do it globally. So there you go.
00:44You see it took all of the vowels that were in that character set and replaced
00:48them with the underscore. So we can use regular expressions.
00:51Now, the regular expressions here work exactly like they do with grep, meaning
00:55that we also have an issue with basic versus extended regular expressions.
00:59So for example, if I put the plus sign in there, it doesn't work anymore,
01:02because the plus is part of the extended regular expression set and just like
01:07grep, we would use the -E option to be able to use those extended features.
01:14So let's try a couple.
01:15Let's say we have for example our fruit file.
01:19That's just cat fruit.txt and in that let's writes a sed expression that will
01:25take the first line that starts with p, any line that begins with p, and we're
01:29going to replace that with space, space, p.
01:32Now, notice I had to repeat the p again. So I'm finding it.
01:36It's going to be part of what gets replaced.
01:38So I want to make sure that I still include it when I finally replace it.
01:41So there we go, and we'll just do our fruit.txt file for that.
01:45So everything that had a p got indented two spaces.
01:49We could also leave out the p and just indent everything two spaces.
01:55A variation on that would be to, instead of having two spaces, put the right
02:00angle quote in there.
02:01That does the same thing as when we quote a mail message, right?
02:03If we reply to a mail message, our mail editor might stick those in front of the
02:07reply that we're doing.
02:08One important thing that you might run into is you might think, well, instead of
02:11spaces in front of every line, what if I wanted to put a tab?
02:14And we have this shortcut for the tab character, which is the \t. That doesn't work here.
02:20sed doesn't understand that \t, or at least the Mac version of sed doesn't.
02:24There are other versions that do, but the Mac version doesn't understand it.
02:27So in order to get it, the trick that you need to know is that in bash if we
02:32want to type a tab character, the special tab character, the way to do it is to
02:36type Ctrl+V and then the actual character.
02:42So hit Ctrl+V and then tab, and then we'll actually get that tab effect.
02:45Ctrl+V works for other characters as well.
02:47Ctrl+V and Enter, Ctrl+V and Escape.
02:50It will type the actual character for you.
02:53You won't need it most of the time, but this is one of those cases where it
02:55definitely comes in handy.
02:56Let me give you one last example in this before we move on.
03:00Inside the directory I am in I've added a new file called homepage.html.
03:05What that is, is just a basic homepage for a fake company. So you can use any HTML you have.
03:11I just wanted to have some HTML to work with.
03:13Let's construct a sed script that will remove all of the tags from this, all the
03:17HTML tags, so that what we're left with is just text.
03:20Well, the way we could that is with sed, we'll use -E, capital E, so we can make
03:26use of the extended sed substitute.
03:28And we know we're going to want to find everything that's inside those tags,
03:32the angle brackets.
03:33So I'll just do that for now.
03:35And then we're going to remove them globally inside homepage.html.
03:39So now we just need to write a bit more of our regular expression.
03:43Inside those angle brackets, what are we going to have?
03:45What's allowed to be in there?
03:46Well, you could say a lot of things.
03:48I'm going to say that the thing that defines it is that it is not those angle brackets.
03:54It could be any other character besides those, and I'm not going to be picky.
03:57And then we'll put our plus sign after it to show that there can be more than one of those.
04:01So that then takes our HTML and strips out those tags.
04:04You certainly can come up with better regular expressions certainly using
04:07more advanced techniques.
04:09You can see, for example, it didn't filter out this first tag because it's
04:12broken across two different lines.
04:13It's not perfect, but you do get the idea of what it can do for you.
04:17Now let's talk about back references.
04:18Back references are actually part of regular expressions and sed makes good use of them.
04:23Let me show you a good example.
04:24Let's say we have echo 'daytime' and we want to change that using sed, and
04:30what we want to make is daytime,
04:32it's going to be made into daylight.
04:37We might be looking for things that are much more complicated.
04:39We might be looking for not the literal daytime, right?
04:43We might be constructing some very fancy regular expression here.
04:46We might be saying, well, look for anything that is ... time, we don't care what it is.
04:51And what we want to do is take that thing, whatever that thing was, and use it again.
04:56We don't know what it is ahead of time.
04:58It's not necessarily day.
04:59It might be some other three letters.
05:01Well, the way that we do that is with a back reference and a back reference is
05:05the backslash and then the number of the back reference.
05:10So we can have more than one of these defined in our search string.
05:13In fact, I believe it supports up to nine, so you can have up to nine of
05:16these and they will then say "Ah, the first set of parentheses, well, that corresponds to \1.
05:21The second set of parenthesis, that corresponds to \2, and so on."
05:26Now, if we try and run this, it doesn't work, and that's because these
05:30parentheses here have to either be escaped to work with basic regular
05:36expressions or if we don't escape them, we have to say this is an
05:41extended regular expression.
05:43So anytime we use those parenthesis in there, it has to either be extended or
05:47they have to be escaped.
05:49And just to make sure that you understand the difference here, let's say
05:51instead of day, let's say that it said something like, we'll put xxx for now.
05:57So you can see that it took those same three characters that it found.
06:00It didn't care whether they were day or something else.
06:03It took those and dropped them into the replacement string.
06:06Let me give you a real world example.
06:08I think that will make it clear why this is really useful.
06:11Let's say that I have a name like Dan Stevens.
06:14I can pass that into a sed script that will say all right, take any characters
06:19that could be in the first name, followed by a space, followed by any
06:23characters, and then reverse them with a comma between them.
06:26Look at that. Dan Stevens suddenly become Stevens, Dan.
06:30So you can see how you can do this, not just to this little bit of input that
06:33I'm sending it, but you could do it to an entire file.
06:36Let's try something similar by using our fruit file.
06:39So I have a sed expression here that's going to look for either apple, pear,
06:43plum, or peach, and for any of those it will append tree after it.
06:47So we get pear tree, then we get raspberry banana, then we get peach tree,
06:52apple tree, pineapple tree.
06:54So any of those that matched our regular expression got reused in our output.
06:58Now, there is a lot more that sed can do, but this shows you some of its most
07:02common uses, and I think it gives you a solid foundation for exploring
07:06further on your own.
Collapse this transcript
cut: Cutting select text portions
00:00So far we've seen Unix tools that will let you find, translate, and replace.
00:05The next useful Unix tool I want to introduce you to is cut.
00:09Cut allows you to cut out selected portions of each line of a file.
00:13We could probably write a sed command that would do something similar to what
00:16cut does, but cut is much simpler and easier to use.
00:19The first thing you need to know about cut is that it can cut three things,
00:22characters, bytes, or fields, and we are always going to need to pick one of those.
00:27We are only going to be looking at characters and fields, and the reason why is
00:31that bytes in English is going to be exactly equivalent to characters,
00:34because every character takes up exactly 1 byte.
00:37And really, bytes are there mostly for when you are thinking about raw data,
00:41not actual characters, but raw data, and you want to grab a certain amount of bytes out of it.
00:46We're working with text files here, so we're really thinking about characters,
00:48and most times that's what you will be doing.
00:50So characters and fields is where we're going to focus.
00:52So let's start with characters.
00:54Notice that I am inside my user directory, inside unix_files, and inside there I
00:59have this file that I created earlier called dir_content.txt.
01:03If we take a look at that file you will see that it's just a directory listing.
01:08It looks very much like the output from ls-la, because that's exactly what I
01:12did, ls-la earlier, and I directed that output into a file.
01:16So this is not my current director listing.
01:18It's a snapshot of what it was previously.
01:20So here's the scenario. Imagine that we have a file like this and we say, you
01:24know what, I like the data here, but I really wish I could just grab a
01:28selected portion of it.
01:29For example, I really wish that I could grab these permissions out of here,
01:33just that line and that part of this line, all the way down, essentially grabbing a column.
01:38Just wish I could grab that column of these permissions out of it.
01:42That's the purpose that cut serves.
01:44So we say cut and then we need to specify an option.
01:47Always have to specify an option. Either -c for characters, which is what we'll
01:52be doing, or -b for bytes, or -f for fields, which we will see in a moment.
01:56And then we tell it what characters we want to cut.
01:58Well, out of each line we'd like to cut characters 2-10.
02:03That's what represents these characters, starting with character 2, going until
02:06we get to character 10.
02:08And then we need to say the file that we want to do that from, and there it is. It's that easy.
02:13Now, notice that it also grabbed this up here, total 144.
02:17That was the top line here.
02:19It went ahead and grabbed that as well.
02:20So be aware of that.
02:22You actually, I believe, can suppress the output of this from your directory
02:26listing if you needed to, but for our purposes, we just really want to
02:29illustrate the way that cut works.
02:30Now, let's say we wanted to cut something else out of this. We can cut more than one thing.
02:35We can grab more data.
02:36So in addition from cutting the permissions, let's say that we also wanted
02:40to keep the file size.
02:41So we wanted the permissions, followed by the file size, and then after that we
02:45will grab the file name as well.
02:47So what we'll be left with is permissions, size, name. Everything else will
02:51end up being removed.
02:53So what we need to do is we look at the data here and you can see that this
02:56is the last column, so what we want to do is we want to find out how many
03:00characters is this?
03:01How many do I need to skip over essentially till I get to more data that I want?
03:05So I am going to use Command+C to copy that.
03:07Let's just do echo and we'll put that in quotes and pipe it into Word Count.
03:13Word Count tells me it's 21 characters.
03:15So now I know I need to skip over 21 characters, so 2-10, and then we'll do a
03:21comma, followed by skipping 21 characters starting with 10, so that would be 31.
03:27Now, I would also like to leave a space between them, so I am actually going to
03:30keep the space in there before it.
03:32So that would be going to 30 and then I'll go up to 35.
03:36That will give me the space plus the four characters here that represent the size.
03:40So there we go, now I have the size.
03:43Let's say I also want to get the name, and it's here.
03:47I want to leave a space here so I'll keep this space in.
03:51So I want to know how wide is that? Copy it.
03:53echo it into Word Count.
03:56That's 14 characters.
03:58So now I go back up here and I add 14 onto this and I come up with 49 to the end.
04:04If you want to go all the way to the end, you can just put a dash with nothing after it.
04:08You can do the same thing at the beginning too if you knew you wanted to get
04:10everything at the beginning.
04:11So now, look at that. I have sort of my own custom ls listing.
04:15We can take this same thing, let's copy it, and we can actually do that.
04:20ll, pipe through this cut statement, and look at that. I can see exactly the data I want.
04:27I can leave everything else out of there.
04:29You could take, for example, your history file and let's grep that for
04:34everything that has fruit in it and then let's take that and let's pipe that
04:37into cut, and I am going to cut everything 24 characters to the end.
04:42So I have got just then the command itself.
04:45I left off everything that was at the beginning of it.
04:47Or we could take ps aux, your listing of all your processes, and let's cut 11-15 and 72-end.
04:55Now, I have just got the process ID and what's actually running? Everything else
05:02that's inside ps aux. pipe it so we don't see everything.
05:06Now, all this stuff in the middle, that's all gone.
05:08I can really condense it down to just what I want to see, just the process ID
05:13and what is running.
05:14I leave out all those other stats.
05:16So that's how cut works.
05:18Cut also has this -f option that is really nice.
05:21I have a file here that I created earlier called us_presidents.tsv,
05:27tab-separated values.
05:28So it's just an information about U.S. Presidents
05:31that is tab separated values.
05:33You could use any other tab delimited file that you wanted to for this.
05:37The tabs are important, and the reason why I say that is because if we use
05:41cut -f, by default what it's going to do is use those tabs to figure out
05:46where the columns are.
05:48So if I say I want fields 2, 6 out of us_presidents.tsv, look at that. I get
05:56just the column that has the President's name in it and then I get just the
06:01state that they're from.
06:02Notice that it also kept the tabs as delimiters between these. So it's great!
06:06You can have something that's like tab- separated values from a spreadsheet and
06:10you can just grab the columns out of it that you want.
06:13We have something here which we can then save as another tab-separated file.
06:18So we can save this as presidents_states.tsv.
06:24And now we have this file saved that we can then continue to work with.
06:27We can now join that with other things, we can use it in sed scripts, whatever
06:31we want to do. We've just zeroed in on the data that we want.
06:34Now, I want to show you that we have another example of a file.
06:37I have us_presidents.csv and that's comma-separated values.
06:42We worked with that earlier as well.
06:44It's the same thing, but instead of tabs we have commas between the data.
06:47If we tried the same thing that we did before but on the csv file, let me
06:52just clear this so you can see, using csv this time, asking for columns 2 and
06:576, it doesn't work.
06:58It gives me everything back, because it was expecting the tab to be the delimiter.
07:03If you want to change the delimiter, then you have to use the -d option. So -d
07:09and then tell it what delimiter is.
07:10So if it's a comma, now it does it.
07:14Notice that it still kept the comma as the delimiter in the output as well, but
07:18the main thing is that this -d followed by what it should be looking for.
07:22By default it's tab.
07:23There are a couple of other options that it offers.
07:25There is a -s option and that would do nothing to lines that don't have delimiters.
07:30It would just let them through on their own.
07:32That's all there is to using cut.
07:33It's a fairly simple tool to use, but it's really quite powerful, especially
07:37when used in conjunction with a lot of the other Unix commands and
07:40techniques that we've learned.
Collapse this transcript
diff: Comparing files
00:00In this movie we're going to take a look at a Unix tool called DIFF, which is
00:03useful for comparing two files.
00:05Imagine that a client sends you revisions to a text document.
00:08You still have the original document but you can't tell where the client changed.
00:12You could put the two documents side-by- side and then scan each one looking for
00:16the differences then every time you find a difference you could take note of it.
00:18That's exactly what DIFF does for you, but much faster and with greater
00:23precision than you could.
00:24Using DIFF is easy.
00:25Notice that I am inside my user directory and inside unix_files and we're going
00:29to be working with two files that I've added to this directory.
00:32The first one is original_file.txt, the other one is revised_file.txt.
00:39Both of these are included in the Exercise Files but they're also easy for
00:42you to create yourself.
00:43The first one just has several lines that say delete, several lines that say
00:47change, and several lines that say append.
00:49And then on the far left side I've included the line numbers for reference.
00:53The revised file has something very similar except that now I've deleted one of
00:57the lines that says delete, changed one of the lines that says change, and
01:01appended a line to the section of lines that say append.
01:04Now what we want to do is ask DIFF to compare these two files and report the
01:08differences between them.
01:09We do that with diff and then as the two arguments we pass in the two
01:14filenames we want to compare.
01:15Typically, you want to put the old or original file on the left.
01:19You don't have to but that's sort of typical. original_file.txt revised_file.txt.
01:25DIFF compares the two files and reports the changes to us.
01:29Now the way that it reports those changes might seem a little cryptic at first.
01:32So let's understand what it's telling us.
01:34It found three changes altogether.
01:36The first change is represented by these two lines.
01:38The d in the first line is letting it know that it detected a deletion.
01:42Something was deleted.
01:43The numbers on either side of the d let us know the line number where this would
01:48occur in each of the two files.
01:50The number on the left corresponds to the file that was passed in on the
01:54left, the first argument.
01:55The number on the right of corresponds to what I call the right file.
01:59That's the second argument.
02:00Notice also then that it tells us the text what was deleted and it has an angle
02:05bracket at the beginning that's acting as an arrow letting us know that this
02:08occurs in the left file, original file.
02:11Second change that it found is described by these four lines.
02:15Now instead of a d we have a c letting us know that there was a change.
02:19Once again, we have the line numbers and you can see why it's useful to
02:22have those line numbers.
02:23Because the position of these two lines, even though they're being
02:26compared, it's changed.
02:27And if we had a lot of deletions, it might have changed a lot.
02:30This line might have jumped up a hundred lines in the file.
02:33Having the line number helps us to be able to locate the change regardless of
02:36what else has happened.
02:37Notice now instead of one line, it gives us two lines with dashes in between
02:42the two and the first one has an arrow pointing to the left letting us know
02:45that this is the text as it exists in the left file, and then we have an arrow
02:49pointing to the right thing, letting us know that this is the text as it exists in the right file.
02:53And then last of all, we have append which uses an a. So we have d, c, and a.
02:58Once again, it tells us the line numbers.
03:00Don't let it throw you.
03:00This says 11 and this says 12.
03:02That's because we deleted line 2.
03:05The arrow this time points to the right letting us know that this is the text
03:08that exist in the right file.
03:10So deletes will always point to the left, appends will always point to the right,
03:14and changes will always have arrows that point in each direction and show
03:17us the text from both.
03:19So even though it may have seemed cryptic at first, once we understand how to
03:22read it, it's actually a very efficient way to describe the changes.
03:26There are a number of other formats that we can output these results in.
03:28This is just the default one.
03:30We'll take a look at those but before we do that, let's take a look at some
03:33of the options that we can pass to DIFF for how it goes about comparing the two files.
03:37If we give DIFF the i option, then it performs a case insensitive comparison.
03:42A capital A and a lowercase a would be considered exactly the same.
03:45It would not bother reporting that difference to us.
03:48If we want it to ignore changes to the blank characters like Space and Tab or
03:52all of the white space or the number of blank lines that are in the file, well,
03:55then we could use the b, w, or capital B options.
03:59DIFF doesn't just compare files.
04:00It'll actually compare whole directories of files.
04:03We can use the lowercase r option to recursively compare two directories.
04:07So then it will look for files that have the same name in each of those two
04:10directories and tell us what the differences are going down each and every
04:14one of those files.
04:15Now if two of those files are identical, it won't report it.
04:18It'll just ignore it and just tell us about the ones that actually have differences.
04:22If we also want it to tell us about the files that are identical, we have to use the s option.
04:27These are the primary options for controlling how DIFF does this comparison but
04:30there are a few others and you can read the Man Pages to see what those are.
04:33But these are the most common ones.
Collapse this transcript
diff: Alternative formats
00:00In this movie we're going to learn to control the format the DIFF uses for output.
00:04We can have an output that's something called copied context, which it would be c
00:08option. We can use the u option for unified context, and then we can have y for
00:13side-by-side comparison.
00:14There's a q option that will tell us only whether the two files are different.
00:18It won't report what that actual changes were to us.
00:21It'll just give us a single line saying yes, the two files are different.
00:24There are also a couple of other output formats but I think these are going to
00:27be the most common and the most useful for you.
00:29Let's take a look at them.
00:30So let me just clear my screen and let's do that same thing but now let's pass
00:35in that -c for copied context.
00:38Now notice what it does is it gives us the whole first section, lines 1 through 11.
00:43It lets you know that here at the beginning. And here it is.
00:45That's everything that was in the first document.
00:48And then below that is everything that was in the second document.
00:50And it gives us a minus here letting us know that something got deleted and it
00:54gives us an exclamation point letting us know when something changed, and a plus
00:58down here letting us know when something was added.
01:01So this gives us essentially one set of changes and then right below it is
01:04another set of changes.
01:05Let's take a look at the y option next because that does essentially the same
01:10thing but it puts them side-by-side.
01:12Now in my screen it can be widened a little bit but if you used a smaller font
01:16in Terminal you would be able to see them really side-by-side.
01:19What it does here is it shows us one file on the left, one file on the right,
01:22and then puts some symbols here in the middle to let us know if something was
01:25deleted, changed, or added.
01:27Now let's look at the u option.
01:29That's actually the one that I prefer.
01:31This gives us unified context.
01:33What that means is it essentially is smashing the two files together.
01:36What we're seeing is not what's in either file one or file two, but if we
01:40merged them together, you'll see that we have line 2 here with a minus next to
01:43it letting us know that this is something that's going to get deleted from the original file.
01:47And down here we can see that the line is added, has a plus on it, and then
01:51the lines that have been changed here have a minus and a plus. The minus lets
01:55you know up here corresponds to original file while the plus corresponds to the revised file.
01:59Let's take a look at the q option so you could see what that does.
02:03If we compare the two files with q, it just comes up and says yes, these two files differ.
02:08That's it.
02:09it doesn't tell us what the changes are.
02:10Now let's just try it with two files that are the same such as the file
02:14compared with itself.
02:15Original file compared to original file.
02:18Now we get no output back.
02:19That's because they are the same.
02:20If we wanted to report the fact that they're the same, we need to use that s option.
02:24And then it comes back and says yes, they are identical.
02:27Now let me just show you a few more things.
02:28Let's go back up and let's pick this -u option version comparing original
02:32file with revised file.
02:33What I want to do now is I want to output that into a file. We'll call it
02:38original_revised and the file extension that want to use would be diff.
02:43That's the standard for things which are an output from DIFF.
02:46So now we have what are the changes between these two documents saved as a document.
02:50Let me also just show you that in a text editor.
02:53I have TextMate installed and I can just use the pipe it into mate and that'll
02:58pop it open in TextMate.
02:59I just want to show you that when you view that diff file it's something like a
03:02text editor that offers code coloring.
03:04It may offer you this kind of coloring for each of the lines which can be really handy.
03:08So now I can really easily see this is what was deleted, the green line down
03:11here is what was added, and the two lines here side-by-side highlights the
03:15two changes for me.
03:16Last of all, I want to show you that you can take this -u version and instead
03:19of piping it into my text editor, I am going to pipe it into another Unix tool called diffstat.
03:24And it has to be the -u option for this to work.
03:27Doing the default one won't work.
03:28But if we pass -u into diffstat, it will come back and it'll tell you what the
03:33changes look like. Just a summary of those changes.
03:35Now a change in a file, neither delete or an append, but a change will be
03:40described as both an insertion and a deletion.
03:43So it lets you know here that it basically has two pluses and two minuses in this file.
03:48Four lines changed altogether.
03:50If we were looking at a comparison of directories, well, then we'd have a
03:52whole list of these.
03:53That might help you to very quickly identify where the bulk of the changes
03:56occurred between the two.
03:58Now at the risk of stating the obvious, let me say that of course DIFF works
04:01best on files that are similar, such as our example where we have an original
04:05file and a file that's been modified.
04:07It's not very useful to compare two things that are really different.
04:10Let's say the Declaration of Independence with the Magna Carta.
04:12Well, then it would give you back every line as a difference. That's not useful.
04:17Of course, they're different on every single line.
04:19But if we're really trying to just track the changes where we know that
04:22something is similar but has a few differences, we really want to just identify
04:26those differences, then DIFF becomes an indispensable tool.
Collapse this transcript
xargs: Passing argument lists to commands
00:00In this movie we'll learn to use xargs to pass argument lists to commands.
00:05xargs is kind of a funny name.
00:06It's short for execute as arguments.
00:09What xargs does is it parses an input stream into items and then it loops
00:13through each item in that list and passes it to a command.
00:16I think it's easier to understand if we actually see it in action.
00:19So you remember that we had the wc command before for word count.
00:23So if we do word count on our lorem_ipsum.txt file that's inside our Unix files
00:28directory, you see that it comes back and it tells us the number of lines,
00:32words, and characters that are in lorem_ipsum.
00:33Now, let's try a variation on this.
00:35Let's say if we want to echo lorem_ipsum .txt and we want to pipe that into wc.
00:43Now what it's doing is it's saying not the file lorem_ipsum.txt, but this
00:47string, this actual text lorem_ipsum.txt, how many characters does that have? There is 16.
00:53So that's what it's doing.
00:54It's not looking at the file.
00:55It's looking at the string.
00:57If we instead want to pass that string to wc as an argument, then we use xargs.
01:03That's what it does.
01:04So now we get the exact same result as we did before.
01:08It's the exact same thing.
01:10It took the string and the input and it passed it to the command wc.
01:15The man pages refer to this as the utility commands.
01:18So it passes it to its utility command.
01:19xargs gives us a nice way to see what it's actually doing using the -t option.
01:24So now we can actually see the command right before it runs.
01:27It will output what its doing.
01:28So it's running wc lorem_ipsum.txt.
01:32Now, if we don't supply a utility command at all, let me just show you what it does.
01:36It actually just echoes.
01:37it calls bin echo and then the file.
01:39So as a default setting, it will just echo that argument back to you.
01:42Now, that explains what xargs does.
01:44It passes its input as an argument to a utility command.
01:48But the power of xargs is in looping through a list of arguments.
01:52Let's imagine that instead of just this that now instead we are going to have
01:55lorem_ipsum.txt us_presidents.csv.
02:01And let's go ahead and put that again with the word count at the end.
02:04Now you can see that it passes both of these as arguments to word count.
02:10So it's the same as if we had typed we lorem_ipsum.txt us_presidents.csv.
02:15But sometimes you don't want xargs to run its command with all of the arguments.
02:20What we want is for it to loop.
02:22And so we can do that by using the -n option to limit how many arguments it passes.
02:26So we'll just say 1 to start with.
02:28We can also take that space away to make it a little more clear that the 1 goes
02:31with the n, and now it will take one argument, pass it to wc, and then it will
02:37loop and it will take the next argument and pass it to wc.
02:39So n is the number of arguments that we use on each loop through.
02:43So now that you see the difference between what it did before. Now it's actually
02:47calling wc two times, not once, but twice.
02:50And to see an example of this using higher numbers of n, let's say that we had
02:54echo 1 2 3 4, and let's pipe that into xargs, and then -n2, and we don't have to
03:02specify anything. We're just going to echo those back.
03:04Now you see what it does.
03:051 and 2 get passed in as an argument, then 3 and 4 get passed in as an argument.
03:08-t will make that really clear.
03:10It's passing in the first two, then it goes through again. A second loop and
03:14passes in the next two.
03:16If there had been an uneven number, well, then that last one would just get
03:19passed in by itself.
03:21So for example, let's say that we have ls to list our directory. We can pipe that into xargs.
03:26That will be all of the file names in our directory, and we'll do -n 3 and
03:31echo, and now you see we get three file names and then we get a new line.
03:35We get three more file names, because it's just echoing each of those file names broken up.
03:39xargs also has a -L option which limits what it takes in.
03:43Let me show you that.
03:45If I just had echo 1 2 3 4 and I pipe that into xargs with the capital L 2
03:53option, you don't really notice a difference, because it's all just one line.
03:57But let's instead use a multiline document and it will become a lot clearer.
04:00Let's say we have head of lorem_ipsum.txt, pipe it into xargs, and we will use
04:05-L 2, and I won't specify the echo.
04:08That's the default. What we're seeing is the first two lines of the file
04:13echoing back to us.
04:14If I instead use -n, you'll see that we get the first two words of each line.
04:19Now, the n and L options are mutually exclusive, so you don't want to use both.
04:23And they can be a little bit tricky, because xargs can find its arguments by
04:26splitting on either spaces or on lines, and it depends a little bit on the
04:30format that the input was sent in and whether or not you've specified the n or L the option.
04:35It can be a little tricky, so you may have to play with it a bit to get it to do
04:38exactly what you want.
04:39Let me show you another example.
04:40Let's say we have cat, our file fruit.txt, and we'll pipe that into xargs.
04:46So what xargs is going to do is just echo each one of those back in a line.
04:50But here is what I want to show you.
04:51We can actually instead of just saying we want to echo this, we can actually
04:55put a placeholder. We'll specify it with the capital I option, then we can use
04:59the two curly braces and then we can actually place this somewhere in position.
05:04So for example, I can have buy more:
05:07and then it will drop in the value where I've put this placeholder. See what it does?
05:14So now we have the ability to actually not just take that argument, but to take
05:17that argument and put it inside something, to put in a specific place rather
05:21than just forcing it to just use the argument by itself.
05:25You don't have to use these curly braces.
05:26That's the most common one, but you can actually specify absolutely anything you want here.
05:31So I could have fruit. You just want to make it something that's unique. :FRUIT.
05:39Right? And it will drop it in there.
05:40So all you're doing is saying all right, with the -I option here is what I am
05:44going to declare as my placeholder and wherever you see that, that's where the
05:47argument ought to go.
05:48There is one last catch to using xargs. And that's that xargs, when it tries to
05:53figure out where the arguments are, it will split things up based on either
05:56spaces or control characters.
05:58This can cause some problems.
05:59So for example, let's say I do ls of my Library directory. So inside Library,
06:05let's take a look at what's in there.
06:07Let's just grab the first few of those by piping this through grep.
06:11So in grep I'll look for A, followed by anything that comes after it, and so it
06:15will just show me those first four.
06:18Now, let's take that list and let's pipe that into xargs and let's just have
06:21it echo it back to us.
06:22That's all we're going to do. We'll use the n1 option, so it will break it.
06:25Each item that it finds, each argument, will get sent through to echo one time.
06:30See what happened? Application Support got broken in half.
06:34Autosave Information got broken in half.
06:36It thought they were each different arguments, because it split it up on the space.
06:39So you see why this causes the problem.
06:42In order to get around this what we need do is the -0 option and that will tell
06:48it to split on null characters as separators instead of on spaces and new lines.
06:53So now when I hit Return, now it correctly does break them up.
06:58The main place that you are going to use that -0 option is with file names.
07:02If you're working with file names, you probably want to use that -0 option to be safe.
07:07So it's a bit of a quirk, but it is something that you have to keep in mind as
07:10you're passing things to it is, is there a chance that there will be a space in
07:14the data that I'm passing in that would screw things up?
07:16That gives you a good explanation of what xargs does.
07:20Next, let's try some examples of how we can put it into action and make it work for us.
Collapse this transcript
xargs: Usage examples
00:00Now that we have seen how xargs works, I want you to help you to see the power
00:03that it gives by demonstrating some of its uses.
00:06There's no limits what you can do with xargs, but these example should give you
00:09an idea of how other people typically use it and get you thinking about ways to
00:12make it work for you.
00:13The first example I want to show you is how to use xargs with what I call File Manifest.
00:18Notice that I am inside my user directory and inside UNIX files and in there,
00:22there is a file called file_manifest.txt.
00:25It's just simply a list of file names of files that are in the same current directory.
00:30The idea here is that I have selected the files that I care about and have
00:32arranged them in a certain order.
00:34Now I can use xargs to do something with those.
00:37Now what you do with them is up to you.
00:38You can open them, you might grep them, you might print them, or you might just
00:43want to concatenate their contents together.
00:45They might represent file templates that we want to copy over whenever we
00:48start a new project.
00:50For now, I'm just going to use cat just to show you the contents of them and
00:53then pipe it through less, so that we just don't get a huge stream of output.
00:56So there you go, you can see that that's exactly what it did.
00:59It took each of these files and put them together in the order I specified.
01:03Imagine that you had 20 different files that all belonged to one project and
01:06you wanted to assemble those together in a certain order for presentation or something.
01:11You could use this file manifest technique to be able to manage them.
01:14It's not just file names that we put in a file that we can use.
01:17We could for example use our fruit file, fruit.txt.
01:20That's just going to be a simple list of fruit.
01:22Let's take that and let's pipe it through sort and unique.
01:25So we have the unique list of fruit. And then we will send that to xargs.
01:30And in xargs, we will use our placeholder specifier, make a directory, including
01:35the parent directories, and we will put it on our desktop, a folder called
01:40fruits and in that we will put a folder for each of these fruits.
01:45There it is, we have fruits now that appeared on our desktop. I am going to
01:47open that folder up.
01:48I have got a folder for each one of my fruits.
01:51Imagine that we have got a class of 30 students and we need to create a folder
01:55to put each student's work in for all 30 students.
01:58We take the list of their first names and last names, pass it to xargs and boom,
02:02we have created 30 folders with just one single command.
02:05You can see how it can really speed up repetitive tasks that we normally
02:08would do in the Finder.
02:09Let me give you another example. Let's say that we have our process list and we
02:13want to pipe that through grep and we are going to look for some process, some
02:17set of processes that have gone bad somehow, we need to get rid of. There are
02:21bad processes we'll call them and if I just hit Return on that, you will see
02:25that it comes up and if finds the grep that I am doing for. bad process.
02:28That's the only process that matches.
02:30So this is just a demo, and what we will do is we will pass that in to cut and
02:34we will grab, 11-15, which will represent this process ID here, and then we will
02:39take all of those process IDs that it finds, pass them in to xargs with kill -9.
02:45Boom, we just killed all of our bad processes.
02:48Now it came up and told me there were no such processes and that's because
02:51this process that if did find, grep bad processes, is gone by the time it gets
02:55over here to kill it.
02:57It started and it stopped before kill could ever get a hold of it.
03:00But you see the concept and it shows you how you can use xargs in some unusual places.
03:04In general xargs works really well with grep.
03:06So for example, we had grep for apple inside all the files called fruit.txt and
03:12it gives us back all of those matches.
03:15Remember we used the -l option in front of it, dash lowercase L. Now it gives us
03:20just the file names themselves.
03:22So now these files are ready to be passed into xargs.
03:25We have found everything that had a match. Now we can do whatever we want with
03:28those files that have a match. We can take them and
03:30we can pipe them into xarg's word count.
03:33We can find out the word counts of those. Or we can concatenate them altogether.
03:37It also works really well with find.
03:39So for example, we could find everything that's in my test directory and
03:43everything that's typed as a standard file, and pass that to xargs and change
03:48the permissions on that to be 755.
03:51Now remember, I told you that sometimes we need to use this -0 as an option to xargs.
03:57Well, dind is one of the places where we really want to use it and find even
04:01has a special option of its own called print0.
04:04So print0 will make sure that the null character is used to separate them and
04:08then the -0 on xargs will make sure the xargs uses that null character.
04:13So it's a way for the two of them to communicate effectively.
04:15We can go ahead and hit Return on that.
04:17Let's try another example.
04:18Let's do find and we will look everything in the current directory whose
04:22name matches fruit.
04:24We will do that one again and we are going to use print zero, pipe it through
04:28xargs with -0, and then let's use our placeholder specifier and what we are going
04:34to do is copy each one of those arguments to Desktop and we are going to use
04:40that placeholder again, but also append.backup to the end.
04:44So I hit Return and guess what? It went and found each one of those files that
04:49had a match, send it to xargs, and then copied it as a backup file to my desktop.
04:54Now, let's say that we want to remove all of those backup files.
04:57So we have Find and Desktop.
05:00Everything on the Desktop whose name has backup at the end and we will do the print0.
05:07Pipe it through xargs with -0 and then we can use rm.
05:11Now you might think well, let's use rm-i for interactive.
05:15Actually xargs doesn't allow you to do interactive.
05:18If you want xargs to be interactive, you have to use the -p option for prompt.
05:23And this will work for any kind of Xarg's thing and basically it will prompt
05:26you before it actually does it.
05:27It gives you a chance to say yes or no.
05:29There is one other thing that I want to caution you about here, which is that
05:32when we are doing a find, especially if you are doing a find that is going to
05:35then do copy or remove or anything like that, it's a good idea to make sure that
05:39you are not searching too deeply in the folders.
05:42If I really just want to get rid of these things that are on my Desktop, then I
05:45need to specify the depth is going to be equal to one.
05:48Just look in the current immediate folder, don't go any deeper, or if I want it
05:52to allow to go two folders deep, then you could specify depth 2.
05:55You want to make sure that you don't accidentally grep more files than you intend to.
05:58All right, let's hit Return on this.
05:59You will see xargs give you a prompt saying hey, do you want to remove all these files?
06:03First I'll start with saying n and then Return, so that's no.
06:06I am just going to make a change to it now.
06:08I am going to say -n1 as an option.
06:11So it will take each one of these as a separate argument.
06:13Now, it comes up and says, ah, do you want to remove this one?
06:16Y, boom, it's gone.
06:18Y. We get a chance to approve each one of them before they disappear.
06:23The -n option makes sure that they are sent in, each one as its own argument.
06:26And I have shown you that xargs works well with grep and it works well with find
06:30and it works really well if we use both.
06:31Let me just paste in a fake example here.
06:34Imagine that I am finding everything in the current directory whose filename
06:37has invoice anywhere in it.
06:39Pass that to xargs and then tell grep, hey, look inside that subset of
06:43files and see if you find the word programming and tell me what those files names are.
06:47Of course, it is fake example. I don't have any files called invoice.
06:50Imagine that we are a web developer and we are looking for all files in our
06:54website that have .html at the end, pass those off, and grep those to find a
07:00certain tag that we are looking for, like h3.
07:01Now again, this is just a demo example, but you get the idea.
07:05Now not only would I be able to use find and then pass it to xargs and grep it
07:09but what I am getting back is a list of file names because I used that -l option.
07:13So guess what?
07:14I can use xargs again on the file names that it returns from that.
07:18Let me show you a real example.
07:19Here is one where we say all right, find everything whose file has fruit.txt,
07:23grep it for mango inside the file and then pass that to xargs a second time,
07:29and this time we are going to copy it to our desktop. So there we go.
07:33Boom, it searched for the file name, looked for the right file content, and then copied
07:38the file to our desktop.
07:39These examples are just a small sample of what you can do with xargs.
07:42I want you to think of using xargs, whenever you want to perform a command on
07:46many items in the list. That list can be recorded in a file, as we saw with
07:50the file manifest, or it can be dynamically generated by another command, as
07:53we did with grep and find.
07:55And if you learn to use xargs effectively, it will really speed up your workflow.
Collapse this transcript
9. Useful Mac-Only Commands and Techniques
Finder integration
00:00Almost everything we've learned so far has been portable, meaning that it
00:03will work in any version of Unix, not just in Mac OS X. Of course, we did
00:07discuss a few Mac idiosyncrasies along the way, but the commands and
00:10techniques are fundamental Unix.
00:12In this chapter, we're going to look at some techniques that will work only on
00:15Mac OS X. We'll begin by looking at how we can integrate our work in the Unix
00:19environment with our work in the Finder.
00:21The simplest of these is how we can take a Finder path and be able to use it in Unix.
00:26So let's say we're inside the Finder, we're working, we've got windows open,
00:29and we suddenly want to take this folder here and reference it inside Unix for some reason.
00:35Well, all we have to do is grab it, drag it in, and let go.
00:37In Mac OS X, we'll then paste the path that Unix needs to reference that same thing.
00:42Now I switch to Unix.
00:44I'll hit Ctrl+A to go to the front of the line. cd space.
00:47Now I've moved into that Sites directory.
00:49You can use this for any command that takes a path, whether it's copying or
00:53moving or making symbolic links. For anything that uses a path you can simply
00:57drag the object from the Finder into the Terminal window and then Mac will give you the path.
01:01This is really useful if you need a path to a deeply nested directory.
01:05Instead of typing the seven or eight parts of the path to get there, if it's
01:09already open in your Finder window, well then just drag it in there.
01:12Save yourself the typing.
01:13Now you can also do the reverse, that is, take a Unix path, and use it in the Finder.
01:18Mac OS X has a Unix command called open that will open things in the Finder.
01:21folders, files, applications, even URLs.
01:25So let's change to our Home directory, and from here if we just say open
01:30unix_files, then it opens that folder.
01:33If we want to open a specific file, we say open unix_files/lorem_ipsum.txt and
01:38now it opens up that text file in whatever our default text editor is.
01:42For me, that's TextMate. It may be something else for you.
01:45Essentially, it's just like if we double-click on that file or that folder.
01:49One technique that comes in really handy is that if you're moving around inside
01:53Unix and you get to a certain directory and you really wish you had that
01:56directory open in the Finder, well open period. That references the current
01:59directory so it pops it up in the Finder.
02:01Then anything that you're more comfortable doing inside the Finder, maybe
02:05renaming files, throwing things in the trash, all those things can be done in the Finder.
02:09And anything you're more comfortable doing in Unix, you can then do in Unix.
02:12You can do the same thing with the parent directory with dot dot.
02:15Open doesn't open files and folders.
02:17It'll also open the applications.
02:19So for example, open -a calculator will open up the calculator.
02:25The -a option basically says go to the Applications folder and find a Mac
02:30application there with the name that matches.
02:32If you want to open a specific file in a specific application, well, then you
02:35just say open -a TextEdit and then let's use our lorem_ipsum.txt.
02:41Now it opens up inside TextEdit.
02:43One time-saving tip is that you can create aliases for some of these that you
02:46use often, so like alias firefox= 'open -a firefox'.
02:53Now whenever you type firefox, it opens up Firefox for you.
02:57Remember if you want to use these aliases, you'll need to put them in your bashrc file.
03:01open will also accept input from standard n if we use the f option.
03:05So for example, ls -la can be piped into open with the f option, and then it
03:10will open up the contents.
03:12Instead of outputting it to the screen here in our Terminal, it will open it up
03:16in TextEdit in this case.
03:18This is nice because it allows us to use a lot of the power of Unix to construct
03:21exactly what we want and then we can send it to a regular Mac application.
03:26So for example, I can find the fields in us_presidents.tsv that I want and I can
03:31send them to Numbers or Excel or another program like that.
03:34And it will open it up, and there it is, and you can see that it's got them in
03:38nice columns for me.
03:39Another nice trick is that we can look at the man pages in Unix in preview.
03:43So if we type man -t and then whatever we want to search for.
03:47The t is just going to say format this as PostScript and then it will pass that
03:52off to fa Preview, so that takes a second while it composes it.
03:57And the last thing that open will do that's really useful is open will open URLs for you.
04:02Basically, what it is doing is that it says "All right, if what you're sending me
04:05is http:// well, then I know it's a URL and I know I need to open it up with
04:10whatever your default browser is."
04:12So for example, if we want to open lynda.com then it will open up my default
04:16browser which is Firefox and open up the lynda.com web site.
04:19Again, this is a place where you can create aliases for things that you use often.
04:23So if there are web sites that you frequent often, you can make aliases for them
04:27or, you could keep a file that has a list of many URLs and you could use xargs
04:32to open them all at once for you, or you could grep that file for a subset of
04:36URLs and then use xargs on it.
04:38I think you can see how we've learned to combine some of the Unix techniques
04:41that we've learned to speed up our workflow in the Finder.
Collapse this transcript
Clipboard integration
00:00On the Mac you're probably with the concept of the Clipboard.
00:03You're inside a document in one application, you select some text, you copy it,
00:07you switch to another document or another application, and you paste it there.
00:11Well, in Unix we don't have a global clipboard like that.
00:13But we can make use of the same one that the Mac Finder uses.
00:16And we can use it within Unix or we can use it to ferry data back and forth
00:20between Unix and the Finder.
00:22To do it we'll use a pair of Mac-only Unix commands called pbcopy and pbpaste.
00:27The pb stands for Pasteboard.
00:29Now when you're going to select something and copy it in the Finder, you use
00:32your mouse to select it.
00:34But in Unix we don't have a mouse.
00:36I mean of course I have the mouse here.
00:37That's the Mac OS allowing me to work with Terminal.
00:41But in Unix itself, it's all command line.
00:43So in order to select something we're going to need to have input.
00:48And we've already seen how to work with input in Unix.
00:50So let's say for example that I hit ls -lah.
00:53If I just hit Return, that'll go to standard out.
00:56But instead what I can do is pipe that output as input into pbcopy.
01:01Now that same text that would've been output is on my Clipboard.
01:04We can see that if we just switch into TextMate real quick.
01:07We'll open a new document and paste it in there.
01:09This can be really handy when we're working in Unix and let's say we're trying
01:12to pull out a couple of columns out of our us_presidents tab separated values file.
01:16Well, we can find everything that we want.
01:19Now we certainly could select this and then we could scroll back up the page,
01:22but that might be a lot.
01:24Instead, a much simpler way would just be to simply take that and send it to pbcopy.
01:28Now without any output and without having to switch and use our mouse to copy
01:32the text, we've got it on our Clipboard.
01:34We can switch to the Finder and we can paste it in there.
01:37And we can use our other techniques for directing input, so pbcopy and let's
01:41pipe in our lorem_ipsum.txt file.
01:44Now the entire contents of that file are on the Clipboard.
01:48We didn't open it, we didn't have to scroll up at the Terminal output and select
01:52it and then copy it, fill up our Terminal window.
01:54All we did was take the contents of the file, put it on our Clipboard so we're
01:58ready to do something with it.
01:59Now the companion to pbcopy is pbpaste.
02:02So we just do pbpaste by itself.
02:04It outputs whatever is on the Clipboard to the standard out. So there we are.
02:09We see it in the Terminal window.
02:10We can instead direct that output. We can for example, send it to a file, so clipboard.txt.
02:17Now that content is in a file.
02:19One nice trick that I like to use with directing output from pbpaste is
02:23to create an alias.
02:24I'll call it pbsort and from that what we'll do is we'll say take whatever is on
02:27the Clipboard and paste it, pipe it into sort, and then pipe the results of that
02:32back onto the Clipboard.
02:34Replace what's on the Clipboard essentially with now the new sorted version.
02:37Now remember, aliases that you want to keep around you'll need to put in your bashrc file.
02:41But this will serve our purposes.
02:43Let's say that I have a list: monkey, zebra, lion, tiger, and bear.
02:46I'll take that, send it in to translate to convert the commas into new lines so
02:50each one will get its own line, and then I'll put it on my Clipboard.
02:54Now I'll just call my pbsort and now it's sorted.
02:57pbpaste, you can see it, or we can switch into another document somewhere else
03:02and paste it in there.
03:03Now the original list doesn't have to start in Unix either.
03:06We could, for example, copy this, put it on our Clipboard, switch over to Unix,
03:11and regardless of what directory we are in, we just type pbsort. Boom!
03:14My Clipboard is now sorted.
03:16We come back into whatever application we were working in and we paste the
03:19sorted version back in.
03:21The last trick that I want to show you is how to use multiple clipboards.
03:25Have you ever wished that you had more than one clipboard when you were
03:27working on the Mac?
03:28Well, it may surprise you to learn that Mac OS X actually has four clipboards.
03:32It has the general Clipboard which we've been using here and then it has one
03:35called find, one called font, and one called ruler.
03:39The find one you may have bumped into before because it's whenever you do a
03:42find in something like your web browser for example. You do a find there, it
03:47puts it on the Clipboard, and then when you switch to another application,
03:50you do a find there, you may see that same value pop up, and you may have
03:53wondered how did it get that, how did it have that same value that was in
03:56that other application?
03:57Well, it's because it was on the Find Clipboard.
04:00From Unix we can actually directly access these.
04:03So for example, let's say we have First, and we'll send that to pbcopy, and then
04:08we'll specify the clipboard we want with pboard followed by either the words
04:12general, font, find, or ruler.
04:14So general is the one we've been using.
04:16Let's now echo "Second" and let's pipe this to pbcopy, but this time the
04:23Pasteboard that we'll use will be the find.
04:27Now if we just say pbpaste, which one will we get?
04:30Well, we get the contents of the general one.
04:32But if we say pbpaste with pboard find, now we get the contents of the second one.
04:37It can be a really handy way for you to juggle data.
04:39If there is something on your Clipboard that you don't want to lose, well, you
04:42can just switch it to another Clipboard.
04:43Just pipe it off of one Clipboard onto the Ruler Clipboard let's say, and then
04:48go ahead and do the regular copy/paste you need.
04:50When you're done you can issue another command that will pipe it back from the
04:54Ruler Clipboard onto your general Clipboard and you won't lose the data.
04:58I think you'll find that using pbcopy and pbpaste will really enhance the way
05:02you work with the Clipboard.
Collapse this transcript
Screen capture
00:00In this movie we're going to learn to take screen captures from the command line.
00:04Do you already know how to do screen captures in the Mac?
00:06It's simple. You just hold down the Command key, the Shift key and hit the number
00:103 key and it will take a screenshot of your screen and put it on your Desktop.
00:15There is another version of that. If you hold down Command+Shift+4 you'll get an
00:20interactive screen capture which means that you get crosshairs.
00:23You select exactly the portion of the screen that you wanted to capture and when
00:27you let go just that portion of the screen gets turned into a capture as a file
00:31on your Desktop. We can do the exact same thing from the command line plus
00:35there are a few extra options that we don't normally have available to us.
00:38Let's see at its most basic first.
00:40From the command line you just simply type screencapture. It's that easy. Followed
00:46by the name of the file where you want it to go.
00:48So screencapture and let's put it on our Desktop and then I'm going to call
00:52it screen_capture.png.
00:56By default the screen captures are going to be PNG files.
00:59Now already this is a slight improvement over the regular Mac version, because
01:03we get to specify our own location and our own filename.
01:06Whereas we can do the other way the Mac gives it a filename based on the time
01:10that the picture was taken and puts it on your desktop.
01:12So let's hit Return and it takes the screenshot and puts it on the Desktop.
01:16Now in addition we have a number of options that we can specify.
01:19There is the lowercase -i option and that's put us in that interactive capture
01:23mode exactly, the same as if we done Command+Shift+4. Once you have those
01:27crosshairs and you decide you don't want to take the screen picture you can just
01:30hit Escape key to cancel out of it.
01:32There is a lowercase -m option if you wanted to capture your main monitor only.
01:36That's of course only if you have multiple monitors.
01:38By default the Mac will capture all monitors.
01:41Then there is a capital -C for showing the cursor.
01:44By default the cursor is not shown in the screen capture. If you did want the
01:48cursor for some reason, you couldn't do that from the Mac, but you can do it
01:51from the command line.
01:53You can use the -t option followed by a Format if you wanted it in a different
01:56format than png. You can use pdf, jpg, tiff.
02:01There are number of others as well, but these are the main ones.
02:04The capital -T allows you to specify a Delay.
02:07So you put the -T option followed by a number of seconds that you wanted it to wait
02:11before snapping the picture.
02:13Once it finishes snapping the picture you can use the -P Option to open that
02:16file with Preview or the -M option to send it directly to Mac Mail and put it
02:21in a mail message. Or if you don't want to even have a file you can use the
02:24lowercase -c option and capture it directly to the clipboard.
02:27Let's try a few to these out.
02:28So this time instead of the simple version I'll do screencapture, but I'll
02:34specify some options.
02:35Let's say I'm going to use lowercase -m to get my main monitor, capital C to
02:40include the cursor. We're going to open in preview when we're done with the
02:43capital P, and then let's put a delay on it capital -T 3 and wait 3 seconds.
02:48Then let's also change the format using lowercase -t, make it a jpg file, and this
02:53time I'm going to save it in my current directory which is the Unix files
02:57directory inside my user directory.
02:59So I'll just save it there and I'll call it screen_capture.jpg since the
03:05JPEG file this time.
03:07So there we go. I hit Return.
03:08It waits 3 seconds and then it snaps the picture and it opens it up in Preview for us.
03:13So here is the actual screen capture.
03:15So again all of the special configuration options are available in the man pages
03:19if you need to look them up.
03:20Now if is the kind of thing you find yourself doing often, then create an alias for it.
03:23That has all the configurations that you want. Put that in your bashrc file and
03:27then you can just type something simple like screencap and voila!
03:30You'll get your screen capture configured exactly the way you wanted.
03:34So again this is the exact same screen capture you can normally do on the
03:36Mac, but we just have these extra enhancements available to us through the power of Unix.
Collapse this transcript
Shut down, reboot, and sleep
00:00In this movie I'm going to show you how you can shut down, reboot, and sleep your
00:04computer from the Unix command line.
00:06Not only can we perform these actions just like we can in the Finder, but from the
00:10command line we get an extra cool bonus we can schedule these actions.
00:14Now before we get started I want you to proceed carefully. I don't want to be
00:17responsible if you put your computer to sleep or reboot and you lose data in a
00:20file that you're in the process of editing.
00:22So take a moment and make sure that all your files are saved and everything
00:26so that if you did accidentally trigger a shutdown you wouldn't lose anything important.
00:30Now the command that we're going to use for all three of these, for shut down,
00:33reboot and sleep, is going to be the shutdown command. That may not seem
00:37intuitive, but that's the command that we use for all three of them and in order
00:40to use the shutdown command we always have to use it with sudo.
00:43So sudo shutdown and then immediately after we have to specify one of three
00:48options. We have to tell it we want to shut down, reboot or sleep, and we do that
00:52with the h, r or s option. h stands for halt, so halt would be to shut down.
00:58All three of these options then after that need a time. They want you to specify
01:01what time you should shut down and the time format can be one of three things.
01:06You can either say shut down now, shut down in a certain number of minutes from
01:10now and this case use the plus sign followed by a number, or at a specified date
01:15and the date format will be the two last digits of the year followed by two
01:19digits for the month, two digits for the date, two digits for the hour, and two
01:23digits for the minute.
01:24Now it's a little difficult for me to demonstrate shutting down my computer, so
01:28let's just take a look at what some of these might look like.
01:30So if we wanted to just do shutdown now the same way that the Finder does it,
01:34well then you would do sudo shutdown with the H option for halt and then the
01:38word now. If we wanted to schedule it for 45 minutes from now then you could do
01:42the same thing but use +45.
01:45And if we wanted to shut down at a specified date and time well then you use the
01:48date, remember for the year you leave off the first two digits, so it would be
01:52110601 and then zeroes to represent midnight.
01:57Reboot works exactly the same way the only difference is that now we're using
02:00the r option instead of the h option.
02:03So the first one, reboot now, is the exact same behavior as the Finder has or we
02:07can schedule in the future at either a relative or an absolute date.
02:10And then for sleep we have the same thing but with the s option.
02:14One place that I find this especially useful is that if I'm working late at
02:18night, it's almost bed time and I'm doing some long-running process. I'm FTPing a
02:22whole lot of files up to a server and its going to take another 30 minutes.
02:26Well I can go in to Unix and I can say all right, one hour from now I want my
02:30Mac to go to sleep. Then I can go to bed, my file transfer are finish, and then
02:34when I'm done my computer will just go to sleep, ready for me to then come back
02:37in the next morning.
02:38Now in addition to the shutdown command there also is a Unix command for halt
02:43and reboot, but they're not preferable. It's better to use shutdown.
02:47So even though you may see halt and reboot in there, shutdown is a better choice.
02:51Now for sleep there is a sleep command in Unix, but it's totally different.
02:56Because in the days when Unix was embedded computers didn't go to sleep.
02:59They were either on or they were off. They didn't have energy-saving modes.
03:03So the sleep command in Unix actually just tells Unix to wait for a specified
03:08amount of time, so if I say sleep 3, then it waits for three seconds and then it
03:13pops back up and give me another command prompt. You can use this with commands.
03:17So sleep 3 and then we could do something hello and wait three seconds and it
03:23will do the thing that I asked it to do.
03:25So it does have a use especially once we start doing scripting in Unix, but
03:29it's completely unrelated from sleeping your computer the way that you're
03:32normally used to doing it.
Collapse this transcript
Text to speech
00:00In this movie we'll learn how to use the Mac's text-to-speech functionality
00:04from the command line.
00:05We can type something and then our Mac will save it for us.
00:08Now before we start making our Mac talk, be sure to check your computers volume
00:12level make sure it's not set a something too high.
00:14The way that we are going to do it is using the Mac only Unix command say,
00:19and then after that we'll tell it what we wanted to say, so we're going to
00:22have "Unix is awesome".
00:25(Computer voice: Unix is awesome.)
00:26That's it there is all there is to it. Now the voice that is talking in is the
00:30voice that set as the default voice for your Mac. There are also other voices as well.
00:35We go into the Finder hold down Command+Shift+U it will open up the
00:39Utilities folder and then right below Terminal you see there something
00:42called VoiceOver Utility.
00:44If you open that up and you click on the Speech option, you'll see that what the
00:47default voice is set out and you can click on this and see list of all the
00:51choices for all the voices that you can pick.
00:54If you want to use a different voice from the command line you can do it on a
00:57per command basis so we could have say hello and then use the -v option to
01:03specify the voice, so let's say with Vicki.
01:05(Female computer voice: Hello.)
01:07Or Fred. (Male computer voice: Hello.)
01:09Or Whisper. (Male computer voice whispering: Hello.)
01:14Or Zarvox. (Computer voice: Hello.)
01:18So you can have fun playing with that, trying all those out.
01:21In addition to saying a single string we also have the ability to pipe in input.
01:25So here I'm taking a string from echo.
01:27But it could really come out of any command at all. Pipe it in to say and we can
01:32have it them say it.
01:32(Computer voice: Dumm dee dee dum dee dee dum.)
01:36Or you can have a read from a file. I've got a file on here just called
01:40shakespeare.txt, just a simple Shakespeare sonnet. I can say that with the -f
01:45option, so the shakespeare.txt.
01:47(computer voice: Shall I compare thee to a summer's day? Thou art more lovely and more temperate.)
01:52And Ctrl+C will exit out, if you want or need to break out of that. Remember that
01:56you don't have to listen to the whole thing.
01:58In addition to reading in content from a file, you can also send the
02:02text-to-speech output to a sound file by using the -o option.
02:06It's not exactly books on tape quality, but for someone who is visually
02:10impaired this could be really useful. Another handy technique is to use say as
02:14a notification that'll long- running process has completed. Just put it
02:18immediately after the command using a semicolon then go to work on something
02:21else while your process runs. When it's done you'll get an audio message
02:25letting you know that its finished.
02:27And then lastly you can have some fun with this.
02:29Wait until someone steps away from their computer then ran a command like the
02:33last one to make them think that their computer is talking to them.
Collapse this transcript
Spotlight integration: Searching metadata
00:00Spotlight is a fast and powerful Macintosh find tool that's based on file metadata.
00:05We can access Spotlight from the command line too and I'm going to be honest with you.
00:09This isn't my first choice for finding files.
00:11Most times if I'm working in the Finder I need to find something based on
00:14metadata and typically going to go up here to the Spotlight menu in the upper
00:18right corner and type my query there. And most times if I'm working in Unix in
00:22the command line, I'm going to tend to use find and rep, but there are times
00:26when you really need Unix to have access to the metadata of the Spotlight keeps.
00:30So let's see how we can use it from the command line.
00:33The command that we're going to using is mdfind. The md stands for metadata.
00:37Now the metadata for file contains much more information about the file than we
00:41normally have access to from the command line.
00:43As an example, let's say that you have an MP3 or a song that you've purchased on
00:47iTunes. Well that has a file name. It's probably the same name as the song and we
00:52can see that in the file system. We could search it for using find or grep, but it
00:56also has a lot more information associated with it.
00:59It has the artists name, the album name, the track number, the running time, the
01:04bit rate that it is encoded at.
01:05All of that information is stored in the files metadata and we have the ability
01:09to access it by using Spotlight or by using mdfind.
01:12The way that we use it is just simply to tell mdfind what our query is.
01:16So mdfind and then new and then we hit Return and it does the exact same find as if
01:22we'd come up here and typed New into Spotlight.
01:25Now you could add more to your search the same ways as you can type other terms
01:29into the Spotlight window, so I reduced the number that I got here. But really
01:33whenever possible I really recommend very strongly that you use -onlyin option
01:39and then tell it where you want it to look and then just give it a regular path.
01:43This will massively limit the amount of searching that it does.
01:46So it'll make it a faster search and it'll also give you less results that you don't want.
01:50Now if you really do want to search your entire hard drive, then by all means
01:54go ahead and leave it out, but most times you at least know roughly where you
01:58want to be looking.
01:59So you can go ahead and say ah this is in my documents folder, this is in my
02:02music folder, this is in my sites folder, and limit it that way. I also want to
02:06show you that you could include negatives in there.
02:08So for example, I got three files back I could say well I don't want anything
02:12that has join in it.
02:14Well now left out the one joins.txt. It just gave me the other two files results back.
02:18So again it's just like if we typed in the Spotlight menu. The documentation
02:22actually says that it's a really good idea to use the interpret option passed
02:27as well to make sure that it really does interpreted exactly like you typed in there.
02:31Now my experience is that most times you don't need it. Most times that
02:35interpret option is overkill, but I just want to mention it just in case you're
02:38wondering why a certain search here isn't coming up. You may need to force it to
02:43strictly interpret the same with the spotlight query window would.
02:46In addition to asking for a query string we can also use the name option and
02:52specify something that has exactly a file name that matches.
02:55So you see we got back something slightly different this time. Now the hits were
02:59based on the file name new and new.
03:02If you remember when we're using the regular find it also had a print O option
03:07that allowed it use null as a separator between each of the results. That made it
03:11really easy than the pass off to xargs and xargs was enable to use it reliably.
03:15Well, we have the same thing here and it's the -o option, so then we can type that
03:19into xargs and then in the xargs we would need to use the -o option there as
03:24well and then we can do something with it. Let's say we'll use our open command.
03:28So now we'll pop off those open in text made for me.
03:31Now this is the simple usage of it and probably the way that you'll use it most
03:35of the time, but I do want to show you that you can go even deeper and we'll
03:38take a look at that in a next movie.
Collapse this transcript
Spotlight integration: Metadata attributes
00:00I want to look at another command related to metadata, which is mdls.
00:04So we're looking at metadata listing and we can look at a file, like
00:08our lorem_ipsum file.
00:09And we can peek at the metadata for this file.
00:11So now we can see all the metadata for it, which can help us to figure out what
00:14we're trying to search on.
00:15And you can see that there is a lot more stuff stored in here that we
00:18normally have access to.
00:19Let's just look at another one, just as contrast, mdls. We'll look at this JPEG
00:24we created, the screen_capture.
00:26Notice here it's got all sorts of things like in the color ProfileName.
00:29It's got the PixelWidth, the PixelHeight, and all this metadata that's in here
00:33we can actually search on as well.
00:36So you could find images that match certain width and height parameters.
00:39You can be really specific.
00:40And those for each one of these we've got the key over here and the value on this side.
00:44So the key is in the name of the attribute that we want to look for.
00:48So let's say for example this one, this is the Metadata Item File System Name
00:53that we're looking for. That's the key.
00:55So let's just copy that and we're going to now construct a new query using that to search.
01:00mdfind and I'm going to use my -onlyin option again. unix_files and then in
01:07single quotes I'm going to put that, equals, and what do I want it to equal, and I put
01:12it in double quotes so they can tell the difference, and I'm going to search for
01:15lorem_ipsum as our first example.
01:17So lorem_ipsum, I've got an * on either side.
01:20That is used as a wild card for any characters that might be on either side of it.
01:24Then I hit Return and it found our lorem_ipsum file.
01:28Now notice if I take the wild card characters away, now it doesn't find it.
01:32It is looking for an exact match, right?
01:34That's what this == here is telling it that it should be an exact match.
01:37And notice that I said put single quotes around this.
01:40Now it would work fine in this case if you reversed them and you did the double
01:44quotes in the outside and the single quotes in the inside, but when we start
01:47working with time, it doesn't always work.
01:50And so once you start working with time, you want to make sure that you're
01:52working with single quotes.
01:53A lot of people run into problems and wonder why it's not working for them.
01:57Now you'll notice that I told you to use single quotes around the outside and
02:01the double quotes on the inside.
02:02It would have worked if we've swapped them and done the other way around. In this case.
02:07But in some cases that's not true, especially like the ones we're about to see
02:10when we're working with time.
02:12So let's say that we have the exact same thing we had before, but now instead of
02:17KMD, we're going to look at ItemFSCreationDate and we're going to say greater than or equal to
02:25and now we can use the special time object, time.today to start with. Single quotes.
02:32Again, single quotes are important when working with all of these times. Oops!
02:35I left a space here. That's why it didn't find anything.
02:38Let's remove that space and try again.
02:41And now it finds the Shakespeare file that I created earlier today.
02:44In addition to using this keyword today, you can also use yesterday, this_month,
02:49or this_year, and there is also a one that's for now.
02:53Now it's not very useful to search for things that are now, but we can provide
02:57an argument to that that is a relative time to now in seconds.
03:02So -36000, let's say, inside parentheses.
03:06Let me just widen my window so that's not wrapping and you can see a little bit better.
03:10So now -36000 seconds and now it shows me any files that meet that.
03:15I didn't have any. If I just broaden it by another 0, now you can see the
03:19ones that come back.
03:20Now searching in seconds might not be the most useful. We can also provide that
03:24same kind of option to today as well.
03:26So today(-2) is everything in the last 2 days.
03:29Now we're not specifying seconds anymore. Wwe're specifying in days. Or if we did
03:34something like this_week(-1).
03:37Well now we'll see all the files that we were created this week, sending for
03:41this month and this year, and if you do this year, then the number you
03:44specify is in years.
03:46In addition to having relative time, you can also specify absolute time by using ISO.
03:52So, time.iso. Again, this is where it is important to make sure that you're
03:56using single quotes. The double quotes won't work in this case. And then you put
03:59a time in the ISO format.
04:02So this is what the ISO format looks like the year, the month, the date with
04:06dashes between it, space and then the time, the hour, minute, and seconds with
04:10colons, and then the time zone offset after that.
04:13So now it will come back and tell me all the files that we were created since that time.
04:18So again, let me just say that searching by all of these attributes is the most
04:22powerful lowest level you can do. You don't have to do that. You can just simply
04:27put those simple query strings in there and have it find everything.
04:31If you really do know that you want to target something really specific though,
04:34I just want you to know that you have the ability to go in and find exactly the
04:38metadata that you want.
Collapse this transcript
Using AppleScript
00:00In this movie I'll show you how to use the Unix command line to communicate
00:03with the AppleScript.
00:05I'm not going to try to teach you to write AppleScript. I'm certainly no expert
00:08on it myself, but I will demonstrate a few useful basics.
00:11But if you already know how to write scripts on your own, well, then you'll be
00:14able to quickly link the power of AppleScript with the power of Unix and do
00:17some really cool things.
00:18Now the way we're going to call AppleScripts is by using the Mac only
00:22Unix command osascript.
00:25OSA stands for Open Scripting Architecture.
00:27And after that, we can either provide the file name of the script that we want to
00:31run, or rather the path to that file, or we can use the -e option and actually
00:35give our instructions right here in the string.
00:38So I can say set volume output muted true.
00:43And that will mute our computer. Or I can say output muted false.
00:47These two are handy because we can put them in an alias.
00:50So for example, I could just jump to the beginning of the line here. alias mute=
00:55and I'll put double quotes around it, jump to the end of the line and put double
00:59quotes at the end of that.
01:00and then let's do the same thing this time with false. We'll make it unmute.
01:06Now I can say mute, unmute.
01:09Now remember, if you want to keep those aliases around, you'll need to put them
01:12in your bashrc file.
01:14We can also use AppleScript to talk to different applications, and the Finder is
01:18actually considered application.
01:20So we can have an AppleScript that looks like this, osascript -e 'tell
01:26application "Finder" to display dialog "Hello"'. Hit Return.
01:34You see it starts bouncing down here. I click on it, and there is my dialog box.
01:39Once I click OK, it comes back and it actually returns a value to me, which I
01:43could also catch and do something within Unix if I needed to.
01:47Now once we start talking the applications, very quickly one line stops being enough.
01:52Usually we don't just want to tell an application to do one simple thing, but we
01:55want to give it a set of instructions.
01:57So that's when you really go about writing scripts and putting them in files.
02:00I've created a sample script that you can take a look at.
02:03It's going to be inside the Unix files folder that's included with the exercise files.
02:08And I'll just let you see what it looks like.
02:09It's pretty small script.
02:10It just says tell application iTunes and now instead of just a single thing,
02:14we're going to have a list of things that are activate, set the sound
02:17volume, play, wait 10 seconds, switch to the next track, wait 5 seconds, and then finally quit.
02:23Now if we want to run the script we just type osascript and then the name of it,
02:28control and then itunes.scpt.
02:31That's typically what you would name the end of your scripts. So it would be .scpt.
02:35Hit Return.
02:37Wait one moment while iTunes opens up.
02:38Then it will play this song for 10 seconds.
02:43(Music playing)
02:48Switches to the next track.
02:51That should play this for 5 seconds and then it shuts down.
02:54Now you can look up more information about iTunes and you can find out there is
02:57a lot more that you can do.
02:58You can manage your playlist, things like that.
03:00You can search for things. All that you can then do from the command line by
03:04integrating Unix with AppleScripts.
03:06Let me give you another example so you can see how we can actually talk to an
03:10application and have it return data to us that we can then work within Unix.
03:14Let's take a look at another sample script that I've included in the exercise files.
03:17You can control the address book, so control_address_book.scpt.
03:23And there is another very simple script.
03:24It says tell the application Address Book create an empty list called emailList,
03:29then count the people that are in the address book, and then for each one of
03:33them go through and get the value of that person's email and put it into the
03:37emailList and at the end return that item to us. So let's try it.
03:41So osascript control_address_book.
03:47You can see that it opens up, then it comes back and it gives me a list of all
03:50the email addresses that are in there.
03:52Now this is data, this is data that we can use, so we can take that and we can
03:55pipe it into something.
03:56Let's pipe that into seed. We have a comma-delimited list, but I want to take out
04:00the space that is right there. That should do it.
04:05So now it's a comma-delimited list without that extra space.
04:09Now let's take that and let's put that through tr.
04:12Tell that comma it should turn into a line return.
04:16Now it's at each line and now let's take that and pipe it into sort and finally
04:22let's go ahead for good measure and put it through uniq.
04:25And then if we wanted at the end of that we could pipe that then into a file
04:28and we would have an export, a dump, of all of the email addresses that were in there.
04:32Now we could get more creative about the kinds of things that we pull out by
04:36using a script. Maybe you want to pull everyone's address out or you want to
04:39pull the first name and the last name.
04:41The idea here is to show you that you can tell these applications to send data
04:46back to you that you can then work within Unix.
04:50You can put on your clipboard.
04:51You can send it to another script that will then send it to another application.
04:54There is almost no limit to what you can do here, because now you've the ability
04:58to use AppleScript not just to work in Unix, but actually control programs and
05:03have them do your bidding.
05:04Now if you are a hard-core AppleScript user, I also just want to mention that
05:08there is one other command in Unix that maybe useful, which is called osacompile
05:11and that will actually compile scripts for you as well.
05:15So for most of you, osascript will be enough just be able to run scripts, but
05:19for some people you may want to make use of the osacompile command as well.
Collapse this transcript
System configurations: Viewing and setting
00:00Mac OS X has hundreds of configurations and settings to control your
00:04environment that you work in.
00:06Some of these can be set in your system preferences but others are actually
00:08inaccessible to most users.
00:10But through Unix we can access those configurations and even modify them.
00:14In this movie we'll learn how to do that. Before we begin,
00:17keep in mind two things about setting configurations.
00:20The first is you want to be super careful that you don't accidentally change a
00:23configuration that you didn't mean to change.
00:25You might set something into an undesirable state.
00:28The second thing is that some changes are going to take effect immediately, but
00:32others are going to require either that you log out and back in or that you
00:36completely restart your Mac.
00:37So just because you don't see a change right away doesn't mean that it didn't
00:40actually take effect.
00:41It really just depends at what point the Mac loads in that particular
00:45preference, and that's going to vary from preference to preference.
00:48Let's start by seeing where the configurations are stored. I'll use Command+N to
00:52get a new window in the Finder and I'll open another Finder window here to put
00:55next to it, and then this one I'll use Command and click on the bar to go up to Macintosh HD.
01:02Now we have a Library folder here and we have a Library folder here. These are
01:06where the different configurations are stored.
01:08These are the configurations which are global and apply to every user on the system.
01:12these are the ones that are just for me in my particular User.
01:15Inside the Library for each of them, there is also a Preferences folder.
01:19That's where these are typically stored.
01:21Now they may exist in other places in some cases. Here is the Preferences for
01:24this one, there we are.
01:25But most of the time these are the two places that you'll find them and
01:28they really depends on whether the preferences are global preference or user preference.
01:32Let's go inside the user one here and let's take a look at this. I'll actually
01:35close up the global one.
01:36And you notice almost all of these have the same format. The name is always com, period,
01:42the company name, followed by the application name, and then typically it ends
01:45in plist at the end.
01:47Remember this format because we're going to need to use it.
01:49Plist let's you know that it's a preference list. We can open that up in a text
01:53document but if we double-click on one, we'll open up the Apple Property List
01:57Editor that they give you with your Mac.
01:59I've opened up the one is for the Finder.
02:00So do the same, just so you can follow along with me.
02:02Now what we she here are the keys and values for all the configuration options.
02:08So for example the Preferences window location has the following coordinates.
02:13My EmptyTrashProgressWindowLocation here will be placed at the following coordinates.
02:17It remembers where we've moved that progress window as the trash is emptied.
02:21If we move it someplace else, it keeps track of those coordinates in its
02:24preference list and that's why it opens up in the same place every time.
02:28Now you can open up a lot of these, your SearchViewSettings, you see what's
02:31inside there. RecentFolders, DesktopVolumePositions,
02:35ComputerViewSettings, BrowserWindowState, and open up you can dig around and see what's there.
02:39If you double-click on something here you can actually change these values.
02:43So you can change them directly via the Property List Editor.
02:46We're going to do instead is learn to read and write these values directly
02:49from Unix without using the Property List Editor, and I'll show you why that's a better solution.
02:54The way that we're going to do that is by using this Unix command called Defaults.
02:58It's a program that's a Mac only Unix program that will manage these
03:02default settings for us.
03:04One benefit is that it takes care of whether or not these are user preferences
03:08or global preferences.
03:09It goes and finds it in the right place and makes the change for us and we
03:12don't have to dig around at all.
03:15And after the defaults command we can put several other commands that we
03:18want defaults to run.
03:19We're going to be looking at read and write. There are few others and you
03:23can look at the man pages to see what those are, but read and write are the most useful.
03:26So read the domain and the key that we want to read. That will return the value
03:31to us so we know what the current setting is.
03:34Or we can set a value by saying defaults write, the domain, the key, and the value.
03:39And that domain is typically in the format com.companyname.appname. Remember we
03:44just saw that, com.apple.Finder.plist. Well that's typically the domain and
03:49it's the same thing as the file name.
03:52So for example we can do defaults read com.apple.finder and then we can read
03:58CopyProgressWindowLocation and that gives us that same string that we saw in
04:05the plist editor right here, 169, 270, and it is a string.
04:10Now we can change that value using write.
04:13So now instead of that let's use Ctrl+A to shoot to beginning of line, we'll
04:18change this from read to write, and use Ctrl+E to go to end of the line, and now
04:23we need to provide a value. So the value here is just going to be a string,
04:27which means it's in quotes, and inside there we'll just put 168, 270.
04:33Put a space between so it's really formatted the exact same way. Make sure we don't mess anything up.
04:37Now we just change that first value from being 169 to 168, essentially one pixel
04:42of difference for where that window will be located.
04:44If we go back to the Property List Editor and let's close that up, just open
04:49that again, Finder, and now you see that the value is changed. 168, 270.
04:54So that's really all there is to it.
04:56It's just targeting the preference that you want with the right key name and
05:00then either reading or writing a value to it.
05:02Now the other thing that's nice about defaults though is that some settings
05:06won't be listed. Sometimes even the Config file itself won't exist.
05:10It won't even be in the Preferences folder.
05:13Defaults will take care of creating those files for you as needed and make sure
05:17that they are in the right format and everything.
05:19Now how do you know about these settings and what to change, if you can't go in
05:22and see them in the plist files?
05:24Well quite honestly, you typically learn about them from the Internet.
05:27Word gets out from Mac software developers who know the Mac API really well and
05:32they know what these available options are and so then word gets out that this
05:36is the way you change something.
05:37It spreads on the Internet until people find about the sort of mysterious hidden
05:41options that they can then type the commands here into defaults and it will set
05:45it for them and create the files as needed.
05:47In the next movie we'll take a look at some popular changes.
Collapse this transcript
System configurations: Examples
00:00In this movie, I want to show you a few of the most popular system
00:03configuration changes.
00:05The first one will show all the dot files in the Finder.
00:07Remember that by default the Finder hides all our dot files so that we can't see them,
00:12but we could change that behavior. If we want to make them visible we'd
00:15say defaults write com.apple. Finder and then ShowAllFiles true.
00:20If you ever decide you want to turn it off again, ShowAllFiles false.
00:22Or if you want the Unix path to display in the Finder windows up at the top,
00:28well its default is write com.apple. Finder, and then _FXShowPosixPathInTitle
00:35using all the correct capitalization, and then -bool TRUE, setting a Boolean to true.
00:42If you wanted to go away then you'd just set it to bool False.
00:46We can also configure some of the defaults for screen capturing. We saw a little
00:49bit about how to trigger screen capturing from Unix.
00:52We can change how the Mac does its screen capturing permanently even in the
00:56Finder by setting defaults write com. apple.screen capture. Type is we can set
01:02a different file type.
01:03I have listed off most of them there for you. You could pick one of these and if
01:06you say, oh you know what I wish it would capture it and save it as a PDF file
01:09every time, well you can have it do that.
01:12You also change the location where it places that screen capture.
01:15By default it's going to put it on your user's desktop, but if you use defaults
01:19write com.apple.screen capture location, then you can provide the path to where
01:24your documents save instead, so maybe you say I want to save it in
01:27/users/Kevin/pictures or /pictures/ screen captures. It can be whatever you want.
01:36The one caveat with that is that you do want to use a full file path. You don't
01:40use any kind of shortcuts like the till date, represent your user directory.
01:43Give it the full entire working path so that it can reliably find it.
01:47And last of all you can change the background image for when you log into your
01:51Mac by using the defaults as well.
01:54You know the picture that I'm talking about?
01:55It's not the desktop picture that you get after you've logged in.
01:58This is the image that's behind that log-in screen.
02:01Currently there's not a way to change that image inside System Preferences.
02:05Maybe they'll make that possible in the future, but right now that's a hidden
02:08option. You can't change that picture.
02:10Well let's say you want to put your company logo in the background or something
02:13or pictures of your kids there, right behind the login screen,
02:16as soon as your computer comes up. You can. All you have to do is use defaults to
02:20change com.apple.loginwindow DesktopPicture to the path to the new picture
02:25that you want to change it to.
02:27Notice that I've also used sudo in front of this one.
02:30That's because these defaults live in that system configuration, every user will
02:34get this preference, and so we need sudo to make that change system wide.
02:38Once again you want to provide a full path.
02:40Don't have any shortcuts to it.
02:42Now a lot of people have resorted to a low-tech version of making the same
02:46change and that is they go into their library of desktop pictures and they move Aqua
02:50Blue.jpg out-of-the-way and they put whatever image they want in its place
02:55within name Aqua Blue.jpg.
02:56That's bad for a number of reasons.
02:58First of all now Aqua Blue is no longer there if you need it for something else
03:02and your other image is mislabeled.
03:04It's better to do at this way with the preferences. And so you can just change it
03:07to this, and if you want to change it back to Aqua Blue later, you just change
03:10the preference again.
03:11So you can see the increase level of power and control that Unix gives you over your Mac.
03:15Not only can you do really powerful cool things in Unix but you actually can get
03:19under the hood of your Mac and make changes to the way that it works as well.
Collapse this transcript
Conclusion
Conclusion
00:00I want to thank you for taking Unix for Mac OS X Users.
00:03Over the course of this training title we've come a long way, from simply
00:06changing directories in the command line all the way to power user techniques.
00:10And don't worry if it takes a while for some of these commands become a routine.
00:13Remember that you always have the man pages to remind you of the command syntax and
00:17its available options.
00:18I've been using Unix for 20 years and I still look up things all the time, and
00:23those built-in man pages? They're also one of the best resources for you to
00:26continue advancing your Unix skills.
00:29One point that I hope you've picked up on while we we're learning is that
00:31Unix is as much a part of your Mac as the Finder is. In fact Unix is what
00:35makes the Finder possible.
00:37The Finder is a layer of convenience that sits on top of Unix.
00:40So when you want convenience, use the Finder. When you want power, use Unix.
00:44And when you can switch back and forth, and integrate both in your workflow then
00:48you can work faster and get more power out of your computer.
00:51And you may not appreciate it at first
00:53but the Unix is the most fundamental building block of the Internet and all modern computers.
00:58Concept such as shells, user ownership or permissions, directing standard input
01:02and output, controlling processes, regular expressions, all these things show up
01:06in other operating systems, applications, and programming languages.
01:10Now that you know about them, you'll begin to notice how often they come up.
01:14Unix was first and everyone built on its solid foundation, so by becoming
01:18comfortable in Unix you've not only empowered yourself as a Mac user,
01:21you've given yourself a computer literacy that can be applied to hundreds
01:24of other uses.
Collapse this transcript


Suggested courses to watch next:

Mac OS X Lion Essential Training (6h 4m)
Christopher Breen


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,405 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,893 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.