IntroductionIntroduction| 00:04 | Welcome to Unix for Mac OS X users.
| | 00:06 | My name is Kevin Skoglund and as a web
developer I probably spend as much time
| | 00:09 | working in Unix as I do writing code.
| | 00:12 | In this course we are going to learn to
use the powerful Unix operating system
| | 00:15 | that's built into Mac OS X. I'll walk
you through the fundamental concepts used
| | 00:19 | in Unix, we will discover the Unix file
system and learn to create, find, copy
| | 00:24 | and delete files and directories all
from the command line, and we'll see how
| | 00:27 | user accounts function in a multi-user
environment, and learn how to manage file
| | 00:31 | ownership and user permissions.
| | 00:33 | We will also cover dozens of useful
Unix commands, everything from simple
| | 00:37 | calculators to tools that monitor and
manage every process running on your computer.
| | 00:42 | I'll introduce you to some power user
techniques that will allow you to find,
| | 00:46 | alter, or capture almost any
text or system information.
| | 00:49 | Now most of what we cover will be
useful in any Unix environment, whether it's
| | 00:53 | Mac, Linux, or something else.
| | 00:54 | But in the final chapter I'll
demonstrate many useful Mac only commands and
| | 00:58 | techniques that can speed up your
workflow and give you more power and
| | 01:01 | control over your Mac.
| | 01:02 | It doesn't matter if you're a complete
beginner or have some prior experience.
| | 01:06 | We are going to cover all the
fundamentals you need to become comfortable
| | 01:09 | working from the Unix command line.
| | 01:11 | So let's get started learning Unix.
| | Collapse this transcript |
| Using the exercise files| 00:00 | If you are a premium member of the
lynda.com Training Library or if you're
| | 00:04 | watching this tutorial on a disc,
you'll have access to the Exercise Files that
| | 00:07 | are used throughout this title.
| | 00:09 | The Exercise Files for this title are
arranged by chapter and by movie, and you
| | 00:14 | can find the Exercise Files that
correspond movie that you're watching by first
| | 00:18 | looking for the chapter
number and then the movie number.
| | 00:21 | You'll want to copy the contents of that
folder into your user directory or to
| | 00:24 | another convenient location.
| | 00:26 | It's always a good idea to make a copy
so that you still have the original to
| | 00:29 | refer back to if you make changes.
| | 00:31 | To do this on my Mac I open up a new
window using Command+N, which by default
| | 00:36 | opens in my user directory, and then I
can simply Option+Drag over the folder
| | 00:40 | contents from the
Exercise Files to create a copy.
| | 00:45 | Once you do that your files will be the
same as mine at the start of that movie
| | 00:48 | and you'd be able to follow
right along with me from there.
| | 00:52 | For some movies, the Exercise
Files include one or more dot files.
| | 00:56 | These filenames start with a period and
are normally hidden from view by the Finder.
| | 00:59 | So these files would be able to show up
in the Exercise Files they've been renamed.
| | 01:03 | We removed the period and we added an
_example to the end of the filename, to help make it
| | 01:08 | clear that the file will not work as is.
| | 01:11 | In order to work with these
files you have two options.
| | 01:14 | The first is that you can open the file in
a text editor to view and copy its contents.
| | 01:18 | But take note that you may not be able
to double-click the file to open it like
| | 01:21 | I did, because there is no file
extension at the end, like .txt.
| | 01:26 | In that case you can drag the file
onto the application's icon where you can
| | 01:30 | select it using the
application's Open File menu option.
| | 01:33 | The other choice is that you can copy and
rename the file from the Unix command line.
| | 01:38 | You want to use the Copy command so
that you can make sure you retain the
| | 01:41 | original one and I recommend using the
-i option to make sure that you don't
| | 01:44 | accidentally overwrite an existing file.
| | 01:47 | After that, to get the path to the file
the shortcut is simply to drag the file
| | 01:51 | into the Terminal and that will write
the full path you need out and then you
| | 01:55 | can put the location of
where you want to put it.
| | 01:57 | This 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:06 | Once you do that the file will be copied and
you'll be ready to work right along with me.
| | 02:10 | You can also just use the Exercise Files as a
reference to check your work as you go along.
| | 02:15 | Now you won't need any special
software installed. Everything you need is
| | 02:18 | already included with Mac OS X.
| | 02:21 | If you're on a monthly or annual
subscriber to lynda.com you will not have the
| | 02:24 | Exercise Files that accompany this tutorial.
| | 02:26 | But you can follow along with me.
| | 02:28 | Everything that's in the Exercise Files
we will create during the tutorials.
| | 02:32 | So as long as you continue to work
right along with me your files will exactly
| | 02:36 | mirror what's in the Exercise Files,
and remember that you can pause the video
| | 02:40 | and rewind if you need more
time to copy something down.
| | Collapse this transcript |
|
|
1. Introduction to UnixWhat is Unix?| 00:00 | To start things off let's begin by
getting an understanding of what Unix is and
| | 00:04 | how Unix got to the point where it is today.
| | 00:06 | Unix is an operating system.
| | 00:08 | It was created during 1969 to 1971 by
AT&T employees working at Bell Labs.
| | 00:12 | A lot of these Bell Labs employees have
been involved in designing a mainframe
| | 00:17 | operating system called MULTICS, which
was short for Multiplexed Information
| | 00:23 | and Computing Service.
| | 00:25 | Back then, mainframes were expensive
so users were granted time-sharing slots
| | 00:29 | to do their work and to use the power
for the mainframe, often by logging in from
| | 00:33 | a remote terminal that had
far less computing power.
| | 00:36 | MULTICS was designed to manage that
remote login time-sharing process.
| | 00:40 | But MULTICS became such a huge and complex
project that Bell Labs decided to pull out of it.
| | 00:45 | The Bell Labs employees who'd worked
on MULTICS still like the project's
| | 00:48 | goals and so, almost as a side project
they worked on building a smaller and
| | 00:52 | simpler version of it.
| | 00:54 | Originally, their version only supported
a single user, so that as a play on the
| | 00:58 | name they called it UNICS,
| | 01:00 | Uniplexed Information and
Computing Service instead of Multiplexed.
| | 01:04 | Very quickly though it became able
to support multiple users and so they
| | 01:08 | renamed it Unix with an X, which doesn't
actually stand for anything anymore. It's just Unix.
| | 01:13 | Now at this point Unix is just another
mainframe operating system being used
| | 01:17 | internally by Bell Labs.
| | 01:18 | But two important events give
Unix a big boost in popularity.
| | 01:23 | First, in 1972 Unix was
rewritten in the C programming language.
| | 01:28 | Up until that point, operating systems
were typically written in Assembly language.
| | 01:32 | Assembly was a very low-level
programming language that required the code to be
| | 01:35 | written for specific computer hardware.
| | 01:38 | It's very fast, but it's not portable.
| | 01:40 | If you wanted to port an OS to a new
computer type, then you have to rewrite
| | 01:43 | major portions of your code to do it.
| | 01:45 | The C programming language was a
programming language originally designed for
| | 01:49 | the Unix operating system to write
programs for the Unix operating system.
| | 01:53 | It's a low-level programming language
but not as low-level as Assembly, and so C
| | 01:58 | makes writing programs easier
than Assembly and also makes them
| | 02:01 | hardware-independent.
| | 02:02 | So the important development here is
that not only do they write programs in C,
| | 02:07 | but they took the additional step and
said, "Hey, what if we rewrote the entire
| | 02:10 | operating system in C, so that
then Unix becomes portable too?
| | 02:14 | We don't have to rewrite the operating
system whenever we want to take it to a
| | 02:18 | new type of hardware."
| | 02:20 | The next important event is the
spread of Unix outside AT&T. Because of a
| | 02:24 | court order in an antitrust case
AT&T had been forbidden from entering the
| | 02:29 | computer software business.
| | 02:30 | So therefore they could not sell the Unix
operating system. They can only give it away.
| | 02:35 | So that's exactly what they did.
| | 02:37 | If you wrote to them and asked them
for a copy of Unix, they would give you a
| | 02:40 | free license and send you
all the source code for it.
| | 02:42 | That made it very attractive to
government agencies, universities, and
| | 02:45 | corporations who had these mainframe
computers because now they could get a free
| | 02:50 | license and all the source code to
be able to run on their mainframes.
| | 02:54 | Perhaps the most important of
those three is actually universities.
| | 02:57 | Because if you think back, in 1975 this
is a point when computer programming is
| | 03:02 | really beginning to take off and there
are a lot of people at universities who
| | 03:04 | are learning about computers for the
first time, getting access to mainframe
| | 03:08 | computers for the first time, and what
are they using? They are using Unix.
| | 03:13 | So Unix becomes the first
thing that these programmers learn.
| | 03:16 | So they go on to find all these
software companies throughout the 80s and 90s
| | 03:20 | and the thing that they first
started working with was Unix.
| | 03:23 | So the first time release in 1975 is
often referred to as System Five and that's
| | 03:28 | kind of the first public version of Unix.
| | 03:31 | But over time more branches and improvements
are released and that keeps going even today.
| | 03:36 | The most famous of these is probably
the Berkeley Software Distribution, also
| | 03:39 | just called BSD for short. That came
out in 1977 and then there've been other
| | 03:45 | ones that are either open source,
closed source, or mixed source.
| | 03:48 | Linux is open source. Closed source
versions, each major software company
| | 03:52 | seems to have their own.
| | 03:53 | Sun Oracle has Solaris, IBM has
AIX, and Hewlett-Packard has HP UX.
| | 03:59 | And of course the mixed source version,
mixture of open source software and
| | 04:02 | closed source software, is Apple's Mac OS X.
| | 04:06 | So it's gotten to the point now with all
these branches and improvements that no
| | 04:09 | one is really using Unix, the pure
System Five Unix that originally came out.
| | 04:14 | So Unix now really means a Unix-like system.
| | 04:18 | There are all these different
versions, they all have their quirks and
| | 04:20 | peculiarities, they are a little bit
different, but they all work according to
| | 04:23 | the same principles and are very, very similar.
| | 04:26 | So if you hear Unix now,
it really can mean all of these things.
| | 04:30 | In fact mobile devices now are Unix too.
| | 04:32 | iPhone, the iPad and Android are all
based on Unix. They are Unix like systems.
| | 04:37 | Now of course the version that we're most
interested in is going to be Mac OS X Unix.
| | 04:42 | Mac OS X is based on BSD Unix,
the Berkeley Software Distribution.
| | 04:47 | But in addition to that there is
code that was developed at NeXT.
| | 04:50 | NeXT is a software company that Steve
Jobs founded after leaving Apple and they
| | 04:54 | created code that they called NeXTSTEP,
which went on top of BSD Unix. When Apple
| | 04:58 | bought NeXT they got all of that code
and took it and added more improvements to
| | 05:03 | it and called the
combination of those three Darwin.
| | 05:06 | So if you hear anyone refer
to Darwin, that's what it is.
| | 05:09 | It's BSD Unix plus NeXTSTEP's code
plus Apple's continued improvements to it
| | 05:14 | that make up Darwin, which is the Unix
that sits underneath Mac OS X. Mac OS X
| | 05:20 | includes a lot more than that.
| | 05:21 | It's the Finder and all
these other things as well.
| | 05:23 | But Unix is under the hood.
| | 05:25 | The Finder and system
preferences interact with Unix.
| | 05:28 | Now we can interact with Unix directly
by using the Terminal application and
| | 05:33 | we'll start doing that in the next movie.
| | 05:35 | The command line is going to allow us
to type the commands that we want Unix to
| | 05:38 | do, instead of using the graphical user
interface that you're familiar with, with
| | 05:43 | your typical Mac
operating system. So why do this?
| | 05:46 | Why use Unix directly when you
have this graphical user interface?
| | 05:50 | Well there is a lot more
power in the command line.
| | 05:53 | Imagine this scenario and think
about how you would do it in the Finder.
| | 05:56 | Search your hard drive for every file
whose name contains invoice and which was
| | 06:00 | created after a certain date.
| | 06:02 | Then remove the file extension from each
filename and save that list to a text file.
| | 06:07 | Now with Unix that type of data
intensive task is very easy to do.
| | 06:11 | It's not the answer to everything.
| | 06:12 | You could never do in Unix what you do
in Photoshop for example and it's nice to
| | 06:16 | be able to just drag and drop files
into folders with the mouse instead of
| | 06:20 | having to type the command into the keyboard.
| | 06:22 | But as a general rule when working with
data, non-data elements can get in your way.
| | 06:27 | I think of it as being very similar to
the difference between car transmissions.
| | 06:31 | If you have an automatic transmission you
give up control for having the convenience.
| | 06:35 | You don't have to worry about shifting gears.
| | 06:37 | It just does it for you.
| | 06:38 | But you're not necessarily getting the best
shifting possible or exactly what you want.
| | 06:43 | With a manual transmission you give up
the convenience but instead you get control.
| | 06:47 | So essentially what we are going to
be doing in this training title is
| | 06:50 | learning to drive stick.
| | 06:52 | Now one final note before we do that.
| | 06:55 | Most of all we cover in this course is
going to apply to any flavor of Unix.
| | 06:59 | All Linux distributions, Red Hat, Ubuntu,
DBM, Solaris, AIX, each flavor of Unix
| | 07:05 | may require a few modifications.
| | 07:07 | Maybe the options to a command won't
be exactly the same or maybe there are
| | 07:11 | some new commands that exist on one of
those distributions but not on another one.
| | 07:14 | But you'll have enough knowledge that
you should be able to quickly adapt to
| | 07:17 | those systems and that makes this
course useful not just for Mac OS X users,
| | 07:21 | but also for web developers who plan to
deploy their website to a remote LINUX server.
| | Collapse this transcript |
| The terminal application| 00:00 | As I explained in the last movie, the
way that we'll interact with Unix is going
| | 00:04 | to be from the command line and the way
that we'll do that on a Mac is going to
| | 00:08 | be using the Terminal application.
| | 00:10 | It's going to provide a command line
access to Unix and it comes preinstalled
| | 00:13 | with Mac OS X. So if you have
Mac OS X, you have this application.
| | 00:17 | It's going to be located inside your
Applications folder, inside the folder
| | 00:21 | called Utilities, and there
you'll see the program Terminal.
| | 00:25 | A very useful shortcut is that if
you're in the Finder you can type
| | 00:28 | Shift+Command+U and it will open the
Utilities folder directly and there you'll see it.
| | 00:32 | Just try that together and we can
launch Terminal and see how it works.
| | 00:36 | So here I am. You can see that I
am inside my Finder. I'll hold down
| | 00:38 | Shift+Command+U and it will open up
my Utilities folder and right down here
| | 00:43 | you'll see that I have Terminal.
| | 00:44 | Now I could just double-click it to
launch it, but because we're going to be
| | 00:47 | using it a lot throughout this
training title I am going to drag it into the
| | 00:50 | dock down here so I'll
have good easy access to it.
| | 00:53 | So I close that window and now I can
launch it and now what we're seeing is Unix
| | 00:58 | or our command line access to Unix.
| | 01:00 | You can see that gives us our last login
information followed by a bunch of text
| | 01:05 | which is our prompt, and then after
that is our cursor where we can type. So we
| | 01:09 | can just type hello for example and then
I can hit the Delete key to erase that.
| | 01:13 | So that's where we are going to type
in our commands and then when we're done
| | 01:16 | we'll hit Return after each command
and then Unix will do whatever we've
| | 01:19 | asked and respond to us.
| | 01:21 | Let's take a look at a couple of
things we can do to customize our Terminal
| | 01:24 | environment before we move on.
| | 01:26 | The first is that by holding down the
Command key, either with or without the
| | 01:29 | Shift key, and pressing Plus or Minus
we can change the font size and the
| | 01:34 | window size as well.
| | 01:35 | So things will scroll up or down if you
want something a little bigger to look at.
| | 01:38 | An even better way to do this
though I think is to go into the
| | 01:42 | Terminal Preferences.
| | 01:44 | Under Terminal Preferences we can
change what Window settings we use to start up.
| | 01:48 | Mine right now uses basic.
| | 01:50 | If I click Settings we can see
what those settings are for basic.
| | 01:53 | I also have a number of other ones here,
including one that I've created myself, OTL_Prefs.
| | 01:58 | You can use the plus sign here to
create a new one or you can actually
| | 02:01 | duplicate settings from one if
you want to make changes, just a few
| | 02:05 | modifications to give it its own name.
| | 02:07 | Here is mine for OTL_Prefs. What I've
done here is I've changed my cursor from a
| | 02:11 | block cursor, which we see
here, to being just an underline.
| | 02:15 | That's a little easier to see. When my
cursor is on the top of the character you
| | 02:18 | will be able to see the
character underneath a little easier.
| | 02:20 | I've also changed the text colors
here and under Window I've changed the
| | 02:24 | Background Color to black.
| | 02:26 | I think that this color text on a
black background is a little easier to read
| | 02:30 | for me and also I think in the video it's
easier for you to be able to read as well.
| | 02:34 | So I've made those changes.
| | 02:35 | The other real change I think I've made
here is that under Advanced I've turned
| | 02:39 | off the bell that sounds at times.
| | 02:42 | It's sort of a warning bell.
| | 02:43 | It bothers me. I don't like it.
| | 02:45 | It's just a personal preference.
I don't like having that audible bell.
| | 02:48 | But you can play around with these things.
| | 02:50 | Don't really mess with the Shell and
the Keyboard, those are a little more
| | 02:52 | advanced, but feel free to go ahead
and make changes to Text, Window, and this
| | 02:57 | Advanced window as well and see if you
can get an environment that you like.
| | 02:59 | You also see I am using a
larger font size here as well.
| | 03:02 | So I am going to make this my default
now by clicking Default and I am going
| | 03:06 | to go to Startup and make sure that that
says New window will start up with OTL_Prefs.
| | 03:10 | So there we are. If I close this now,
I can open a new window by going to
| | 03:15 | Shell > New Window or you can see I could
just use the Command+N key to get a new
| | 03:19 | window like you normally would a Finder
window and it will open up now with my
| | 03:23 | new color scheme, larger
fonts and everything like that.
| | 03:26 | So just wanted you to see how I arrived
with this different look. Feel free to
| | 03:29 | customize it to your needs.
| | 03:31 | There is one more thing in addition
to the plus/minus, which makes your
| | 03:34 | font bigger/smaller.
| | 03:35 | Another nice shortcut to know is Command
+K, which you'll see from up here under
| | 03:41 | the View, is Clear Scrollback.
| | 03:44 | What that does is as I'm typing, I am
just going to hit Return a whole bunch of
| | 03:46 | times here, you will see that it
keeps scrolling and we started getting a
| | 03:50 | scrollbar here that let's us go back
and review what we've done previously.
| | 03:53 | That's called the scrollback, and if
we use Clear Scrollback or Command+K,
| | 03:58 | it clears that for us and we no longer
have it available to us to scroll back up.
| | 04:02 | So it's useful if you sort of want to
get everything off your screen and get a
| | 04:05 | clean environment again, you can use
that Command+K trick and you may see me do
| | 04:09 | that from time-to-time.
| | 04:10 | There is no harm in leaving all those
old commands there. You can actually go
| | 04:13 | back and review them.
| | 04:14 | You can even search them if you need to
search for something that you did previously.
| | 04:18 | So sometimes it can be really
useful to have that scrollback.
| | 04:20 | It's really up to you how you want to use it.
| | Collapse this transcript |
| Logging in and using the command prompt| 00:00 | Now I want to just talk a little bit more
about what's happening when we launch Terminal.
| | 00:04 | Notice here this first line, Last login.
That's telling us the time and date
| | 00:08 | that we last logged into Unix.
| | 00:09 | Every time we open a new window it logs
us into Unix again as a new Unix session.
| | 00:14 | We can have several of those open at once.
| | 00:16 | I'll use Command+N to open a new
window and now you can see that I have a
| | 00:20 | different Last login time.
| | 00:22 | There is no problem having several
of these open once. Each one is a Unix
| | 00:25 | session and each time I'm
being logged in as Kevin.
| | 00:28 | I'm being automatically logged in on the Mac.
| | 00:32 | Other Unix systems don't
automatically log you in.
| | 00:34 | You'd put in your username and your
password for it to know who you are.
| | 00:38 | But your Mac already knows who you are.
| | 00:40 | Now if you are using your Mac as a
single-user environment, that may come as a
| | 00:44 | bit of surprise to you because you may
not even think about the fact that you
| | 00:47 | are logged in as you.
| | 00:49 | But if you go to Apple menu and down
to System Preferences and into Accounts,
| | 00:53 | you can see that we can
manage different user accounts.
| | 00:55 | Now you may only have one or you may
have been taking advantage of this feature
| | 00:59 | and you may have family members or
co-workers or people like that who have
| | 01:02 | accounts on the machine as well.
| | 01:03 | But for a lot of people they just use
a single-user account and under Login
| | 01:07 | Options you may even have
Automatic login set to True.
| | 01:09 | In which case you don't even
enter your username and password then.
| | 01:13 | It remembers that it does it for you.
| | 01:14 | So your Mac is completely turned off,
you turn it on, and it automatically logs
| | 01:19 | you in with your username and password,
and voila, you are logged in as you.
| | 01:24 | It's very much the same user
experience that you would've had with OS 9 which
| | 01:27 | was a single-user system.
| | 01:29 | But once we moved OS X with this
Unix underneath, Unix is fundamentally a
| | 01:33 | multi-user environment.
| | 01:35 | Even if you're only using it for just
you, it still has all those multi-user
| | 01:39 | features and that's going to have some
applications later on that we'll need to talk about.
| | 01:43 | So realize this: that when you first fire up
your Mac you are being logged in as a user.
| | 01:47 | Even if you don't explicitly put in
your username and password, and therefore
| | 01:52 | when you start your Unix session using the
Terminal you're also being logged in as that user.
| | 01:57 | So the next I want is to look at the
second line and see what's going on here.
| | 02:01 | What we have to begin with is
what we call the command prompt.
| | 02:04 | This is prompting us for some action, and
we'll see how to configure this later on.
| | 02:08 | Right now by default, it's going to
be configured to the host name, the
| | 02:12 | directory that we are in and then the
user that we're logged in as and then this
| | 02:16 | dollar sign here which is the prompt.
| | 02:18 | Dollar sign space and then my cursor is
here waiting for me to type something.
| | 02:22 | It's prompting me to give it some commands.
| | 02:25 | So I am going type a simple command,
echo and then a single quote followed by
| | 02:29 | Hello World and another single quote.
| | 02:32 | I'll hit Return. That tells that all
right, I'm done typing, take this command
| | 02:36 | and execute it and it does.
| | 02:38 | You'll see the response it gives me back
and then I get the command prompt again
| | 02:41 | saying, "Okay, I've done what you
asked and now I'm ready for more input."
| | 02:45 | Let me go ahead and just type a few
more commands in here just so we have them.
| | 02:48 | echo 'Hello', echo 'World' and then I
want to show you that you can use the up
| | 02:54 | arrows and down arrows to
go back to previous commands.
| | 02:57 | So you can access those previous
commands using that and that's an
| | 02:59 | indispensable tool.
| | 03:00 | You are going to be using
that all the time, up and down.
| | 03:03 | You can even then use the forward and back
arrows to go in and edit the line and make changes.
| | 03:08 | So it can save you a lot
of typing. You say, oops!
| | 03:10 | You know what? I meant to do that
command but I meant to have three
| | 03:13 | exclamation points after it.
| | 03:15 | Well you don't have to do the whole re-
typing. You just go back, make the change
| | 03:18 | you want and then hit Return again.
| | 03:20 | There are some other useful shortcuts
for working with command line that I want
| | 03:24 | to just take a look at.
| | 03:24 | But before we do I just want to show
you that you can use Exit to exit a
| | 03:28 | session or you can just simply close the window.
That does the exact same thing; it exits you out.
| | 03:33 | So if I hit exit, you'll see now it
says logout and now it doesn't take any
| | 03:36 | input from me anymore.
| | 03:37 | I can then go up and close the window
and whenever I want to log back in, I just
| | 03:41 | open a new window and now it's
ready to take my input again.
| | 03:44 | I am logged back in.
| | 03:45 | All right, let's look at some other shortcuts.
| | 03:47 | As I said we've got the up and down
arrows to review previous commands. Ctrl+A
| | 03:52 | will move your cursor
back to the start of a line.
| | 03:55 | So if you've got a long line of 50
characters and you want to make a change at
| | 03:58 | the beginning or the change you want
is closer to the beginning, Ctrl+A will
| | 04:02 | just shoot your cursor back to the
beginning. Ctrl+E will go to the end.
| | 04:05 | So I remember it by thinking A is the
beginning of the alphabet, E stands for
| | 04:09 | End and that will shoot
me to the end of the line.
| | 04:11 | On the Mac, inside Terminal, you can
also Option+Click a point on a line and it
| | 04:16 | will take your cursor to that point.
| | 04:18 | I find that that can be
a little buggy sometimes.
| | 04:20 | It doesn't always work exactly like I expect,
but it can be a really quick way to get there.
| | 04:25 | So try that first.
| | 04:26 | Option+Click the spot on the line where
you wanted to go to and it should just
| | 04:29 | move your cursor back along.
| | 04:31 | Another very useful tool is Auto Complete.
| | 04:33 | If you're in the middle of typing
something and then you hit Tab it'll try to
| | 04:36 | guess what the rest of it is,
based on the available commands or the
| | 04:40 | available filenames.
| | 04:41 | So that's really useful if you want to
type a whole filename. You don't have to
| | 04:44 | type the whole thing; you can just type
enough that it can tell the difference
| | 04:47 | and hit Tab and it will finish it for you.
| | 04:49 | Tab+Tab can be used whenever the Auto
Complete doesn't work, because it's not
| | 04:55 | sure what the matches are.
| | 04:56 | So let's say here's five
files that are all very similar.
| | 04:59 | You type the first few letters and it
can't tell from that which one you want.
| | 05:03 | Tab+Tab will show you a list of
what all of those possible matches are.
| | 05:08 | And then the Command key plus the Tilde
key will cycle between Terminal windows
| | 05:12 | if you have several windows open and
Command+K as I mentioned before will clear
| | 05:16 | the screen and scroll back.
| | Collapse this transcript |
| Command structure| 00:00 | Let's talk a little bit more about the
structure of the command so that we make
| | 00:03 | sure we always use them in the right way.
| | 00:05 | There is a very definite structure in Unix.
| | 00:06 | Commands are always going to be the
command followed by the options followed by
| | 00:11 | the arguments, always in that order.
Command then options then arguments.
| | 00:15 | You can't put the options after the arguments.
| | 00:17 | They have to come in the middle.
| | 00:19 | The command is always a single word, always.
| | 00:22 | It's the thing we want to do.
| | 00:24 | So in our case it was echo.
| | 00:25 | That was the command that we were showing.
echo (space) and then we didn't have any options.
| | 00:30 | Options are optional.
| | 00:32 | So options, if we have them, will modify
the behavior of the command in some way.
| | 00:36 | We'll see how to see what the
available options are for each command.
| | 00:38 | But for now just understand that it's
command space, then the options, and then a
| | 00:43 | space followed by whatever the arguments are.
| | 00:46 | In our case the argument
was a string, Hello World.
| | 00:48 | It's the thing that we want the command
to use when doing whatever that command does.
| | 00:53 | So echo 'Hello
World' echoes 'Hello World'.
| | 00:57 | 'Hello World' is the argument.
| | 00:58 | Let's take a look at some examples and
we'll be able then to see the different
| | 01:01 | forms that options can also take.
| | 01:05 | So as we saw we had just
simple echo 'Hello World'.
| | 01:07 | If we wanted to pass in an option,
we could pass in the -n option that
| | 01:11 | suppresses a new line Return after it
outputs Hello World. So we don't get a
| | 01:16 | new line at the end.
| | 01:18 | It would just give us the
command prompt immediately.
| | 01:20 | So you can see there we divided them
again with spaces, the command, then the
| | 01:23 | options, then the argument.
| | 01:26 | Notice also that the option
has this hyphen in front of it.
| | 01:29 | That's the other clue that we give to
Unix that we're talking about an option
| | 01:32 | here and not an argument.
| | 01:34 | We don't always have to have an
argument either, for example, we could have
| | 01:38 | ruby -v.
| | 01:40 | That 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:48 | software version, the command version.
| | 01:50 | We also can do ruby --version.
| | 01:54 | It does the exact same thing.
| | 01:55 | It's just a different way
of specifying the option.
| | 01:57 | So you'll usually either have a
single dash followed by a letter, or dash dsah
| | 02:03 | followed by a keyword.
| | 02:04 | It tends to not be a letter in that case.
| | 02:06 | It tends to be a key word, but both of
them work the same, -v or --version.
| | 02:11 | We can also have a list of options
ls -l -a -h and then the argument, the
| | 02:19 | Desktop. That's actually a folder that
we're talking about in that case, a directory.
| | 02:23 | So we can have multiple options.
| | 02:24 | Typically what you have in that case
though is we smash them altogether. It has
| | 02:28 | the exact same effect.
| | 02:29 | We could mix and match these in any
way we want. l, a and h together are the
| | 02:33 | same as if we had broken them up.
| | 02:35 | Notice that the spaces make it pretty easy to
tell where the command and option arguments are.
| | 02:42 | There is an exception to this, which
is that sometimes an option wants an
| | 02:46 | argument of its own.
| | 02:47 | So for example the banner command
-w specifies the width of the banner.
| | 02:53 | It wants to know how wide you want it.
| | 02:55 | So if we say 50, we need to pass that
in as an argument to the w. So 50 is not
| | 02:59 | an argument to banner; 50 is
an argument to the w option.
| | 03:03 | Hello World is actually the
one and only argument for banner.
| | 03:07 | To make that less confusing, a lot of
times what you will see is people just
| | 03:10 | eliminate that space. banner -w50 and
that makes it clear that this 50 belongs
| | 03:16 | to the w option followed by the string.
| | 03:18 | We can also have multiple arguments.
Just to show you that, we can have cat with
| | 03:23 | the -n option or without it.
| | 03:25 | -n option will number the lines and
then cat will output the content of file1
| | 03:30 | followed by the content of file2 and it
will just use -n option to number those.
| | 03:35 | So hopefully that gives you a feel for
how these work. Let's try out a couple just so
| | 03:38 | you can see how they work.
| | 03:39 | So here I have a command prompt. So as we
saw before, we could do echo 'Hello World'.
| | 03:45 | Incidentally you can use double
quotes instead of the single quotes.
| | 03:48 | It works exactly the same.
| | 03:49 | I just had opted for the single quotes.
| | 03:52 | And then we can do the same thing, but
we can back up here and put in the -n option
| | 03:56 | and now we get the same
thing without the new line return.
| | 04:00 | Let's go ahead and get a new one
here and let's type the ls command.
| | 04:04 | It shows me a listing of the directories.
| | 04:06 | ls -lah will give me a
different listing of those directories.
| | 04:12 | In the next chapter, we'll talk more
about directories and these different
| | 04:15 | listing options, but as you can see now,
we get two different things based on
| | 04:19 | the options that we put out.
| | 04:20 | We can also do banner -w50 and
let's do 'Hello World'. We'll just see
| | 04:28 | what banner does for us. There it is.
| | 04:29 | You can use the scroll, you can scroll
back up here, and you can see that it
| | 04:31 | outputs this big banner, 'Hello World.'
It's horizontal, but that's suitable then
| | 04:39 | for printing on your printer, if you
wanted to have a big easy banner done that way.
| | 04:42 | It is just sort of a fun little program.
| | 04:43 | Another thing worth noting is that you
can put semicolons between commands if
| | 04:48 | you want to do several
things at once. So echo 'Hello '; echo 'World'.
| | 04:58 | Now it's going to do one command.
Semicolon tells that now we're going to do a
| | 05:01 | new command, and then we've got the
next command right after it, so semicolon
| | 05:05 | to break up commands.
| | 05:06 | We can also of course put in that -n
option in front of hello and we get it all
| | 05:12 | on one line, Hello and World.
| | 05:13 | That's actually two
separate commands that's doing.
| | 05:15 | So that's the fundamentals of
command. Just remember that it is always the
| | 05:18 | command followed by the options
then followed by the arguments.
| | Collapse this transcript |
| Kernel and shells| 00:00 | Before we dive in to actually using Unix,
there's one last fundamental concept
| | 00:04 | that I want to make sure that you
understand, because we're going to be using
| | 00:06 | it a few times later on and that is the
difference between the kernel and the shells.
| | 00:10 | So the kernel is the core of
the operating system in Unix.
| | 00:15 | It's what takes care of
allocating time and memory to program,
| | 00:18 | really doing sort of very
fundamental root level management of how programs
| | 00:22 | go about doing their thing.
| | 00:23 | Mac OS X uses the Mach
kernel inside Darwin to do that.
| | 00:28 | So if you ever hear
someone refer to Mach kernel,
| | 00:30 | that's what they are referring to,
is this very central part of the Unix
| | 00:33 | operating system that really just takes
care of how the operating system handles
| | 00:37 | very basic tasks like memory management.
| | 00:40 | Then outside of that there is the shell.
| | 00:43 | The shell is the outer
layer of the operating system.
| | 00:44 | That's what we see when we
open up a Terminal window.
| | 00:47 | We're working in the shell.
| | 00:49 | It interacts with the user and we can
think of it as our working environment.
| | 00:52 | The shell will send requests to the kernel.
| | 00:54 | The kernel will then do its thing and
results will then be returned back to the
| | 00:59 | shell so that we can interact with them again.
| | 01:01 | Mac OS X by default uses what's called
the Bash shell, B-A-S-H, but it includes
| | 01:07 | other choices as well.
| | 01:08 | Let's talk about what some of those
other choices are, because it also gives you
| | 01:11 | a little bit more of the
history of Unix as well.
| | 01:14 | The popular shells are sh,
which is the very first one.
| | 01:18 | It's called the Thompson Shell and it
was created in 1971 and that was the main
| | 01:23 | working environment that
people had for a long time in Unix.
| | 01:26 | In 1977 something called the Bourne
Shell was created and that was a replacement
| | 01:31 | for the Thompson Shell.
| | 01:32 | So it completely replaced it.
| | 01:34 | It has the same name, sh, short for
shell, and it just replaced the old one.
| | 01:39 | Starting though in 1979, people said
"Hey, instead of replacing shells all the
| | 01:42 | time, let's get people choices.
| | 01:44 | Let's have different shells and then if
someone loves one shell or another, they
| | 01:47 | can just switch between them."
| | 01:48 | They started having
different names at that point.
| | 01:50 | We have csh for C Shell, a little
bit of programmer humor there.
| | 01:54 | Then we have the Tabbed C Shell in 1979,
tcsh, the Korn Shell, ksh, then we have
| | 02:02 | the Bourne-Again Shell, again another
bit of programmer humor. Take off the
| | 02:06 | Bourne Shell, this is the Bourne-Again
Shell, which is bash, and that's the one
| | 02:10 | that we have by default.
| | 02:11 | And then the Z Shell.
| | 02:13 | It's one of the most recent.
| | 02:14 | There are a lot of others.
They almost all end in sh.
| | 02:17 | You can go some place like Wikipedia
to get a full list of all of those, but
| | 02:20 | these are the main ones and these are all
installed on Mac OS X. So we have the choice.
| | 02:25 | These are all there waiting for us
to use. Actually I think that back.
| | 02:28 | Thompson Shell has gone.
| | 02:30 | It's been completely replaced by the
Bourne Shell, but the other ones are all
| | 02:33 | there on the Mac for us to choose from.
| | 02:35 | For beginners, the difference
between these shells is tiny.
| | 02:38 | A lot of the difference between them
is going to be the features that are
| | 02:41 | available to high-end users for
task like advanced shell scripting.
| | 02:46 | For now, stick with bash to learn.
| | 02:48 | You can always switch later, but I want
you to understand that you are working
| | 02:51 | in the bash shell when you log in.
| | 02:54 | Now you may be tempted to think that a
shell is just like an operating system,
| | 02:59 | because we can pick between these
different ones, that it somehow like the
| | 03:02 | difference between Windows
and Mac or something. It's not.
| | 03:05 | It's just a working environment that
we can choose and move between and in fact
| | 03:09 | we can move from inside one shell into
another shell without leaving the old one.
| | 03:14 | We can nest them inside each other.
| | 03:15 | It's just like moving
into another working layer.
| | 03:18 | Let's go to the Terminal and take a closer look.
| | 03:21 | So here I am in my Terminal, and you see
up at the top here it actually tells me
| | 03:23 | that I'm in the Bash Shell.
| | 03:25 | It's right there at the top.
| | 03:26 | That's a nice giveaway.
| | 03:27 | I can also find out what the login shell,
the one that is going to put me in by
| | 03:31 | default is, with echo $SHELL.
| | 03:37 | That's like I can know it is an
environment variable. It has the $ in all capitals.
| | 03:41 | And it says "Oh, bin and bash.
| | 03:43 | That's what I am going to start
you up in when I first launch."
| | 03:46 | That's actually changeable here in
Terminal > Preferences, under Startup.
| | 03:50 | Shells open with the default login
shell or we can pick something else.
| | 03:55 | So if we want to launch it with a
different shell, we can do that there.
| | 03:57 | Now again, that's the login shell that
we're seeing. If we want to see the shell
| | 04:00 | that we're in right now, it's echo $0.
| | 04:06 | That's the shell that we are working
in right now. Not the login shell, the
| | 04:10 | shell that's going to be run when we
launch this, but the shell that we're in.
| | 04:13 | Why that's important is because
we can go inside other shells.
| | 04:16 | Let's take an example. Let's go into
the Tabbed C Shell, tcsh. Just type it at
| | 04:22 | the command line and boom! We changed shells.
| | 04:24 | Now just to show you the difference, let's do
echo $SHELL and you'll see that we still get bash.
| | 04:29 | If we do echo $0, now you can see
that we are inside the Tabbed C Shell.
| | 04:34 | Just to show you again, let's try a
few other. Let's go bash. Now I am inside bash.
| | 04:38 | Let's do csh. Now I am inside the C
Shell. sh, now I am inside the Bourne Shell.
| | 04:44 | Now to get back out of each of these,
you just type exit and you can see that
| | 04:47 | the prompt changes slightly each time as
I back out, until finally I get back to
| | 04:52 | my original prompt and now I know
that I am back at the Bash Shell.
| | 04:57 | So as you can see, you can move between these.
| | 04:59 | You are just moving into a new working
layer, a new working environment.
| | 05:02 | You won't need to do this;
you can just be in bash all the time.
| | 05:07 | But I just want you to know that that's
where you are, that you are inside bash,
| | 05:11 | and that as an advanced user
you do have these other options.
| | 05:14 | So if you hear people talking about, "Well,
I know how to do this in the Z shell,
| | 05:18 | but I don't know how to do it in bash,"
that's what they are talking about.
| | 05:22 | They are talking about these
different working environments.
| | Collapse this transcript |
| Unix manual pages| 00:00 | This movie may very well be the most
valuable movie in this entire training title,
| | 00:04 | even though it's very simple.
| | 00:06 | The Unix manual pages are an invaluable
resource for helping you figure out what
| | 00:10 | you want to when you're working in Unix.
| | 00:12 | In fact, I learned 80% of the
Unix I know from the manual pages.
| | 00:16 | These are going to be your
best friend during your journey.
| | 00:19 | The manual pages, often simply refer to
as just the man pages, is called that
| | 00:23 | because the command to get to them is
just man, and the man followed by whatever
| | 00:28 | we want to look up in the manual.
| | 00:30 | man echo will give us the
manual pages for the echo command.
| | 00:33 | Go ahead and hit that, you will see
at the top BSD General Commands Manual.
| | 00:37 | The name echo, what does it do?
| | 00:39 | Write arguments to the standard output.
| | 00:41 | Then 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:48 | bottom we get a colon.
| | 00:50 | Now let's just know that
there's more. We can see more.
| | 00:52 | To see the next page, you can simply
hit the Spacebar or you can use F and B to
| | 00:57 | go forwards and back. F forward, B backwards.
| | 01:00 | Q will exit for quit.
| | 01:03 | Those are very common options in Unix.
| | 01:05 | So let's go ahead and hit the
Spacebar. We get down to the end.
| | 01:07 | It says END. Now we have to hit Q and
now the manual pages are gone.
| | 01:11 | We're right back where we were.
| | 01:12 | They don't stay there and crowd up our view.
| | 01:14 | They just disappear.
| | 01:15 | If we need them again, we can
call them again. So that's it.
| | 01:18 | That's how we find out how all these
commands work and what all the options
| | 01:20 | available to them are.
| | 01:22 | It's extremely helpful.
| | 01:23 | That way you don't have to refer to
back to my videos every time you have a
| | 01:26 | question about something.
You just ask Unix, "Hey Unix!
| | 01:28 | I need to refresher on what the
options are I can pass into this."
| | 01:31 | You will use it all the time.
| | 01:33 | man actually has its own manual page too.
man man and there you are, you can see
| | 01:37 | the different options that
are available for the manual.
| | 01:40 | There is also an abbreviated version of
that, man -h or man -help, and those will
| | 01:46 | just give you a quick overview of what the
options are that you can pass into to manual.
| | 01:51 | The most interesting of these options is
the k option and that says the k option
| | 01:55 | is the same as apropos. What is apropos?
Well, man apropos. Look it up, search
| | 02:03 | the whatis database for strings.
| | 02:05 | It searches a set database files
containing short descriptions of system
| | 02:08 | commands for keywords and displays
the result on the standard output.
| | 02:12 | Let's try it. man -k and
then let's put in banner.
| | 02:16 | That was the command that we used earlier.
"Oh look," it says, "oh banner, I found that."
| | 02:20 | Print large banner on printer.
| | 02:22 | Now apropos works the same way, apropos.
We just don't pass in the -k. apropos
| | 02:29 | already looks for the
keywords, things that match banner.
| | 02:31 | Now just to show you the difference
there, if we take away the first if you
| | 02:35 | notice that just make the apropos ban,
it's going to look for the keyword ban,
| | 02:39 | and it found three this time. Not just
banner, but it had a couple of others. bandwidth.
| | 02:44 | That's where it found the word ban in
this one and then down here abandon,
| | 02:48 | it found ban there, so it pulled up three
different possibilities and said "Maybe
| | 02:51 | you mean one of these."
| | 02:52 | So it could help us find the right
command and find out what it does.
| | 02:56 | Now when we looked at the manual pages
for apropos, it said that it was looking
| | 02:59 | in the whatis database.
| | 03:01 | We also have the ability to say whatis
banner, and it will tell us that exact
| | 03:05 | same description. The one thing about whatis
is it doesn't do keyword searching. Whatis ban
| | 03:11 | comes up and says there is nothing there.
| | 03:13 | So apropos or man with the -k for the
keyword, look for the this keyword.
| | 03:20 | Those are the ways to find out things if you
aren't sure what the name of something is,
| | 03:23 | you can look and say, "Hmm!
| | 03:24 | Is there anything related to video, is
there anything related to print," and you
| | 03:29 | can find everything that's related to print.
| | 03:30 | Now that might be a long list with
thousands of entries, but it still might help
| | 03:34 | you to find what you were looking for.
| | 03:36 | But the most important tool is going to be
man, so that you can find out what things do.
| | 03:42 | man ls will then come up and tell us
that ls lists our directory contents and
| | 03:47 | give us all the options that we can pass in.
| | 03:49 | You can see there's a very long list
of options here, so there's no way you
| | 03:52 | could remember what all these are.
| | 03:54 | You might remember a few of the key
ones, but you would want to refer to the
| | 03:56 | manual pages to see the rest.
| | 03:57 | So it is indispensable tool. Definitely
if in doubt, try the manual pages first.
| | 04:03 | man followed by whatever the command is.
| | Collapse this transcript |
|
|
2. Filesystem BasicsThe working directory| 00:00 | In this chapter we're going to take a
look at the Unix file system and how we
| | 00:04 | can work with files and directories.
| | 00:06 | I want to start that off by talking
about the concept of the working directory.
| | 00:09 | This is an important concept.
| | 00:10 | It's the directory where we are right now.
| | 00:13 | So when we issue commands, it's
important to know which working directory we are in,
| | 00:17 | because that's where those
commands are going to happen.
| | 00:19 | That's where they are going to take place.
| | 00:21 | So if we say look for a file, it's
going to look for a file in that directory,
| | 00:25 | unless we tell it something.
| | 00:26 | So it's important to know where we are.
| | 00:29 | The best analogy is the graphical user
interface of the Finder. Right here I am inside kevin.
| | 00:35 | That's my user directory and you can see
that I have all my different folders here.
| | 00:39 | If I double-click on Public,
I go inside the Public folder.
| | 00:43 | I've changed my working directory
from being in kevin to being in Public.
| | 00:48 | I can go back and I've changed my
working directory now back to kevin.
| | 00:53 | It's the directory that we are seeing
and we are working. It's very similar.
| | 00:57 | Notice though that that only happens in
the Finder when this sidebar is there.
| | 01:01 | If I click this button over here and
make the sidebar disappear, now I get
| | 01:04 | a different behavior.
| | 01:05 | I double-click on Public
and I get a whole new window.
| | 01:08 | That's not a good analogy,
because that's not the way it works.
| | 01:10 | We're not getting a new window at all.
| | 01:12 | So this is the one that works for the
analogy where we have moving around and
| | 01:16 | this window is just shifting and changing
into different directories as we go up and down.
| | 01:21 | Now we can jump to different places.
| | 01:23 | We can jump to our desktop.
| | 01:25 | We can jump to our Applications folder.
| | 01:28 | We don't have to move
around in sort of a linear way.
| | 01:30 | This will take me back to my user
directory, but that's the analogy to think of.
| | 01:35 | This is the working directory in the same
way that we have a working directory here.
| | 01:39 | So what working directory are we in here?
| | 01:40 | pwd, that's the present working directory
and there is my present working directory.
| | 01:47 | It returns the path to me that describes
what we're seeing in the window over here.
| | 01:53 | If I actually hold down the Ctrl key
while I click on kevin, you can see that
| | 01:57 | it tells me, ah, Macintosh Hard Drive>
Users and then Kevin. That's where I'm located.
| | 02:04 | So in the root of the hard drive user
is kevin, in the same way that this tells me
| | 02:08 | the root of the hard drive.
| | 02:09 | That's the first slash,
/Users/Kevin. That's the path.
| | 02:13 | If you use the Windows at all, it's the
backslash on Windows, but on Mac and on
| | 02:17 | Unix it's always the forward slash,
That's what you use for a path separator in
| | 02:21 | both Mac and Unix, is the forward slash.
| | 02:23 | So in the next couple of movies we're
going to learn more about seeing the
| | 02:27 | different directories and how to move
around between them, but keep in mind that
| | 02:30 | you can always use this pwd to find out
your present working directory and that
| | 02:35 | you are always in a single
directory when you're in Unix.
| | 02:38 | So wherever you are at this moment
that's the place that you are residing, much
| | 02:42 | in the same way as if you had a window
open like this and all of your commands
| | 02:45 | were being typed relative to that window.
| | Collapse this transcript |
| Listing files and directories| 00:00 | In the last movie we became familiar
with the concept of the working directory
| | 00:03 | and we saw how we could use pwd to
show us our present working directory.
| | 00:08 | Now what I want us to do is see how to
see what files and folders are inside
| | 00:12 | that present working directory,
and we do that with the ls command.
| | 00:16 | We got a peek at it earlier. ls on a
line by itself will just list the contents
| | 00:21 | of our present working directory. Desktop,
Documents, Downloads, Library and so on.
| | 00:25 | If we look over here that exactly
matches what we see in the Finder window.
| | 00:29 | Desktop, Documents, Downloads and so on.
| | 00:32 | In addition we can pass in some options to ls.
| | 00:35 | We could do man ls and see a list of
all of those, but I am just going to show
| | 00:39 | you the couple that I
think are the most important.
| | 00:41 | ls -l is going to give us a
different kind of listing.
| | 00:47 | ls -l option, now we get
them in a vertical format.
| | 00:52 | Notice the last column there still has
the file names, but going top to bottom
| | 00:57 | and then over to the left, all those
other columns give us information about the file,
| | 01:01 | the size of it, the permissions
on it, the owner of it, the time, all
| | 01:04 | those things we'll talk about lot later
on, we'll get into those, but just ls -l
| | 01:09 | will show you this different version of it.
| | 01:12 | If we add another option to it, we can
do it with or without the l, but I like
| | 01:15 | doing it with it. ls -la and hit Return.
Now we get that same list, but notice
| | 01:22 | that we got a few extra files there.
| | 01:24 | There is ., .., .CFUserTextEncoding,
.DS_Store, .Trash and .bash_history.
| | 01:31 | Now you may or may not have exactly
those same ones. Don't worry about it.
| | 01:35 | The important thing to understand is that
the dot represents the current directory.
| | 01:40 | That's all it is.
| | 01:41 | It's that sort of symbol or a placeholder
that means this directory that I'm in right now.
| | 01:47 | Dot dot refers to the parent of this directory.
| | 01:50 | It's the directory that's right above
this, and in this case because our pwd
| | 01:55 | returns users kevin, .. is a reference to Users.
| | 01:59 | But we are always going to have dot and
dot dot to refer to those two directories.
| | 02:03 | The other four files there are what we
call dot files, because they begin with
| | 02:08 | the dot and dot files
are invisible config files.
| | 02:12 | Notice that they don't show up here.
| | 02:14 | The Finder is set to hide dot files by default.
| | 02:17 | We don't need to see them.
They are just configuration files.
| | 02:20 | So for example .Trash is
files that are in the trash.
| | 02:25 | That's where my trash is.
| | 02:26 | If you ever wondered how does it know
when files are in the trash, well, it
| | 02:29 | moves them from let's say your
desktop into this folder and it sits there
| | 02:33 | waiting in this hidden folder until you throw
them away, and then it empties out that folder.
| | 02:38 | That's where those trash files are stored.
| | 02:39 | Then we have DS_Store.
| | 02:42 | That's actually for the Mac desktop,
the Finder, to store different options
| | 02:46 | about how we're viewing this folder.
| | 02:48 | Whether it is in this sort of layout,
the size of the window, the position of
| | 02:51 | the icons, all that gets stored in
this little configuration DS_Store.
| | 02:56 | Bash_history we'll talk about later,
but that's where we store history of those
| | 02:59 | commands that we have been typing and
then user text encoding is another just
| | 03:03 | configuration file that the Mac uses.
| | 03:06 | Notice that there is also
another difference here.
| | 03:08 | Some of these have Ds at the front
of the line while some of them just
| | 03:12 | simply have dashes.
| | 03:13 | The dashes is a file. The d is a directory.
| | 03:16 | That's what it's letting you know.
| | 03:17 | That's your tip-off of whether
something is a directory or not, is whether that
| | 03:20 | line begins with the D.
| | 03:21 | There is a third possibility that
sometimes shows up there that we'll talk
| | 03:24 | about later. Instead of D and dash
you could have an L, which will be for a
| | 03:28 | link or a shortcut.
| | 03:29 | It's like an alias in Unix.
| | 03:31 | So that's how we list the files.
| | 03:32 | That's how we see the files that are there.
| | 03:34 | We can pass in just ls, if we
just want a very simple list.
| | 03:37 | We can pass in ls -la if we want that
longer list and actually one other thing I
| | 03:42 | like to add as an option is ls -lah,
and that returns the size of these
| | 03:48 | different things in human readable
format. 510 bytes, 15 kB, 1.1 k. That just
| | 03:56 | gives us a nicer size that I think
is a little more pleasant to read.
| | Collapse this transcript |
| Moving around the filesystem| 00:00 | Over the last couple of movies we saw
how we can use pwd to see our present
| | 00:03 | working directory and ls either with
or without options to see the files and
| | 00:08 | folders that are in the
present working directory.
| | 00:10 | Now I want us to talk about how we
can move around and change our working
| | 00:14 | directory and to do that we use the cd
command. So cd followed by the name of the
| | 00:20 | directory that we want to change into.
| | 00:22 | So for example library.
| | 00:23 | Now our present working directory is changed.
| | 00:26 | We are now inside the library.
| | 00:27 | It's the same thing as if we go over
here and we double-click on library.
| | 00:31 | Now we have changed into the library.
| | 00:32 | We can also hit the back arrow and change back.
| | 00:36 | Here, do the same thing from the command
line. We would type cd space and then dot, dot.
| | 00:43 | Remember that I told you that I told
you that dot, dot when we are seeing this
| | 00:45 | listing is special file name.
| | 00:48 | That references the parent of the
current directory. dot is the current
| | 00:52 | directory. dot, dot is the
parent of this directory.
| | 00:55 | You can see here that it
actually says d in front of that.
| | 00:57 | It treats it just like a
directory and we can change into it.
| | 01:00 | So it says, "I don't know what the name
of the parent is, I don't care, I want
| | 01:04 | to go backwards one."
| | 01:05 | It's the same as hitting that back arrow.
| | 01:07 | So now we are back our
back in original directory.
| | 01:10 | We also saw that these are file
separators, the forward slash. We can use those to
| | 01:15 | be able to move several directories at once.
| | 01:17 | So cd Library/preferences will move us
into the preferences folder. Now our pwd
| | 01:25 | is into the library preferences,
we moved two directories at once.
| | 01:28 | It's very important that they start with
library and not a slash in front of it.
| | 01:33 | Not like this and I will
explain that in a moment.
| | 01:35 | We can also go backwards the same way cd../..
| | 01:40 | Go to the parent of this directory and
then to the parent of that directory.
| | 01:44 | So we've now moved back two
directories after we went in two directories.
| | 01:47 | I also want to talk about a shortcut
which is for auto completing file names.
| | 01:52 | So if we say cd space and then just
type an L and then hit the Tab key, you can
| | 01:57 | see that it says, "Oh, I looked at the list
of available files and folders in the
| | 02:00 | current working directory and from that
the only one I saw that starts with an
| | 02:05 | L is Library, so that's probably what you
mean" and it went ahead and filled it out for us.
| | 02:09 | If it can't guess it, let's say we
start typing Pref and then we hit Tab,
| | 02:14 | it says Preference. Well actually no,
we are looking for Preferences.
| | 02:18 | It didn't put the full name.
| | 02:19 | Hit Tab a second time and it comes up
and says "Oh, I am trying to decide between
| | 02:24 | Preference panes and Preferences, those
are the two that I can't distinguish," so
| | 02:27 | got to give me a little more to go on.
| | 02:29 | So it finishes as much as it can and
then it stops and waits for you to do it.
| | 02:34 | So library preference on its own, if
I hit Return, it says no such file or
| | 02:38 | directory. Preferences that directory does exist.
| | 02:42 | So I just want to make sure that you
see how auto complete works, because
| | 02:45 | that's a really helpful thing when you
are moving around to be able to start
| | 02:49 | typing a name and hit Tab and have it finish
the rest of the directory or file name for you.
| | 02:53 | Now I mentioned earlier that you did
not want to put this beginning slash
| | 02:57 | in front of it. That's because
whenever something has a beginning slash it's
| | 03:00 | an absolute path. That root cd/ that
represents the very beginning of our hard drive.
| | 03:07 | That's the root of the hard drive. cd/,
now we are through to our hard drive.
| | 03:12 | If we do cd users and then Kevin, we
move forward back into users in Kevin.
| | 03:19 | That's a relative path, relative from
the root where we were, move into users
| | 03:24 | and 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:33 | So the one without the slash is
starting with the working directory, I am going
| | 03:37 | to give you the path, the one with the
slash says, "I am going to give you the
| | 03:40 | entire path starting from
the root of the hard drive."
| | 03:42 | You are not going to use anything
relative to where you are now. It doesn't matter
| | 03:46 | where you are. This path is the same
if I am inside preferences or if I am
| | 03:50 | inside library. cd /user/
Kevin is the same directory.
| | 03:56 | That user home directory we keep moving
into, right. We are going to do that a lot.
| | 04:00 | That's our directory that's where lot
of our files and folders are located.
| | 04:03 | So there is actually a shortcut for it,
cd, and then the twiddle or the Tilde key
| | 04:09 | and that will take us
into our user directory.
| | 04:11 | So if I move into the preferences and then cd
back, it takes me back to my user directory.
| | 04:18 | There is one other thing that's good
to know which is cd space and then just
| | 04:23 | dash takes you back to the
last directory where you were.
| | 04:26 | So it just toggles between the current
directory and the most recent directory.
| | 04:32 | So cd- now I am back in my Preferences folder.
cd- again now I am back in my users folder.
| | 04:37 | So that's helpful if you need to just
move between two directories. You want to
| | 04:41 | switch to a directory, do a few things,
and then switch back to where you were.
| | 04:44 | That can be a really helpful tool.
| | 04:46 | Now if you do any other cds while you
are in there, then suddenly your directory
| | 04:50 | will change and you won't be able to
get back to where you were anymore, but if
| | 04:53 | you just need to toggle between two,
that cd- can be really helpful.
| | Collapse this transcript |
| Filesystem organization| 00:00 | Now that we have gone over the basics
of working with the file system, we know
| | 00:03 | how to view the files and directories
and our present working directory and how
| | 00:06 | to move around the file system,
| | 00:08 | I want to stop and take a look at the
big picture for what the file system looks
| | 00:11 | like and how it's organized, because
there is a very standard way that Unix goes
| | 00:15 | about organizing it and Mac OS X then
has its own sort of version of that.
| | 00:20 | So in a typical Unix organization, you
have the root of the hard drive, we saw
| | 00:23 | that with just simply the slash, and
inside the root there is typically several
| | 00:27 | folders. bin which is where the
binaries or programs are stored.
| | 00:31 | These are Unix programs, not programs
like Photoshop and Microsoft Word.
| | 00:37 | Those are Mac applications.
These are Unix programs.
| | 00:40 | Sbin is for system binary, system
program that it uses. dev is where there are
| | 00:45 | references and files for different
devices like hard drives keyboard, mouse,
| | 00:49 | etcetera. /etc which is also pronounced
as etcetera or "easy" by some people.
| | 00:56 | That's where system configurations go.
| | 00:58 | Then there is home, which is where user
home directories go. On most Unix systems
| | 01:02 | once you log in, you will be placed
into a folder inside the home. That's where
| | 01:07 | your files and folders would live.
Not on Mac but on most Unix systems.
| | 01:11 | Lib is a place for storing libraries of
code that need to be referred to various
| | 01:15 | programs. tmp is temporary, that's for
temporary files, things that you don't
| | 01:21 | mind if someone comes in and wipes
the file away. You won't miss it.
| | 01:25 | That's where you would put in tmp.
| | 01:27 | And then var is for various,
mostly files that the system uses.
| | 01:31 | Then there is usr, which is short for
user and that's where the user would put
| | 01:35 | programs tools and libraries, not
their files. Their files would be in their
| | 01:38 | home directory, but the programs that
they would have installed just for them,
| | 01:42 | they put those in the usr
directory, not in the system bin.
| | 01:46 | There would be things that
are for the users in the usr bin.
| | 01:48 | Most players of Unix are going
to adhere to this basic structure.
| | 01:52 | They may make small changes here and
there and maybe add a folder, take away a
| | 01:55 | folder, but it's basically the same file
structure and for putting things in the right place.
| | 02:01 | On the Mac though there are some
additional files that you should know about.
| | 02:05 | Most of them are probably familiar to you.
| | 02:06 | There are Applications, Library, Network,
System, Users, and Volumes and it's kind
| | 02:10 | of self-explanatory what all of those are.
| | 02:12 | Just notice that the Applications and
Library are for Mac programs, not Unix
| | 02:16 | programs, Mac programs and
Library is for Mac libraries of code.
| | 02:20 | We have a separate place for
putting the libraries that Unix needs.
| | 02:24 | These are just libraries that
are used by Mac applications.
| | 02:27 | System is where Mac OS X itself lives,
and then Users is what it uses for
| | 02:32 | its home directory.
| | 02:33 | It does not use /home to
put the user directories.
| | 02:36 | It puts them all in /Users.
| | 02:38 | Notice these are all capitalized.
These are also all visible to you.
| | 02:42 | If you go to the root of the hard
drive in the Finder you will see them.
| | 02:44 | The other ones are not.
| | 02:46 | It's been told to hide them from you.
| | 02:47 | Even though they are not .files names,
it's still has said, these are some
| | 02:51 | basic configuration files that Unix uses and
the Mac user doesn't normally need to see those.
| | 02:56 | If they need to see it from the command
line, they will still see it, but from
| | 02:59 | the Finder we are going to hide them.
| | 03:00 | There are also these dot files we
talked about before. .DS_Store holds folder
| | 03:05 | view options and icon positions that
exists in a lot of your directories.
| | 03:10 | You will see that in lot of places.
| | 03:11 | Sometimes you will see .MacOSX. That's
just the directory for Mac OS X to store
| | 03:15 | some options. .Trash we talked about
is the user trash and then two that you
| | 03:21 | might see pop up here and there which
are .hotfiles.btree and .Spotlight-V100.
| | 03:26 | Leave those alone if you see them.
These are used by Spotlight or Mac OS X to
| | 03:32 | quickly organize things.
| | 03:33 | It's to better index and find files,
things that it's going to need often.
| | 03:37 | So that gives you sort of the big
picture. You also have both of these in
| | 03:40 | your Mac OS X. You have the Unix one
and then you also have the Mac one
| | 03:43 | sitting side by side.
| | 03:44 | So if you want to surf around, you can
go in the command line, you can use cd to
| | 03:48 | move around, poke about, see what's
in all these different directories.
| | 03:51 | You will see that the Mac one does make
some customizations to the original one.
| | 03:56 | There are few things that are
different but for the most part this is sort of
| | 03:59 | what you can expect.
| | 04:00 | You can expect that the Unix one is
going to exist on most Unix systems and Mac
| | 04:04 | is going to add these other files in there.
| | 04:06 | So hopefully that will just demystify
it for you a little bit and get you a
| | 04:09 | little bit familiar with where
different code is going to live.
| | Collapse this transcript |
|
|
3. Working with Files and DirectoriesNaming files| 00:00 | In this movie, we are going to be
working with files and directories in Unix.
| | 00:04 | Before we see how to create files in
Unix, I want to just first talk about the
| | 00:07 | way we name files, to make sure that
we follow the standard Unix rules and
| | 00:11 | conventions, and that's because they are
a little bit different than what you are
| | 00:13 | used to in working with Mac OS X's Finder.
| | 00:16 | The first rule is that there
is a maximum for 255 characters.
| | 00:19 | That's the same as you are used
and 255 should be more than enough.
| | 00:23 | The second rule is that you
want to avoid most symbols.
| | 00:26 | I have listed off the main ones there,
but you really want to stay away from
| | 00:30 | almost all symbols and the reason why
is that in Unix a lot of these from the
| | 00:34 | command line have special meaning.
| | 00:36 | In Unix, as it's trying to parse the
line and figure out what you mean, may
| | 00:39 | confuse the symbol on a file name for being a
command or an operator that it normally uses.
| | 00:44 | Now in some of these cases, Unix will
allow you to use the symbol but then
| | 00:49 | whenever you want to reference that file
name and do something with it, you will
| | 00:52 | have to escape the symbol. You have
to put a backslash in front of it.
| | 00:55 | So you end up typing a lot of
backslashes to escape the characters, which is just
| | 00:59 | more needless typing.
| | 01:00 | So we just typically want to
stay away from them altogether.
| | 01:02 | What you do want to use are letters,
numbers, and the period, underscore, and hyphen.
| | 01:09 | Remember period has a special meaning.
If it's the first letter of the file name,
| | 01:13 | it becomes a dot file and it gets
treated a little bit differently and it won't
| | 01:17 | show up in the Mac OS X Finder.
| | 01:19 | You also don't want to put a hyphen as
the first character of a name because if
| | 01:22 | you remember when we talked about
options, options begin with a hyphen.
| | 01:26 | So ls -la, that hyphen in front of la
indicates that l and a are options.
| | 01:32 | So we don't want to put hyphen as the
first name of our file so that it never
| | 01:35 | gets confused with being options.
| | 01:37 | Typically, even though we can use
uppercase letters, you want to stick to
| | 01:42 | lower case letters in Unix and the reason why
is because Unix typically is case sensitive.
| | 01:48 | In other words, MyFile with capital
M and F is different from myfile with
| | 01:53 | lowercase m and f. They would be
considered two different files in Unix.
| | 01:58 | This applies to most Unix systems.
Mac OS X is a special case because the
| | 02:03 | default formatting for a hard drive is
to use a case insensitive formatting for
| | 02:08 | your hard drive and that means that then
Unix on a Mac becomes case insensitive.
| | 02:14 | Those myfile and MyFile
would refer to the same thing.
| | 02:18 | That's different from most Unix
systems and it's important to know the
| | 02:21 | difference because it can cause
problems if you are dragging files over from
| | 02:25 | another Unix system on to your Mac,
because one of them is case sensitive and
| | 02:29 | one of them is case insensitive.
| | 02:31 | If you have two files, myfile and
MyFile, on one system they are allowed to
| | 02:35 | co-exist, but on your Mac they are
not allowed to co-exist. They are
| | 02:38 | considered the same file name.
| | 02:39 | so keep that in mind, we would
typically just stick with lowercase for all file
| | 02:43 | names in Unix and avoid the issue altogether.
| | 02:45 | The other thing is different from
working the Finder is that underscores
| | 02:49 | are better than spaces.
| | 02:50 | It's no big deal to put spaces in
your filename inside the Finder.
| | 02:54 | You can certainly use them in your
filename in Unix but remember spaces are how
| | 02:59 | Unix can tell the difference between
commands, options, and then all of the
| | 03:03 | different arguments.
| | 03:04 | Spaces are what help break that up
and delineate the different parts.
| | 03:08 | So if we have spaces in our file ame,
Unix can't tell that that should be one
| | 03:12 | continuous filename.
| | 03:13 | So every time we typed a name you have
to put a backslash in front of the space
| | 03:17 | to escape it, just like we
have to escape those symbols.
| | 03:21 | So instead of having to do that all
the time, it's better just to avoid them.
| | 03:24 | The other thing you can do is put
quotes around names that have spaces in them
| | 03:28 | and then it will know ah, this is one complete unit.
| | 03:30 | I don't need to break that up.
| | 03:32 | The last thing is that you want to use
file endings whenever possible. They are
| | 03:35 | not required but they are very helpful, .txt
for text files, .html for HTML fille, and so on.
| | 03:42 | It really helps you when you are
looking at your listings to differentiate to
| | 03:45 | what are files from commands and directories.
| | 03:48 | Now there are a few other rules.
| | 03:50 | For example, you can't name your file
dot, or dot, dot, because obviously those
| | 03:54 | have special meaning we have
already talked about, either the current
| | 03:57 | directory or the parent directory.
| | 03:59 | You wouldn't want to name a file the
same name as a Unix command. Now if you put
| | 04:03 | a file ending at the end of it that
won't be a problem, but you just don't want
| | 04:06 | to call something a file echo, because
echo is one of the commands that we're
| | 04:09 | going to use and you want to make
sure that Unix can tell the difference.
| | 04:13 | Before we go onto creating files, let
me just show you what I mean about this
| | 04:15 | spaces inside the filename,
just so you get an idea of that.
| | 04:18 | Okay, so here I am in my Terminal and
I am in my home directory. I will write
| | 04:22 | cd and I am in my user directory and from
here there is my library folder cd, library.
| | 04:29 | Inside my library, if we do ls, you
will see that there is Application Support
| | 04:34 | that has a space in it.
| | 04:35 | That's one of the folders that Apple
created and Apple put a space in it.
| | 04:38 | Perfectly legal to have a space in it,
nothing wrong with it, but it creates a
| | 04:42 | problem because now watch when I could
do cd into Application, I will hit the Tab,
| | 04:48 | you will see what the auto
complete had to do. The auto complete put this
| | 04:51 | backslash right here in front of the
space to indicate this is all one thing.
| | 04:57 | cd, don't take this as
being two separate things.
| | 05:00 | Look what happens if I go back here and
take those out, cd Applications Support.
| | 05:04 | Well you can look at it and
tell what's going to happen.
| | 05:06 | It's going to say "cd into Application?
That's the folder? And then I don't know
| | 05:11 | what I am supposed to do with Support,"
but it's going to try and just go in
| | 05:14 | Applications and say there
is no such file or directory.
| | 05:16 | So it really does have to have
that backslash in front of it.
| | 05:19 | Application, hit Return, and
you can see I went in there.
| | 05:23 | You also, as I said, can put quotes around it.
| | 05:25 | "Application Support" and it
gets me that the same way.
| | 05:29 | So those are the two ways that we
have to make sure that Unix knows this
| | 05:32 | one complete thing.
| | 05:33 | It's one file name or one directory name.
| | 05:36 | It's not supposed to be two
separate things. Disregard that space.
| | Collapse this transcript |
| Creating files| 00:00 | Now that we understand about file
naming, we are actually ready to start
| | 00:03 | creating some files and there are
three main ways to create files in Unix.
| | 00:08 | The first is using a text editor
and Unix has its own text editor.
| | 00:11 | We are not going to be using the text
editors that you have on your Mac like
| | 00:14 | Microsoft Word or whatever it might be.
| | 00:16 | We are going to be using Unix's text
editors and we are going to see how to do
| | 00:19 | that in the next movie.
| | 00:20 | There is also a technique where we can
direct output from a command and put that
| | 00:25 | in a file and we will see
that in a future chapter.
| | 00:28 | Then there is the simplest of all,
the one that we are going to look at right
| | 00:31 | now, which is just touch.
| | 00:33 | Let's go back to our command line
and see how the touch command works.
| | 00:36 | Back in my home directory, we can just
see what's in there right now, ls -la.
| | 00:41 | We will see the full list. Before we do touch,
let's take a look at what touch actually does.
| | 00:45 | Let's look in the manual for touch.
| | 00:48 | Change file access and modification times.
| | 00:51 | Well that doesn't sound like it has
anything to do with creating files.
| | 00:55 | But here is the thing. touch is a very
simple command that Unix users use all
| | 00:58 | the time and what it does is it just
reaches out to a file and if it exists it
| | 01:04 | touches it and updates its access time.
| | 01:06 | If it doesn't exist, it creates it
for us and it's that second part that is
| | 01:10 | what I want us to see.
| | 01:11 | So let's just do touch somefile.txt and
now let's do a listing again and there it is.
| | 01:18 | It's the last one there.
| | 01:19 | It's a zero file. There is nothing inside of it.
You can see that the size of the file here.
| | 01:24 | All I did was reach out and say, "Oh
you want me to touch that file? Well it
| | 01:27 | doesn't exist. I will create it for you."
| | 01:28 | Now the other way of using touch
would just be to say, let's say,
| | 01:32 | touch .bash_history and see
my history file right there.
| | 01:35 | You can see the last
access time for it was 15:59.
| | 01:37 | I will touch the bash_history and then
let's go up, ls -la, now we can see that
| | 01:43 | the time has been updated. Now it's 16:12.
| | 01:45 | Now let me be the more conventional
way of using touch, but the thing is that
| | 01:49 | Unix users use touch to
create files all the time.
| | 01:52 | It's a really, really simple thing to
do. You want to make sure that the file
| | 01:55 | exists, so you touch it.
| | 01:57 | If it exists already, fine.
| | 01:59 | It's just going to update its access
time and it's there, but if not, it will
| | 02:02 | create it for us so that we can be
assured that that file is there whenever we
| | 02:06 | do a future operation.
| | 02:07 | So just keep it in mind as one of the tools.
| | 02:10 | Now the file it creates is empty and
in most cases what's more useful is
| | 02:14 | actually to be able to put content in the file.
| | 02:16 | So for that we are going to
need to use a text editor.
| | Collapse this transcript |
| Unix text editors| 00:00 | In this movie, we are going to
take a look at the text editors that
| | 00:02 | are available for Unix.
| | 00:04 | If you remember back to our discussion
about the history of Unix, Unix has been
| | 00:07 | around a really long time and it pre-
dates all of the graphical user interfaces
| | 00:11 | in the modern day text editors that we are used to.
| | 00:14 | So what did they use in those really
early days, when Unix had first been invented?
| | 00:17 | Well, they used a text editor called ed,
for edit text, and it was really simple
| | 00:23 | and is not at all user friendly.
| | 00:24 | So it's not something that we even
want to think about now, but in the early
| | 00:27 | days that was the very first thing.
| | 00:28 | Well, then over time people said, "It
would be great if we had something that was
| | 00:32 | more of a visual text editor. We can
see the page that we are working with."
| | 00:36 | And so they came with something called vi.
| | 00:38 | That's visual editing mode and later
that was improved and replaced with vim.
| | 00:43 | So you can still type vi, but chances
are it will take you into vim, because vim
| | 00:46 | has pretty much replaced it.
| | 00:48 | It's just vi with some improvements.
| | 00:50 | vim is very much still in use. There
are a lot of Unix users that love it.
| | 00:55 | The reason why they love it is because your
fingers get to stay in the center of your keyboard.
| | 00:59 | Its' a modal editor, so you
switch modes. You hit a special key.
| | 01:04 | It switches you into a different mode
and then now your keystrokes on those main
| | 01:09 | letter keys do something different,
let's say move your cursor around.
| | 01:13 | Then you switch into editing mode.
Now your keystrokes are actually editing
| | 01:17 | the text. Then you switch back out of editing
mode into move the cursor around mode and so on.
| | 01:22 | So they like it because their
fingers really stay in the center of the
| | 01:24 | keyboard and it's very fast.
| | 01:26 | It's tough for beginners, because you
have get used to switching between these
| | 01:29 | different modes and sometimes you might
hit a key thinking you are going to get
| | 01:33 | the letter t, but in fact you have
actually triggered a different action because
| | 01:37 | you were in a different mode.
| | 01:38 | Another very popular editor
in the Unix community is Emacs.
| | 01:42 | It stands for editor macros.
| | 01:44 | Its power really comes from the fact
that it has lots and lots of macros that
| | 01:47 | allow you to do complex task, so it
really allows you to do all sort of
| | 01:51 | things besides just simply edit the text.
You can think of it as being a Swiss army knife.
| | 01:55 | Again not that great for beginners because
you have to learn what all these macros are.
| | 01:59 | It has all these power there but it
requires you to really learn how it works,
| | 02:03 | the same way that vim does.
| | 02:05 | A better choice for beginners
is pico which later became nano.
| | 02:09 | pico stands for pine composer. pine
was a very simple email program that will
| | 02:14 | allow you to send emails from the command line.
| | 02:17 | So you would compose your simple email
message using this pine composer and then
| | 02:21 | send the message up, very user friendly.
| | 02:23 | pico became an editor on its own
separate from the mail program.
| | 02:27 | Later that was improved and replaced
with nano and nano is a little bit of take
| | 02:31 | off of pico because pico can be
prefixed to a size or a number.
| | 02:36 | nano is 1000 times larger than pico and nano
is now what we have and what we will be using.
| | 02:42 | If you type pico on your Mac,
you will just get put into nano.
| | 02:45 | It has completely replaced it.
| | 02:46 | It has basic features.
It is very easy to use.
| | 02:49 | So the easiest way to use nano is
just to type the nano command. nano boom!
| | 02:54 | Now I am inside nano.
| | 02:56 | This is my text editor. I can use the
Return key to go down, use the arrow key to
| | 03:01 | go up, and I can just type. Hello!
| | 03:04 | Whenever you want to exit the
program you will see at the bottom it gives
| | 03:07 | you hints about things it's going to do.
You see that there is a little up arrow there.
| | 03:11 | That's the equivalent of the Ctrl key.
| | 03:13 | So Ctrl+X, for example, will allow me to
exist. Ctrl+G would get help and show me
| | 03:18 | lots of help information.
| | 03:19 | Let's go ahead and exit for now.
| | 03:21 | Ctrl+X. It comes up and
says Save modified buffer?
| | 03:25 | So do I want to save the changes that I made.
| | 03:27 | That's what it's asking.
| | 03:28 | It's the same kind of pop-up window
that you get if you try and close a
| | 03:31 | Microsoft Word file without
saving your changes first.
| | 03:34 | So do we want to save these changes.
If I say No, just type it in, you will see I
| | 03:39 | come right back here.
| | 03:40 | If I had said Yes, then it
would prompt me for a file name.
| | 03:42 | There is another way that you can use
nano. We can use nano on an existing file
| | 03:47 | or nano on a new file.
| | 03:48 | If I type nano followed by newfile.txt
then it will go ahead and have that name
| | 03:56 | waiting for me when I am done.
| | 03:57 | Let's go ahead and try it a real quick.
nano newfile and This is a new file and
| | 04:03 | then we will do Ctrl+X, save changes,
this time I am going to hit Y for yes, and
| | 04:08 | it says File Name to Write to.
| | 04:10 | Do you want to write it to newfile.txt?
| | 04:12 | So it has already got the name that I
gave it there waiting. I just hit the
| | 04:16 | Return key and there I am. Now if you do
ls -la, you will see that that new file is there.
| | 04:22 | You can also use it on an existing file.
Let's take the somefile.txt that we
| | 04:26 | created before. W can just do Nano
somefile.txt. There we are, there are no
| | 04:33 | lines in there but you can see that I
am inside this file and I will just type
| | 04:36 | changes, and then Ctr+X, Y, voila!
| | 04:40 | There I am out of there and now we can
see that that file actually has those
| | 04:43 | changes. nano somefile.
There is my changes inside there.
| | 04:48 | Now as I said before, you just use the
arrow keys to move around. You can hit
| | 04:52 | the Delete key to backspace, to
erase thing,s and then type replacement
| | 04:56 | characters just at the cursor,
exactly like you would expect.
| | 04:59 | The other main features that you are
going to use from this are all down here
| | 05:02 | at the bottom. You can search through
a document using the W, where is, and
| | 05:06 | that will search the document for the next
occurrence of whatever it is you are looking for.
| | 05:10 | Previous page and next page. If you
have a long document, you can use the Ctrl
| | 05:14 | key with either Y or V to go forwards
and backwards and then you might also
| | 05:18 | frequently use K to cut text.
| | 05:20 | So you can cut text sort of to cut and
then uncut. It is the same thing is paste.
| | 05:24 | We will cut a line of text and
then you will paste it somewhere else.
| | 05:28 | There are lots and lots of other options.
You can use the help menu to find out
| | 05:31 | a lot of things that you can do here.
| | 05:33 | You can also go to http://www.
nanoeditor.org and that will also give you
| | 05:42 | more information there.
| | 05:43 | So www.nano-editor.org and it's sort of
a user's manual for all the things that
| | 05:48 | you can do with nano.
| | 05:49 | But this is really what we are going
to be using for. It's just these real
| | 05:52 | basics,. Just sort of open it up,
edit some text and exit back out of it.
| | 05:55 | Now you may be wondering, well, what
about other text editors things that you
| | 05:58 | are most familiar with, can I use those?
| | 06:00 | Absolutely. If you have something like
TextMate, things that just write plain
| | 06:05 | text files, those are fine to use.
| | 06:07 | Something like Microsoft Word though,
you would not want to use because
| | 06:10 | Microsoft Word saves in its file not
just text but also its formatting
| | 06:15 | information that has to be
interpreted by Microsoft Word.
| | 06:18 | So we couldn't open it up in nano or we
would see all sorts of characters that
| | 06:22 | don't mean anything to nano, which do
have a special meaning to Microsoft Word.
| | 06:25 | So plain text editors are fine, but using
something like Microsoft Word, you want
| | 06:29 | to make sure that you save
your files as plain text.
| | 06:32 | Since we are really trying to focus
on Unix, let's try and stay inside the
| | 06:35 | command line and use the
tools that Unix gives us.
| | Collapse this transcript |
| Reading files| 00:00 | Now we will learn how to
read the contents of a file.
| | 00:03 | Now you may be thinking, why not to use
Nano to open and read the contents of a file?
| | 00:07 | Well, you certainly can.
In fact I do it all the time.
| | 00:10 | But what we are interested in here are
tools which are designed just for reading files.
| | 00:14 | This difference will become important
later because we'll learn that to read a
| | 00:17 | file really just means to output the
text to your screen and we'll learn to
| | 00:21 | direct that output that places besides
our screen and even to other commands.
| | 00:25 | A tool like nano which depends on
user interaction would do too much for us
| | 00:29 | and it would get in the way.
| | 00:30 | Now, we have a couple of options
for reading files. The first is cat.
| | 00:35 | cat is the simplest and the one
that you would use most often.
| | 00:38 | It has kind of a funny name,
because cat is short for concatenate.
| | 00:42 | You see, cat doesn't just read a file.
| | 00:44 | It will concatenate or
join several files together.
| | 00:48 | You can think of it as read and output
the first file, then immediately read and
| | 00:51 | output the second file, and
then the next file and so on.
| | 00:54 | That's its classic and intended use.
| | 00:57 | But if we give cat only one file as
an argument, well then it just reads
| | 01:00 | that one file and quits.
| | 01:02 | There's no second file to ever go on to.
| | 01:04 | So it has a bit of a quirky name,
because its name implies using more than one file,
| | 01:08 | while we probably are going to
use cat more often on just single files.
| | 01:13 | But we can't use it for both cases.
| | 01:15 | But there's one big problem with
using cat when we want to review a long
| | 01:19 | file on the screen.
| | 01:20 | With cat all the output is
red and displayed at once.
| | 01:24 | That leaves you scrolling up through
the output to find the start of your very
| | 01:27 | long file and then
scrolling down again as you read.
| | 01:31 | So, for this case when we have a long file,
| | 01:33 | we have another command
that's better called more.
| | 01:36 | more gives you paginated output.
| | 01:38 | It outputs one page of text and then
waits for you to hit the spacebar to move
| | 01:42 | to the next page. Much easier
but more also had a problem.
| | 01:46 | more doesn't allow you to
go backwards to the pages.
| | 01:49 | You could move through the pages
forward, but if you want it to go back, well,
| | 01:52 | then you had to exit out of it
and start again from the beginning.
| | 01:55 | That wasn't very convenient, especially
if you just wanted to go back one page
| | 01:58 | for just a quick second
before you moved on again.
| | 02:01 | So as an improvement on more, we have less.
| | 02:04 | Now, obviously the name less is a
play off of more and less offers the ability
| | 02:09 | to scroll backwards.
| | 02:10 | That also has a little bit memory use.
Where more would load the entire document
| | 02:14 | memory, less will just load
one page into memory at a time.
| | 02:17 | It's a bit of a Unix joke that since
less was an improvement on more, that you
| | 02:21 | can say less is greater than more.
| | 02:23 | Now, on the Mac you can actually say
that less is more, because more has been
| | 02:27 | completely replaced by less.
| | 02:29 | You can still type more, but the
command that actually will execute is less.
| | 02:34 | The man pages use less.
| | 02:35 | So we have already seen them in action
and we have gotten a bit familiar with it.
| | 02:38 | We can use F to go forward, B to go
backward, and Q to quit and return to
| | 02:43 | the command prompt.
| | 02:44 | Let's try them out.
| | 02:45 | So in Terminal, I am in my user
directory, and I'll just show you that I have
| | 02:49 | got a couple of new files in there.
| | 02:50 | The first one is called lorem_ipsum.txt
and with that is a very long file filled
| | 02:55 | with vague Latin text.
| | 02:57 | Then I also have this file
short_file.txt, which is also including the exercise
| | 03:02 | files but it's a very simple file.
You couldn't come up with any short file.
| | 03:04 | I just wanted to have something to
contrast against the very long lorem_ipsum.
| | 03:09 | So let's start trying these out.
| | 03:10 | So I've got cat and then I can say
cat short_file.txt. There you see it.
| | 03:16 | It just output the contents of the file.
| | 03:19 | Now, as I said, what cat is really
for is for concatenating together.
| | 03:22 | So if we had cat short_file.txt and
then space followed by newfile.txt.
| | 03:28 | That's just a simple one line file we
created in the last movie. There they are.
| | 03:32 | It puts those together.
| | 03:34 | So we've got now the short_file followed by
the newfile, one immediately after the other.
| | 03:38 | So that's what
concatenate is really designed for.
| | 03:41 | Even though most of the time, you'll
probably use it in the first usage just
| | 03:44 | with a single file name. So let's try.
| | 03:46 | I am going to clear the screen using
Command+K. Just cat that lorem_ipsum.
| | 03:53 | Here we go. Ready? Hit Return. Boom!
| | 03:54 | It output all of this text and
now look at this scrolled back.
| | 03:57 | Right, I would have to scroll all the
way up here and find what I was looking for,
| | 04:01 | not really that useful.
| | 04:03 | So instead what you would
want to use is more or less.
| | 04:07 | So on the Mac less is more, so we don't
need to bother playing with more at all.
| | 04:10 | We'll just go straight to less and let's
try out with lorem_ipsum again and I am
| | 04:15 | going to clear the screen just so we have that
old stuff out of there again. And there we are.
| | 04:19 | Now look at the bottom. It's waiting.
| | 04:22 | It shows me just one page.
| | 04:23 | I am going to hit Space. Now I get the next
page. Hit space again. I get the next page.
| | 04:28 | And as I said, F and B allow
us to go forward and backwards.
| | 04:31 | So we can toggle between those and then
when we are finally done we can hit
| | 04:35 | Q to get out of it.
| | 04:36 | There's one other really useful command
in there I want to show you, which is
| | 04:40 | that you can G to go to
the beginning or the end.
| | 04:44 | G by itself goes to start of the
document. Uppercase G goes to the end of the
| | 04:49 | document, so Shift+G. So let's try it.
Let's do Shift+G and we jump to the end.
| | 04:54 | You could see it says END down there,
and I do just a regular g, lowercase g.
| | 04:59 | It pops you back up to the top of the document.
| | 05:01 | So that's a good and useful to know.
| | 05:03 | There are also a couple of
nice options that you can add in.
| | 05:06 | You can do less with a capital M for lorem_
ipsum and it gives you a little bit better prompt.
| | 05:11 | So now you can see at the bottom it
actually tells me which lines I'm viewing
| | 05:14 | and how far I am through the document.
I get a percentage. There is another one.
| | 05:18 | Let's go back here and
do n. That shows line numbers.
| | 05:22 | So it numbers all those lines for me
and there's lots more options that you
| | 05:27 | can use with less.
The man pages for it are very long.
| | 05:29 | So you can definitely go in there and
see all the different things that you can
| | 05:32 | do with it, but that's
will get you started with it.
| | Collapse this transcript |
| Reading portions of files| 00:00 | In this movie, we'll look at two
other useful commands for reading files.
| | 00:04 | They are head and tail.
| | 00:06 | The difference is with both of these commands,
they will read just a portion of the file.
| | 00:10 | head displays the lines from the
beginning of the file. This is useful for just
| | 00:14 | peeking at the start of the file,
let's say viewing the first ten lines.
| | 00:18 | tail does the same thing but in reverse.
| | 00:20 | It displays lines from the end of a file.
| | 00:22 | This comes in very handy, when you
want to look at files like access logs or
| | 00:25 | error logs where the latest entries
in the file will be added to the end.
| | 00:30 | So for example, you can view the tail
of a web server's access log to see the
| | 00:33 | most recent requests made to the web server.
| | 00:36 | tail also has a very useful option, the f
option which allows us to follow the file.
| | 00:42 | It reads the tail of the file normally,
but then instead of exiting back to the
| | 00:45 | command prompt, it will
watch for changes to the file.
| | 00:48 | Whenever a new line is added, it will
show us that line. We are following the
| | 00:53 | file waiting for updates.
| | 00:54 | It is a lot like following a
news story for the latest updates.
| | 00:57 | Let's see how we can use these. head
lorem_ipsum. Voila, you see we just see
| | 01:03 | those first ten lines I guess.
| | 01:04 | We can change one of the configurations
is to be able to change how many lines
| | 01:08 | we see, but I am seeing the default there.
| | 01:10 | So you can see we can peek at the
beginning of the file, and tail does the same
| | 01:13 | thing but looking at the bottom of the file.
| | 01:15 | I'll just clear the screen
so we can see the difference.
| | 01:18 | So there we are seeing the very bottom of file.
| | 01:20 | Now, as I said, one of the places the tail is
really useful is being able to follow a file.
| | 01:25 | Let's try that real quick.
| | 01:26 | Lets type tail -f and that
will allow us to follow the file and let's
| | 01:32 | try it with this newfile.txt
that we created in the last movie.
| | 01:35 | Just a nice simple file.
There it is, there is the output.
| | 01:39 | And you can see that it's
just sitting there waiting.
| | 01:41 | We didn't get our command prompt back.
| | 01:43 | That's because it's following the
file. The process is still going on.
| | 01:46 | To get out of it, in case you feel
stuck, it's Control+C. That's one of the key
| | 01:51 | combinations that's really common on
Unix to be able to get out of a program to
| | 01:54 | exit out, we'll be seeing that a little
later on but for now, but for now just
| | 01:58 | Control+C will get you
back out into your prompt.
| | 02:00 | Let's do it one more time so that
we can see what it actually does.
| | 02:03 | I am going to open up a new window with
Command+N and let's just edit that with nano.
| | 02:07 | So newfile.txt, there it is. I'll go
down here. I'll make a new line here.
| | 02:12 | I'll say This is a new line in this file.
| | 02:17 | I'll hit Return. Now exit out of it and
save those changes to newfile and look
| | 02:23 | over here, look what happened.
| | 02:24 | It went ahead and actually
echoed this line a second time.
| | 02:27 | I think that's because of the line return that
I put in, but this is a new line of this file.
| | 02:32 | So it just followed the progress of the
file. As new entries get made into there,
| | 02:35 | we'll see what those are.
| | 02:36 | So that's a really useful tool.
| | 02:38 | And then Control+C to get out of there.
| | 02:40 | If you want to try that out with tailing a file,
| | 02:43 | one place that you can try that is to
tail your system logs and that's stored in
| | 02:47 | var/log/system.log.
That's where that's contained.
| | 02:52 | I am not going to show you mine, but
you can take a look at your system and
| | 02:54 | see what's in there.
| | 02:55 | If you have a web server running,
you can do that on a Mac at least.
| | 02:59 | Those are stored in apache2/access_log and
you can watch requests come into the web server.
| | 03:05 | Or I believe its error_log is where
you would usually find the errors that come in.
| | 03:11 | So you could tail both of
those files and try that out.
| | 03:13 | Now just as a quick summary again,
you want to use cat for small files,
| | 03:19 | you want to use less for large files, and
you want to use head and tail when you
| | 03:23 | wanted to take a peek at the
beginning or the end of a file.
| | Collapse this transcript |
| Creating directories| 00:00 | I want us to see how we can go back creating
directories that we could put our files into.
| | 00:04 | Directories are essentially folders,
but in Unix you are always going to
| | 00:07 | call them directories.
| | 00:09 | So start getting use to that terminology.
| | 00:10 | Instead of calling it a
folder, call it a directory.
| | 00:13 | Making a directory is really simple.
| | 00:15 | We just say mkdir, space, and then
the name of the directory we want.
| | 00:20 | So I am going to create one called
testdir. Hit Return and it created it.
| | 00:25 | It didn't give me any output there.
If I do ls-la you'll see that it created.
| | 00:30 | If we come down here at the very last
entry, you can see there's a directory
| | 00:33 | because it has this d at the front
and that differentiates it from just a
| | 00:36 | standard file which has a dash.
| | 00:38 | Now if you were to try it again and
create the same directory again, you'll get
| | 00:42 | an error letting you know that file exists.
| | 00:44 | It says File exists but it's actually
letting us know that the directory exists
| | 00:48 | and then makes sure we don't
accidentally overwrite something we don't mean to.
| | 00:52 | Now, if we want to create a directory
inside a directory, we can just pass in a path,
| | 00:55 | mkdir testdir/test1.
| | 00:56 | That will create the test1
directory inside testdir.
| | 01:05 | It's just a path, nice and
simple. So that will work.
| | 01:08 | What if I want to go two deep though?
| | 01:11 | If we do test1/test2, now I am saying
in addition to going inside the test
| | 01:16 | directory I want you to create two directories.
| | 01:18 | The test1 directory that doesn't exist
followed by the test2 to directory that
| | 01:21 | doesn't exist. This won't work.
| | 01:23 | If we try this, it will come back and
say, "Oops! Sorry, test1 doesn't exist."
| | 01:28 | In order to make it work,
we need to pass in the -p option.
| | 01:32 | p is parents, the parent directories.
| | 01:34 | So create all parent directories
as needed until you get to the final
| | 01:39 | directory that I've got.
| | 01:40 | That's very helpful if we want to create
a large directory structure. We can say
| | 01:43 | all right, not just this folder but also
these folder is deeper down, create all
| | 01:47 | of them all at once.
| | 01:48 | That's nice handy trick.
| | 01:50 | And if we did ls-la,and we'll type in
the directory, testdir/, it will give us
| | 01:57 | the contents of that directory.
| | 01:58 | The other thing I think is
kind of nice is the -v option.
| | 02:02 | So we say mkdir -v is for verbose,
let's use that p option again, testdir/test1,
| | 02:10 | and let's create another one called test3
that will sit right next to test2.
| | 02:14 | So there you see it gives me some feedback
and lets me know which directories it created.
| | 02:18 | If it created four directories, we
would have four entries there, telling us
| | 02:21 | each one that it created.
| | 02:23 | So that's really all to creating directories.
| | 02:25 | Just make sure that you create a
directory with the name that doesn't already
| | 02:28 | exist and if you can pass in a full
path to the directory that you want, but if
| | 02:33 | some of those directories in that path
don't already exist, you will want to
| | 02:36 | also pass in the -p options
so it will create them as well.
| | Collapse this transcript |
| Moving and renaming files and directories| 00:00 | In this movie we'll talk about
moving and renaming files and directories.
| | 00:04 | I'll start out by just doing ls -la
to see what's in our user directory.
| | 00:08 | A couple files that I have added
here that are in the exercise files.
| | 00:11 | overwrite_tests1 and overwrite_test2,
those are just real simple files.
| | 00:15 | Just so you can see what's in them, just
overwrite_test1. I just put a bit of text in there.
| | 00:20 | We'll be using that a little
later on to talk about overwriting.
| | 00:23 | Everything else are things we have
been working on before and should be
| | 00:26 | available in exercise files.
| | 00:28 | So we want to move files around.
All we need is the mv command.
| | 00:33 | mv, the file that we want to move, let's
say we want to move newfile.txt, and then
| | 00:39 | a space and then the path where we
want to move it to. This can either be a
| | 00:43 | relative or an absolute path.
| | 00:45 | In this case, I'll just say a relative
path. testdir/newfile.txt. There it is.
| | 00:54 | It moved it into that directory.
| | 00:56 | If we do ls -la testdir, you can see now
that that's where the file exists and
| | 01:02 | it's no longer in this
directory. So it did move it.
| | 01:05 | It moved it into that deeper directory.
| | 01:07 | We can move it back. I am going to
actually change directories into the
| | 01:10 | testdir to do it, just to show
you that we can use newfile.txt with
| | 01:16 | ../newfile.txt and that works.
| | 01:20 | That's the parent directory.
| | 01:22 | So its saying go back one directory,
that this file and send it back one
| | 01:26 | directory. That's what it did.
| | 01:28 | We can take a look in this folder and
see that it's not there anymore and if we
| | 01:31 | go backwards one directory,
| | 01:33 | let's do ls -la, and we can now
see that its back here again.
| | 01:37 | Now the move command is also smart.
| | 01:39 | If we say move newfile.txt testdir and
nothing after it, we could put the slash
| | 01:47 | or not, it will move the file into
that directory with the same name.
| | 01:50 | It assumes the rest of it for us
because testdir is an existing directory.
| | 01:55 | That's the key part. It says,
"Oh! I see this existing directory.
| | 01:59 | I am guessing that you want to
move this file into that directory."
| | 02:02 | So you can save yourself a little bit of typing.
| | 02:04 | So move it into there, and same thing.
Let's change directories into the test
| | 02:09 | directory and you'll see that there it is.
| | 02:13 | Now, we can move it backwards
the same way. mv newfile.txt ..
| | 02:20 | We are saying parent directory
and this is saying "Oh!
| | 02:22 | the parent directory exists, so I am
guessing you want to take this file and put
| | 02:25 | it back there," and it
throws it back into that folder.
| | 02:28 | That's a nice shorthand.
| | 02:29 | And directories move the
exact same way as files.
| | 02:32 | Let's say for example that from
here, I am inside the test directory.
| | 02:35 | Let me show you we have got inside
test directory, we have got test1 and that
| | 02:39 | has two folders in it.
| | 02:41 | test2 and test3 that we created.
| | 02:42 | Lets say we want to move test
folder 3 into test folder 2.
| | 02:46 | We would do that with mv and we would
say inside test folder 1 you'll find
| | 02:53 | test folder 3. Where do we want to move
it, the path? Well, that would be inside
| | 02:57 | test1 inside test2, and again we can
either specify it by putting test3 and
| | 03:04 | being completely clear about it or
we can let it assume, and because test2
| | 03:09 | exists, it'll say," I am guessing you
want to put it inside that directory not
| | 03:13 | replace that directory."
| | 03:15 | That's key. Because that directory
exists already, it's not going to replace it.
| | 03:19 | It will put it inside of that directory.
| | 03:21 | Now, we can just check that out by
doing ls test1/test2 and we can see that
| | 03:27 | test3 is inside there.
| | 03:28 | In addition to moving files,
we can also rename files.
| | 03:33 | If you use the man pages for man rename,
you would be like "Oh, seems to be obvious.
| | 03:39 | That's the way that you go about renaming files."
| | 03:41 | No, it doesn't work.
| | 03:43 | It doesn't work on the Mac and it
doesn't work on a lot of systems.
| | 03:45 | It's very unreliable.
| | 03:47 | Most Unix people use mv for
renaming. It's how you go about renaming.
| | 03:51 | So let's back up a directory here.
| | 03:54 | I am back in my user directory again
and let's try renaming that new file and we
| | 03:59 | can rename it while we move it. Rename
newfile.txt. I could rename it in place.
| | 04:05 | I could just call it new_file.txt
and look at what that would do.
| | 04:10 | It would move this file and there's
not something existing, there's not a
| | 04:13 | directory called new_file.txt, so it says,
| | 04:16 | "Oh, you must want to rename it to that."
| | 04:19 | I can do it at the same time. I can
say all right, let's move it into the test
| | 04:22 | directory and rename it all at the same time.
| | 04:25 | Take a newfile, move it in test
directory and give it this new name. Let's just
| | 04:30 | take a look at that and
you can see there it is.
| | 04:33 | So it gave you that new name. You can see
I added the underscore is the difference.
| | 04:37 | We can do the same thing with directories.
| | 04:38 | So lets change the name of that testdir
that we have been working with and let's
| | 04:41 | start calling it unix_files.
| | 04:45 | So now, I am saying move this test
directory and move it to the same place I am
| | 04:49 | now but with this new file name.
| | 04:51 | Again, notice that the important
difference here is that this directory
| | 04:55 | doesn't already exist.
| | 04:57 | So it's not going to move it into a folder.
| | 05:00 | If the name that I gave it is the
second argument was a folder that exists,
| | 05:03 | then it wouldn't rename it.
| | 05:05 | It would just move it into it.
| | 05:06 | So notice that difference.
| | 05:07 | So there we are. So now, let's
rename it. Let's do ls -la one more time.
| | 05:13 | And you'll see that now
it's been renamed to unix_files.
| | 05:15 | Now, let's take a look at some of the
options that we can use with mv, because
| | 05:19 | they are pretty important.
| | 05:20 | There are four main options and these
are important options because they are
| | 05:23 | used for a lot of things having
to do with files and directories.
| | 05:27 | The -n option says no overwriting.
Don't accidentally overwrite a file.
| | 05:32 | When I move this file, don't replace it, okay.
| | 05:35 | -f says force that overwriting to happen.
| | 05:38 | It says all right, when I move this
file, if something is already in it's way,
| | 05:41 | blow it out of the way and put
yourself in it's place. It's destructive.
| | 05:44 | Interactive gives us a choice.
| | 05:47 | It just pops up it says, oh, wait a minute.
| | 05:49 | You want to move this file into
that directory with this file name?
| | 05:53 | But there's actually already
something in there. What do you want to do?
| | 05:56 | And then verbose is very similar with
what we were working with directories.
| | 05:59 | It just kind of gives you some
reporting information as it's going about doing
| | 06:03 | the move so that you know that
it succeeded. -n, -f, -i and -v.
| | 06:07 | Now, here is the super important thing to
know. By default move will overwrite files.
| | 06:13 | The -f option is turned on by default.
| | 06:16 | So you would want to use -n or
-i to make sure that you don't
| | 06:21 | accidentally overwrite files.
| | 06:23 | Let's see this in action.
| | 06:25 | As I said at the beginning, we
have these files overwrite_test1
| | 06:28 | and overwrite_test2.
| | 06:29 | That's so that we can play around
with these and have some files that we
| | 06:31 | can safely overwrite.
| | 06:33 | So if I do mv -n, that's the no
overwrite option, and I tell it to
| | 06:39 | overwrite_test1 on top of overwrite_test2,
| | 06:43 | I am telling it essentially rename this file.
| | 06:46 | The thing is that there's
already a file with that name.
| | 06:48 | The -n option, it doesn't do it.
| | 06:50 | Do ls -la, you see that
they're both still there.
| | 06:54 | It did not actually do it.
| | 06:56 | If we do it with the v option, see if
we get any more information back from it.
| | 07:02 | Not overwritten, so
verbose comes up and tells us.
| | 07:05 | Now let's try the same thing, but
this time let's do it with interactive.
| | 07:08 | I am going to use Ctrl+A to shoot to
the front of the line and then from
| | 07:13 | there I can just put in the i. So that
will try and rename it, but then when it
| | 07:17 | pops up it says, oh,
wait, do you want to overwrite that? Yes or no?
| | 07:20 | You may hit n for no, hit Return,
and then it will not overwrite it.
| | 07:25 | So that's the best one I think.
| | 07:27 | I really like the i option.
| | 07:28 | And then the f option is the default,
so I will just do it without any options.
| | 07:33 | Let me just remove that
so you can see what happens.
| | 07:36 | This would be the default behavior. We
just think we are innocently renaming the
| | 07:39 | file, but in fact we overwrote it.
| | 07:43 | So now if we cat overwrite_test2,
you see that it actually has the content
| | 07:49 | that was in test1.
Something to be very careful about.
| | 07:52 | And in the movie about aliases later
on, we are going to talk about how to
| | 07:56 | change that default behavior so that
you can set it so that it'll always does
| | 08:01 | either no overwriting or
interactive overwriting.
| | 08:04 | Now as an exercise on your own, go
ahead and make sure that you have this
| | 08:07 | directory called unix_files.
| | 08:08 | Create a new directory if you need
to and move all these files that we
| | 08:11 | have been working in,
| | 08:13 | like lorem-ipsum for example,
move those into unix_files.
| | 08:17 | Let's go ahead and put all of the files
that we have worked with in there
| | 08:20 | and start to have one place for those
where we can work with them in that directory.
| | 08:23 | And take a little time. Try moving
around, try creating new directories, try
| | 08:26 | creating new files, we know now how
to do that, and get use to moving around
| | 08:30 | and see what happens.
| | Collapse this transcript |
| Copying files and directories| 00:00 | Now, I would like to
talk about how to copy them.
| | 00:01 | Let's take a look. First of all, we'll do ls -la.
| | 00:05 | Notice that I'm inside my unix_files
directory now, which is inside my user root.
| | 00:10 | At the end of the last movie we
moved everything into this folder.
| | 00:13 | So you can see I have got all of
my unix_files just in this folder.
| | 00:16 | Now, I'll start by doing cat new_file.txt.
| | 00:21 | It's just a very simple text file.
| | 00:23 | Let's copy that. We use cp to copy and
we start by saying copy these source.
| | 00:30 | Our source for this is going to be file.txt.
| | 00:33 | Again, we can provide a
full path to that source.
| | 00:36 | It doesn't have to be something
that's in this current directory.
| | 00:38 | It can be an absolute path or a relative
path. And then the destination, the target.
| | 00:43 | And again, absolute or relative path.
| | 00:46 | In this case, we are going to
call it newer_file.txt. It did it.
| | 00:51 | Say ls -la,. We can now see the newer
file there and cat newer_file and we see
| | 00:57 | that it copied the content
as well. So it's that easy.
| | 01:00 | It really is just using
the cp command to do a copy.
| | 01:04 | The options that we can pass in for
copy are the same as they were for move.
| | 01:06 | n is for no overwriting, f is for force
overwriting, i is for interactive -- ask me
| | 01:12 | before deciding whether to overwrite or
not and v is for verbose, just to give
| | 01:16 | you a little more information
about what it's trying to do.
| | 01:19 | And also like move, force
overwriting is the default. Let's try that.
| | 01:23 | So I just created this newer_file.txt.
Now let's try copying short_file.txt and
| | 01:30 | we'll also put it at newer_file.txt.
| | 01:32 | So I am basically saying copy short_file
and its target should be newer_file.txt.
| | 01:38 | It did it.
| | 01:39 | It didn't object at all and if we say
now cat newer_file and see what's in it.
| | 01:46 | You can see it has that short_file text
in it. The same thing as short_file,
| | 01:49 | not the same thing as newer_file.
| | 01:51 | So it replaced it destructively.
| | 01:54 | So we would want to use n for don't
replace or i for interactive to ask me
| | 01:58 | before we actually do it.
| | 01:59 | Again, when we get to the aliases
chapter, we'll talk about how we can change
| | 02:03 | that default all the time.
| | 02:04 | And the directories work pretty much
the same way with one important change.
| | 02:08 | I'll just clear this.
| | 02:10 | So cp test1, that's the directory
that's in the folder where I am now.
| | 02:14 | Let's just call it test1_copy.
| | 02:19 | It's a directory so it was not copied.
| | 02:21 | It does that just to make sure that you
know, hey, this has a whole lot of stuff.
| | 02:24 | If I make a copy of it,
I may be here for a while.
| | 02:27 | That may be copying up lots of stuff,
taking a lot of hard drive space.
| | 02:30 | Make sure that really want copy it directly.
| | 02:32 | To do that, we do cp -R for recursive copy.
| | 02:38 | Keep copying down the line recursively
for every single folder that's inside
| | 02:43 | this directory and that will allow us to copy
test1 to test1_copy. Take a look, there it is.
| | 02:51 | So that -R option, is going to come a lot.
| | 02:53 | R is really the option.
Generally lowercase r works as well.
| | 02:58 | So, you don't have to worry too much
about remembering which one it is, but
| | 03:01 | technically it should be the
capital R. That's really all there is to
| | 03:05 | being able to copy files.
| | Collapse this transcript |
| Deleting files and directories| 00:00 | Now that we have seen how to create, move,
rename and copy files, we are ready to
| | 00:05 | see how to go about
deleting files and directories.
| | 00:08 | Removing is very simple. Before I show
you how, let me also just make the caveat,
| | 00:13 | this is not the same thing as in the
Finder when you delete something and it
| | 00:16 | moves into your trash.
| | 00:18 | If your remember ls -la, our user folder,
and you'll see that there's this special
| | 00:24 | folder called Trash and that is where things go.
| | 00:28 | That's what's actually down here. When we
put files in the trash that's where they exist.
| | 00:33 | When we empty the trash, it empties them out.
| | 00:35 | What happens when you delete a file in
the Finder is it's actually moving it.
| | 00:39 | It's not to empty the trash.
| | 00:41 | That it actually deletes it.
| | 00:42 | So there is a sort of buffer
time where you can recover a file.
| | 00:45 | Delete in Unix doesn't work that way.
| | 00:47 | When you remove something, it is gone.
| | 00:49 | It is gone right away.
| | 00:51 | It doesn't move int to the trash.
| | 00:52 | You can move things to your trash to
if you want. No problem you can do that.
| | 00:56 | You can move a file into the trash and
it would sit there and wait until you
| | 00:59 | actually have decided to empty your trash.
| | 01:01 | But that's not what we are talking about here.
| | 01:03 | So let's just notice I am inside my
unix_files folder in my user directory.
| | 01:08 | Lets take a look at the files that are in there.
| | 01:09 | Let's start by deleting this
somefile.txt that we created a little while ago.
| | 01:14 | To delete it, all we use is
rm for remove, nice and simple.
| | 01:18 | rm somefile.txt and now it's gone completely.
| | 01:24 | So again, use it carefully. The file
is completely gone, not recoverable,
| | 01:28 | we can't get it back.
| | 01:29 | So use that very wisely.
| | 01:31 | What about with directories?
| | 01:32 | Well let's try that. Let's
try making a new directory.
| | 01:35 | We'll call it delete_me. Now we have made
a directory and now let's try removing it.
| | 01:40 | remove delete_me. It doesn't work.
| | 01:43 | It comes and says ah, this is a directory.
| | 01:45 | To remove directories, we have two
options and there's a difference between them.
| | 01:49 | The first is we can say rmdir. Just
like we had make directory, now we have the
| | 01:55 | reverse of that, remove directory.
delete_me. Voila, deleted it. It's gone now.
| | 02:01 | Now, the one thing about remove
directory is it will only remove
| | 02:05 | directories that are empty.
| | 02:07 | Let's try and remove directory on
test1 copy that we created earlier.
| | 02:12 | Directory not empty.
| | 02:14 | So therefore, we have to go
inside that directory and empty it out.
| | 02:18 | Inside that directory, there may be more
things and more things and more things.
| | 02:21 | It could take a lot of emptying things out.
| | 02:24 | In that case, what we have is actually
the same thing as we had in copy recursive.
| | 02:28 | Remember we had cp -R and that's how he
created the copy of test1 to test1_copy.
| | 02:34 | Well, we have the same thing
remove, with the capital R, test1_copy.
| | 02:40 | So again, use this very carefully.
| | 02:42 | It will remove everything
that's in test1_copy recursively.
| | 02:48 | So it doesn't care
whether they are empty or not.
| | 02:49 | It will remove non-empty directories.
| | 02:52 | There it is. Now it's gone, take a look.
| | 02:55 | Now test1_copy is gone too.
| | 02:56 | So to review rm only removes files.
| | 03:00 | rmdir only remove empty directories.
| | 03:05 | It's up to us to go inside and empty
them out, and there's no way to override that.
| | 03:09 | There's no option you can
pass in. rmdir is always safe that way.
| | 03:14 | That makes it good.
| | 03:15 | It's a good choice to use most of the time.
| | 03:17 | rm -R is really powerful.
| | 03:20 | That's the one that will remove files
and directories recursively and it doesn't
| | 03:25 | care if the directories are empty by default.
| | 03:27 | So just be careful with that one.
| | 03:28 | That's the really powerful one.
| | 03:30 | That's a great tool to have in your toolbox.
| | 03:32 | But just be careful, as
you are cutting away things.
| | 03:34 | Make sure you don't accidentally
cut away something you don't need to.
| | Collapse this transcript |
| Finder aliases in Unix| 00:00 | Over the course of the next several
movies I want to take a look at how to
| | 00:03 | work with links in Unix.
| | 00:05 | Conceptually, links are similar to file
aliases that you create in the Mac OS X Finder.
| | 00:10 | You may already be familiar with those,
but they're not the same thing and it's
| | 00:13 | important for us to understand
the differences between them.
| | 00:16 | We'll start by taking a look at the
way that the Mac OS X Finder aliases work.
| | 00:20 | And to begin with, let's
just create a simple file.
| | 00:23 | So notice that I am already inside my
unix_files directory and I'll create a new file.
| | 00:27 | I'll call it linkedfile.txt and in
it, I'll just put some sample text, Link test.
| | 00:34 | Ctrl+X to exit, yes to save the
changes, and return to accept the name that
| | 00:39 | it wanted to give me.
| | 00:40 | Now if we do ls -la, we can see
that file we just created right here.
| | 00:44 | Notice that the size of the file is 10.
| | 00:47 | That's because there are
just ten characters in it.
| | 00:49 | That's what it's storing and keeping track of,
and it's just a normal regular file here.
| | 00:53 | That's what this dash at
the beginning indicates.
| | 00:55 | Pay attention to those as we look at all
these example, because they will change
| | 00:58 | between the different options
that we are going to look at.
| | 01:00 | Now let's create an aliases to it in
the Finder. This is not in Unix, this is
| | 01:04 | in the Mac OS X Finder.
| | 01:05 | We find the file inside unix_file,
here is linkedfile.txt, and then we can
| | 01:09 | create an alias in a couple of ways.
| | 01:11 | From the File menu, we can select
Make Alias or we can use the Command+L
| | 01:15 | shortcut that you see there, or we can
actually Option+Command+Drag the file and
| | 01:19 | it'll create an alias that way.
| | 01:20 | I am just going to choose the File menu
item and I am going to rename this file
| | 01:24 | as well to be alias_to_linkedfile, okay.
| | 01:30 | Notice that it has an arrow on the
icon to indicate that's it's an alias.
| | 01:34 | That's an indication in the Finder that
if we were to double-click on this file
| | 01:37 | it would act as if it were
opening the original file.
| | 01:41 | It would find that
original and open that file up.
| | 01:44 | Now, there's a couple of important points
about the way that the Finder aliases work.
| | 01:48 | First if the file or the alias
moves it still points to the file.
| | 01:52 | The alias still points to the file,
no matter where we relocate the two of
| | 01:55 | them in the file system.
| | 01:57 | The Finder will help make sure that
the two can always find each other.
| | 02:00 | And if the file is deleted, then the
alias will break, because the alias will
| | 02:05 | still be sitting there but when we
double-click on it, it will try go and
| | 02:08 | find the original file.
| | 02:09 | But the original file is now
gone so it can't open it up.
| | 02:12 | So the alias essentially
still sits there as a dead end.
| | 02:14 | We can also make aliases of folders as well.
| | 02:18 | So if we click on test1 and we pick
Make Alias, it will make and alias for
| | 02:22 | that as well, and I'll just rename that alias_
to_test1, and I will pop back up to the top.
| | 02:28 | Now, lets take a look at these in Unix.
| | 02:31 | So ls -la, I'll just clear my screen first.
| | 02:34 | So take a look at each of these. Notice
that both of them have much larger file sizes.
| | 02:40 | This one right here is significantly larger.
| | 02:42 | They both also are just regular
files. Notice the dash at the beginning.
| | 02:45 | So that just lets us know that they're just
regular files that point to these other ones.
| | 02:50 | So what's in these files
that makes them be so large?
| | 02:53 | Well, that's the information that
the Finder is using to be able to keep
| | 02:57 | track of these things.
| | 02:58 | So that as the two move around in the
file system, it has all the information it
| | 03:01 | needs to be able to locate that file.
| | 03:03 | That's what's inside there.
| | 03:05 | So on the Finder side, if we were
to double-click on alias_to_test1,
| | 03:10 | it opens test1 for us.
| | 03:11 | If we click alias_to_linkedfile,
you see that it opens up for us.
| | 03:17 | But on the Unix side, that's not the case.
| | 03:19 | Lets try opening up the file in our text editor.
| | 03:21 | nano alias_to_linkedfile.
| | 03:25 | Well, look what we get back.
| | 03:27 | All sorts of information that Finder is
using, but it did not actually use the file.
| | 03:33 | It did not alias the original file.
| | 03:36 | And if we do cd alias_to_test1 and
try and change directory, it says oops,
| | 03:43 | that's not a directory.
| | 03:44 | We can't change directory
into that. That's a file.
| | 03:46 | So these files are very useful to the
Finder and for the Finder to do its thing,
| | 03:52 | but they are useless
to us outside of the Finder.
| | 03:56 | They are really for the Finder and they
do their job very well, for the Finder.
| | 03:59 | But in Unix, these kind of aliases are useless.
| | 04:03 | Instead what we are going to need to
use in Unix is Unix version of aliases
| | 04:08 | which are called links.
| | Collapse this transcript |
| Hard links| 00:00 | In Unix we are going to
want to make use of links.
| | 00:03 | And the first kind of links that we are
going to look at in Unix are hard links.
| | 00:07 | The way that we make a hard link is
just simply to have ln, short for link,
| | 00:11 | space and then the file that we want
to link. That could either just be a file
| | 00:16 | name or could be a full path to a file,
if the file is located somewhere else.
| | 00:20 | Followed by a space and then followed
by the name of the hard link or if we
| | 00:25 | want to put that link somewhere else,
the path where we want to locate it, as
| | 00:28 | if we were creating a file.
| | 00:30 | But if we are in a single directory,
than we would just put the file that we
| | 00:34 | want to link and then the hard
link that we want to make to that file.
| | 00:37 | What this will do is make a reference
to a file in the file system, the same
| | 00:41 | way that making a Finder alias would do.
| | 00:44 | And like the Finder, it will
not break if the file is moved.
| | 00:47 | But there's one important difference
about the way that hard links work.
| | 00:51 | Remember in the Finder, if we threw
away the original file, the alias still is there,
| | 00:56 | but it's broken.
| | 00:57 | It doesn't point anything anymore.
| | 00:58 | It was just an alias to that original file.
| | 01:02 | Well, that's not the case with hard links.
| | 01:04 | With hard links, they don't
break if the file is deleted.
| | 01:07 | Now that could be a little weird to work
with it first, until you get used to it and
| | 01:11 | understand what they are really doing.
| | 01:13 | But when you think about it, every time
we see a file listed for us, all it is
| | 01:18 | is a reference to a file in the file system.
| | 01:21 | So making a hard link is really no different.
| | 01:23 | It's just making another reference
to that same file in this file system.
| | 01:28 | Both file names that we see on the
screen are pointing at the same spot on our
| | 01:32 | hard drive at that same file storage space.
| | 01:36 | So what we are doing with the hard
link is allowing two different names to
| | 01:40 | point to that same thing.
| | 01:41 | That's different than the way
the Mac OS X Finder works.
| | 01:44 | The Mac OS X Finder there's
definitely an actual file that points to that
| | 01:48 | space on the hard drive and then
there's this other thing called an alias
| | 01:53 | which points to that original file, not to the
spot on the hard drive but to that other file.
| | 01:58 | Let's try that out in Unix
so you can get the hang of it.
| | 02:01 | So notice that here I am inside my
Unix files,and in the last movie, ls -la,
| | 02:07 | you can see that I have my
linkedfile.txt that I'll be linking to and then I
| | 02:11 | also had these other aliases here.
| | 02:13 | Now these aliases are not included in
the exercise files because they don't
| | 02:17 | travel well. They really are designed
for making references on my hard drive, so
| | 02:20 | you won't find them there.
| | 02:22 | But it's easy enough to
create these aliases in the Finder.
| | 02:24 | What we want to focus on though is this
linkedfile.txt and so we want to make a hard link to it.
| | 02:29 | Well, we use ln and then a space and
then the name of the file we want to link.
| | 02:34 | So linkedfile.txt followed by a
space and then the name of the link that
| | 02:40 | we want to give it.
| | 02:41 | I am going to call this one hardlink.
| | 02:43 | So it's always in that format: the link,
followed by the source, the thing we are
| | 02:47 | targeting, and then the name
of the link is the last item.
| | 02:51 | Now let's do ls -la again, and
let's take a look at what's there now.
| | 02:55 | So here's my hardline. Notice that the
size of this is much, much more smaller
| | 03:00 | than the size of the aliases I created.
| | 03:02 | It is exactly the same size as
linkedfile.txt and that makes sense.
| | 03:07 | These are two files pointing at
the same spot on the hard drive.
| | 03:12 | And if we open it up, let's do cat
hardlink, now you see it has the exact
| | 03:18 | same text inside of it.
| | 03:19 | Let's take a look over here in the
Finder and just see it here. In the Finder it
| | 03:24 | didn't' give it any kind of icon here
because it didn't have .txt or anything
| | 03:27 | after it, so it just
gave it a generic file icon.
| | 03:30 | But you can see that it does not have
the little arrow that the Finder uses to
| | 03:34 | indicate the special
alias files that it creates.
| | 03:37 | So as far as the Finder is
concerned, this is just another file.
| | 03:40 | Let's create another
directory that we can move this into.
| | 03:43 | I'll make a directory. I'll just
call it linkdir and then let's move the
| | 03:49 | original file, linkedfile.txt,
let's move that file into linkdir.
| | 03:55 | So now we have ls -la, we can see
we have our hardline, and then we have
| | 04:00 | linkdir and if we look inside linkdir,
you can see that we have our linked file.
| | 04:06 | Now, let's try opening up that hardlink again.
| | 04:08 | So same thing, hardlink, yup!
| | 04:10 | It's still got that same data on it.
| | 04:12 | It was able to still keep track
of it, even though we moved it.
| | 04:15 | That's to be expected.
| | 04:17 | Here's the weird part. If we delete on
one of these, and it doesn't matter which
| | 04:21 | one we delete, if we delete hardlink
or we delete linkedfile.txt, the other one
| | 04:26 | will still contain the data.
| | 04:28 | It will not remove it from
our hard drive. Let's try that.
| | 04:30 | What I am going to do is I am
going to remove the original.
| | 04:34 | I'll do rm inside linkdir and
we'll get rid of linkedfile.txt.
| | 04:41 | So now, the file is removed. Let's
just take a look there and see, yup,
| | 04:44 | it's actually gone.
| | 04:45 | Now let's take a look at our hardlink again.
| | 04:48 | See, it still contains that information,
even though we threw away the original.
| | 04:53 | The hardlink is exactly equivalent to that file.
| | 04:57 | So there are times when
having a hardlink can be useful.
| | 05:00 | But it can be a little bit weird,
because you can think that you are throwing
| | 05:02 | away a file, but actually
that file continues to exist.
| | 05:05 | And in a shared Unix environment, that
can be useful because several users could
| | 05:09 | all have a hardlink to a file and if
some makes a change to the file,
| | 05:13 | the change happens for all users.
They share this file.
| | 05:17 | But if one of them throws it away, well,
it doesn't throw it away for everyone else.
| | 05:20 | They still have access to it.
| | 05:22 | So in that context it does make sense.
| | 05:24 | But because of how we are use to
working with Apple aliases or Window
| | 05:27 | shortcuts, it feels a bit odd.
| | Collapse this transcript |
| Symbolic links| 00:00 | In this movie we are going to
look at what's called symbolic links.
| | 00:03 | These are also called sym links for short.
| | 00:06 | So you'll often hear me just say sym link.
| | 00:08 | We create them the same way as we do
the hard links. We just use ln, short for
| | 00:12 | link, but we use the -s option to
indicate that it's a symbolic link.
| | 00:17 | So ln -s and then the file that we
want to link or a path to the file that we
| | 00:22 | want to link, followed by the name of
the link or a path with the name at the end,
| | 00:27 | just like we did for hard links.
| | 00:29 | We have our target
followed by the name of the link.
| | 00:32 | Where symbolic links are very different
from hard links is that symbolic links
| | 00:36 | reference the path to the file.
| | 00:38 | That's what they keep track of,
is the path to get to that file.
| | 00:41 | Not the file itself.
| | 00:43 | So hard links keep track of the file
on the hard drive and point to that file;
| | 00:47 | instead symbolic links keep track of the
fact, which directory is this file embedded in.
| | 00:54 | As a consequence that means that
they break if the file is moved.
| | 00:58 | The file is no longer at that path, well,
then when we try and do something with
| | 01:02 | this links its going to go to that
path and it's going to say, "Oh, wait I was
| | 01:05 | expecting a file to be there," and
it's not and you'll get an error.
| | 01:09 | And of course, if the file is deleted
then, the file will definitely not be at
| | 01:13 | that path any more and so will also break
which is different from the way hard links works.
| | 01:17 | Let's try working with symbolic links.
| | 01:19 | Notice that I am inside Unix files and in
here we have some files already created.
| | 01:25 | You can see that I have hard link
that I created in the last movie.
| | 01:28 | We also have linkdir, which is just an
empty directory that we'll be using again
| | 01:32 | in this movie, and then I have a couple
of aliases that I created in the Finder.
| | 01:36 | Those aren't going to be included in the
exercise file, just because they don't travel well.
| | 01:40 | They really do reference
things on my file system.
| | 01:42 | So you won't have those in exercise files,
but they are easy for you to create.
| | 01:46 | At the end of the hard link movie, we
deleted our original file to see how that
| | 01:50 | worked and see how it kept the data around.
| | 01:52 | So what I want to do now is
I want to recreate that file.
| | 01:55 | So the first thing we want to do is
nano, and let's create linkedfile.txt and
| | 02:01 | this time just give it any kind of
different text from what we had before.
| | 02:04 | This is Another link test.
| | 02:07 | So different than the text we
had before and we'll safe that.
| | 02:11 | And the reason that I want to show you
that it's different is just so that you
| | 02:13 | see that this hard link still has a size
of 10, while our new one has the size of
| | 02:17 | 18 and if we take a look at that
hard link it's still pointing at the old file.
| | 02:22 | So I don't want you to think this is in any way
related to this new file that we just created.
| | 02:26 | So now let's see how we
go back creating sym links.
| | 02:28 | The way we do that is with ln and then
the -s option, the path to the target
| | 02:35 | file that we want to make a link to,
| | 02:36 | so in our case that's going to be
linkedfile.txt, and then the name of the link
| | 02:42 | that we want to create.
| | 02:42 | I am going to call it symlink.
| | 02:44 | Let's take a look at that and you can see
the file and notice that it's very different.
| | 02:49 | Notice right of the bat it says
symlink and then we have this arrow and then
| | 02:54 | the path to the file.
| | 02:55 | We can't really see the path there
because it's in the same directory.
| | 02:58 | But if it were listed somewhere else
on the file system we would have the full
| | 03:03 | path that it would take
to get to that from here.
| | 03:06 | Notice also that at the beginning
it's not a regular file. It's an l file.
| | 03:11 | We have seen the d and we have seen
the dash. This one has an l to let us
| | 03:14 | know that it's a link.
| | 03:16 | The hard link doesn't do that. The
hard link is just like a regular file.
| | 03:20 | If we look at it in the Finder, you'll
see that in the Finder it does give it
| | 03:24 | this little icon to make it look like
its alias just like Finder, but it's not.
| | 03:29 | Looh at the difference here. Look at
the size of this alias we created earlier
| | 03:33 | and look at this size.
| | 03:34 | Here is the difference.
| | 03:36 | You see this path right here.
Count up those characters.
| | 03:39 | There are fourteen characters in that path.
| | 03:42 | That's the fourteen characters that
are being stored inside this file.
| | 03:45 | So, this special type of link
file and what's inside of it?
| | 03:49 | Just the path to find this other thing.
| | 03:51 | So when we try and do something to the
symlink, like cat symlink, what Unix does
| | 03:57 | is it goes to the symlink and it says,
"Oh, wait, this isn't a normal file.
| | 04:00 | this is a link file.
| | 04:02 | I need to read the path that's in here,
go to that path, and try and find the
| | 04:06 | file that's indicated and
do this action to that file."
| | 04:10 | The way that an alias normally works,
it's more like the way the Finder does it.
| | 04:13 | When we do something in the Finder, it's
not pointing to the same file on the file system.
| | 04:18 | It's pointing to the file itself.
| | 04:20 | It's an alias to that file and if
the file goes way the link breaks.
| | 04:25 | The difference is that our symlink, we
can't move the file around or it breaks,
| | 04:30 | because it just keeps track of this path.
| | 04:32 | It's the simplest
possible implementation of this.
| | 04:35 | Whereas the Finder version keeps a
lot more data around, so that it can
| | 04:40 | keep track of this file.
| | 04:42 | That's why its so much bigger is
because it has this added ability that it
| | 04:45 | doesn't break as you move things around.
| | 04:47 | You drop something into a folder?
Then no problem, your alias keeps working.
| | 04:51 | But if you move something into a
folder here with your symlink, let's move
| | 04:55 | linkedfile into linker,
| | 04:59 | now cat our sym link and it says, "Oops
no such follow directory". Because it's
| | 05:04 | still pointing at that same place, but when
it gets there the file is not there anymore.
| | 05:09 | But in the same way, if we move it back,
move linkdir linkedfile.txt, move it
| | 05:15 | into this directory,
| | 05:18 | now we can cat our symlink again and
it works again, because it went to that
| | 05:23 | path and the file was there again.
| | 05:24 | It suddenly magically worked.
| | 05:26 | So, just references the path.
| | 05:27 | Now of course if we threw this file
away, then of course it would break because
| | 05:31 | the file wouldn't be at that location
anymore, just the same as when we moved it.
| | 05:34 | So there's really three different
approaches to this and you can see why Apple
| | 05:37 | came up with their alias approach.
| | 05:39 | It sort of tries to marry the best
aspect of hard link and symlinks together
| | 05:44 | into something new.
| | 05:45 | But there's three different ways of
doing it and you need to understand them all to
| | 05:48 | be able to choose which one you want.
| | 05:50 | Now the short version, 99% of the time,
what you are going to want is symlinks.
| | 05:56 | That's usually what you mean.
| | 05:57 | When you are working in Unix what you
are usually trying to do is give yourself
| | 06:00 | a shortcut to say from this folder, boy,
I really wish I had a shortcut to just
| | 06:05 | shoot over to my web access logs.
| | 06:07 | My access logs aren't going to move. Chances
are they are going to stay in the same place.
| | 06:11 | So I'll make a symlink from here and
then I can just see the end of that
| | 06:15 | directory and boom, I'll be inside my web log.
| | 06:18 | The other nice thing about symlinks
is that they do work in the Finder.
| | 06:21 | So here in the Finder, if I double-
click on this, it opens it up, exactly the
| | 06:25 | same as these other ones do.
| | 06:27 | To the Finder they seem the same, the
only difference being that symlinks are
| | 06:32 | brittle and they do break if files move around.
| | Collapse this transcript |
| Searching for files and directories| 00:00 | Now we've covered most of the
fundamentals of working with files and
| | 00:03 | directories, we want us to see how
we can go about searching files and
| | 00:06 | directories to find certain files.
| | 00:08 | The Finder offers a really
powerful search tool already.
| | 00:11 | It's called Spotlight.
| | 00:12 | You can access it from the upper
right-hand corner of the Finder or most Finder
| | 00:16 | windows also have a field that where
you can type in what you are looking for
| | 00:20 | and it will help you find all the files
that match that criteria and it's very
| | 00:23 | powerful and it's pretty fast.
| | 00:24 | However, we also need to
learn to do it the Unix way.
| | 00:26 | First of all, you may be on
another Unix system at some point.
| | 00:29 | Maybe you're even logged in remotely to a Unix
web server and want to search for things there.
| | 00:33 | Well, Spotlight won't do you any good
on that remote server and also sometimes
| | 00:38 | it will be useful to be able to just do
it from Unix without switching out and
| | 00:41 | in some cases, it will actually be faster.
| | 00:42 | You will be able to find exactly the
results you want by doing it this way.
| | 00:46 | So the way we'll execute a Unix
search is using the Find command.
| | 00:50 | Find, space, and the first argument is
the path that we want to search in so we
| | 00:54 | can limit the search results to certain
folders, and then the expression and the
| | 00:58 | expression could be a lot of different
things, a lot of complexity can go there,
| | 01:02 | but that's going to tell it
what it should try and find.
| | 01:04 | So to just give you a very simple
example, we could have find inside the user
| | 01:09 | Documents folder anything
that had name "someimage.jpg".
| | 01:14 | Those quotes are optional.
| | 01:15 | You don't have to have
them there. Notice that name,
| | 01:17 | even though it has the dash in front
of it, is not an option in the typical
| | 01:21 | sense of being a command option.
| | 01:22 | It is part of the expression.
| | 01:24 | name and someimage are the
expression that we are using here.
| | 01:27 | That's why the option comes after the
first argument, not in the traditional
| | 01:31 | place where options come
between the command and the arguments.
| | 01:34 | It is an argument itself.
| | 01:36 | It is not an option to find.
| | 01:38 | Find can have some options to it.
| | 01:39 | You can use the man pages to look those up.
| | 01:42 | Now that will return anything
that's in Documents that has exactly the
| | 01:46 | name someimage.jpg.
| | 01:48 | Well, that's great if we know exactly
the name of the file we are looking for,
| | 01:51 | but most times we don't.
| | 01:52 | Most times we are looking for
something that's a little fuzzier than that.
| | 01:55 | Maybe we are looking for
everything that has the word image in it or
| | 01:57 | everything that ends in .jpg.
| | 01:59 | When we were to execute those
kinds of finds, we need to employ
| | 02:02 | wildcard characters.
| | 02:03 | So we'll use wildcard characters to
hold the place of where we want to say
| | 02:08 | well, anything could potentially go here.
| | 02:11 | So if we use an asterisk that means
that zero or more characters can be
| | 02:15 | represented by that.
| | 02:16 | It's often called the glob.
| | 02:18 | If we use the question mark,
it represents any one character.
| | 02:22 | It can hold that place and with
square brackets any of the characters in the
| | 02:25 | brackets could have that position.
| | 02:27 | If we try these out, I think you'll get
the hang of the wildcards pretty quick.
| | 02:30 | For this movie, I am
actually not inside my Unix files.
| | 02:33 | I am just inside the root of my User folder
so you can use cd ~ to get in the same place.
| | 02:39 | So from here, most of you should have a
Sites folder and inside there should be
| | 02:43 | something called index.html. ls ~/Sites/.
| | 02:48 | You can see I have something called index.html.
| | 02:50 | That's a default web page that
Apple gives you on your Mac.
| | 02:53 | If you don't have it, then
you can pick any other file.
| | 02:55 | I just wanted to pick something that I
thought everyone would probably have.
| | 02:58 | So if we find inside our Sites folder
something with the name index.html, it
| | 03:05 | comes back and it says "Ah,
here is what I found for you."
| | 03:08 | If we pick something else, let's say, index2
.html, it says "Nope, didn't find anything."
| | 03:13 | Okay, so you see the difference in
behavior whether it finds something or not.
| | 03:16 | It lists everything that it finds.
| | 03:18 | For the wildcards, what we do is we
would just say all right, you know what, I
| | 03:22 | am looking for not just anything that
is index, but anything that has index dot
| | 03:28 | and four characters after it.
| | 03:30 | I don't care what four they are, has to
be four, but those four can be anything.
| | 03:34 | That's what I mean by a wildcard.
| | 03:35 | It's sort of like a joker
being wild in the card game.
| | 03:39 | So it still returns the same result.
| | 03:41 | Let's take away one of those.
| | 03:42 | You'll see that comes back and it says no.
| | 03:44 | If we don't know how many characters,
you might have two, might have three,
| | 03:47 | might have four, well then we would
use the glob, that asterisk, and that says
| | 03:51 | yeah, I recognize that.
| | 03:52 | So let's say for example we wanted
everything in that folder that ends in .html.
| | 03:58 | You might have a circumstance
where you have several index files.
| | 04:02 | We might have index and we might have
index1, index2, index3 and we want to get
| | 04:07 | all of them, well, then we would say
index(123) and now it would return anything
| | 04:12 | to us that had index 1, 2 or 3.
| | 04:15 | Now obviously we don't have those
files, so it didn't return anything but I
| | 04:18 | wanted to show you how
those square brackets work.
| | 04:19 | It basically says this character
position can contain any of the things that's
| | 04:24 | inside the set, 1, 2, or 3.
| | 04:26 | So that's how the wildcards work.
| | 04:28 | Let's try a couple more examples.
| | 04:29 | Let's do find anything inside our user
directory. I'll just clear the screen so
| | 04:34 | you can see this little better.
And where the name ends in .plist.
| | 04:39 | That's the file extension that
Apple gives to a lot of its preferences.
| | 04:44 | So that will be anything in my User
preferences folder that is an application
| | 04:48 | preference or something like that and
a whole lot of these scroll bar and now
| | 04:51 | you see I have got some for Safari, I
have got some for QuickTime, and so on.
| | 04:54 | So that's sort of a classic use
case for how you would do this.
| | 04:57 | Now so far, examples have just included
showing name, but you can also read the
| | 05:02 | man pages and see that you can put in
path, the last modified time, the size of
| | 05:07 | the file, things that match
certain regular expressions.
| | 05:10 | You can do all of that and you can read
the man pages to find out exactly how to
| | 05:13 | do what you are looking for.
| | 05:15 | Find can be pretty complex.
| | 05:16 | Let me show you one other thing though
that I think is pretty cool, which is that
| | 05:19 | we can also provide some
modifier options to the expression.
| | 05:23 | So for example, I had find everything
that was a plist, but let's say I don't
| | 05:27 | want to have the ones that were QuickTime.
| | 05:28 | There were whole lot of those that were
QuickTime. Find everything and not path
| | 05:35 | and then with quotes or
without we can do QuickTime.
| | 05:38 | Notice I have the asterisk at
the beginning and at the end.
| | 05:42 | There may be stuff before it, there
maybe stuff after it. I don't care.
| | 05:46 | I am looking for anything whose
path contains this string, QuickTime.
| | 05:50 | I don't want to see those.
| | 05:52 | So you see how you use and, not, and then
path. Path is the same as name, but the
| | 05:57 | operators and, not, and or are all things
that you could use. So let's try that.
| | 06:02 | Now you'll see I get
another list slightly different.
| | 06:04 | Now we can say okay, but what about
everything that's not in our Preferences file?
| | 06:08 | Let's say -and -not -path *
Preferences. I get a narrower list, dropped
| | 06:17 | everything up that had preferences.
| | 06:18 | So you can see how it
starts to become very powerful.
| | 06:20 | You can start to hone in on
exactly what you're looking for.
| | 06:23 | As I said, find is a really powerful tool.
| | 06:25 | It goes much deeper than this.
| | 06:27 | I just want to make sure that I gave you
a good introduction to it so that it at
| | 06:29 | least gets you started with the basics.
| | Collapse this transcript |
|
|
4. Ownership and PermissionsWho am I?| 00:00 | In this chapter, we're going to talk
about file ownership and privileges.
| | 00:03 | Let's begin by making sure we can answer
the question "who am I?" In chapter 1, we
| | 00:07 | talked about how Unix is fundamentally
designed to be a multiuser environment,
| | 00:11 | and we've talked about how we log in
each time we open a Terminal window.
| | 00:15 | Now in Unix, a user is someone who has
an account on the system and each user
| | 00:20 | on the system wants to keep their private files
private and only share certain files with others.
| | 00:25 | Otherwise, if everyone has access to
everything all the time ,then what's the
| | 00:28 | point of even having logins and user accounts?
| | 00:31 | So fundamental aspect of this
multiuser environment is that a user has total
| | 00:36 | access to their own files, limited
access to other people's files, and so that
| | 00:41 | we can have collaboration, we have the
ability to grant permission to certain
| | 00:45 | files to certain people.
| | 00:46 | Before we can do that though, we need
to understand who we are and in Unix,= we
| | 00:50 | can just simply type whoamI
and it will tell you who you are.
| | 00:54 | Now that may seem like a really sort of
obvious answer because you're on your Mac.
| | 00:58 | It's just you, you know you're logged
in as you, but when you're working in
| | 01:01 | Unix, it isn't always completely clear.
| | 01:04 | You can actually switch users.
| | 01:06 | We'll see how to do that little later.
| | 01:07 | You may be logged into a remote Unix
server like a web server. There are Web servers
| | 01:11 | where I can log in as a specific user
or I could login as root, and then it's
| | 01:17 | important to know who am I right now.
| | 01:19 | If you can't do something that you
think you ought to be able to do. If you
| | 01:22 | can't open and read a file that you
think you ought to have permission to read,
| | 01:26 | well you can just type whoamI as a
sort of sanity check to be like oh right,
| | 01:31 | when I started this whole process,
I logged in as a different user.
| | 01:34 | I need to exit out and log back in as
that other user or switch users so that
| | 01:39 | then I have permission to
do the thing that I want do.
| | 01:42 | So it is an important concept in Unix.
| | 01:44 | To keep most of the user's private
files, each user gets a home directory.
| | 01:48 | We saw that earlier.
| | 01:49 | We used cd and then the twiddle or
tilde as a shortcut and that's the shortcut
| | 01:55 | to get to my home directory
for kevin's home directory.
| | 01:59 | Whoever I'm logged in as
that's where it's going to send me.
| | 02:02 | Incidentally, that value is stored in home.
| | 02:05 | So $home, that's where my home directory is.
| | 02:08 | Each user has a different
value when they logged in for home.
| | 02:11 | For OS X those are located inside
capital users and then the username.
| | 02:17 | On a lot of other Unix systems, it will
be home/ and then whatever the username is.
| | 02:22 | So it will be something like that.
| | 02:23 | So just to show you it's not there
on the Mac, but that's where it'd live
| | 02:27 | on the Unix systems.
| | 02:28 | So now that we're clear on who we are
right now, where we should be putting most
| | 02:31 | of our private files, we are set up
well to talk about the idea of file
| | 02:35 | ownership and permission and
how we go about changing those.
| | 02:38 | One thing that we touched on earlier
that I want to make sure I mention again is
| | 02:40 | that you can create other users.
| | 02:42 | So you can go to your Apple menu > System
Preferences and then from Accounts, you
| | 02:48 | can click lock here to make changes and
then click the plus sign and then you will be able
| | 02:52 | to create new users.
| | 02:53 | If you then want to switch and log
out as a user, it'll quit all your
| | 02:57 | programs and everything.
| | 02:58 | So anything you have been working on
will be gone, but what you'll do is from
| | 03:01 | the Apple menu, you pick Log Out as that user.
| | 03:05 | You don't have to shut down, you don't
have to restart, you can just log out.
| | 03:08 | Your Mac is still running. Unix is
still running. You've just logged out as one
| | 03:12 | user and it gives you the
opportunity to log back in as a different user.
| | 03:16 | So you can try it out.
| | 03:17 | You can create another user and you
can try out a lot of the things that
| | 03:19 | we're going to do here and you'll
be able to see how the ownership and
| | 03:22 | permissions affect things.
| | 03:23 | Now you may not want to do that a lot
because you are going to have to quit out
| | 03:26 | of everything and then relaunch it all
again, but it might be worth doing at
| | 03:30 | some point during the exercises just
to watch several of the movies in this
| | 03:33 | chapter and then log out as one user,
log back in as a different user, and see
| | 03:38 | what you have the ability to do.
| | 03:40 | Now some of you out there may be
thinking "Well, I am the only user on my Mac and
| | 03:43 | I'm always going to be the only user on
my Mac and I am not going to work on any
| | 03:46 | remote Unix servers or anything.
| | 03:48 | So I can skip all this. I don't need to know it."
| | 03:50 | You can't. You really do need to
still understand the idea of ownership and
| | 03:54 | permissions because it's such a
fundamental part of the way Unix works that
| | 03:58 | you can't work in Unix without
understanding how it works and how to
| | 04:01 | manipulate them.
| | Collapse this transcript |
| Unix groups| 00:00 | In addition to having users on a system,
there is also this idea of groups in Unix.
| | 00:05 | A group is a set of users and each user
belongs to at least one group, a primary group,
| | 00:11 | and it can belong to any
number of other groups as well.
| | 00:14 | Groups are useful for
associating a group of users with a file.
| | 00:17 | So, file's permissions can be set to
allow group members to access files,
| | 00:21 | directories or commands, while making
sure that people who are not members of
| | 00:24 | that group can not.
| | 00:25 | So for example in a large
corporation you might have a group called Human
| | 00:28 | Resources, Legal, or Tech-Support and
those users would be granted access to
| | 00:34 | certain files and then by adding a
user to one of those groups, they would
| | 00:37 | automatically gain access to all of
those files and you wouldn't have to manage
| | 00:41 | access user by user or file by file.
| | 00:44 | However, setting up managing
groups really applies to shared servers and
| | 00:48 | falls under Unix system administration
more than Unix usage, and on the Mac you
| | 00:53 | really don't need groups that often.
| | 00:55 | You don't really use it in a
multi-user environment that much.
| | 00:58 | So groups really don't apply that often either.
| | 01:02 | But even though you
probably won't need to use it,
| | 01:04 | I think it's still a good idea for you at
least know about the concept because one
| | 01:07 | day you may very well find yourself
working on a well-managed server that does
| | 01:10 | use groups effectively and you will
want to know who can access which files.
| | 01:14 | So for now, I just want to let you know
that you can type groups and you can see
| | 01:18 | the groups that you belong to.
| | 01:19 | So these are the groups that Apple has
us belong to and Apple then makes these
| | 01:23 | users able to do certain things
that have certain permissions.
| | 01:26 | As I said, you'll almost never need
to worry about which group you're in.
| | 01:30 | Your Mac manages that for you.
| | 01:31 | It puts you in the right groups.
| | 01:33 | If you ought to be an admin, who has
the admin privileges, well, then you're
| | 01:36 | in the Admin group.
| | 01:37 | All the other groups that you are
part of may depend on what different
| | 01:40 | software you have installed or what
different processes you have running, don't
| | 01:43 | worry about that, okay.
| | 01:44 | I just want you to see that you are a
part of a group because groups is going
| | 01:47 | to come up when we talk about file and
directory ownership, which we're going to
| | 01:51 | do in the next movie.
| | Collapse this transcript |
| File and directory ownership| 00:00 | Now that we've talked about users and
we've talked about groups, we're ready to
| | 00:03 | talk about file and directory ownership.
| | 00:05 | Ownership is an essential part of
working in a multiuser environment.
| | 00:08 | It is how Unix can tell which files
belong to you, which ones you can access
| | 00:12 | and which ones you can't. You can see
the ownership of files and directories
| | 00:16 | whenever we do ls -la.
| | 00:19 | It's the second and third
columns that you see there.
| | 00:21 | So the owner of all of these files and
directories except for one is going to be kevin.
| | 00:26 | The one that you see as
root is the parent directory of that.
| | 00:30 | That's the /Users directory.
| | 00:33 | That's going to be the parent
directory and it's owned by root.
| | 00:36 | But my User folder is owned by me
and all the things that are inside that
| | 00:40 | folder are owned by me.
| | 00:41 | The group that owns it, or you can say
the group that's assigned to it, is staff.
| | 00:47 | Now we don't need to worry too much
about groups on Mac, but I just want you to
| | 00:50 | see it so that you have
the conceptual understanding.
| | 00:52 | For contrast, let's take a look at
the other user that I've set up on my
| | 00:55 | computer, ls -la /Users/lynda/.
| | 00:59 | So we can take a look at her directory
and her files and we can see that the
| | 01:03 | owner of all of these files is lynda.
| | 01:06 | The group is the same, the group is
staff. But hers is lynda where mine is
| | 01:10 | kevin, so we have different ownerships.
| | 01:12 | You can now see that.
| | 01:13 | You can also see that the owners in
the group are maintained separately.
| | 01:16 | So when we talk about permissions, we'll
be able to set permissions based on the
| | 01:20 | owner and on the group.
| | 01:22 | We can do it separately. And so
therefore files that I'm the owner of, I can
| | 01:25 | have one set of permissions, but files that are
in my group, I can potentially share with Lynda.
| | 01:30 | I could have files that are readable by
anyone in my group and then Lynda and I
| | 01:34 | could both read those files.
| | 01:36 | Even though we don't own the file, we
could share it and have those group settings.
| | 01:40 | So what I'd like us to do is I am going
to change into my Unix files directory
| | 01:45 | that's in my User home directory.
| | 01:46 | Let's clear my screen
so we can see those files.
| | 01:49 | I am going to create a new file in here.
| | 01:51 | I will just do it using touch
and I'll call it ownership.txt.
| | 01:55 | You can really create this anywhere.
It's just to have a file that we can work with.
| | 01:58 | So let's do ls -la again.
| | 02:00 | I'll clear the screen.
| | 02:01 | So now we can see that ownership is
there and any new file that you create is
| | 02:06 | going to be owned by you. That makes sense, right?
| | 02:09 | It's also going to get your default group.
| | 02:10 | Everyone has a default group and that's
what's going to get assigned to it by default.
| | 02:14 | How do we go about changing that?
| | 02:15 | Well, we do it with the command called
chown or chown, depending how you want to
| | 02:20 | say it, but it stands for change ownership.
| | 02:22 | That's where it comes from, change
ownership, and so we use chown, space, and
| | 02:27 | then we put in the user:, the group.
| | 02:31 | So we could say, for example, kevin:staff
| | 02:33 | and then the name of the file
or directory and that would change the
| | 02:38 | privileges and in this case,
I'm changing it from kevin:staff to the kevin:staff.
| | 02:43 | It's redundant, but you can see that it does it.
| | 02:46 | We also can specify those by just putting
in chown kevin ownership.txt, like that.
| | 02:51 | That will change just the owner. Or we
can change just the group by doing it with
| | 02:57 | the colon and the group.
| | 02:58 | The colon stays with the group and
that lets it know all right, I am
| | 03:01 | not changing the owner.
| | 03:02 | I am only changing the group for this.
| | 03:04 | So we also can do the same thing
for directories just so you see.
| | 03:07 | Let's do-- we have test1 as a
directory there. Go ahead and just we're not
| | 03:13 | actually changing them to anything different.
| | 03:15 | We are just changing the same one and
that will change the owner of the directory.
| | 03:19 | Directories have owners too. But it
doesn't change all the contents that are
| | 03:22 | inside the directory.
| | 03:23 | They all keep whatever
ownership they already had.
| | 03:25 | If we want to change everything inside
of it then we use the R option, -R.
| | 03:32 | Just like we had for copy and remove, we
have this R which stands for recursive to
| | 03:37 | tell it okay, not just this top
directory, but everything in it, recursively
| | 03:41 | move through all of the contents of this
directory and change all of those ownerships.
| | 03:45 | Okay, so that's how you do is with -R.
| | 03:47 | Just for an example, let's try
chown lynda:staff ownership.txt.
| | 03:55 | Comes up and it says Operation not permitted.
| | 03:57 | Well, you may be thinking "Well, geez
what uses this chown if I can only change
| | 04:01 | it to myself, to the person
that already belongs to it?"
| | 04:04 | Well, that's true and there's actually a way
around it which I'll show you in just a moment.
| | 04:07 | The reason why you can't do it is
because we don't want to be able to just give
| | 04:11 | files to other users for security reasons.
| | 04:14 | We wouldn't want to, for example, be
able to launch a process and have some kind
| | 04:18 | of program running and then be able to
stop and change its ownership permissions
| | 04:22 | to belong to someone else.
| | 04:23 | That's a security concern.
| | 04:25 | So we are not able to do that.
| | 04:27 | The way that you can do it though
is if you are an administrator on the
| | 04:30 | machine. On the Mac that's something
that could be managed in those System
| | 04:33 | Preferences for Accounts.
| | 04:35 | If we do that, we can use sudo.
| | 04:37 | We'll be talking about sudo later in
this chapter, but I just want to show you
| | 04:40 | for now that you can do it. That if we
say sudo chown lynda:staff ownership.txt,
| | 04:47 | it overrides that and it says okay, sure.
| | 04:49 | It wants to know your password to make
sure that you are allowed to do this and
| | 04:53 | I put in my user password that I
use to log in as kevin, so it's just
| | 04:56 | re-authenticating me to be able to do this.
| | 04:59 | Now ls -la and now you can see that
it's owned by lynda. Now let me do it.
| | 05:04 | So sudo in front of it lets us do
chown and so chown is really not that useful
| | 05:09 | without that sudo, but I wanted just to
go ahead and see it now even though we
| | 05:12 | are not going to talk about
sudo for a couple of movies.
| | 05:14 | Let's just change it back. Let's make it my
file again, kevin, there we are and ls -la.
| | 05:23 | Now you can see that it changed it back.
| | 05:26 | You may have noticed that it didn't
ask me for a password the second time.
| | 05:29 | It remembered that I had recently entered it.
| | 05:30 | But we'll talk more about how that
works when we cover sudo in depth.
| | 05:33 | Now before we end and move on to talk
about permissions, I just want to give you
| | 05:36 | a anecdote of a real world example where
I ran into this that it was not sort of
| | 05:40 | web development related, which is the
place where I normally end up dealing with
| | 05:44 | ownership and permissions.
| | 05:45 | I bought music from iTunes and I
wanted to share it with my wife who is
| | 05:49 | authorized to use my music.
| | 05:50 | So I took it out of iTunes by dragging
it on to my desktop and then over our
| | 05:54 | local home network I put them on her Mac.
| | 05:56 | When it got to her Mac though, she
couldn't open them up and she tends to me
| | 05:59 | saying "Hey, this isn't
working. I don't know why not."
| | 06:01 | Well, it turns out that the
owner of the file had not changed.
| | 06:04 | It still was being owned by me.
| | 06:06 | So even though the files were now on
her computer, she didn't have ownership of
| | 06:11 | those files and wasn't able to open them.
| | 06:13 | So I just went on her computer,
opened up Terminal, changed the ownership
| | 06:17 | recursively for the folder, and boom!
| | 06:19 | Suddenly she had the ability.
| | 06:20 | She now owned the files and she
was able then to play the music.
| | 06:24 | So that's the case we're really knowing
what's going on under the hood in Mac OS X
| | 06:27 | and being able to work with Unix
can really help you out in real-world
| | 06:31 | non-command line scenarios.
| | 06:34 | Now that we know about the file
ownership, we are ready to talk about file and
| | 06:37 | directory permissions, which is
what we'll do in the next movie.
| | Collapse this transcript |
| File and directory permissions| 00:00 | Now that we understand file and
directory ownership, we are ready to look at
| | 00:03 | file and directory permissions.
| | 00:05 | The way that we see permissions is
using the ls -la, just like we did for owner,
| | 00:09 | to show us the full listing.
| | 00:11 | Notice that I am already inside my user
directory inside the unix_files folder.
| | 00:15 | In this listing, we just got through
talking about the owner column,
| | 00:18 | the column where it says kevin over and
over and then the one where it says staff
| | 00:22 | that's the group column.
| | 00:23 | What I want to talk about now is that
very first block there, all the rws and
| | 00:27 | dashes that are there.
| | 00:29 | The very first character that you
see we already said was an indicator of
| | 00:33 | whether or not it's a
directory, a file, or a link.
| | 00:35 | d for directory, dash for
file, l for symbolic link.
| | 00:38 | The next nine characters after that is a
notation that indicates the permissions
| | 00:44 | for each of these files and directories.
| | 00:46 | So before we can go about changing the
permissions, we need to understand what
| | 00:49 | these symbols are trying to tell
us about the current permissions.
| | 00:52 | So we need to know how to decode them.
| | 00:54 | We refer to this system as being
alpha notation because we are using the
| | 00:58 | alphabet to describe the different permissions.
| | 01:00 | Imagine that we have three categories.
| | 01:02 | We have our user. which is our owner category.
| | 01:04 | We are going to call it user.
| | 01:06 | That's the first category.
| | 01:07 | The second one is group, everyone
who belongs to the group, and then the
| | 01:10 | third category is other.
| | 01:11 | That's everyone else who
might have access to this file.
| | 01:14 | So user, group, and other are our three categories.
| | 01:17 | And for each of them we can
set three kinds of permissions.
| | 01:20 | We can set read permissions, whether
or not you can read the contents of a
| | 01:23 | file or a directory.
| | 01:24 | write, whether or not we can actually make
changes to a file or make changes to a directory.
| | 01:30 | And then execute, which for a file
would mean that we could run it like a
| | 01:33 | program or a script.
| | 01:35 | For a directory, it means
that we can search inside of it.
| | 01:38 | That's what it means to be
able to execute on a directory.
| | 01:40 | Notice in this table that I've got
yes's and no's, indicating which permissions
| | 01:44 | I'd like to give each of these three categories.
| | 01:47 | So I want the owner of the file to be
able to read, write, and execute a file.
| | 01:51 | The group, I'd like to just
be able to read and write it.
| | 01:54 | They can't execute it.
| | 01:56 | For everyone else, they can only read it.
| | 01:58 | I don't want them to make changes.
| | 01:59 | So the only people should be able to
make changes are the user and the group.
| | 02:03 | So what we do for each of those, read,
write, and execute, is we use the letter r,
| | 02:07 | w, or x to indicate it and then we
essentially add them up, so that what we end
| | 02:12 | up with for the user is r, w, and x
and they can do all three things.
| | 02:16 | The group is r and w, but not an x and
we put a placeholder dash in place of it.
| | 02:23 | For everyone else, they can
only read. So they get r and two dashes.
| | 02:27 | Notice now we have nine characters.
| | 02:28 | You take all of those, you smash them
together, and that is the nine-character
| | 02:32 | permission string that we are
seeing in our directory listing.
| | 02:35 | So for example, if we take this file
lorem_ipsum.txt, you can see that its
| | 02:40 | permissions allow me, the owner, to
read and write to the file but not
| | 02:44 | execute it and that's fine.
| | 02:46 | Frequently we don't have execute turned on for
files because it's not a script. It's a text file.
| | 02:50 | We don't need to run this.
| | 02:51 | We are just going to be
reading and writing to it.
| | 02:54 | For the group, which would be
everyone in the group staff, which you'll
| | 02:57 | remember included the other user I
created, lynda, has the ability to read the
| | 03:01 | file, but not to write to it or
execute it, and then everyone else,
| | 03:04 | everyone else who might ever come in
contact with this file, is able to read it,
| | 03:08 | but not to write and execute to it.
| | 03:10 | Now you can see that, for example, that
there is different permissions down here
| | 03:13 | for the test directory.
| | 03:15 | By default, directories are given
the x so that they are searchable.
| | 03:18 | It's essentially the same thing as
being able to read, so we're able to search
| | 03:21 | inside of it if we can read what's in it.
| | 03:23 | So, by default, when you create a
directory, it will include that as well.
| | 03:26 | But you can see that the group staff and
everyone else can't write to this directory.
| | 03:29 | They can just read it and search it.
| | 03:31 | So let's see how this
prevents people from having access.
| | 03:34 | Let's take a look as a contrast.
| | 03:36 | I have the other user on here, which is lynda,
and now we can see the contents of her directory.
| | 03:42 | Here is the directory. Remember this
dot represents the current directory.
| | 03:45 | So this is the directory we are looking at.
| | 03:47 | We as part of her group have read and
execute permissions on this directory.
| | 03:51 | That's why we are able to see this listing.
| | 03:53 | Let's try now to do the same thing,
but let's add pictures to begin.
| | 03:57 | We are going to try and
look at her vacation photos.
| | 03:59 | Notice what the permissions are down here.
| | 04:01 | She has read, write, and
execute privileges, but that's it.
| | 04:04 | No one else has any privileges.
| | 04:06 | So if we hit Return, it
says "Oops, permission denied."
| | 04:08 | If we do cd into that folder, you will
see it says "Nope sorry, you can't get in
| | 04:15 | there, permission denied."
| | 04:16 | So you see how it works.
| | 04:17 | You see how these keep us from
getting into other people's stuff.
| | 04:20 | So we can see her user directory, but
we're not able to go any deeper into her
| | 04:23 | documents or her movies or her
pictures or anything like that.
| | Collapse this transcript |
| Setting permissions using alpha notation| 00:00 | Now that we understand how permissions
work, how do we go about changing them?
| | 00:04 | Well, you remember with ownership,
we used chown, change ownership.
| | 00:08 | We are going to use chmod to change permissions.
| | 00:11 | It stands for change mode and that's
what Unix refers to the mode of these files
| | 00:15 | is being what it calls to permissions.
| | 00:17 | So chmod and then we are going
to put in whatever the mode is.
| | 00:20 | I am just going to write the word mode
as placeholder, we will talk about what
| | 00:23 | the mode is, followed by the file name and so
we will just put file name. That's the format.
| | 00:28 | chmod, whatever changes to the
mode we want, and then the file name.
| | 00:32 | So let's look at some example of
what that mode might look like.
| | 00:35 | So first, remember that
we have three categories.
| | 00:38 | We have user, group, and other.
| | 00:40 | Notice the first letters of those.
| | 00:41 | u, g, and o. We are going to be using
those first letters to indicate where we
| | 00:46 | want to make changes to,
which category should get changes.
| | 00:49 | Okay, so u, g, and o, r, w, and, x.
| | 00:51 | So the first one I want to look at is
going to be changing user, group, and
| | 00:57 | other and setting them all equal to something.
| | 01:00 | All right, so it can provide whatever
three letters I want there. I am going to
| | 01:03 | say r, w, and x. This is going to
give all of them all three of those.
| | 01:07 | Now most of the time you don't want to
do that. Most of the time you are trying
| | 01:10 | to set different
permissions for different people.
| | 01:12 | So another way to write that out
would be to say u= and give it whatever
| | 01:16 | permission is for u, followed by a
comma then the g= whatever permissions it
| | 01:21 | should have and then o for other.
| | 01:23 | User, group, and other.
| | 01:24 | Commas separating them.
| | 01:26 | That's a long thing to type and I don't
know anyone who really types it out that way.
| | 01:30 | Typically when you are changing the
permissions, you know what the current
| | 01:33 | permissions something are.
| | 01:35 | what you want to do is change it
to something slightly different.
| | 01:38 | You are like, oh, you know what, I wish
that the user and group both had write
| | 01:41 | permissions to this file.
| | 01:42 | Well, to do that, you
can just use the plus sign.
| | 01:46 | Keep the existing permissions exactly
like they are. All I am interested in is
| | 01:49 | for user and group add the w permission.
| | 01:53 | We can also do the same thing subtractively.
| | 01:55 | We can say all right, I want to take
away the write permission from other.
| | 01:59 | So plus and minus, it's
typically the way people work.
| | 02:01 | Instead of spelling it all out for
each one, typically you just say all right
| | 02:04 | here are the changes that I want to make.
| | 02:08 | Now as I said, u, g, and o are how we
define the three categories that we are
| | 02:10 | targeting. There is one other shortcut
I want to show you which is, is if you
| | 02:13 | are trying to do something to all three
of them and you are using ugo, you can
| | 02:16 | also use a, so that's a shortcut.
Those last two do the exact same thing.
| | 02:21 | a means u, g, and o altogether.
| | 02:23 | Let's try it out, so you can get the hang of it.
| | 02:25 | Okay, I am going erase this line,
because that's not going to do anything for us.
| | 02:28 | Let's do ls -la so we can see the
current permissions and I am going to really
| | 02:33 | target everything on this ownership.txt.
| | 02:35 | You can use any file you want.
You can just create a new file.
| | 02:39 | You can see that the current
permissions are read and write for the owner and
| | 02:43 | just read for the group and other categories.
| | 02:45 | So to begin, let's do chmod and let's
do user group and other equals, so we can
| | 02:51 | see what that looks like and let's say r and x.
| | 02:54 | And we will do it to ownership.txt.
| | 02:57 | So that's typically what a chmod
statement looks like. chmod, then what we want
| | 03:01 | to do to it, followed by the file name.
| | 03:02 | So we are going to add
essentially an x to the last two.
| | 03:06 | ls -la and now you will see that
the x has been added to those two.
| | 03:11 | You see that actually took the w away from
the owner as well, because I set it to equals.
| | 03:15 | So now let's try it slightly different.
| | 03:17 | let's do chmod u=rwx and
let's do group = rw and other.
| | 03:25 | It's going to be equal to just r
for ownership.txt. ls -la again.
| | 03:31 | Now you can see how we changed it using that.
| | 03:34 | All right so we broke it up using
commas. We can also add to it, for example.
| | 03:38 | Then we can say chmod user group and
other, plus x for ownership, ls -la.
| | 03:45 | There we go, you see that it added the
x to it and let's just do chmod a-x so
| | 03:52 | you can see what that does. ls -la again.
| | 03:56 | You can see that it took back away the x.
| | 03:58 | So now that you have the hang of how we
specify these, let's see what the actual
| | 04:02 | practical affects are of these.
| | 04:03 | Let's try chmod and let's do user group and
other. Let's take away their read privileges.
| | 04:10 | Okay, so now we don't have
the ability to read anymore.
| | 04:12 | We just clear the screen so
you can see it. So there we are.
| | 04:15 | We don't have read privileges anymore.
| | 04:17 | Let's try nano ownership.txt. And notice
that it pop opened nano but look at the
| | 04:22 | message that it gives us at
the bottom, permission denied.
| | 04:25 | So I am inside nano, but I can't do anything.
| | 04:27 | I can type here, but I am not actually
inside that file and when I say save,
| | 04:32 | it's going to save to a new file.
| | 04:34 | It's not actually reading the contents
that file into the buffer for me to change.
| | 04:38 | I am just essentially opening nano.
| | 04:40 | So let's try now changing the
ownership so that, say chmod, let's say user
| | 04:47 | group and other ought to be
equal to read for ownership.
| | 04:50 | All right, so now we only have read
permissions. We don't have write permissions.
| | 04:55 | Once again, let's try
opening up nano ownership.txt.
| | 04:58 | So it opened it up.
| | 05:00 | It read it fine. There is nothing
inside this file to begin with, but it did
| | 05:04 | successfully open it.
| | 05:05 | Let's say hello and now let's do exit,
save buffer, yes. File name to write.
| | 05:11 | Now I have the opportunity to provide a
different file name but I am essentially
| | 05:14 | now about to write on to
this file, and I hit Return.
| | 05:17 | It says, "Sorry, permission denied."
| | 05:20 | So we can't write to it.
| | 05:22 | We can write to a different file name
if we want, we still have the ability
| | 05:24 | to save our changes, but we can't
overwrite ownership.txt because we don't
| | 05:29 | have write privileges.
| | 05:30 | So I will just say no to saving those
changes and let's go ahead and just set
| | 05:33 | it back to chmod and we will make user gain
its write privileges back on ownership.txt.
| | 05:41 | Let's take one last look at it and there we go.
| | 05:44 | The other thing I just want to
mention to you is that in addition to doing
| | 05:47 | changes like this, if we have a
directory and we want to change this to the
| | 05:50 | directory, let's say that we want to do
chmod, let's add the write attribute for
| | 05:56 | group, and we will write that
for test.1 for the test directory.
| | 06:01 | If we do this right now, it will change it just
for that top level one, same as the ownership did.
| | 06:05 | If we wanted to change it to everything
that's inside of there, we have to also
| | 06:08 | include the recursive flag.
| | 06:10 | So that's the option -R and that
will recursively change everything
| | 06:15 | that's inside of there,
| | 06:16 | directories and files all the way down the line.
| | 06:19 | So I just want to make sure you know we
have that recursive option available to us.
| | 06:22 | That's important if you are trying to
change everything that's inside of that
| | 06:24 | directory to have the same permissions.
| | 06:26 | That's really all you need to being
able to manage permissions in Unix.
| | 06:29 | You now understand what they are,
you understand how to read the notation
| | 06:33 | that it gives you there on the
directory listing, and you now know how to
| | 06:36 | manipulate them, how to enable and
disable permissions for those three
| | 06:39 | categories one way or another.
| | 06:40 | But before we move on, I wanted to
take a look at how we can also specify
| | 06:44 | these permissions using something
called octal notation and we will do that in
| | 06:47 | the next movie.
| | Collapse this transcript |
| Setting permissions using octal notation| 00:00 | In the last movie we learned how to
work with file permissions and to use chmod
| | 00:04 | to change the permissions that we
wanted for user, group, and other, and to do
| | 00:07 | that we used what we called alpha notation.
| | 00:09 | That's because we're using the alphabet,
u, g, and o for user, group, and other
| | 00:13 | and r, w, and x for read, write, and execute.
| | 00:16 | That's everything you need to know to
change file permissions. That's it.
| | 00:19 | You could get by do everything with that.
| | 00:21 | However, I want you to also learn about
octal notation, because it's extremely popular.
| | 00:26 | It's the way that I change file
permissions and I think a lot of other Unix
| | 00:29 | users do and you're bound to run into it.
| | 00:31 | So I want you to at least
have some exposure to it.
| | 00:34 | So remember here we have u, g, and o, r,
w, and x and we use that to come up
| | 00:39 | with those nine digits rwx, rw-, r--, right?
| | 00:44 | Now imagine that for r, w, and x that
we establish a conversion. Each one of
| | 00:48 | those letters is instead going to be
represented by a number, 4, 2, and 1.
| | 00:53 | r=4, w=2, x=1. So in place of the
yes's and no's for each one of those, let's
| | 01:00 | drop in those numbers.
| | 01:02 | So for the user, the user can read, write,
and execute, so they get 4, 2, and 1.
| | 01:07 | The group can read and write, so they
get 4, 2 and 0, and for other, they can
| | 01:12 | only read, so they get 4 and 0 and 0.
| | 01:15 | Take all those numbers and
add them up. You get 7, 6 and 4.
| | 01:20 | And those three digits are the octal
notation representation of rwxrw-r--.
| | 01:27 | So we're not adding permissions or
taking away permissions; we're defining
| | 01:31 | all nine of those just
using these three numbers.
| | 01:34 | We're saying each one of these numbers
will represent the state of read, write,
| | 01:39 | and execute for each one of those.
| | 01:40 | Let me show you some examples.
| | 01:42 | So for example, let's say we want to
say that a file ought to have all the
| | 01:47 | permissions. Well, read, write, and
execute, all added together, 4, 2 and
| | 01:51 | 1 those add up to 7.
| | 01:53 | That's the highest number we can have.
| | 01:54 | So 777 means give all the permissions
wide open, so everybody can do everything.
| | 02:00 | When we're just looking at, we had 764.
| | 02:02 | That means that the user can read, write,
and execute. For the group they have a 6.
| | 02:08 | That's made up of 4 and 2.
| | 02:10 | That's the only way we can come up with
that number is with 4 and 2, therefore
| | 02:14 | they have read and write
privileges, not execute.
| | 02:17 | And then for other, they have
only read privileges. That's the 4.
| | 02:21 | A very common one that you will see is 755.
| | 02:24 | That means that I as the user have read,
write, and execute. Everyone else just
| | 02:29 | has read and execute privileges.
They don't have write privileges.
| | 02:33 | And then of course, if they didn't
have any of them, it will be 000.
| | 02:36 | That would essentially take away
all privileges from everybody.
| | 02:39 | More commonly what you would see is
something like 700, which would mean I get
| | 02:43 | all privileges, but everyone else gets none.
| | 02:45 | 7 represents read, write,
and execute. So that's it.
| | 02:48 | That is all that is there to it.
| | 02:50 | It's just a really compact concise way
to spell out exactly what you want to
| | 02:54 | have happen with those permissions.
| | 02:56 | Like I said, you can still do it the
other way, but this is a really powerful
| | 02:59 | way once you get the hang of it.
| | 03:00 | What I like you to do now is just
pause the movie with this alpha to octal
| | 03:05 | conversion up, r, w, and x, just to
remind you, and try a few of these.
| | 03:10 | Just try setting the permissions on a test file.
Go ahead and just try 777, see what it does.
| | 03:15 | Try 755, see what it does.
| | 03:18 | Remember that the numbers will always
have to be combinations between 0 and 7.
| | 03:22 | They can't be any higher than that.
| | 03:23 | So, you can just play around and see what
it does, see if you can get the hang of it.
| | 03:26 | Maybe even if you start out using alpha
notation, just every now and then just
| | 03:30 | try the octal one and see, and if
you're going to do all of them wide open
| | 03:33 | anyway, just use 777.
| | 03:35 | It's certainly a lot faster, instead of
typing out user, group, and other equals
| | 03:39 | read, write, and execute, right?
| | 03:40 | It's a lot more characters to type.
| | 03:42 | Once you feel like you have to hang of it,
we'll move on to talking about how we
| | 03:45 | can switch to a different user identity.
| | Collapse this transcript |
| The root user| 00:00 | So far in this chapter we've been talking
about user accounts and user privileges.
| | 00:04 | The things that a user is or is not
allowed to access on the Unix file system.
| | 00:08 | Now I like us to talk about a special
kind of Unix account, which is the root user.
| | 00:13 | The root user is a super user account that
can do absolutely anything on the Unix system.
| | 00:18 | It can open any file, it can run any
program, it can change any permissions.
| | 00:22 | It is an all-powerful user that's not
bound by the normal user permissions that
| | 00:27 | we've been discussing so far.
| | 00:28 | So the root user can see Lynda's
photos and they can read Kevin's documents.
| | 00:33 | In fact, they can even
delete Kevin and Lynda's accounts.
| | 00:35 | When Unix is first installed on a
computer, it's this root user that exists
| | 00:39 | before you've even created the user accounts.
| | 00:41 | So as the root user, you would
first create Kevin's account, give him a
| | 00:45 | password, and then from
then on we can log in as Kevin.
| | 00:47 | Now in Mac OS X, you don't see that
process happen, because the Mac OS X
| | 00:51 | installer does it for you.
| | 00:52 | As you're installing Mac OS X, it
comes up and says, hey, what information do
| | 00:56 | you want for your first user account?
| | 00:58 | You fill it out, you provide the
username and a password, and then acting as
| | 01:02 | the root user, it creates that first user for
you and then it actually disables the root user.
| | 01:08 | So the root user is
disabled by default in Mac OS X.
| | 01:11 | Now the root user can be re-enabled, but you
don't need to and I really don't advise it.
| | 01:16 | So why talk about it then?
| | 01:18 | Well, it's an important Unix concept
and it's going to have implications in a
| | 01:21 | lot of the things that we're going to do.
| | 01:23 | And you may also read or hear
references to it, either in the man pages as you
| | 01:27 | research information about different
commands or on blogs as you go out and
| | 01:32 | continue learning about Unix
after you leave this tutorial.
| | 01:35 | You may even hear in movies or TV shows
where hackers talk about getting root, right?
| | 01:39 | Or having root access.
| | 01:41 | That's what they are talking about.
| | 01:41 | They're talking about
becoming this all-powerful root user.
| | 01:44 | And lastly, it's going to be
important when we talk about sudo.
| | 01:47 | sudo is essentially a command that
lets us temporarily take on those
| | 01:51 | abilities of the root user.
| | 01:54 | And we'll see that how to
do that in the next movie.
| | Collapse this transcript |
| sudo and sudoers| 00:00 | In the previous movie I told you that the
root user is disabled on Mac OS X by default.
| | 00:05 | That's okay.
We don't need it to be enabled.
| | 00:07 | Because as admin users on the Mac, we can
do everything that the root user can do.
| | 00:11 | We just have to do it by using the sudo command.
| | 00:15 | That stands for substitute user and do.
| | 00:18 | Some people mistakenly think that it
stands for superuser do, because the root
| | 00:21 | user is a superuser.
| | 00:23 | But what it's actually doing is
substituting in a different user identity and
| | 00:27 | I'll show you how you can pick a
different user besides root in a moment.
| | 00:30 | But sudo is a command that runs other commands.
| | 00:32 | So it's essentially like prefixing a line
with sudo when you wanted to do it as root.
| | 00:38 | So just as an example, sudo ls -la.
The command is exactly like we would
| | 00:43 | normally type. We're just saying, hey,
don't do this as Kevin, do this as root.
| | 00:47 | Let me show you what happens.
| | 00:48 | We hit Return. It comes
up and it wants a password.
| | 00:51 | Now that's not root's password, because
root is disabled, what it wants is in
| | 00:55 | this case it wants my
password, Kevin's password again.
| | 00:59 | What it wants to do is make sure that we
really aren't admin before we do admin like things.
| | 01:04 | It's a security precaution.
| | 01:05 | So I am going to type in my main user
password that I use to log into this
| | 01:08 | system, and now it does what I asked to do.
| | 01:11 | Obviously this is something that I
could have done without being root, but that
| | 01:15 | should just give you a feel for how we
prefix our normal commands with sudo and
| | 01:19 | get the same results.
| | 01:20 | We got to a peek at this earlier
when we were changing ownership
| | 01:22 | permissions, right?
| | 01:23 | We had to do sudo and then
chown lynda and then let's say
| | 01:28 | unix_files/ownership.txt, and then
that changes the ownership permissions.
| | 01:33 | Now notice when I typed it the second
time it did not ask me for a password again.
| | 01:37 | That's because this password
authentication stays valid for a little while.
| | 01:41 | So that we potentially could
be entering several commands.
| | 01:43 | It would be a pain if every single
thing that we did, we had to keep
| | 01:46 | entering that password.
| | 01:47 | So it remembers it and it
stays valid for about five minutes.
| | 01:50 | That can be configured on
different Unix machines.
| | 01:52 | But it's typically five minutes.
| | 01:54 | Incidentally, you can type sudo -k,
and that will expire it right now.
| | 01:59 | So that then when we try and do
something again, let's change this back to
| | 02:02 | Kevin, now it's going to
ask me for my password again.
| | 02:07 | Okay, so let me just clear the screen
and let me just give you a couple of more
| | 02:10 | examples to show how this work.
| | 02:11 | So let's say I have whom, Right?
| | 02:13 | I am Kevin.
| | 02:15 | Let's do ls -la and let's try
and take a look at Lynda's pictures.
| | 02:19 | It's on here and I don't have
permission to access it, so it comes up and
| | 02:22 | says permission denied.
| | 02:24 | If I now do the same command with sudo
in front of it, Users/lynda/Pictures, now
| | 02:30 | it's going to make me into root before
doing it, and remember the root has the
| | 02:33 | ability to do absolutely anything on
here, including look at Lynda's pictures.
| | 02:38 | So using sudo I can now see what's in there.
| | 02:40 | Now as I said at the beginning of the
movie, root is not the only user you can
| | 02:43 | become. Let me just show
you if you do sudo whoami.
| | 02:47 | It comes up and says, "Oh, I am root."
| | 02:49 | If we use sudo and we use -u, and then
the username that we want a substitute,
| | 02:54 | now whoami, we become lynda.
| | 02:57 | So we are substituting the user
identity before we execute the command.
| | 03:01 | So now we could do the same sort of
thing, we say all right, well, as Lynda,
| | 03:04 | I'd like to now open this
file or look at this directory.
| | 03:07 | So we can become a different user and
take on their privileges and their role
| | 03:11 | just as easily as we can take on root.
| | 03:14 | Most of the time you'll just use sudo
without any options, because what you
| | 03:17 | essentially are saying is I don't
have enough privileges to do what I want
| | 03:20 | right now, like changing ownership.
| | 03:22 | So therefore, I'm temporarily become root
to accomplish what I'm trying to accomplish.
| | 03:27 | Not everyone can do sudo. Only admins,
and that's something that you set up in
| | 03:32 | the Account Preferences, System
Preferences under Accounts, and you can see it
| | 03:37 | says Admin underneath, right?
| | 03:39 | If you make someone who's not an admin
account, they won't have the ability to sudo.
| | 03:43 | That is what an admin is.
| | 03:45 | Now let me show you why
that's true in Unix terms.
| | 03:48 | In Unix there's something
called a sudoers file.
| | 03:54 | So that is stored in cat/etc/sudoers,
that's where it lives, and this is a file
| | 04:00 | that keeps track of the configuration
for sudo and especially who ought to be
| | 04:05 | allowed to execute this command.
| | 04:07 | Because we don't want let everyone do
it. Otherwise again, what's the point
| | 04:10 | in having privileges.
| | 04:11 | So only admins on the system
get these special privileges.
| | 04:14 | We can't view that file, because we
wouldn't want everyone to be able to view it
| | 04:19 | unless we put sudo in front of it.
| | 04:22 | As you can see the
security is very well thought-out.
| | 04:24 | So now let's take a look at it as sudo.
There is all sorts of default settings
| | 04:29 | and everything, but the most important
thing are these lines right here.
| | 04:33 | You don't need to ever change these yourself.
| | 04:35 | The system preferences are
going to handle this for you.
| | 04:38 | What these lines are telling you is
that the root user ought to have the
| | 04:42 | ability to do everything.
| | 04:44 | That's what those three ALLs mean.
| | 04:46 | The group admin also ought to be able to do it.
| | 04:50 | So what it does is it adds you to the
group admin and once you're part of the
| | 04:53 | group admin, well, then you have
the same privileges that root does.
| | 04:57 | You have the ability to become root.
| | 04:59 | Now again, you don't ever need to
actually edit this sudoers file, but I just
| | 05:03 | want you to see what's happening,
because on other Unix systems this is the way
| | 05:06 | that you manage who has sudo
access, is via the suedoers file.
| | 05:10 | The Mac manages it for you.
| | 05:11 | The Mac gives you this nice convenient
interface and it just drops you into the
| | 05:15 | admin group and once you are part
of the admin group, well, then you're
| | 05:17 | automatically added to the sudoers file.
| | Collapse this transcript |
|
|
5. Commands and ProgramsCommand basics| 00:00 | In this chapter we are going to be
taking a look at some Unix commands and
| | 00:03 | programs and seeing lots of
useful things that we can do.
| | 00:05 | I want to start out by just talking
about some of the basics of commands and
| | 00:09 | this is really a follow-up to the
discussion we had back in Chapter 1, where we
| | 00:12 | saw the basic structure of a command.
| | 00:14 | Remember it was command, then
the options, then the arguments.
| | 00:17 | I want to go a little bit deeper
here and take a closer look at commands.
| | 00:20 | Let's start out with just the
simplest command that we've seen so far, echo.
| | 00:23 | So echo 'Hello World,' just
echoes back that string to us, right.
| | 00:28 | That's a classic command.
| | 00:29 | Now a command is actually a program.
| | 00:31 | It's not a big program like Photoshop
or Excel, but it is still a program.
| | 00:35 | We typically just call it a command.
| | 00:37 | The other thing you need to know is
that commands are not somehow magic.
| | 00:40 | They're just files that you are executing.
| | 00:42 | That's all they are.
| | 00:43 | They are files that are executable
and when we execute them, they do the
| | 00:47 | business of the command.
| | 00:48 | You remember when we talked about
where files are located in the Unix file system,
| | 00:52 | I told you that /bin is a place
where we store a lot of these files and
| | 00:56 | that's where echo is.
| | 00:57 | So /bin/echo is where the file is located,
and all we are doing is executing it,
| | 01:02 | and to execute a file, you just
type the file name. That's it.
| | 01:05 | That's all there is to executing
a file and then we'll pass in the
| | 01:08 | argument, 'Hello World'.
| | 01:10 | So when we type echo, it's just a
shortcut for saying go to /bin/echo and run
| | 01:17 | that file with these arguments, and so
commands are essentially a shorthand for
| | 01:23 | going to these files and executing them.
| | 01:24 | We'll talk a bit more about how it
knows where to find these in the next movie.
| | 01:28 | For now, let's look at whereas.
whereis will tell you where that file is
| | 01:32 | located, or you can use which, which
will also tell you where it's located.
| | 01:37 | We can also use whatis to tell us the
simple information about the command.
| | 01:41 | If we do it for echo, we'll get a lot of
page of information as it tries to find
| | 01:44 | everything that references echo.
| | 01:45 | I am going to instead show you with
banner, just so you can see a nice
| | 01:48 | simple one line listing.
| | 01:49 | So the banner tells us, ah, what it does?
| | 01:52 | It prints a large banner on a printer.
| | 01:53 | There are few other helpful things
to know about working with commands.
| | 01:57 | First 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:05 | A lot of times it will just have the
command itself followed by the option.
| | 02:09 | Now that's not always true. A lot
of commands don't respond to these.
| | 02:12 | But if you're trying to figure out how
to work a program,and man pages don't
| | 02:16 | have anything, you might try help.
If you are trying to figure out what version
| | 02:18 | of Ruby you have installed, or you
might try passing in the --v or --version.
| | 02:23 | So those are just common idioms
that are frequently implemented.
| | 02:25 | If you want to exit out of a program
and you can't figure out how to do it,
| | 02:29 | Typically Q, X, Ctrl+Q, Ctrl+X or
the Escape key will get you out of it.
| | 02:34 | We saw a Q when we were
looking at the man pages.
| | 02:36 | Remember man pages use less.
| | 02:38 | That's the command that it's using to
display those pages, and Q exits out of that.
| | 02:43 | When we were working in nano, we saw
how Ctrl+X is what gets us out of nano.
| | 02:47 | I would say probably Q is the most
frequently used. Escape is probably the least
| | 02:50 | frequently used, but you might try all of those.
| | 02:53 | If can't figure how to get out of a program.
| | 02:54 | If you're really stuck then you can
force quit and you can do that with Ctrl+C.
| | 02:58 | That's the Unix way to completely
cancel the process that we're working on.
| | 03:03 | So Ctrl+C will just tell Unix,
hey, stop what you're doing.
| | 03:06 | You also can close the window,
the Terminal window on a Mac.
| | 03:09 | But be advised that if you do that,
the process may keep running. You haven't
| | 03:13 | told Unix to quit it.
| | 03:14 | You've just said, hey, I want
to abandon whatever I'm doing.
| | 03:18 | The process will keep running.
| | 03:19 | When it finishes, it will
just stop at that point.
| | 03:22 | But it doesn't actually stop the
process itself. Ctrl+C is what does that.
| | 03:26 | So be advised about that difference.
| | 03:28 | The other thing you should know is that
you can put semicolons between commands.
| | 03:32 | So we can execute one command, semicolon,
followed immediately by another command.
| | 03:36 | That can be really useful if we are
trying to do two things really fast.
| | 03:39 | Let's say we want to move a log file
to a different name and then we want to
| | 03:43 | create an empty log file back in its place.
| | 03:46 | So in one command we can essentially
do both really fast, within a matter of
| | 03:50 | milliseconds, and as I already
mentioned we have whereis, which, and whatis
| | 03:53 | available to us to find out more
information about these commands.
| | 03:56 | I told you that Unix knows to go look in that
bin directory in order to find the echo command.
| | 04:00 | In the next movie I want to talk
about how it goes about doing that.
| | Collapse this transcript |
| The PATH variable| 00:00 | In the last movie, we saw how commands
and programs are really just files that
| | 00:04 | are being executed, and we saw that we
type echo 'Hello world' that what it's
| | 00:08 | actually doing is executing a
file that is located in bin/echo.
| | 00:11 | I write the argument.
| | 00:13 | Those are doing exact same thing.
| | 00:14 | This is just a shortcut for
finding this file and executing it.
| | 00:19 | So how does Unix do that?
| | 00:20 | How does Unix know to find echo in
this location or to put it another way,
| | 00:24 | if I create another file named echo,
how does Unix know not to use that one
| | 00:28 | instead, or what if I wanted
it to use that one instead?
| | 00:31 | How would I go about doing that?
| | 00:32 | Well, 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:43 | colon-separated list of file paths and
this is the list that Unix will use when
| | 00:48 | trying to locate commands to execute.
| | 00:50 | The early entries take
precedence over the later ones.
| | 00:53 | So what it does is it starts at the left.
| | 00:54 | When we type echo, it says,
"Oh! I'll look in user bin.
| | 00:57 | Do I find echo there? No.
| | 00:59 | All right, I'll look in bin.
Do I find echo there?"
| | 01:01 | Well, in this case, it does and so it runs it.
| | 01:04 | If it didn't though, then it would
look in usr/sbin, sbin, usr/local/bin, and
| | 01:08 | finally user/X11/bin.
| | 01:10 | If it didn't find it in any of those places,
then it would say "Oh, the command is not found."
| | 01:14 | You can see that behavior.
| | 01:15 | If you just type some junk on the
screen and hit Return, it comes up and
| | 01:18 | says command not found.
| | 01:20 | What it actually did was checked all of
those directories in order and then said, "Nope,
| | 01:24 | it wasn't there.
It must not be a command that's available to me."
| | 01:27 | Now, you can change this path in bash.
We do that with just typing PATH, no
| | 01:32 | dollar sign in front of it, equals,
and then the path that we want it to be,
| | 01:36 | Different shells choose a slightly
different technique for setting the path.
| | 01:39 | I am just going to show you the bash way.
| | 01:42 | Let's just grab this string here and
I'll use Command+C and then Command+V to
| | 01:46 | paste it and that will give me
everything except that very last path.
| | 01:50 | So I am just leaving off that last
path and the colon that's before it.
| | 01:53 | Now, if we go up and do echo $PATH,
you can see my path has changed and I've
| | 01:58 | omitted that last directory.
| | 01:59 | What that means is that any
commands or programs that were in that last
| | 02:02 | directory, Unix will no longer look there.
| | 02:05 | It won't be able to find them.
| | 02:06 | I have essentially taken
them out or uninstalled them.
| | 02:09 | We can do the same thing by
adding our own directories.
| | 02:11 | If we have other places we want it
to check, we can add that in there.
| | 02:15 | Frequently, when we do web development,
we want to add a MySQL path so it'll
| | 02:19 | look inside our MySQL folder to
find all those MySQL commands.
| | 02:23 | That's a very common use of this.
| | 02:24 | Let me show you an extreme example.
| | 02:26 | If we set our path equal to just simply usr/
local/bin and let's echo our path, that's it!
| | 02:33 | It's just going to check this one place.
| | 02:35 | We could use echo, we could try
that as an example, but echo is such a
| | 02:39 | fundamental and that it's
actually really built into the system.
| | 02:41 | The system will find it anyway.
| | 02:43 | As a different example, let me use ls.
| | 02:45 | That's another pretty fundamental one but if
we try ls now, it says oops. Command not found.
| | 02:50 | It didn't find the ls command anymore.
| | 02:52 | Now, you could change your path back
and just paste that back in, or I want to
| | 02:56 | show you that if you close the window
and you reopen it now, now if we say ls,
| | 03:02 | we get our directory listing.
| | 03:03 | That's because setting the path in the way
that we did only lasts for the current session.
| | 03:08 | It doesn't stick around.
| | 03:09 | In Chapter 7, we are going to talk
about configuring your environment and
| | 03:12 | you'll be able to set a
path there that will stick.
| | 03:15 | So if you want to know how to do that,
Chapter 7 will cover how to make a path last.
| | 03:19 | But I want to make sure that I show you
here is the concept of the path so that
| | 03:23 | you will understand how it's translating
these commands into executing the files
| | 03:27 | that may be located in different locations.
| | 03:30 | There is a last thing.
| | 03:31 | Let me also just mention that the
command that we learned in the last movie,
| | 03:34 | which is the which command,
| | 03:36 | so if we say which ls, it tells us
which version of ls we are using.
| | 03:40 | What that means is there may be several
versions here, but I'm using your PATH
| | 03:44 | variable and I am telling you
which one I'm going to give you.
| | 03:47 | So that's important. which is slightly
different than whereis because which
| | 03:50 | uses our path to say this
is the one that's active.
| | 03:53 | So it's a really good command to know.
| | 03:54 | So for example, maybe we have several
different versions of Ruby installed.
| | 03:58 | Well then we could say which ruby, and
it will come back and tell us the version
| | 04:02 | of Ruby that we will get if we were to
type a command that started with Ruby.
| | 04:06 | Okay, now that we have those
fundamental concepts of how commands and
| | 04:08 | programs work, we are ready to
actually start diving in and learning some
| | 04:12 | useful commands.
| | Collapse this transcript |
| System information commands| 00:00 | In this chapter, we are going to be
looking at useful commands that we can
| | 00:03 | use in Unix, and I want to start
us off by looking at useful system
| | 00:06 | information commands.
| | 00:07 | The first one we'll look at it is
probably the simplest, which is just simply
| | 00:10 | date and that tells you the current date.
| | 00:12 | That's a nice handy thing.
| | 00:13 | If you want to know what the current date
is, you just type date and it tells you.
| | 00:17 | Keep in mind that this value is the
date that the computer has been set to,
| | 00:21 | which is not necessarily correct.
| | 00:23 | Hopefully, we did set our Mac up to
have the correct date, but it's essentially
| | 00:26 | the same date that you would see up here
on your Mac if you had a clock display.
| | 00:30 | You would set this value via the system
preferences and the date and time settings.
| | 00:34 | So that's what it's telling you.
| | 00:35 | This is the date the system thinks it is.
| | 00:37 | We also have uptime.
| | 00:39 | That's going to report to us
the time that it's been turned on.
| | 00:42 | You can see that I've been up for three hours.
| | 00:44 | So for three hours I've had Unix up and running.
| | 00:47 | That's the time since I booted my computer.
| | 00:49 | It tells you also the time here.
| | 00:51 | Don't need to worry
about the load averages here.
| | 00:53 | That's letting you know just sort
of how well the system is performing.
| | 00:56 | You won't need to worry about that.
| | 00:58 | Notice that it says 2 users.
| | 00:59 | You may be thinking, well, wait a minute!
| | 01:00 | 2 users? Why are there two users?
| | 01:02 | Well, we can actually use
a couple of other commands.
| | 01:05 | Let's use users and that
shows you just one user.
| | 01:09 | So wait a minute, so that
doesn't seem to match. Or who. who shows us a
| | 01:13 | list of all users and what they're doing and
it doesn't de-dupe it. users de-duped the 2.
| | 01:18 | There is actually two Kevins logged in.
| | 01:20 | One of them is the Terminal program that I
am using now when I open up a new bash shell.
| | 01:24 | But keep in mind that I also have
the Finder running here, which is also
| | 01:29 | interfacing with Unix.
| | 01:30 | So it has to be logged in as Kevin as well.
| | 01:32 | So those are the two users, the one
that's sort of outside here in the Finder
| | 01:36 | and the one that's inside this window.
| | 01:38 | I am going to actually open up a new
Terminal window and then from here you will
| | 01:42 | see now we have a second one. We can type who.
| | 01:45 | You can see now we have three.
| | 01:46 | There is ttys000, which is this
other one over here and the other one has
| | 01:50 | been called ttys001.
| | 01:51 | So that's how it keeps them straight.
| | 01:54 | So who shows you each of these
occurrences every time that I log in, whereas if
| | 01:58 | you just type users, it just
shows you the one user, Kevin.
| | 02:01 | It doesn't care the fact
that there are several of them.
| | 02:03 | So those are useful commands.
| | 02:05 | uptime, users, and who.
| | 02:07 | We also have uname, which
returns the operating system name.
| | 02:12 | Now, you may be like "Wait a minute!
| | 02:13 | Darwin? I thought I was using Mac OS X."
Remember when we talked about back, when
| | 02:17 | we said what is Unix?
| | 02:18 | Darwin is the name that Apple gives to
their version of Unix with all the extra
| | 02:23 | code that they've added in on top of it.
All the proprietary stuff as well as
| | 02:26 | the open-source stuff
altogether is called Darwin.
| | 02:28 | So that is the version of
Unix that we are running.
| | 02:30 | OS X is running on top of that.
| | 02:33 | We are talking about the Unix name.
| | 02:35 | That's the u in front of it. uname is Darwin.
| | 02:37 | We also can pass in some options here.
| | 02:40 | uname, dash, and the options are mnrsv
and p and that will return all sorts of
| | 02:47 | information to us, not just the
operating system but you can see it tells me
| | 02:51 | 10.6 is what I am running.
| | 02:52 | It gives me real specific
information about the release that I'm running.
| | 02:56 | It tells me my processor, the
hardware that I'm running on, and actually a
| | 03:00 | shortcut for all of those. For
everything except the P is the A option.
| | 03:04 | That does mnrs and v altogether.
| | 03:07 | P is the processor, and that's the second.
| | 03:09 | You can see that those are actually
the same, the last two entries there but
| | 03:11 | that will do the same thing for you.
| | 03:13 | You can check the man pages to see what
all those are but that just gives you a
| | 03:15 | little more information about
the system that it's running on.
| | 03:18 | We also can use hostname and domainname.
Those are more useful when you're in a
| | 03:22 | networked environment.
| | 03:24 | The hostname would be the host that we
are on and domainname for me is not set
| | 03:28 | to anything but on a web server or some
kind of a shared Unix work server, those
| | 03:33 | might return something more useful to you.
| | 03:35 | So together those commands make up
some tools that we have to find out some
| | 03:38 | basic system information.
| | Collapse this transcript |
| Disk information commands| 00:00 | In this movie we're going to be
looking at some commands to give us some hard
| | 00:03 | drive information, or as Unix
typically refers to it a disk.
| | 00:07 | The two commands we're going
to be looking at are df and du.
| | 00:10 | df stands for disk free space, so it's
going to display the amount of disk free
| | 00:15 | space available to us.
| | 00:16 | If I type a line by itself and hit
Return, it'll come up with the report
| | 00:19 | telling you all of the volumes that it
can see, the hard drives, and how much
| | 00:23 | space is being used and how
much is available on each of them.
| | 00:26 | Notice that it has a couple of devices
here that it's calling volumes that are
| | 00:30 | just sort of utility devices that it's using.
| | 00:32 | You can basically ignore those.
| | 00:34 | The one we care about are
the ones with the big numbers.
| | 00:37 | Main hard drive is listed here as root.
| | 00:39 | That's where it's mounted.
| | 00:40 | If we had other hard drives, maybe you
have your hard drive partitioned or you
| | 00:43 | have external drives plugged in, you
would see those under /volumes/ and then
| | 00:48 | the name of the hard drive.
| | 00:49 | We would see reports for those as well.
| | 00:51 | Now I don't know about you but I
find these numbers a bit hard to read.
| | 00:54 | So one of the options we can use
with df is the -h option for humanize.
| | 00:59 | This will humanize those
numbers so now we say, oh!
| | 01:02 | 39 gigabytes used, 16 gigabytes
available, 71% of it has been used, okay.
| | 01:08 | much easier to read.
| | 01:10 | However, there is a caveat
that comes with those numbers.
| | 01:12 | There are two different ways
that one can calculate gigabytes.
| | 01:15 | You can calculate it using Base-2 or Base-10.
| | 01:18 | It's a bit of a technical point.
| | 01:20 | You may remember in the news there was
some discussion about the fact that hard
| | 01:23 | drive manufacturers were selling hard drives
| | 01:25 | that would say, well, this is a 10
gigabyte drive and then people would get it
| | 01:29 | home and it wasn't a 10 gigabyte drive
once they started putting information on it.
| | 01:33 | It's because of the way that the
gigabytes are calculated were different.
| | 01:36 | So we also have df -H which
uses Base-10 to calculate it.
| | 01:42 | These give us those higher numbers, the
ones that the hard drive manufacturers
| | 01:46 | were using, whereas the lowercase h is
giving us the actual amount of bytes that
| | 01:51 | we could put on there when
we're talking about the data.
| | 01:53 | Now I think you want to stick with the
lowercase h. I recommend that you just
| | 01:57 | use that all the time.
| | 01:58 | It's more widely supported,
as we'll see in a moment.
| | 02:00 | But that capital H, I believe, is the
one that now is used in Snow Leopard.
| | 02:04 | So starting in Snow Leopard, Apple
started using capital H when it looks at
| | 02:07 | sizes for hard drives instead of
the lowercase h. It's really less of a
| | 02:11 | technical point and more of an issue of
customer confusion over the marketing.
| | 02:15 | The second command that we're going to use
is going to be du, which stands for disk usage.
| | 02:20 | And with that we'll provide it the
path that we wanted to look at and tell us
| | 02:24 | about the disk usage of that path.
| | 02:26 | We wouldn't want to do this at the root
of our hard drive because then it would
| | 02:28 | tell us every single file and
folder on the entire hard drive.
| | 02:31 | If you accidentally do that,
hit Ctrl+C to exit out of it.
| | 02:35 | Instead we want to target it a little
more at a direct path. disk usage for in
| | 02:40 | my user folder unix_files.
| | 02:42 | that's where I've got
just a few files set aside.
| | 02:45 | And it comes up and it tells me for
each and every one of the directories that
| | 02:48 | it sees, what the size is.
| | 02:50 | Now just like the other one we
have a -h option that we can pass in.
| | 02:54 | That's a lowercase h. unix_files, and
now it gives me a human readable version.
| | 02:59 | It has a -H option but it's completely
unrelated to human readable output, so
| | 03:05 | you wouldn't want to use it here.
| | 03:06 | That's why I said that lowercase h is
more widely supported as being humanized.
| | 03:10 | So stick with lowercase h.
| | 03:11 | Now you may notice it's only
showing me the directories here.
| | 03:14 | If we want it to also show us the files,
we can put the -a option in front of it.
| | 03:19 | Now you can see it tells us all of
the files and the directories and
| | 03:23 | their sizes, all right?
| | 03:24 | So we get a list of all
those. Fo for -a, think all.
| | 03:27 | Now this is sort of similar to what you
get in the Finder if you do Get Info, right?
| | 03:32 | And it calculates the sizes.
| | 03:33 | But what it does is it
actually summarizes the sizes.
| | 03:36 | The way that we can do
that is by using the depth.
| | 03:39 | So for example we say du,
we'll just do the directories.
| | 03:43 | We'll leave out the files. Humanize the
directory with the depth of 1, so that's
| | 03:47 | the depth option and then the argument
to the option is 1, and then we'll tell it
| | 03:52 | to look at unix_files.
| | 03:54 | And you'll see it just goes one directory deep.
| | 03:56 | It shows me the current directory plus one.
| | 03:58 | If I did the same thing with the 0
option then it says, "Okay, just for the
| | 04:04 | current directory, right?"
| | 04:06 | Show me just this one directory,
summarize the size of that.
| | 04:09 | So a depth option can be really useful
if you don't want all of this output.
| | 04:12 | You really what you want to know is
just this directory, to summarize and tell
| | 04:16 | me how large that is.
| | 04:17 | Now there's another important point
that we need to talk about with disk usage.
| | 04:21 | What it's reporting to us here is the
amount of size that has been set aside
| | 04:25 | for these files on the hard drive.
That's different than the size that the files
| | 04:30 | are actually using.
| | 04:31 | You notice here that all of these
files report 4K as their value, right?
| | 04:36 | But they're not all the same size files.
| | 04:38 | In fact, we can see that.
| | 04:39 | If we do ls -lah, we'll get a listing. Oops!
| | 04:42 | Let'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:51 | take up just 55 bytes.
| | 04:53 | It's because the file system sets
aside blocks of space for files.
| | 04:57 | Even if they don't take up the whole
space, it goes ahead and gives them a whole
| | 05:00 | block on the hard drive.
| | 05:02 | So there's a minimum size for those.
| | 05:04 | Disk usage is returning the space taken
up by the blocks, the space allocated to
| | 05:09 | the files, not the actual file size.
| | 05:13 | So I want you ti understand the difference
and understand that the two aren't the same.
| | 05:15 | ls is reporting the actual size
of the file, whereas du is reporting the
| | 05:21 | allocation of space for that file,
even though it may not be using it.
| | 05:25 | You may have come across this before if
you ever defragmented your hard drive.
| | 05:28 | There are tools out there that lets you to
defragment your hard drive to regain space.
| | 05:33 | One of the things that it's doing is
it's taking the files and by moving them
| | 05:37 | all together so that they're not
fragmented, well, then the block allocation can
| | 05:41 | really be best utilized.
| | 05:42 | And we can really make sure that we're
using only the exact number of blocks
| | 05:46 | that we need, whereas if they're
fragmented and part of the file is spread out
| | 05:49 | at different places on the hard drive,
you may end up with lots of blocks that
| | 05:52 | are partially used.
| | 05:53 | They've been allocated for the
file but not completely used.
| | 05:57 | And that's how you're able to reclaim a
lot of that space when you defragment.
| | 06:00 | So again, the two main commands for
finding out disk information are df for
| | 06:04 | disk free and du for disk usage.
| | Collapse this transcript |
| Viewing processes| 00:00 | Over the course of the next few
movies I want to take a look at some Unix
| | 00:02 | commands for managing processes.
| | 00:05 | Think back to Chapter 1 where we
talked about the difference between the
| | 00:07 | kernel and the shell.
| | 00:08 | In our case the shell is bash.
| | 00:10 | Whenever we run a command inside
our shell, a file executes and it
| | 00:14 | communicates with the kernel.
| | 00:15 | And essentially it says
to the kernel, "Hey kernel!
| | 00:17 | There are some things I need to accomplish here.
| | 00:19 | Can you help me out?"
| | 00:19 | The kernel sets aside some memory
space and starts a process running in it.
| | 00:23 | Then whenever there's output from
that process, it returns it back to the
| | 00:27 | shell for us to see.
| | 00:28 | And whenever the process is finally
done, the kernel then closes it out and
| | 00:32 | reclaims that memory space so that
it can be used by other processes.
| | 00:35 | That's what the kernel does.
| | 00:36 | it manages the processes for us.
| | 00:38 | Now we can have processes that are really
short, like when we run the echo command.
| | 00:42 | That's a really, really short process.
| | 00:44 | It starts, it does
its thing, and then it's over.
| | 00:46 | We could also have longer running processes.
| | 00:48 | Let's say that we're going to
print five pages to a printer.
| | 00:50 | That'll start a process and that process
will just feed information to the printer.
| | 00:54 | After it submits page 1,
it'll go to page 2, and so on.
| | 00:57 | And if the printer runs out of paper,
the printer will communicate that back to
| | 01:01 | the process, which will then let us know
that the printer has run out of paper.
| | 01:04 | That's an example of a long-running process.
| | 01:06 | We can also have background processes.
| | 01:08 | For example, if we have a database
server like MySQL running, it'll just sit and
| | 01:12 | run in the background waiting for
connections, waiting for someone to make a
| | 01:16 | request to it which it'll then respond to.
| | 01:18 | So it just sits in the
background waiting to do its thing.
| | 01:20 | Now what we want to do now is
be able to see those processes.
| | 01:24 | And the easiest way to do
that is with the ps command.
| | 01:26 | So ps stands for process status, and
it's essentially giving us a snapshot of
| | 01:30 | the processes that are running.
| | 01:32 | Now there are lots of
processes running on my machine.
| | 01:34 | By default what ps is showing me are
processes that are owned by me, the user
| | 01:38 | kevin, and also a process
which have a controlling terminal.
| | 01:42 | That is, I am in control of them.
| | 01:44 | They're not background processes.
| | 01:46 | If we want to see processes that are owned
by other users as well, we can say ps -a.
| | 01:51 | Still I am not seeing background processes but
now I see processes that are not owned by me.
| | 01:55 | Notice that the third line there
lists the process they I just ran. ps -a.
| | 01:59 | It's a process that's not owned by me.
| | 02:01 | I own the bash process.
| | 02:03 | Bash started the ps -a running, but it's
actually a process that's owned by root.
| | 02:09 | And when it's done doing its thing,
it returns the results back to me.
| | 02:12 | So it's a bit of a technical point
but we don't necessarily own all the
| | 02:15 | processes that get started.
| | 02:17 | A lot of times process gets started on our
behalf by the kernel, which is acting as root.
| | 02:22 | Now there's one big quirk about ps
that you need to know about and that is
| | 02:25 | that because ps has been around a
long time and it has been modified in
| | 02:29 | different versions of Unix, there's
been a real effort to maintain some
| | 02:32 | backwards compatibility here.
| | 02:34 | So in addition to ps -a, we also can
use ps a, without the dash. One is
| | 02:40 | the classic option passed into
the command that we've been seeing.
| | 02:44 | The other one is using more like
an argument that's being passed in.
| | 02:47 | And you'll see that it gives us very similar
results, not exactly the same but very similar.
| | 02:51 | What we're essentially seeing here
are two different implementations of ps.
| | 02:55 | One from one version of
Unix and one from another.
| | 02:57 | The reason I mention this is because the
most classic way to use ps is ps with aux.
| | 03:05 | Those are the options that we pass in and we
typically don't put the hyphen in front it.
| | 03:08 | It's a bit of an odd bird because it
doesn't match the options that we're
| | 03:12 | normally used to using with the dash.
We can actually do a and x together but
| | 03:17 | the problem is u, because u actually
has a different meaning between the two
| | 03:21 | versions, so therefore we can't do it that way.
| | 03:23 | So we're going to do it this way.
| | 03:25 | a means show me all processes regardless
of who they're owned by, u says include
| | 03:30 | a column showing me the user that
owns the process, and x says show me the
| | 03:35 | background processes too.
| | 03:37 | That's what those three options mean.
| | 03:38 | Let's take a look at it. ps -aux, and
there's a list of all the processes that I
| | 03:42 | have running right now.
| | 03:43 | Let's scroll up the top and take
look at the header information.
| | 03:46 | You can see that first column is
the User, then is the Process ID.
| | 03:49 | Every process gets assigned a unique
ID number to help us keep track of it.
| | 03:53 | It shows us the percentage of the CPU
that's being used, the percentage of the
| | 03:56 | memory that's being used.
| | 03:57 | This is the amount of
virtual memory that's taking up.
| | 04:00 | This is showing us the Terminal.
| | 04:02 | In most cases it's a question mark.
That often means that Mac OS X launched it.
| | 04:05 | If we scroll down to the bottom you'll
see that these processes that we've been
| | 04:08 | working with have 000.
| | 04:10 | That's for our current Terminal.
| | 04:11 | If we own up a new window and
ran commands there it would be 001.
| | 04:15 | So let's go back to the top
again. We have the status.
| | 04:18 | You don't need to worry about those
codes. The time that it started, the amount
| | 04:22 | of time that it's been running, and
then the command itself, which is a path to
| | 04:25 | the file where this command got started.
| | 04:28 | So that gives you some information
to browse over and you can see what's
| | 04:30 | going on in your Mac.
| | 04:31 | Now if you want to use other options,
the man pages can give you a lot more of
| | 04:35 | those information about them.
| | 04:36 | But the most common way that you'll
see it use is ps aux, and that should
| | 04:41 | return a list like this.
| | 04:42 | It'll show you a snapshot of what's
happening with different processes at
| | 04:45 | this moment in time.
| | 04:46 | Now the problem with that is that
it is a snapshot, and things change.
| | 04:50 | things are constantly changing.
| | 04:51 | And it'll be great if we had something
where we can actually watch things like
| | 04:54 | CPU and memory usage as they change over time.
| | 04:57 | And 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:00 | In the last movie we saw how we can use
the ps command to be able to view a list
| | 00:04 | of the Unix processes that are running.
| | 00:06 | But the problem with it is that
sometimes we don't just want a snapshot in a
| | 00:09 | moment in time. We actually want
to be able to watch the processes.
| | 00:13 | We want to be able to monitor them as
the CPU and memory usage goes up and down,
| | 00:17 | whereas processes start and stop.
| | 00:19 | So to do that we need something that's
a little more interactive that will keep
| | 00:23 | refreshing and showing us the
state of things as they change.
| | 00:26 | The tool for doing that is called top.
| | 00:27 | So if you simply type top and hit Return,
you will get a list of the top processes.
| | 00:32 | Now, top processes depends on
what order you've sorted it in.
| | 00:35 | By default, it's sorted by the Process ID.
| | 00:38 | So the most recent processes are at the top.
| | 00:40 | You will see above that that we
actually have a list of some summary
| | 00:43 | information, the CPU usage, the memory
usage, that kind of thing that's going on,
| | 00:47 | and then we get that
information for each one of these as well.
| | 00:50 | So we can see how much memory usage it's
using up and how much of our RAM it's using up.
| | 00:54 | Now perhaps the most important thing
you need to know about top is that you can
| | 00:57 | hit the Q key to get back out of it.
| | 00:59 | So Q will exit out and take
you back to your command line.
| | 01:02 | So 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:09 | By default, it shows however big the window is.
| | 01:11 | But we can say, all right,
| | 01:12 | we only want to see the top 10.
We can specify the sort order.
| | 01:16 | There are some special keys that we
used to say the sort order and I can show
| | 01:19 | you those in the moment.
| | 01:20 | For now let's just say CPU.
| | 01:21 | We're going to sort it by CPU
usage and we can set the refresh rate.
| | 01:25 | We will use the -s for seconds.
| | 01:27 | By default, it's set to 1
second refresh. Let's set it to 3.
| | 01:30 | So every 3 seconds it will refresh and
then we can also pass in a capital U for
| | 01:35 | user showing filter by user.
| | 01:37 | So we are going to see only the user
that we specify, Kevin in this case.
| | 01:40 | So we will see the top 10 processes of
Kevins, sorted by CPU, refreshing every 3 seconds.
| | 01:47 | So there you go!
| | 01:49 | Now, every 3 seconds it will re-update,
but you can see the thing that's using
| | 01:52 | the most CPU is Snapz Pro, which is the
program I'm using to record this video.
| | 01:57 | We have some interactive commands that
we can issue as well, the most important
| | 02:00 | of which is the question mark.
| | 02:01 | That's the same thing as sort of
help. It displays this Help screen.
| | 02:04 | Notice here that it gives you sort orders.
| | 02:06 | Right now, I have it sorted by CPU.
| | 02:08 | That's what it's telling
you here. The current state.
| | 02:10 | We can pick a different thing, we
can sort by time, we can sort by size.
| | 02:14 | That's the resident memory size, which
refers to the RAM that it's using up, and
| | 02:18 | we can sort by user and so on.
| | 02:19 | The man pages will detail what
each of these actually does for you.
| | 02:23 | We can also change the delay just by using s
followed by the delay. So let's try that.
| | 02:27 | Hit any key to continue.
| | 02:29 | Now, we are back here again.
| | 02:29 | Let's change our delay back to 1.
| | 02:30 | I will hit s and 1, hit Return,
and now it updates every 1 second.
| | 02:35 | If we want to see all the users, we
type a capital U and then I can just hit
| | 02:39 | Return and that will show me
all users now, not just Kevin.
| | 02:42 | But one thing that we can't do from
here is change the In value, the number of
| | 02:47 | processes that it's showing.
| | 02:48 | That's not available.
| | 02:49 | But the o, s, and capital U options
are all still available from here.
| | 02:54 | Now, there are some differences
between top and different Unix versions.
| | 02:57 | This is the way it works on the Mac.
| | 02:58 | The Mac is a little bit limited in the
interactive options that you can type.
| | 03:02 | Something like Linux the version of
top actually has a lot more options and
| | 03:05 | shortcuts that you can use
from that interactive page.
| | 03:07 | So check the man pages, check that help
page, and it will help you out with all
| | 03:11 | the different commands that you can use.
| | 03:13 | Let's launch it one last time and I
just want to show you if I go ahead and I
| | 03:17 | launch Firefox, and we can see it pop
up and we can see the memory that it
| | 03:21 | takes up and so on, all right?
| | 03:23 | You can see it up here and then we come
back to Firefox and we quit out of it again.
| | 03:27 | You can see that it goes away.
| | 03:27 | So this is a nice little tool.
| | 03:29 | You can just leave it open and
watch the processes come and go.
| | 03:31 | In the next movie we will actually
take a look at how we can stop these
| | 03:35 | processes from running.
| | Collapse this transcript |
| Stopping processes| 00:00 | Over the course of the last two movies
we've been looking at how we can view and
| | 00:03 | monitor the different
processes that are running in Unix.
| | 00:05 | In this movie I want us to
see how we can stop processes.
| | 00:08 | Now, the number one best way to stop a
process is with Ctrl+C. That assumes that
| | 00:13 | you're running the process inside
your Terminal window. You started it.
| | 00:17 | It's just taking too long or maybe you
changed your mind in the middle of it.
| | 00:20 | Ctrl+C tells Unix, hey!
| | 00:23 | Stop that process, interrupt it,
let's go back to the command line.
| | 00:27 | So that's the best thing, but
sometimes we can't use that.
| | 00:30 | Sometimes there are processes that
are running in the background, or maybe
| | 00:33 | there's a process that we did have
control of but somehow we've lost control.
| | 00:37 | So we can still see it, we can see it's
running there, and we really wish it would go away.
| | 00:41 | Well, to do that, we use the kill command.
| | 00:44 | In order to show you how we would do
it, let's do ps aux. We see a listing
| | 00:48 | there and you will see I have
a listing on here for s000 bash.
| | 00:53 | That's my current bash program.
| | 00:54 | Now I am going to open up a new window.
| | 00:56 | Essentially what I want here is a
process that I am not afraid to kill off,
| | 00:59 | because I don't have an errant process
running right now and I don't want to use
| | 01:03 | sort of advanced tricks to get one running.
| | 01:06 | Instead, I am just going to
create this new Terminal here.
| | 01:09 | Now when I do ps aux you'll see that I see
another bash here and this bash is owned by 0001.
| | 01:17 | Let's clear the screen just
so we can see it nice and well.
| | 01:19 | Here is bash down here and if we scroll up
here, it should be this other bash up here.
| | 01:25 | Now notice 000 is the one that I am on.
| | 01:27 | That's the one I don't want to get rid of.
| | 01:29 | The one I want to get rid of is 001.
| | 01:33 | That's the other window that's going on
and that's the process I am going to kill.
| | 01:36 | I am going to forcibly just
stop that bash from running.
| | 01:40 | What I need is the process ID.
| | 01:42 | That's how we tell Unix which process we want.
| | 01:45 | Every single process has a unique ID,
so it's the way that we can reference it.
| | 01:49 | What we use is kill 1837.
| | 01:53 | That's the process ID.
| | 01:55 | We have killed it, except that we haven't.
| | 01:57 | Some processes can't be killed by
using kill on its own like this.
| | 02:01 | So always try kill first.
| | 02:03 | It'll do its best to kill it off.
| | 02:04 | But there are cases where it'll say,
you know what, I think you probably
| | 02:07 | don't want to do that.
| | 02:09 | This is one of those cases, because
it's running bash in a Terminal window.
| | 02:11 | So it says, you know what, I could kill that
off but I think that's probably not a great idea.
| | 02:16 | So it didn't do it.
| | 02:17 | Let's just take a look here. ps aux, I
will clear the screen, just so we can see
| | 02:21 | it is still running.
| | 02:22 | Right here it is 001, bash, same process ID.
| | 02:26 | But if we pass it the -9
option, that says, yeah!
| | 02:30 | I know better than you.
| | 02:31 | It means really, really, really
kill this thing off. Forcibly kill it.
| | 02:36 | So kill -9 and then the process ID number.
| | 02:39 | Now, I killed it off.
| | 02:40 | Let's take ps aux again. Take a look.
| | 02:43 | Notice that it's not there.
| | 02:44 | The only bash that I have is 000 and
come over here to the other Terminal and
| | 02:49 | look, process completed.
| | 02:51 | The process is done.
| | 02:51 | If I hit anything, I don't get
anything in that window anymore.
| | 02:54 | I killed off that process.
| | 02:55 | But you can do this anytime you've
got a process that's taking up tons of
| | 02:59 | memory, tons of CPU usage,
something you don't have control over.
| | 03:03 | Obviously you want to be careful.
A lot of these processes your system needs.
| | 03:06 | You don't want to just start
killing off processes willy-nilly.
| | 03:09 | But it is a technique if there is a
process that you know needs to be killed.
| | 03:13 | This is how you would do it.
| | 03:14 | First, try kill and then
kill with the - 9 option as well.
| | Collapse this transcript |
| Text file helpers| 00:00 | In this movie we will take a look at
some Unix commands that can help us when
| | 00:03 | working with text files.
| | 00:04 | Now when I say text file, I am really talking
about a file that has nothing in it but text.
| | 00:09 | That's very different from a
Microsoft Word document which contains both
| | 00:12 | formatting information and text.
| | 00:15 | These commands would not help us in those cases.
| | 00:17 | For example, if we try to do a word
count of a Microsoft Word document,
| | 00:20 | it would try and count all of the
formatting information as well as the actual
| | 00:25 | text of the document.
| | 00:26 | So in all these cases we are really
talking about working with pure text files.
| | 00:29 | So the three that we are going to be
looking at are wc, which is short for word
| | 00:32 | count, sort for sorting lines, and
unique, uniq. There is no ue on the end of it
| | 00:38 | and that's for filtering
in and out repeated lines.
| | 00:40 | Let's take a look at all
three and see how they work.
| | 00:43 | So in Terminal, notice that I am already
inside my user directory and I'm inside
| | 00:46 | my unix_files directory. I can do ls-la
on them and we will see that I've added
| | 00:51 | a new file here called fruit.txt.
| | 00:54 | Take a look at the contents of that file.
| | 00:55 | It's a very simple file.
It just contains a list of fruit.
| | 00:58 | Notice a couple of things about it though.
| | 01:00 | Notice that they are not sorted.
Notice that there are repeats in there
| | 01:04 | and notice in particular that strawberry
is repeated immediately one after the other.
| | 01:08 | So you can create your own. You can
pause the movie and copy this list down if
| | 01:11 | you want. They're also
included in the Exercise Files.
| | 01:14 | The important thing is to make sure
that it's not sorted, that it contains
| | 01:16 | duplicates, and at least one of those
duplicates are one immediately following another.
| | 01:20 | So let's try this out now.
| | 01:22 | Let's look at our first one. We have wc.
| | 01:24 | That's for word count and all we do is
say the file name that we want. wc fruit.
| | 01:28 | It comes up and it gives me 3
numbers followed by the name of the file.
| | 01:32 | The first number is the number of
lines in the file, the second number is the
| | 01:36 | number of words in the file, and a
word is a set of characters with spaces
| | 01:40 | on either side of it.
| | 01:41 | That's what it considers a word. And
then the third value is the characters in
| | 01:45 | the file, the number of
letters that have been used.
| | 01:47 | So again, if we take a look at the file,
cat fruit, you can see that there are 13
| | 01:51 | lines, each line has one word, so
there's also 13 words, and if we add it all
| | 01:56 | up, there are 99 characters in that file.
| | 01:58 | Let's try it with something a little
more useful. Remember we have this lorem
| | 02:01 | ipsum text, which is just fake
Latin text, just a long document.
| | 02:05 | Let's do wc lorem_ipsum.txt.
| | 02:08 | You will see that it comes up and it
tells me that that file has 523 lines,
| | 02:13 | 5,289 words, and 36,232 characters.
| | 02:19 | Now I just want to point out
something here, if we do head lorem_ipsum.
| | 02:22 | We can take a peek at the beginning.
Each one of these lines actually has a
| | 02:25 | line return after it.
| | 02:26 | I did that on purpose when I created
this file. There is a line return at the
| | 02:30 | end of each of these lines.
| | 02:31 | Now, with a lot of paragraphs,
that would not be true, right?
| | 02:34 | They would not be wrapped like this,
they would just continue, and you wouldn't
| | 02:37 | have a line return, you would just keep going.
| | 02:39 | So every paragraph would be considered one line.
| | 02:42 | So keep that in mind.
| | 02:43 | When we are talking about lines.
We're not talking about the lines that you
| | 02:46 | see on your screen necessarily; we are talking
about a line up until it gets to a line return.
| | 02:51 | So that's really all
there is to doing word count.
| | 02:53 | It's a very simple
utility that can be very useful.
| | 02:56 | Let's try out working with the next one
which is sort, and all we do for sort is
| | 02:59 | we say sort the lines in
a file. sort fruit.txt.
| | 03:03 | So now it gives us that same
list back but it's been sorted.
| | 03:07 | Now notice it did not
actually change the file itself.
| | 03:09 | If I do, the original file is untouched.
| | 03:12 | What it did was just take that file, sort it,
and output the results to me on the screen.
| | 03:16 | Now I could copy and paste it or, as
we will see in the next chapter, I could
| | 03:19 | then send that to a file.
| | 03:21 | We will see how to do that.
| | 03:22 | I want to make sure you realize that
it's not actually sorting the contents of
| | 03:25 | the text file itself. All right!
| | 03:26 | Let's try that with our lorem_ipsum.
| | 03:28 | Let's try sort lorem_ipsum.txt.
| | 03:31 | Again, each one of these has a line return
after it, so each one is considered a line.
| | 03:36 | If they were paragraphs, then
this would not behave the same way.
| | 03:39 | Notice that we are seeing all of
the Vs down here at the bottom.
| | 03:41 | I just want to scroll up a bit through
the alphabet, so we get up here to the As.
| | 03:45 | Here we have the As. Now, notice this.
| | 03:47 | Lowercase a and then we start over with
capital V. Capital letters and lowercase
| | 03:53 | letters are treated
differently by sort by default.
| | 03:56 | Now we can pass-in the f
option and we can change that.
| | 03:59 | So let's do -f. I will clear the screen,
just so we have everything gone from before.
| | 04:04 | And now look. You see that they are
mixed up, the uppercase and lowercase.
| | 04:07 | That's the f option.
| | 04:08 | There is also a reverse sort.
| | 04:11 | Let's take out the f option
and do an r. That's for reverse.
| | 04:15 | Now we see a lot of blank lines here.
| | 04:18 | If we scroll up you'll see the
blank lines count as lines.
| | 04:21 | They get sorted before the As.
| | 04:22 | So there is the As, we keep going up.
| | 04:24 | Here is the Cs, and the Ds, and so on.
| | 04:26 | So that's reverse sort and it also
helps you to see that these blank lines are
| | 04:30 | still included as lines and then the
last thing let's do is as if we sort, sort
| | 04:35 | fruit reverse first, just so you
can see that it reverse sorts those.
| | 04:38 | That's nice and easy to see. And
let's do sort -u and that's the option
| | 04:42 | for sorted and uniq.
| | 04:45 | So we get rid of all those
duplicates at the same time.
| | 04:47 | sorted and unique. Now
each entry just occurs once.
| | 04:51 | Notice what it did there.
| | 04:52 | It sorted them, and it made sure
we only had each one, one time.
| | 04:54 | So sort has its built in uniq option.
| | 04:57 | There are a couple of other options.
You can look at the man pages to see what
| | 05:00 | those are, but sort is a
really, really useful tool.
| | 05:02 | It actually works well with other commands.
| | 05:05 | We'll see that later.
| | 05:05 | We'll see how to use two commands together.
| | 05:07 | The sort is really useful for that.
| | 05:09 | Another one that works well
with other commands is uniq.
| | 05:12 | Uniq allows you to filter in or
out repeated lines in the file.
| | 05:16 | The default is to de-dupe the repeated lines.
| | 05:18 | Let me show you by using it on our
fruit file and it's easy to see.
| | 05:23 | Notice that Apple is still in there twice.
| | 05:26 | It did not de-dupe that, but
strawberry is only in there once because
| | 05:30 | strawberry is in there immediately after.
| | 05:32 | So it does not sort them and de-dupe them.
| | 05:35 | It only goes down the line and says, ah,
| | 05:36 | is this line the same as the next line?
| | 05:39 | If it is, reduce it down to one, de-
dupe it till we only have one line.
| | 05:43 | But it does not jump ahead
and look around to other ones.
| | 05:46 | We would have to sort it
if we wanted that behavior.
| | 05:48 | But one of the nice features of uniq is that
it has a couple of other options we can pass in.
| | 05:51 | uniq-d fruit will return the lines
that are repeated. So it tells me oh,
| | 05:59 | there was one line that was
repeated in here. It was strawberry.
| | 06:01 | That's a nice handy tool.
| | 06:02 | There is also uniq-u and this
just shows me the unduplicated lines.
| | 06:08 | That's why we have the u. So now I
am seeing everything, but strawberry,
| | 06:12 | strawberry has been completely taken out.
| | 06:14 | Not just de-duped, but actually removed.
| | 06:16 | So those are complementary of each other.
| | 06:18 | Between unique -d and uniq -u you get
the same thing that you get if you did the
| | 06:23 | total list from uniq.
| | 06:25 | Having no option shows you the entire
list both duplicated and non-duplicated
| | 06:29 | but with no duplicates
immediately following each other.
| | 06:32 | So again, if your goal is really to
completely de-dupe it, then you either need
| | 06:36 | to use sort with the uniq option or you
will need to use uniq in combination with
| | 06:41 | other commands which we will
learn how to do in a little bit later.
| | 06:44 | So these are all three really simple
tools, but they can be very powerful when
| | 06:47 | you're working with lots of data in text files.
| | Collapse this transcript |
| Utility programs| 00:00 | In this movie, I want to show you
several utility programs that Unix provides
| | 00:04 | that I think can be really useful to you.
| | 00:05 | We are going to look at cal and also a
variation on that called ncal, which is
| | 00:09 | a calendar program.
| | 00:11 | bc, which is a calculator program.
| | 00:13 | bc stands for bench calculator.
| | 00:15 | Then we have expire, which
is an expression evaluator.
| | 00:18 | essentially it's a simple one line calculator.
| | 00:21 | We are just evaluating a mathematical
expression. And then units which allows us
| | 00:25 | to do units of measure conversion.
| | 00:27 | So for example, from feet to meters.
| | 00:29 | Let's try all of them out.
| | 00:31 | We will just start with the
simplest one, which is calendar.
| | 00:33 | Just type cal and look what you got.
| | 00:35 | You get the current
month's calendar. That's it.
| | 00:38 | That's all it does.
| | 00:38 | It is very simple, shows you by
default the current month's calendar.
| | 00:41 | So let's type in the month 12,
followed by 2020. So we are going to see what
| | 00:46 | December of 2020 looks like.
| | 00:48 | As an exercise, plug in your month and
year that you were born and you can see
| | 00:53 | what day of the week that you were born on.
| | 00:54 | Now in addition we also have the
ability to take a look at a full year by
| | 00:58 | using the y option.
| | 01:00 | So this will be the year 2000. We can see
what that full-year of calendar looks like.
| | 01:04 | Now, of course, we could do the same thing
for this current year as well. There we go.
| | 01:10 | That's what the current year looks
like and then let me just show you the
| | 01:13 | variation on this, which is ncal.
| | 01:14 | It has some slightly different options to it.
| | 01:17 | It's actually a different program but
what it mainly does is it rotates the
| | 01:20 | calendar so that the days of the
week run down the left side instead of
| | 01:24 | running across the top.
| | 01:25 | You can look at the man pages to see
what's some of those additional options are.
| | 01:28 | Personally, I prefer the standard cal
most of the time, but I just want you to
| | 01:31 | know that this is there as well.
| | 01:32 | Okay, let's move on to look at the next
one, which is bc for bench calculator.
| | 01:37 | If we just type bc and hit Return,
it drops us into this calculator.
| | 01:41 | It's a program that runs and it actually
has a pretty rich programming language.
| | 01:45 | So there is a lot of
things that you can do in here.
| | 01:46 | We are just going to use it for the most basic.
| | 01:49 | I am not going to try and teach you
everything there is to know about it.
| | 01:51 | I just want you to know it's there
and get an idea of how it works.
| | 01:54 | So just 1+1 and it reports the results
once we hit Return. That's what it does for us.
| | 01:59 | To get out, we type quit
and we exit back out again.
| | 02:02 | So let's go back in there and take a
look at a couple of other things about it.
| | 02:06 | So let's try some expressions. 3+4 times
210.3 and there it gives us the result.
| | 02:15 | Let's try another one. 1000 divide by 9.
| | 02:19 | Now wait a minute.
| | 02:20 | I think about the math here for a second.
| | 02:22 | That doesn't seem quite right, because
9 times 111 actually equals only 999.
| | 02:28 | We are missing some decimal places here.
| | 02:31 | There is an important point about this,
is that the precision that it reports the
| | 02:36 | results is arbitrary.
| | 02:38 | We can change the precision that it
reports those results and the way we do that
| | 02:41 | is what scale equals and
then whatever scale we want.
| | 02:45 | That's how many decimal
places we are looking at.
| | 02:47 | So now when we try 1009, you can use
the up arrow to get there as well, just
| | 02:52 | like you normally can.
| | 02:53 | Now it says okay, it's actually equal to 111. and
| | 02:57 | then 10 decimal places of 1s because
that's the precision than I told it to report.
| | 03:02 | You can change the scale to be 5 and
of course it will only give you 5 back.
| | 03:07 | So that's an important point, that you
do have to tell it how much precision you
| | 03:12 | wanted to return to you.
| | 03:13 | Now again, there is lots more that you
can do with the bench calculator, but at
| | 03:16 | its simplest, this is what it is.
| | 03:18 | Just a calculator where you can type in
expressions and they will return results
| | 03:22 | and then wait for you to do another calculation.
| | 03:24 | Let's quit out of that.
| | 03:25 | If you really just want to calculate a
really simple expression then you have
| | 03:29 | expr available to you.
| | 03:31 | Expr says all right, calculate the
expression and I am going to just show you
| | 03:34 | what won't work first. 1+1.
| | 03:37 | It doesn't realize that
it needs to break that up.
| | 03:40 | You have to put spaces in here.
| | 03:41 | Let me just show you if I try it first.
| | 03:44 | It just comes back and says, oh, the value
of that word, that thing, that unit is 1+1.
| | 03:51 | It returns it to you.
| | 03:52 | What we need to do is put spaces,
so it says, ah! I recognize.
| | 03:55 | The first thing is a 1, the next
thing is what you want to do to it and the
| | 03:59 | next thing is the 1.
| | 04:00 | That's because we are in the Unix
command line and Unix is taking each one
| | 04:04 | of these as arguments.
| | 04:07 | Even though it's an expression
when we are in bc, we don't have that
| | 04:10 | Unix command line issue.
| | 04:11 | But here we really need an expression to
see ah, there are three arguments here.
| | 04:16 | So we need those spaces.
| | 04:17 | Also because we are on the command
line, if we wanted to do something
| | 04:20 | like multiplication, 1122 times 3344,
this won't work because this has
| | 04:29 | special meaning in Unix. It's a wildcard.
| | 04:32 | Remember that?
| | 04:33 | So if I hit Return, comes up as oops,
| | 04:35 | I don't know what do you mean.
| | 04:36 | We have to escape that first.
Just like we had to escape spaces in the
| | 04:40 | file names, we need to escape it
to let the command line know, hey, I
| | 04:43 | mean the literal asterisk.
| | 04:45 | I'm not using this as a
wildcard. And then it works properly.
| | 04:48 | Okay, so that's the things you need to know.
| | 04:49 | Make sure that you put spaces there,
make sure that you escape characters that
| | 04:53 | the command line is going to
interpret as something different.
| | 04:56 | But you're going to see that's
probably the simpler calculator then
| | 04:59 | actually dropping into bc.
| | 05:01 | bc is really designed to switch into it,
to do lots of calculations over and
| | 05:05 | over, to sort of leave it running while
you're working at your workbench, which is
| | 05:09 | why it's a bench calculator, whereas
expression really just does a real simple
| | 05:13 | expression evaluation.
| | 05:14 | Okay, let's look at units.
| | 05:16 | units is another one that drops you into a mode.
| | 05:19 | It comes up and tells me how many units
it has in its library and then it asks
| | 05:24 | you have what, and that
you can put in a value here.
| | 05:28 | Let's say I have 1 foot, all right.
| | 05:30 | We are talking about a unit of measure 1
foot and what I want is meters and then
| | 05:36 | it comes back and it tells me that is
equal to 0.3048 meters. Then it gives me the
| | 05:44 | second line here which is
the reverse calculation.
| | 05:48 | That's what it would
take to go the opposite way.
| | 05:50 | Most times you can just ignore that.
| | 05:51 | It gives it to you because there are
times when the precision is actually better
| | 05:56 | in that second line than
it is in the first line.
| | 05:58 | Most of the time you can
just ignore that other line.
| | 06:01 | Then it pops up and it says all right,
you have what? And it asks again.
| | 06:04 | So let's just try a few more.
| | 06:05 | Let's say 1 inch into centimeters.
| | 06:09 | 1 inch is equal to 2.54 centimeters.
| | 06:12 | 5 gallons is equal to liters.
| | 06:18 | There we go and let's try 3 furlongs into miles.
| | 06:23 | Let's try 72 degF, degrees Fahrenheit,
followed by degrees Celsius and it turns
| | 06:33 | up and tells what 72 degrees
Fahrenheit is equal to in Celsius.
| | 06:36 | So when we finally want to get
out of this, there's no graceful
| | 06:38 | way, unfortunately.
| | 06:39 | We can't just type quit,
which just seems a shame.
| | 06:42 | Instead you would hit Ctrl+C to exit out of
it, just like you would force quit a program.
| | 06:46 | There is also a usage of units from
the command line if we put it in quotes.
| | 06:50 | So '2 liters' into 'quarts' and that
comes back and just returns it and then
| | 06:56 | it comes back to the command line, more like
the expression evaluator that we were just using.
| | 07:00 | That's a really handy tool.
| | 07:01 | It's nice to be able to just very
quickly drop into units and be able to figure
| | 07:06 | out the conversion that you're working with.
| | 07:07 | So you have this tool available
to you on your Mac all the time.
| | 07:10 | Just open up Terminal, type units and
voila, you can do all the conversions that
| | 07:14 | you are trying to figure out.
| | 07:15 | So I think all four of these
utility programs can be really useful.
| | 07:18 | So try and make yourself a mental note.
| | 07:19 | The next time I want to do some
calculations and the next time I want to do
| | 07:23 | some unit conversions, to switch into
Unix, and actually try them out and get
| | 07:26 | used to using these.
| | Collapse this transcript |
| Using the command history| 00:00 | In this movie, we are going to learn
how to utilize the Unix command history.
| | 00:04 | Early on in this training, we learned
how to use the up arrow to go back and
| | 00:07 | review commands that we had issued previously.
| | 00:09 | We can then either reissue those
commands or make edits to them.
| | 00:11 | But what you may not have thought about
is how does Unix remember those commands?
| | 00:15 | Because even if we close the Terminal
window, even if we shut down our Mac and
| | 00:19 | reboot it, Unix still remembers
what our previous commands are.
| | 00:22 | So it must be keeping track
of them somewhere, and it is.
| | 00:25 | It does it inside a file and that
file is inside our user directory, ls -la.
| | 00:30 | You can see it here.
| | 00:32 | It's a dot file, bash_history.
| | 00:34 | And this is where the bash shell
stores its history of commands.
| | 00:38 | We will take a look at
the contents of that file.
| | 00:40 | You will see that it
lists our previous commands.
| | 00:42 | It's just one line per command.
| | 00:44 | Now yours is probably a lot longer than mine.
| | 00:45 | I actually went in and edited
mine to make it a lot shorter.
| | 00:48 | I want you to notice something.
| | 00:49 | If we hit our up arrow now, our last
command was actually to look at the bash
| | 00:54 | history and the command before that
was to see what's in the directory and the
| | 00:57 | command before that was to change to
our user directory, but none of those are
| | 01:00 | being reflected in this bash history file.
| | 01:02 | That's because bash writes to the
bash history file whenever it quits.
| | 01:06 | So once we quit the sessions, close
out this window, it'll write our reset
| | 01:11 | commands to that bash history file,
and the next time we come in, bash will
| | 01:14 | read the bash history to reload those
commands and make sure that it has them
| | 01:18 | in memory ready to go.
| | 01:19 | Until then, until we quit, it just
keeps them and holds on to the main memory.
| | 01:22 | So, essentially these commands that we
see here in this list are the commands
| | 01:26 | that were issued prior to our current login.
| | 01:29 | If we want to the full list of
commands, then we need to issue the history
| | 01:33 | command. Just simply history
and then we will see a list.
| | 01:36 | What we are seeing here is a list of
all previous commands, those that were in
| | 01:40 | the bash history, plus the more recent ones.
| | 01:42 | Notice that the list is numbered.
We can use these reference numbers to do
| | 01:46 | that command again.
| | 01:47 | This is much easier if we have done a
whole lot of things than hitting the up
| | 01:50 | arrow over and over and over till you find
the command that you did a long time ago.
| | 01:54 | Then chances are if you're like me, you still
will end up using that up arrow a lot of the time.
| | 01:58 | There is something that
was done a long time ago, you know,
| | 02:00 | 50 commands ago, then just go to
your history and look for it this way.
| | 02:04 | So how do we reference these numbers?
| | 02:05 | Well, there are some shortcut
commands to work with the history and you can
| | 02:09 | recognize them all easily, because
they all start with the exclamation point.
| | 02:12 | Exclamation point followed by the number
of the command that we want to execute.
| | 02:16 | So for example, if we want to
execute the calendar command, it's just
| | 02:19 | !1, and there it is.
| | 02:21 | It runs it. Notice that it first tells
us what command it's actually doing and
| | 02:25 | then it goes ahead and does it.
| | 02:26 | Now, also I want you to notice, if you
hit the up arrow, that we don't see the !1 anymore.
| | 02:32 | That was just a shortcut, right.
What we actually see is the command itself,
| | 02:36 | which is really useful, because
then, for example, we can edit this.
| | 02:39 | So we wouldn't be able to edit it if we
didn't have the actual command itself.
| | 02:43 | So I can make an edit here.
| | 02:44 | I am just going to hit the
down arrow without hitting Return.
| | 02:47 | Okay, it's very important,
because I want to show you something.
| | 02:49 | So I have made a change to this
line. Hit the down arrow again.
| | 02:52 | Now let's type history one more time.
| | 02:53 | Notice now that it does show that command that I
just executed a moment ago, but it changed it.
| | 02:59 | Now it says Cal 6 2008. So hitting the
up arrow, making edits to command, it
| | 03:04 | does change them in the history.
| | 03:05 | And it includes an asterisk next to the
number to let you know that it was edited.
| | 03:09 | So even though that command never
ran, it's in our history that way.
| | 03:12 | All right, let me show you the next
shortcut, which is to use the exclamation
| | 03:15 | point and then a minus sign and then the
number of commands back that we want to go.
| | 03:21 | So for example, if I want to run a
command that was two commands ago,
| | 03:24 | that one command back would be my history.
| | 03:27 | Two commands back could be that cal command.
| | 03:29 | I am going to hit Return
and you will see what it does.
| | 03:31 | It actually did this command, Cal 6 2008.
| | 03:34 | Even though that's the
amended command, it still does it.
| | 03:39 | But the minus 2, you can do minus 3,
minus 4 whatever, it's always relative
| | 03:42 | to where you are now.
| | 03:43 | If I do minus 2 now, it won't
give me that cal again, right?
| | 03:47 | It's a moving target.
| | 03:48 | Now two commands back
would be the history command.
| | 03:51 | So it's relative to where you are, how
many commands back, but if you have done
| | 03:54 | something, you do one command, you do
two commands, you do three commands, you
| | 03:57 | do the fourth command, and say, I want
to do that, first one again, well, it's
| | 04:01 | real easy just to say all right, I
want to go back four commands and do that
| | 04:04 | command that I did a moment ago.
| | 04:06 | In most cases it's probably going to
be easier to use the absolute id value
| | 04:10 | instead of this relative one.
| | 04:11 | Another nice shortcut is that we can
actually use the exclamation point followed
| | 04:16 | by alphanumeric characters and it
will go back and try and find the command
| | 04:21 | that begins with those characters.
| | 04:22 | So for example, you see up here
that I have command number 10 is to
| | 04:26 | evaluate this expression.
| | 04:28 | If I type expr and then hit Return,
that was the most recent command I did
| | 04:33 | that began with expr.
| | 04:34 | Now I didn't actually have to type the
whole command. I could just type ex and
| | 04:37 | it would have done the same thing.
| | 04:39 | It again tells me what command it's issuing
and then gives me the results of that command.
| | 04:43 | That's a nice handy trick.
| | 04:44 | So let's say that you added a file
using nano and then you do a few other
| | 04:48 | things and then you say oh you know
what, I needed to edit that file again,
| | 04:51 | well, 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:57 | Keep in mind of course, if you move
around and change directories, then that
| | 05:01 | command may not work the way it used to,
because it's always being executed from
| | 05:05 | where you are in the file system.
| | 05:06 | So you might have to modify the path to the
file that you want to edit in order to get it.
| | 05:10 | There's another very useful shortcut
that I use all the time and that's to use
| | 05:14 | exclamation point, exclamation point.
| | 05:17 | This is the same thing as doing
exclamation point minus 1. Where it's most
| | 05:21 | useful is let's say we were doing a
chown and we are going to chown on unix files.
| | 05:27 | Inside there we had
a file called ownership.txt.
| | 05:31 | That in the ownership movie, we were
talking about changing ownership of that file,
| | 05:33 | but remember, operation
not permitted. I can't do that.
| | 05:37 | I need to do it with sudo in front of it.
| | 05:39 | So it's a classic use case. We say sudo!!.
| | 05:43 | Right? Saying no, I really mean it.
| | 05:46 | Do the same command, but
this time prefix it with sudo.
| | 05:49 | See how that works?
| | 05:51 | So it doesn't just execute the command.
| | 05:52 | It's actually a reference to the command.
| | 05:54 | I will use the up arrow, so I
can change that ownership back.
| | 05:57 | Notice that once again the command itself
has changed so that now sudo is in front of it.
| | 06:03 | We are actually seeing the actual
command itself, not sudo!!,
| | 06:08 | which is perfect for
a use case like this, where I want to go
| | 06:10 | back and edit the line and re-execute it again.
| | 06:13 | Another one that's really handy is
exclamation point followed by a dollar sign.
| | 06:17 | What that does is it references the
arguments from the previous command, so
| | 06:21 | let's say for example that I do nano
and let's go in to my unix files and I am
| | 06:27 | going to edit the file
fruit.txt. Here is my file.
| | 06:31 | I go in, I make some changes.
| | 06:33 | When I am finally done, Ctrl+X and I exit out.
| | 06:36 | Then let's say I go, you know
what, did I make change correctly?
| | 06:39 | I want to look at my changes.
| | 06:41 | I want to cat that file.
| | 06:42 | Well, I could type the whole thing again,
but I also can use exclamation point
| | 06:46 | dollar sign and what that references is
the arguments to the previous command.
| | 06:51 | So, not the nano part, but the
unixfile/fruit.txt. And there it is.
| | 06:56 | See, here is the command
that it actually executed.
| | 06:58 | It shows you that first.
| | 07:01 | That's what it did.
| | 07:01 | it grabbed just this last part
here and used it with the new command.
| | 07:06 | That's super handy because a lot of
times, you want to have a file and you want
| | 07:10 | to do several things to that file, right?
| | 07:12 | You might want to move it and then you
might want to copy it and then you might
| | 07:15 | want to edit it. You can just keep
using this to keep referring to that same
| | 07:19 | file without typing it over and over again.
| | 07:21 | You can read the man pages on history to see
some of the other things you can do with it.
| | 07:24 | I just want to give you a
couple of options that are helpful.
| | 07:26 | Let's say we have history and let's
say that I want to get rid of a line.
| | 07:30 | Let's say that this line 27 that's here.
| | 07:32 | We 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:40 | You will see that it's gone
and everything shifted up one.
| | 07:43 | So the numbers are not absolute.
| | 07:45 | Each command is not completely bound to
these numbers, but if we delete it, they
| | 07:48 | shift up so that it doesn't leave a space.
| | 07:50 | And if we want to clear all of
our history, you can use history-C.
| | 07:53 | That will clear it. Or you can just get
rid of that bash history file. You can
| | 07:57 | just remove the file.
| | 07:59 | So just so you have a quick reference,
here is a chart that shows where all of
| | 08:02 | those shortcuts are, just so you
can see them and refer to them.
| | 08:05 | Make sure that you understand what
they all do, and try and get used to using
| | 08:08 | them, because they will make life easier.
| | 08:10 | You may feel a little odd at first, but
if you get used to using them, I think
| | 08:13 | you will find that they really
speed things up for you a lot.
| | Collapse this transcript |
|
|
6. Directing Input and OutputStandard input and standard output| 00:00 | In this chapter we're going to be
talking about how we can modify the input and
| | 00:04 | output that Unix uses and before we use
do that we first need to understand the
| | 00:07 | concept of standard input and standard
output and you may already be able to
| | 00:11 | guess what these standard input
and output are if you think about it.
| | 00:14 | The input is the keyboard.
| | 00:15 | That's its main way of getting input into Unix.
| | 00:18 | It's the things that you type on the
keyboard and the way of getting information out,
| | 00:22 | standard output, is typically the
text Terminal, the window, the screen that
| | 00:26 | you're seeing inside Terminal.
| | 00:28 | So that's where it outputs its results to.
| | 00:30 | So when we type a command, we type in the
command, we hit Return, that's the input.
| | 00:34 | It does its process and at the end of it,
it outputs the results to the screen
| | 00:38 | so that we can see it.
| | 00:39 | What we're going to be
doing is changing each of those.
| | 00:41 | I also want to mention the common
abbreviations for standard input are just
| | 00:46 | stdin and for output its stdout.
| | 00:48 | So if you see that, that's what it is
referring to, is standard in and standard out,
| | 00:52 | and there are actually sort of
special files on Unix that correspond to
| | 00:56 | these and that's inside that dev
folder. dev is where information about
| | 00:59 | different devices stored.
| | 01:01 | So inside dev there is a stdin and
stdout, which are files that are really sort
| | 01:06 | of references to these two things,
to the keyboard and to the Terminal.
| | 01:10 | What we want to do is figure out how
we can work with commands so that we can
| | 01:14 | change the input or the output.
| | 01:17 | So let's start by looking in the next
movie at how we can direct the output not
| | 01:20 | to the screen, but instead to go into a file.
| | Collapse this transcript |
| Directing output to a file| 00:00 | In the last movie I told you that the
standard output in Unix is going to be to
| | 00:04 | the screen and we can see that.
| | 00:06 | Notice that I'm inside my user
directory, inside Unix files and I'm just
| | 00:10 | going to type sort and fruit.txt. This
is the command we did before, just sorts
| | 00:14 | this list of fruit.
| | 00:15 | Now I didn't actually change the file
itself, all right. Sort just took it and
| | 00:21 | the output of that command was to my screen.
| | 00:23 | Not back to that file. Just simply
output it to the screen and that's what
| | 00:26 | we're seeing there.
| | 00:27 | What we want do now is change the output.
| | 00:30 | We want it to go somewhere else.
| | 00:31 | The way we do that as we say sort
fruit.txt and then after that we use the
| | 00:38 | greater than sign or an arrow pointing
to the right. Essentially that's the way
| | 00:42 | we're directing the output and we want
to direct it to a filename and the file
| | 00:47 | doesn't have to exist already.
| | 00:48 | We're going to create a
new file, sorted_fruit.txt.
| | 00:52 | So that's it. That's all there there is to it.
| | 00:54 | We simply say our instead of sending
this information to the screen direct it
| | 00:58 | to sorted fruit.txt. Hit Return, and I
don't get any output because the output
| | 01:03 | didn't come to my screen anymore but
if I do ls -la we now see that there's
| | 01:08 | this sorted_fruit file and if we take a
look at that sorted_fruit file there it
| | 01:12 | is, there is my output.
| | 01:13 | So extremely useful, especially for
something like sort like that. Then we
| | 01:17 | actually have the sorted version of
the file and now we can do something like
| | 01:20 | call unique on that file right.
| | 01:23 | Before we saw the unique didn't work
unless the results were sorted . Now we have
| | 01:27 | the ability to do that.
| | 01:28 | Now we could save this unique
version if we wanted, right.
| | 01:30 | Go ahead and try that your own. Save it
to something called unique sorted_fruit.
| | 01:35 | So any command that we typically get
output back to the screen we can actually
| | 01:39 | have be directed to a file. So for
example let's say "Hello World" right.
| | 01:43 | echo, and instead echo coming back to me let's
put that in the file, right, hello_world.txt.
| | 01:50 | It did it. We did do ls -lah, right.
We saw that command before. Normally that
| | 01:56 | returns our directory listing.
| | 01:57 | Let's save that to our file, content.txt.
| | 02:00 | Let's try history. We just saw how to
work with history. Do recent history.txt.
| | 02:06 | So you can see when I just do ls -la
you can see that all these files have now
| | 02:10 | been created and you could do a cat on
any of them and you'd see the content
| | 02:14 | and it's exactly what would've come your
screen but now it's going to a file instead.
| | 02:18 | Let 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:28 | Now notice we already have an existing
file called hello_world.txt, which just
| | 02:33 | contains the text hello world.
| | 02:35 | So what do you think what we'll get when we
take a look at that file now? It's very long.
| | 02:39 | Well let's scroll up to the top of it
just to show you that it completely
| | 02:42 | replaced what was there, right.
| | 02:44 | It is destructive, so it does overwrite.
So you're essentially saying make this output
| | 02:51 | completely take over this file.
| | 02:53 | All right, we're going to learn how to
append to our file in the next movie, to
| | 02:56 | add more content to it.
| | 02:58 | What this does is replaces it, okay.
So very important, make sure that you
| | 03:02 | understand that otherwise you might
accidentally target a file that you don't mean to.
| | 03:06 | Let me just show you a classic use case
for this which is, if we do, let's say
| | 03:10 | we have two files here and I
have a file called new file.txt.
| | 03:15 | It just has a couple lines in it, and
I have a another version of it called
| | 03:18 | newer_file.txt, right. Different text to
that. Notice the lines that are in each one.
| | 03:24 | Cat you remember concatenates, right.
| | 03:26 | So new_file.txt concatenated with
newer_file.txt and let's put the output
| | 03:32 | to joined.txt, okay. So there it is.
| | 03:37 | It outputs those two concatenated
together and directed the output to this
| | 03:41 | file called joined.
| | 03:42 | Now if we take a look at joined,
you can see that we get both of those
| | 03:45 | together in one file.
| | 03:47 | And as I noted before, of course if we
go up and we change the order of those,
| | 03:51 | let's edit this so that we take
new and newer and reverse them.
| | 03:56 | Now if we do cat joined you'll see
that it flipped the order of them and
| | 04:01 | replaced the old one. The old one is completely
gone. So again it overwrote that one destructively.
| | 04:06 | If we instead want to append the file,
to add to it, we need to do something else
| | 04:10 | and that's what we'll see
how to do in the next movie.
| | Collapse this transcript |
| Appending to a file| 00:00 | In the last movie we saw how we can
redirect outputs, so that instead of being
| | 00:04 | output to standard out which is our screen,
| | 00:06 | instead it's directed to a file.
| | 00:08 | In the process of doing that we saw that it
actually overwrites the contents of the file.
| | 00:12 | So we completely destroy whatever
was in the file by doing it that way.
| | 00:16 | What we're going to see now is how we
can append to the end of the file.
| | 00:20 | So we are still directing the output to
a file, but we're just doing it without
| | 00:24 | destroying what was already there.
| | 00:26 | Notice 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:34 | created in the last movie.
| | 00:35 | Let's begin by creating a list, nano
people.txt. Let's just put a list of
| | 00:41 | people's names in here, Lynda,
Bob, Susan, Larry, and Anne.
| | 00:48 | I'm going to hit Return. I want to show
you something that nano does here. If I
| | 00:51 | hit the down arrow you'll see it
actually gives me one extra line.
| | 00:54 | So there is an automatic sort of implied
extra line there. I'm going to remove that, okay.
| | 00:58 | So just make sure that we have only
one line return after Anne then I'll hit
| | 01:02 | Ctrl+X. Yes save the changes to people.txt.
| | 01:06 | So let's take a look, people.txt,
and there is the list of people.
| | 01:10 | Now 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:18 | it'll completely erase our old list.
| | 01:20 | What we want instead is to
append it to the end of the list.
| | 01:23 | It's very easy. We just use
two of those greater than signs.
| | 01:27 | Append it to the end of the
list instead of getting rid of it.
| | 01:30 | So now it echoes that line there. If we
now say cat people.txt, now we see Claire
| | 01:35 | at the end of the list.
| | 01:36 | I want to add John to the list,
same thing, John, and add it to people.txt.
| | 01:42 | Now John is added to the list.
| | 01:43 | That's it, that's all there is to it.
| | 01:45 | Let's just try another example. We were
working with a new file.txt and we were
| | 01:51 | concatenating it before with
newer_file and putting in a joined.
| | 01:55 | I'm just going to put new file in there,
so that replaces whatever was there.
| | 01:59 | Now let's do it again, but this time
let's do newer file and append it. Notice
| | 02:06 | the difference between those two. I'm
taking a different file and I'm appending
| | 02:09 | it to the end what was there.
| | 02:11 | That's the same thing as if I
concatenated those together. And then just let's go
| | 02:15 | ahead and do new_file in
there one more time, okay.
| | 02:18 | So you can probably guess what we'd
expect to be in there. If we take a look at
| | 02:22 | that file, we have new_file then
newer_file, then new_file, okay.
| | 02:27 | So it's really all there is to it.
It's pretty easy as far as the concept goes.
| | 02:30 | The hard part is just making sure that
you have the discipline to remember to
| | 02:34 | use the greater than sign, just one of
them, when you want to completely replace
| | 02:38 | the content and two of them
when you want to append it.
| | Collapse this transcript |
| Directing input from a file| 00:00 | Now that we've seen how to change the
standard output to go to a file, I'd like
| | 00:04 | to see how to direct input from a file
that is instead of using our standard
| | 00:08 | input which would be our keyboard,
the things that we type into the command line,
| | 00:12 | we are going to use input from a file.
| | 00:15 | Notice that I'm inside my User
directory and I'm inside unix_files.
| | 00:19 | These files are in the Exercise Files as well.
| | 00:21 | The one I'm going to be using
to start with is this fruit file.
| | 00:24 | You will remember what's in there.
| | 00:25 | It's just a list of fruit, all
right, a bunch of unordered fruit with
| | 00:29 | duplicates in there as well.
| | 00:30 | So what I'm going to do is I'm going to use
the sort and we saw that we could do fruit.txt.
| | 00:35 | Essentially what this is doing when
we call that is providing as argument
| | 00:40 | fruit.txt which the sort function
then uses as its standard input.
| | 00:45 | So it's already doing what we're
talking about for us, but I want to show you
| | 00:49 | the way that we can do it all the time,
sort of universally for any command
| | 00:53 | that takes input, even if the input is
just from the keyboard. We can then use
| | 00:57 | this less than sign.
| | 00:59 | That's the opposite of what we used for
output. It's facing the other direction,
| | 01:02 | pointing to the left.
| | 01:03 | What we are saying is take this
fruit.txt file, use that as your input, and send
| | 01:09 | it to the sort command.
| | 01:11 | Use it as if we had actually typed those
commands in there. And you will see that
| | 01:15 | it gives us back through the results.
| | 01:16 | Now those results are still coming to our
screen. We are not redirecting those right now.
| | 01:20 | We are focused just on directing the input.
| | 01:22 | Let's try another one, wc <
lorem_ipsum.txt. So there you see.
| | 01:27 | Now it's using the contents of that
and passing it into wc to count it.
| | 01:32 | Notice that it doesn't show the file
name anymore, which it did before when we
| | 01:35 | called it the other way, right.
| | 01:37 | When we call it this way,
it actually uses that file name.
| | 01:41 | So this is providing the file as an argument.
| | 01:44 | This is providing that file as input
that's being passed to this command.
| | 01:49 | It is a subtle difference.
| | 01:50 | Let me show you one way that
maybe a little more obvious.
| | 01:52 | Let's say that we echo, a
mathematical expression, (3*4) + (11*37).
| | 02:01 | Okay, so I've just got a simple
expression and I'm going to output that to a file.
| | 02:04 | I'll call it calculation.txt.
| | 02:06 | We know how to redirect our output.
| | 02:08 | Now I'm going to use bc, the
calculator that we used in the last chapter.
| | 02:12 | Before we used it from the command
line, but instead I'm going to say hey,
| | 02:16 | instead of using what I type, instead
take the contents of this file and pass
| | 02:21 | those into bc, and bc happily takes
those and returns a result to us.
| | 02:26 | So I think it's a pretty
straightforward thing, but there are two important
| | 02:28 | points that I want to make sure you understand.
| | 02:30 | One is that we can both
redirect the input and the output.
| | 02:33 | So let's say for example, if I do
sort < fruit.txt, the output right now is
| | 02:39 | going to come to my screen.
| | 02:40 | If instead I want it to go somewhere
else, well then I need to direct it as well,
| | 02:44 | sorted_fruit.txt.
| | 02:47 | Okay, see what it's going to do?
| | 02:50 | The input is the fruit.txt;
the output is the sorted_fruit.txt.
| | 02:55 | It's the same thing as if we
actually had sort and we piped in fruit.txt)
| | 03:03 | > sorted_fruit.txt.
| | 03:05 | All right, that works exactly the same.
| | 03:06 | It has the exact same results.
| | 03:08 | The parentheses aren't necessary though
because we always put the input first.
| | 03:13 | Right, it's always the input, then the output.
| | 03:16 | So I just wanted to see that you can
use them together and also that the input
| | 03:19 | always needs to come before the output.
| | 03:21 | I also want to make another point here that
the arguments to these always have to be files.
| | 03:26 | Okay, it's very important.
| | 03:27 | We're working with files
when we talk about this.
| | 03:30 | You can't for example do
sort < cat fruit.txt, right.
| | 03:36 | That's going to read the file?
| | 03:37 | No. We want the file itself, not what we
get when we read the file piped into it.
| | 03:41 | It says no such file or directory is cat.
| | 03:43 | It's expecting a file or a
directory to be passed in there.
| | 03:46 | Same thing if we say lorem_ipsum.txt
piped into wc as output, right. This won't
| | 03:56 | work either because in this case it
expects it to have output and the file on
| | 04:00 | its own doesn't have any output.
| | 04:01 | We have to cat the file to have output
and then we need to pipe it into a file.
| | 04:07 | The receiver has to be a file.
| | 04:08 | If I just do wc, not word count, that's
actually a file called wc and it happily
| | 04:13 | does it. ls -la and there we see we
have a file called wc at the bottom.
| | 04:17 | Okay, so I just want to make sure that
you understand the thing going in using
| | 04:20 | the less than sign needs to be a file
and the thing that's coming out of the
| | 04:24 | greater than sign needs to also be a file.
| | 04:26 | All right, so let's go ahead and remove that.
| | 04:28 | The last thing, let's just show
you if we use unique case an example.
| | 04:32 | Let's take our sorted_fruit.
txt now and let's output it to
| | 04:35 | unique_sorted_fruit.txt.
| | 04:39 | Okay, so that works and it should have
sorted it. cat unique_sorted_fruit.txt.
| | 04:44 | So we went through several steps to get
that sorted_fruit doing it this way, right.
| | 04:48 | And we know that there is an option for
sort that would let us do all in
| | 04:52 | one step and make it unique.
Certainly could use that.
| | 04:54 | But what I am interested in really
making point about is the way that we sort of
| | 04:58 | use these files as intermediaries, right.
| | 05:01 | We took a file, we sorted
it, we saved it to a file.
| | 05:04 | Then we take that sorted file, we
passed that into unique, and we saved that
| | 05:07 | off as another file.
| | 05:08 | It would be great if instead we could
take the output from one command and send
| | 05:13 | it straight to another
command without a file, right.
| | 05:16 | We can't do that with these operators
because these operators are expecting this
| | 05:20 | and this to both be files.
| | 05:22 | Instead we need another tool to do that.
| | 05:24 | We call that the pipe.
| | 05:25 | That's what we will look at in the next movie.
| | Collapse this transcript |
| Piping output to input| 00:00 | Up until now we've been seeing how to
direct input and output, but we've been
| | 00:04 | doing it to files and from files.
| | 00:07 | So we have to have these files as sort
of the intermediary step between them.
| | 00:11 | What would be great as we could actually
take the output from a command and use that
| | 00:15 | as the input into another command,
without having to go to a file in between.
| | 00:19 | It's something that you are
going to use all the time in Unix.
| | 00:22 | In fact you'll probably use it more often
that you will either of those file redirects.
| | 00:27 | To do it, we use
something that we called the pipe.
| | 00:29 | I'm just going to type one just
so you see what it looks like.
| | 00:31 | We hold down the Shift
key and then the Backslash.
| | 00:34 | That's the key that is above the Return key.
| | 00:37 | You get this upright pipe.
| | 00:38 | So that's what I refer to whenever you
hear me say pipe and typically we say we
| | 00:42 | pipe something into something, right?
| | 00:45 | We use it as a verb as well.
| | 00:47 | So if we have something like echo
"Hello World", we know that would
| | 00:51 | normally output to our Terminal, but if
instead we use the pipe, we can redirect
| | 00:56 | that into another Unix command.
| | 00:59 | Don't use a file here.
| | 01:01 | A file is used with those other operators.
| | 01:03 | Here we can use another command.
| | 01:04 | Let's just do wc as a simple one.
| | 01:05 | So Word Count comes up and it reports to me
| | 01:08 | there is one line, two words, 12 characters.
| | 01:11 | You see what it did?
| | 01:12 | So it took the output from one command,
piped it into the other one, so that it
| | 01:16 | received it as input and
then that's what it uses.
| | 01:19 | Let's take another example, let's
say echo, let's put in our mathematical
| | 01:22 | expression we used before, (3*4) +
(11*37), and let's pipe that into bc, right.
| | 01:32 | Before, remember I saved it to a file
and then I took that file as input to bc.
| | 01:37 | Now I'm just saying all right, take
this directly and put it into bc, right.
| | 01:41 | It gives me back a result.
| | 01:42 | I piped this output from echo into bc.
| | 01:46 | If we take our fruit file, remember
I'm inside my unix_files directory, right
| | 01:51 | here, ls -la. I have this file
fruit.txt that we've worked with a few times.
| | 01:55 | Let's cat that fruit file.
| | 01:58 | It's just a list of fruit in an
unsorted order and let's pipe that into sort.
| | 02:03 | Notice before I was taking the file
and sending it to sort. Now I'm actually
| | 02:07 | outputting it, right. This is
generating output and that output is what's being
| | 02:11 | sit in their, okay. Ot's a subtle
difference, but it is different and you see we
| | 02:15 | get back this result.
| | 02:16 | Let's not take that and
let's pipe that through unique.
| | 02:19 | So now I get the unique version.
| | 02:21 | Now we've seen a couple of different
ways that we can do this, that we can sort
| | 02:24 | them and make them unique.
| | 02:25 | Sort has an option that does it.
| | 02:26 | We can take it from sort into a
file and then a file back into unique.
| | 02:30 | I wanted you to see what we're doing here.
| | 02:32 | We're piping it along from one command to
another, so we get a string of commands.
| | 02:36 | The first one is catting the file,
then we're sorting it, then we're doing
| | 02:39 | unique and at the end of all that,
we could actually output it. Let's say
| | 02:43 | unique_sorted_fruit.txt.
| | 02:45 | I already have this file, so it's
going to overwrite it. There it goes.
| | 02:48 | So now all of that went through all
those commands and at the end of it then I
| | 02:52 | directed the output to a file.
| | 02:54 | See how that works?
| | 02:55 | It's really, really useful.
| | 02:56 | You could also do something similar to
what we did before where we could say
| | 02:58 | well, sort should get its
input from fruit.txt, all right.
| | 03:03 | We know what that's going to do, right.
That does that and then we could take
| | 03:06 | that and we could pipe that into unique.
| | 03:09 | We know what that does, right, and then
we could take that and we can send that
| | 03:13 | out to unique_sorted_fruit, but
that's a little less readable.
| | 03:16 | If you just compare the two, I
think it's a lot easier to understand
| | 03:19 | what's happening here.
| | 03:20 | We're reading the file, sending it to
the sort, sending it to unique, then it
| | 03:23 | ends up in the file.
| | 03:24 | The other one, you have to jump
back and forth to read a little bit.
| | 03:27 | So, both of them accomplish the exact
same thing. I just think that the second
| | 03:31 | one is perhaps a little more readable.
| | 03:33 | Another place where this is really
useful is we have a long file like our
| | 03:37 | lorem-ipsum file, right. It's very long.
| | 03:39 | We can pipe it into less. There we go.
| | 03:42 | Now we are in less.
| | 03:43 | We can go page by page.
| | 03:45 | Now of course that's the same thing.
We could do less lorem-ipsum.txt and that
| | 03:48 | gives us the same results. This takes a
file as its argument, but for something
| | 03:53 | like ps aux, now that
doesn't take a file as an argument.
| | 03:57 | That's the processor output that we saw.
| | 03:59 | So we are seeing a list of all
our processes piped through less.
| | 04:03 | Guess what? Now we get pagination.
| | 04:04 | We can go through them page by page and we
can use forward and back to look at those.
| | 04:08 | All right, there is a very common
usage of it is to take something that we're
| | 04:11 | working with and pipe it into less.
| | 04:14 | We will be using this type a lot
especially when we get to the chapter on the
| | 04:17 | Unix Power Tools where we look
at a lot of the advanced features.
| | 04:19 | We will be able to use
this pipe to great effect.
| | 04:22 | So the main thing to remember with
pipe though is that unlike the other
| | 04:25 | operators, we're piping it into a
command, not into a file, into a command and
| | 04:30 | the output from it should be the
output from a command, not from a file.
| | 04:35 | Even here this is actually
output of a command, not a file.
| | Collapse this transcript |
| Suppressing output| 00:00 | Now that we've talked about the basics
of redirecting input and output,
| | 00:03 | there is one last redirection
technique that you should know about.
| | 00:07 | Instead of redirecting output to a file
or into a command, what if we want to
| | 00:10 | have no output at all?
| | 00:12 | This is especially useful when you
become advanced enough that you're able to
| | 00:15 | setup scripts on a server that run
automatically at 4 a.m. even when there's not
| | 00:19 | anyone logged in to view the output.
| | 00:21 | At that point you don't need the output,
right. You just want it to just do this
| | 00:25 | thing and the output should just go away,
and there may also be occasions where
| | 00:29 | you want to run a program or you just
don't want to see the output. You just
| | 00:32 | want the program to do its
thing and get its results.
| | 00:35 | Well we could just put the output to a
file and then throw away the file, but
| | 00:39 | there is an even easier way to do it.
We can redirect the output to a special file
| | 00:43 | that referred to as the null device.
| | 00:46 | It's also sometimes called the bit bucket
or the black hole, and it lives at /dev/null.
| | 00:49 | Do you remember I told you that there
were the special files for input and
| | 00:53 | output that were represented at dev
/standard in and dev/standard out?
| | 00:57 | Well, dev/null is another one
that's in that same folder.
| | 01:00 | It's a special device where anything
that's directed there Unix just discards.
| | 01:05 | It just like a black hole. We just send
data there and Unix says "Oh, yeah okay,
| | 01:09 | never mind." It's gone.
| | 01:11 | It's not kept in memory.
| | 01:12 | It's not kept in the file.
| | 01:13 | It just disappears.
| | 01:14 | And the way you send things to dev/null is
the same way that you send them to any file.
| | 01:19 | So for example let's just take a simple
listing right, ls -la. Let's send that
| | 01:23 | to dev/null, always at the root, all
right,. Always we have to make sure we
| | 01:28 | specify that so it knows the absolute
path dev/null. We get no output.
| | 01:33 | cat lorem_ipsum, let's send that to dev/
null. All right, there is nothing there.
| | 01:38 | If we want to bring in nothing, let's
bring in nothing from dev/null, all right.
| | 01:43 | It echoes back nothing to us.
| | 01:46 | So it works just like a file.
| | 01:48 | It's just like a special file, but it has
this unique property of always staying empty.
| | 01:53 | Now in truth you may not have need for
this very often but it is an important
| | 01:57 | concept I think for you to know, and so
I want you to recognize it whenever you
| | 02:00 | come across it in your future Unix
endeavors, so that you know how you can have
| | 02:04 | something that has absolutely no output.
| | 02:06 | The way you suppress output is
you direct the output to dev/null.
| | Collapse this transcript |
|
|
7. Configuring Your Working EnvironmentProfile, login, and resource files| 00:01 | In this chapter, we're going to take a
look at how we can configure our working
| | 00:04 | environment in Unix, or put another way,
how can you customize bash to your
| | 00:07 | personal preferences?
| | 00:08 | Well, the way we'll do that is
providing commands inside bash resource files.
| | 00:13 | Let's take a look at those files and
understand how bash works with them.
| | 00:16 | First, let's talk about the initialization
that bash goes through whenever we first log in.
| | 00:21 | Now on a Mac, logging into bash is the
same thing as opening up a Terminal window.
| | 00:26 | You open up a new Terminal window and
it logs us into the bash shell, right?
| | 00:30 | It auto logs you in as the
user that you are in the Finder.
| | 00:33 | Well, when it does that, the first
thing that bash does upon login is it reads
| | 00:36 | any commands that are inside the
directory etc inside the file profile.
| | 00:41 | Those are master default
commands that every bash user will get.
| | 00:45 | We don't want to mess with
those. Leave those alone.
| | 00:47 | Those are default settings.
| | 00:48 | Let Apple take care of
managing those for you, all right.
| | 00:51 | That's the defaults all users get.
| | 00:53 | Instead, we want to take care of just
personal customizations that apply only to us.
| | 00:57 | So because these are our personal
customizations, they are going to live
| | 01:00 | inside our user directory.
| | 01:01 | So notice that I've got four file paths there.
| | 01:04 | Each one begins with that tilde
indicate that it's in the user directory.
| | 01:07 | Also notice that each of
them begins with a period.
| | 01:10 | These are dot files.
| | 01:11 | They are not visible in the Finder and
the dot is to indicate that it's really
| | 01:14 | a configuration file.
| | 01:16 | So we have four choices.
| | 01:17 | We have .bash_profile, .bash_login,
.profile, and .login, and what bash does
| | 01:21 | is after finishes reading that main
profile, it goes to our user directory and
| | 01:25 | it sees which one of these four files it
can find in the order that I have them listed.
| | 01:30 | When it finds one, it reads the
commands that are in there and then it
| | 01:33 | ignores the rest of those.
| | 01:35 | Okay, so it just goes finds one
and only executes one file, so there is
| | 01:39 | no point in having both
.bash_profile and .bash_login.
| | 01:43 | Any commands that are in .bash
_login will just get ignored.
| | 01:45 | So 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:53 | are there for historical and
backwards compatibility reasons and they also
| | 01:57 | would potentially apply to
working in other shells as well.
| | 02:00 | But the things that we put in these
configuration files are often very shell specific.
| | 02:05 | So the things we put in there, the
syntax that we use, might only apply to bash.
| | 02:09 | So if we use other shells, we're really
better off using their profiles instead.
| | 02:13 | So let's put everything inside .bash_profile.
| | 02:16 | Now that only covers when we login to a
new shell by opening a new Terminal window.
| | 02:20 | If from the command line we type the
word bash, it opens up a new bash shell,
| | 02:26 | but it's not a login shell.
| | 02:28 | There is a distinction between that.
| | 02:30 | The login shell is the first time
that we come to it when we open up that
| | 02:33 | Terminal window. Typing bash on the
command line opens up a sub-shell, and
| | 02:37 | whenever we start a new bash sub-shell
then it executes the commands that
| | 02:41 | are inside .bashrc.
| | 02:42 | RC is to indicate resource.
| | 02:46 | So here is the thing.
| | 02:47 | Anything that we put in .bash_profile
doesn't become available to us in the sub-shell.
| | 02:53 | Anything we put into .bashrc isn't
available to us until we go into a sub-shell.
| | 02:58 | So we're going to want to have a
way so that we can have one set of
| | 03:00 | commands that are available to us in
both cases, right, because we want the
| | 03:03 | same configuration regardless of
whether in this master shell or whether
| | 03:07 | inside a sub-shell.
| | 03:08 | Now you may not be in a sub-shell very
often, but it's still going to be a good
| | 03:12 | idea to set it up the way
that I'm going to show you.
| | 03:15 | Before we do that, let me also just
mention that there is one last file that
| | 03:18 | is the logout file.
| | 03:20 | Whenever we log out, it will run any
commands that are inside .bash_logout.
| | 03:26 | Okay, so those are going to be the logout files.
| | 03:28 | If we want to perform specific actions
as we're logging out, we can do them here.
| | 03:32 | Now as I said, this is the load order for
the bash shell, which is what we're working in.
| | 03:36 | If you want to work in other
shells, you can go to Wikipedia.
| | 03:39 | They have a nice explanation of the
load order for all those different shells.
| | 03:42 | So you'll be able to figure out
exactly where you have to put your
| | 03:45 | configurations for those shells.
| | 03:46 | Okay, so now how do we handle this problem
between the login shell and the sub-shell?
| | 03:50 | Well, it's a really easy solution.
| | 03:52 | What we'll have to do is just add a bit
of code to our .bash_profile that says
| | 03:56 | if you can find this .bashrc file,
well, then load everything that's in it.
| | 04:01 | This is really the only bash
scripting that we're going to do.
| | 04:03 | But I'm going to just give it to you,
Say if the file exists,--that's what's
| | 04:07 | inside those square brackets is a
shortcut for whether the file exists or not.
| | 04:11 | Then read in the .bashrc file. That's it.
| | 04:15 | That source is just saying all right.
| | 04:16 | Let's execute whatever is in there.
| | 04:18 | So we'll put this in our .bash_profile
and now all of our commands and all our
| | 04:22 | configuration can go in .bashrc.
| | 04:24 | Whenever we log in, it
will run that .bashrc file.
| | 04:27 | Whenever we switch to a new shell,
it will run that .bashrc file.
| | 04:31 | Okay, it's a very simple solution that
allows us to have the same configuration
| | 04:35 | applied to both cases. Let's try it out.
| | 04:38 | So here I am in my main user directory.
Make sure you're in the same place. ls-la.
| | 04:42 | You'll see that I have my bash history
file, but I don't have any of those other
| | 04:47 | configuration files that we just talked about.
| | 04:50 | So the first thing let's do, let's
create our .bashrc file. All right!
| | 04:54 | We can just do that with touch.bashrc.
| | 04:57 | Notice that there is no
underscore and its just bashrc.
| | 04:59 | Now we have that file.
| | 05:01 | It is empty, but the file exists.
| | 05:03 | Now let's create our .bash_profile.
| | 05:04 | nano .bash_profile.
| | 05:08 | There it is and inside nano what we're
going to do is just paste in that code
| | 05:12 | that I just gave you.
| | 05:13 | So pause the movie if you want to copy it down.
Make sure that you get the punctuation
| | 05:16 | and everything correct.
| | 05:18 | Notice also that this if statement ends with fi.
| | 05:21 | That's how we close an if statement, right?
| | 05:23 | So it starts with if and it ends with fi.
| | 05:26 | So make sure that you got that right.
| | 05:28 | That's not a mistake.
| | 05:29 | Then when you're done, we're going to save
that file and we'll save it to .bash_profile.
| | 05:34 | Now we can use that trick that we
learned in the history section just to take
| | 05:37 | a look at the file, see what's in there,
and we can also do the same thing for .bashrc.
| | 05:41 | You'll see that there is
nothing inside that file. All right!
| | 05:45 | But both of them exist.
| | 05:46 | Now we see both of them there.
| | 05:47 | Okay, so we're set up now.
| | 05:49 | Now we've set things up so that
the .bashrc file is our master file.
| | 05:53 | Now 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:05 | it paste in some more commands. All right! TO HERE
| | 06:07 | Now you can put whatever you want here.
| | 06:08 | These are all things we've covered before.
| | 06:10 | I've got echo in a new line.
| | 06:12 | then echo without making a new line.
| | 06:14 | Welcome to Unix on Mac OS X. and then
a semicolon for a new command, whoami,
| | 06:20 | so that I put my name.
| | 06:21 | and then another new line.
| | 06:23 | and then another blank line followed
by Today is the date and with the date,
| | 06:27 | I've provided a format that's just
string from Time formats, you can look
| | 06:31 | those up if you need reference for what all
the symbols mean, we'll see it in just a second.
| | 06:35 | And then I've got an echo.
| | 06:36 | and then I'm using Cal to display a calendar
that will show the current month. and then Uptime.
| | 06:41 | All right!
| | 06:42 | So, all basic things that we've
learned before, Ctrl+X, and yes, all
| | 06:46 | right, there we are.
| | 06:47 | Now let's quit this window and
let's open up a new one, all right?
| | 06:50 | So I'm going to just close this window.
| | 06:52 | I'll click up here in the red dot, and now Command
+N will open up a New window. Look what it did.
| | 06:59 | It came up and gave me all
those commands upon login.
| | 07:02 | If I type bash, I don't get them. All right!
| | 07:05 | I am in a new shell, now
but I'm not in my login shell.
| | 07:08 | They are only executed one time.
| | 07:09 | Let's just see the difference.
| | 07:11 | Let's go into .bashrc and let's put in
just the last part, that Uptime, right?
| | 07:17 | So whenever we login to a new shell,
we'll just report the Uptime to us followed
| | 07:21 | by a blank line and then keep going
with whatever it was doing. All right!
| | 07:23 | So let's exit out of that. Let's now go bash.
| | 07:27 | All right! There it is.
| | 07:29 | Now executes what's in that .bashrc file.
| | 07:31 | Let's go back and exit out of this again.
| | 07:33 | Let's close this window.
| | 07:35 | Let's open up a new one now.
| | 07:37 | It says yes, we do want to
close it, and here we go.
| | 07:41 | Notice now it gives me the Uptime twice.
| | 07:43 | We know why that is.
| | 07:45 | We do nano .bash_profile.
| | 07:49 | Now you can see that it's doing the
Uptime here and then it's doing it again
| | 07:53 | when it reads in the file here.
| | 07:54 | Okay, so that should help you
understand how these two things are working.
| | 07:58 | Incidentally, in these configuration files a
Pound sign indicates a comment. All right!
| | 08:03 | So that comments it out.
| | 08:05 | I can make all sort of comments here,
and I can say for example, at the top of
| | 08:08 | this, I put a comment that says, This
only runs on user login, and then I can
| | 08:18 | say, I'll just actually delete these lines.
| | 08:20 | Let's go ahead and just remove those and
say, This loads in the configuration in
| | 08:30 | .bashrc, put all configuration there.
| | 08:36 | So that's how we can do comments by
just using those Pound signs or hash.
| | 08:40 | So let's Ctrl+X out of there,
and now we can save that file.
| | 08:44 | Once again, we'll close that window and
open a new one and now you can see only
| | 08:47 | reports Uptime to us.
| | 08:48 | One time, I type bash, we get a new shell.
| | 08:51 | It just does the Uptime for us again.
| | 08:53 | Okay, now you can put
whatever you want in there.
| | 08:55 | I'm not trying to tell you
have to it exactly my way.
| | 08:57 | I just want to give you some ideas on
what's possible, and mostly to show you
| | 09:01 | which files to put your things in
and the sort of interplay that happens
| | 09:05 | between those two files.
| | 09:06 | Now that we understand where we should
locate our changes, let's look at what
| | 09:09 | other configurations are possible.
| | Collapse this transcript |
| Setting command aliases| 00:00 | In this movie, we'll see how we can use
command aliases to help us to work more
| | 00:04 | efficiently in Unix.
| | 00:05 | Now, I want to make sure that you
understand that the aliases that we're talking
| | 00:07 | about here are not the same
thing as the Finder aliases, right?
| | 00:10 | Remember that we can click on a file or
folder, we can go to the File menu,
| | 00:15 | we can pull down and select Make Alias.
| | 00:17 | That's a Finder alias for files and folders.
| | 00:19 | That's different than what we're doing here.
| | 00:20 | What we're doing here is command aliases.
| | 00:23 | Making the same kinds of shortcuts,
but shortcuts that will execute
| | 00:26 | different commands in bash.
| | 00:27 | The first command that we should
learn is just simply alias by itself.
| | 00:31 | This will return a list of all
the currently defined aliases.
| | 00:34 | So right now I have none.
| | 00:35 | You probably don't either.
| | 00:36 | So to create our first one we just use
the alias command again and then the
| | 00:41 | shortcut that we want to use,
essentially the alias name.
| | 00:44 | So the first alias I'm going
to create, I'm going to use ll.
| | 00:45 | I'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:53 | So what I'm saying now essentially is,
whenever I type ll on the command line
| | 00:57 | I'd like bash to execute ls -la for me.
| | 01:02 | You can see the savings that
that offers. ll Return.
| | 01:04 | There it does it.
| | 01:05 | Now, instead of typing six characters,
and I often mistype those six characters,
| | 01:10 | instead now I can just type a very
quick ll and I get that list back.
| | 01:14 | ll is a very, very common one.
| | 01:16 | In fact, a lot of Unix machines are
preconfigured to offer you that alias.
| | 01:19 | Now, we can create aliases with
absolutely any name that we want to perform
| | 01:23 | any task that we want.
| | 01:24 | You just want to be careful that you
don't use an alias name that is for an
| | 01:28 | existing Unix command.
| | 01:30 | We don't want to overwrite that.
| | 01:31 | So mv obviously is a move command.
| | 01:35 | We don't want to create an alias for
mv that would do something else, right?
| | 01:38 | Because then suddenly mv
would stop working for us.
| | 01:41 | So you can use the man pages or the
commands like which that we saw earlier to
| | 01:45 | see what the commands are, to make
sure that you're not accidentally
| | 01:47 | overwriting something.
| | 01:48 | Let's create another one now.
| | 01:49 | Let's do alias hello= and I'm
going to have it echo "Hello World!"
| | 01:57 | I wanted to show you this example
partly to show you that you can use aliases
| | 01:59 | for absolutely anything, but I also
wanted to show you the pairings of quotes.
| | 02:03 | It really doesn't matter whether we
use single quotes or double quotes on the
| | 02:06 | inside or the outside. The
important part is the pairing of these.
| | 02:09 | We want to make sure that the alias
command can tell what the entire string that
| | 02:14 | it's aliasing ought to be.
| | 02:15 | And that's what those single quotes indicate.
| | 02:17 | If we wanted to use double quotes in all
cases, we would just need to escape that
| | 02:20 | string by using the backslash in front of them.
| | 02:22 | So I just want to make sure
that I point that out to you.
| | 02:24 | So now we have that.
| | 02:25 | Now, if we type alias, you'll see that
we get the list of aliases that includes
| | 02:29 | hello and typing hello
does exactly as advertised.
| | 02:32 | It echoes back Hello World!
| | 02:34 | If we want to remove an alias,
then we just type unalias.
| | 02:37 | unalias hello, and now it's gone.
| | 02:40 | alias and hello, command not found.
| | 02:44 | So in addition to making sure that
you're not overwriting a real Unix command,
| | 02:47 | here is the catch about aliases though.
| | 02:49 | Aliases only last for the current login.
| | 02:52 | So ll, it's a valid command.
| | 02:54 | Close the window and open a new one, just
clear the screen. ll. Nope, command not found.
| | 03:00 | So we typed it in the command line.
| | 03:02 | It made an alias and that stuck
around only for the current login.
| | 03:05 | What we want is for those aliases to be
defined for us every time that we log in to Bash.
| | 03:11 | So where do we put commands that
we want to run every single time?
| | 03:14 | Well, we do those is our
configuration files, either in .bash_profile or
| | 03:18 | even better in .bashrc.
| | 03:20 | So we'll write our alias commands in there.
| | 03:23 | Whenever we log in to bash, whenever
we open a new shell, what will happen
| | 03:27 | is bash will read those commands,
define those aliases for us, and they'll
| | 03:31 | be available to us.
| | 03:32 | So let's do that. .bashrc, and we'll
just drop-down here and we'll just call the
| | 03:37 | alias command just like we did before.
| | 03:38 | Remember we did the cal
command, for example, before.
| | 03:41 | It's no big deal to write
our normal shell commands here.
| | 03:45 | la, and I'm going to go ahead and
add the h and g options to that.
| | 03:48 | Those are h for humanize and g
will just help to colorize the output.
| | 03:52 | Now, as I said, you can do
all sorts of variations on this.
| | 03:54 | You might have something that is la, that
does ls -a, or you might have lh that does ls -h.
| | 04:02 | Any combination you want. You can come up
with whatever suits your personal taste.
| | 04:06 | I'm going to just show you some more that
are common that a lot of people like to use.
| | 04:10 | We have cd into our home
directory as just simply home.
| | 04:14 | That will go to our user home.
| | 04:16 | Alias up, a lot of people like this.
Just cd to go into the parent directory.
| | 04:20 | That will go to up one.
| | 04:21 | And alias h, history, so that
will just simply show your history.
| | 04:27 | Now, again, you don't have to use
these, but these are just ones that I
| | 04:29 | frequently see other people use.
| | 04:32 | Another nice use of aliases is to
actually redefine some of the Unix
| | 04:36 | commands but with options.
| | 04:38 | Let me show you what I mean.
| | 04:39 | We say we wouldn't want to overwrite
something like mv, move, because we need that,
| | 04:43 | but we can overwrite it with options.
| | 04:45 | So whenever someone types mv, well,
it's actually the same thing as saying mv
| | 04:49 | with the interactive option.
| | 04:51 | Same thing for copy and remove, and
basically that interactive will just pop-up
| | 04:54 | and tell us before it actually
overwrites a file or deletes a file.
| | 04:57 | So that's a nice way to make mv, cp,
and rm sort of idiot proof. They will
| | 05:01 | always prompt you and say, are you sure
you want to do this before I destroy it?
| | 05:04 | We can also on the df and du commands,
we can provide the -h for humanize, and
| | 05:10 | for making a directory we can provide
the -p option and that will then make
| | 05:14 | sure that we create any
parent directories that we need.
| | 05:16 | Now, if you don't want to use any of
these, you can simply comment them out with
| | 05:20 | that hash or pound sign.
| | 05:22 | Another common thing that a lot of
Unix users like to do is sort of correct
| | 05:26 | their own typos ahead of time.
| | 05:27 | So if you find yourself that you
frequently type pdw instead of pwd, well then
| | 05:31 | you can make an alias.
| | 05:33 | No sense in wasting time correcting your typo.
| | 05:35 | If you find you do it often with sort
of bumble fingers, well then, fix it.
| | 05:38 | Make pdw equal to pwd, and you'll know
what those frequent misspellings are, right,
| | 05:42 | because they're going to be different
for every person, but I just wanted to
| | 05:44 | show you that's an example of the
kind of things you can use aliases for.
| | 05:47 | So now that we have all these in
our .bashrc file, let's try it out.
| | 05:50 | Ctrl+X. We'll save it into our .bashrc.
| | 05:54 | Now let's try one of those out.
| | 05:55 | Let's try that pdw that I
just created. Oh, command not found.
| | 05:59 | Now, why is that?
| | 06:01 | Because it hasn't read in
that .bashrc file yet, right?
| | 06:04 | We've got to close this window and then
it will open it. Otherwise we could also
| | 06:08 | type source .bashrc and
that would do the same thing.
| | 06:12 | That reads in that bash file,
now our pdw alias works.
| | 06:15 | So again, either close that window or
run source on that file so that it will
| | 06:20 | actually read in the contents of that
file to make those aliases available.
| | 06:23 | But now even when I close my window
and open a new one, now I can do ll and I
| | 06:27 | still get my directory listing and I
get all these items humanized and now my
| | 06:31 | entries show up in color.
| | 06:32 | So I think aliases become a really
indispensable tool in working with Unix.
| | 06:37 | For me personally, I've developed a
sort of mini library of aliases that I like
| | 06:41 | to use to help me do things faster
and I take those from machine to machine.
| | 06:45 | When I move to a new Unix environment,
I want to have my aliases available so
| | 06:49 | I can do things like type ll and get those
same results that I'm used to working with.
| | 06:53 | So I encourage you to experiment with
aliases and really try and build up some
| | 06:57 | aliases that work well for you.
| | Collapse this transcript |
| Setting and exporting environment variables| 00:01 | In this movie we're going to learn
more about environment variables which are
| | 00:04 | also known as shell variables.
| | 00:05 | We're going to do that so that we can
begin to use environment variables to
| | 00:08 | configure our working environment.
| | 00:11 | We've touched on shell variables already.
| | 00:13 | For example, we saw that we can have
echo $SHELL and that will return the
| | 00:17 | default login shell for the current user.
| | 00:20 | The shell variable in this case is
called SHELL and it's in all capitals.
| | 00:24 | The dollar sign is actually not
part of the shell variable name.
| | 00:26 | The dollar sign is there to serve as
an indication to Unix that we want it to
| | 00:30 | return the value that's
stored in the shell variable.
| | 00:33 | We can also define our
own shell variables as well.
| | 00:35 | For example, I can have MYNAME=
and then inside quotes put your name.
| | 00:41 | Then I could echo that back the same way.
| | 00:44 | echo $MYNAME, and it returns the
value that we've set inside that variable.
| | 00:48 | Notice that I did not use the dollar
sign when I set the value of MYNAME. I only
| | 00:52 | use it when I wanted to retrieve the value.
| | 00:55 | Just like the command aliases that
we were creating in the last movie,
| | 00:57 | variables that we set during the
current bash session will disappear once we
| | 01:01 | log out of that session.
| | 01:03 | And like aliases if we want those to be
defined all the time, well they need
| | 01:06 | to be stored in either our
.bash_profile or our .bashrc file.
| | 01:11 | As I said before, I think the
.bashrc file is the better choice.
| | 01:15 | So let's put those in there.
| | 01:16 | Notice I am inside my main user
directory. Open up that file.
| | 01:21 | You can put it really anywhere in here.
| | 01:22 | I am going to put it right here near the top.
| | 01:24 | So I'll just say MYNAME= and
then I'll put in Kevin Skoglund.
| | 01:30 | Now we'll exit. I'll save the file.
| | 01:32 | Now if we first just try echo $MYNAME,
we still have it saved from before.
| | 01:38 | To really test this, what we need to do
is actually close this window and open a
| | 01:42 | new one and then we can try it
again. echo $MYNAME, and there it is.
| | 01:47 | Now you can see that it's set all the time.
| | 01:48 | Every time we open up a bash session,
this variable will be defined for us.
| | 01:53 | So that takes care making it available the bash.
| | 01:55 | But there's one more thing that we need to do.
| | 01:57 | As I've shown it here, MYNAME won't
be available to the child processes
| | 02:02 | that bash starts for us.
| | 02:04 | It'll only be available in bash itself.
| | 02:06 | To indicate to bash it should also pass
along these variables or export them to
| | 02:12 | other commands, programs, and scripts,
| | 02:14 | well then we need to use the export command.
| | 02:16 | Let's go up to nano .bashrc again, and
then now we can just say export MYNAME.
| | 02:26 | And now bash knows that it also should export
it to all of the programs that it runs as well.
| | 02:32 | So every time it launches a program, it'll
make this MYNAME variable available as well.
| | 02:36 | Now you can see here I did it in two lines.
| | 02:38 | I set it in one line and
then I exported it in the other.
| | 02:40 | You can actually do both in the same line,
and I'll show you that in just a moment.
| | 02:44 | Now while it is moderately useful to
define your own variables, it's even more
| | 02:48 | useful to use variables to set some
configuration options for our Unix
| | 02:51 | environment and the programs that we use.
| | 02:53 | For example, if you read the man pages
for the command less, you'll learn that
| | 02:57 | there's an environment variable that
can be used to configure the default
| | 03:00 | settings that less will use when it starts up.
| | 03:02 | So to do that we'll just use export
LESS= and then inside quotes we'll put
| | 03:09 | whatever options we want LESS to use.
| | 03:12 | Notice first of all that I also did the
exporting and the setting all on one line.
| | 03:16 | Before I did it on two lines.
| | 03:17 | Here I am doing it in one.
| | 03:18 | export LESS= and then we can use options.
| | 03:21 | So we could use the m, the lowercase
m. That will give us a medium verbose
| | 03:26 | prompt every time we start up LESS or
we could use capital M. That would be the
| | 03:29 | maximum verbose prompt, right,
the longest prompt possible.
| | 03:33 | We can put capital N. That
would give us some line numbers.
| | 03:37 | You can go through the man pages for
LESS and decide what options you want.
| | 03:40 | I am just going to use the -M option here.
| | 03:42 | So I am going to use Ctrl+X to exit,
and I am already exporting it so that
| | 03:47 | means it will be available not just in
bash but inside LESS, because LESS is a
| | 03:51 | program that bash starts.
| | 03:53 | If you want you can play with it.
| | 03:54 | You can try taking away the export and
you'll see that it doesn't get passed along.
| | 03:58 | Now if we just do right here
echo $LESS, we don't have a value.
| | 04:02 | That's because bashrc has not
been run since we made that change.
| | 04:06 | To do that we either need to close
the window and reopen it, or we can
| | 04:09 | call source .bashrc.
| | 04:12 | That will actually execute the program.
| | 04:15 | And now if we go up and say echo
$LESS again, you see that value is there.
| | 04:19 | Let's test it out. Let's actually call
less on inside of our unix_files. We have
| | 04:24 | that lorem_ipsum.txt file.
| | 04:26 | Let's call it on that.
| | 04:28 | And you'll see now I get that
maximum verbose prompt at the bottom.
| | 04:32 | So that's the default option
being used every time I call less.
| | 04:36 | So this is the way that you do it.
| | 04:37 | You can interact with different
programs that you might run by setting these
| | 04:40 | values, but make sure that you export
them so that they're used not just in
| | 04:43 | bash, but actually pushed out to the
program that gets launched as well and
| | 04:48 | made available there.
| | 04:49 | In the next couple of movies I want to
show you some more useful environment
| | 04:51 | variables that we can set.
| | Collapse this transcript |
| Setting the PATH variable| 00:01 | We first took a look at the
path variable back in Chapter 5.
| | 00:03 | You can jump back there if
you need a quick refresher.
| | 00:05 | Path is a colon delimited list of file
paths that Unix uses when it's trying to
| | 00:10 | locate a command that you want it to run.
| | 00:12 | And we can take a look at that with echo $PATH.
| | 00:15 | There you see each of the
paths that it's going to check in.
| | 00:18 | For example, /usr/bin and then a colon
followed by the next path /bin and then a
| | 00:23 | colon and so on down the line.
| | 00:24 | And Unix will look in each of those
directories, trying to locate a command in that order.
| | 00:28 | So for example, if we type less unix_
files/lorem_ipsum.txt, then the process
| | 00:34 | that Unix went through when trying to
run the LESS command was to first look
| | 00:38 | for it in this directory and then if it
didn't find it, to look for it in this
| | 00:41 | directory, and so on all the way down the line.
| | 00:44 | In this movie I want to still learn to
modify our path variable using what we
| | 00:47 | just learned in the last movie
about setting environment variables.
| | 00:51 | To begin with, let's just do it here
from the command line. PATH= and we'll
| | 00:55 | just use an empty string.
| | 00:56 | So now we're telling it don't look in any
directories when trying to find these commands.
| | 01:00 | We're essentially unsetting the path.
| | 01:03 | Now let's try that same command
again. less unix_files/lorem_ipsum.txt.
| | 01:07 | It comes and it says "Oops! less?
| | 01:10 | I've never heard of it. I can't find it."
| | 01:12 | Now less is still a functioning program.
| | 01:13 | We didn't disable less.
| | 01:15 | What we did was we took away
Unix's ability to find the command less.
| | 01:20 | We can still run it if we did /usr/bin
less and then unix_files/lorem_ipsum.txt,
| | 01:28 | and now it still works.
| | 01:29 | We just had to go ahead and specify the
full path to find the command instead of
| | 01:34 | having it built into our path that Unix
would automatically use to look for it.
| | 01:39 | So you see what the path does and you
also see how you can screw yourself up if
| | 01:43 | you get your path wrong.
| | 01:45 | Now luckily all we have to do is reset
this environment variable back to this
| | 01:48 | default string and we will be all set,
or as we saw in the last movie we can
| | 01:52 | actually just close our window and
reopen a new one, and now echo $PATH will
| | 01:58 | return the original string again,
because it wasn't saved between bash sessions.
| | 02:03 | Now, of course most of the time we
probably do want our path variable setting to
| | 02:07 | stick around, so we're going to
want to put this in our bashrc file.
| | 02:10 | nano .bashrc, and then in here we can do
export PATH= and then whatever we want
| | 02:17 | to set that value to.
| | 02:19 | Obviously, we don't want to unset it,
so instead let's just type in the current
| | 02:23 | setting and we can use that as a
starting point for making modifications.
| | 02:27 | Now what kind of modifications
might you want to make to the path?
| | 02:29 | Well, you might want to
take something out of the path.
| | 02:32 | You might say, "Oh you know what?
| | 02:33 | I don't want it to look
in this directory anymore.
| | 02:35 | I want to just take that out of the equation."
| | 02:37 | So you can make that change if you wanted.
| | 02:40 | More often what we want to do is we want to
control the order in which the path happens.
| | 02:44 | So for example, a very common one is to
say all right, let's take usr/local/bin.
| | 02:47 | I'll actually copy that and use Option
to shoot to the beginning of the line
| | 02:53 | here, and we'll want to put that at
the beginning. So now it'll look for
| | 02:57 | usr/local/bin before it starts
looking in those other directories.
| | 03:01 | That gives me, the user, the opportunity
to put a file in there. Maybe I want to
| | 03:06 | run my own version of less, right?
| | 03:08 | So I put my own version in usr/local/
bin and it'll get to my version before it
| | 03:12 | gets to the version that's inside usr/bin.
| | 03:15 | Now we could do it this way by just
declaring a new string for the path, but a
| | 03:18 | more common way to do it is just to
amend the existing variable by adding to it.
| | 03:24 | Let me show you how we do that.
| | 03:25 | Let me just use Option and
I'll shoot down here to the end.
| | 03:28 | Let me just erase a bit. There we are.
| | 03:30 | And let's just take all of this away, all
the way back to here, and then let's use $PATH.
| | 03:40 | So what I am doing is I am calling
the existing value of path, which is that
| | 03:44 | string we just had, and I am going to
use that inside my new string but put my
| | 03:48 | new path at the beginning, and set that
whole thing then equal to my new version of path.
| | 03:53 | It's no problem to use path both here and here.
| | 03:55 | However, what is a problem is the single quotes.
| | 03:59 | We have to use double quotes whenever
we want it to pull in the value of this
| | 04:02 | dollar sign variable.
| | 04:04 | If we just use single quotes then
it will take it as a literal string.
| | 04:07 | it'll think we want it to look inside a
directory called $PATH, which doesn't exist.
| | 04:13 | What we want is we've to grab that variable,
so the double quotes are very important.
| | 04:16 | So we can also put our new code at
the end, if we wanted it to look in
| | 04:20 | usr/local/bin, after its normal
places. We can put it at the end.
| | 04:25 | But I want to try and get in front of it.
| | 04:27 | I want to say all right, I am not
trying to add to the path to the additional
| | 04:30 | places that you might check.
| | 04:31 | I am trying to supersede
whatever is in your additional path.
| | 04:34 | A very common setting to use and one that I
use in a lot of my trainings is usr/local/bin:
| | 04:38 | followed by usr/local/ and then sbin
and then colon and then if you have MySQL
| | 04:47 | installed, usr/local/mysql/bin.
| | 04:50 | So that's only if you have MySQL installed
| | 04:52 | you want to do that last one but what
I am saying is all right, basically
| | 04:55 | checking my three main local
directories where I might have put things.
| | 04:59 | And then if you can't find something
there, then go look in all the additional
| | 05:02 | places where you might
normally look for commands.
| | 05:04 | Now you may realize that actually usr/
local/bin is defined inside PATH, right?
| | 05:12 | So it's actually going
to be in the string twice.
| | 05:14 | That's not a big deal.
| | 05:15 | Don't worry about that.
| | 05:16 | It's really not a major concern
that it puts both of those in the path.
| | 05:19 | If you really cared about it, you could
put the full string or edit it out somehow.
| | 05:23 | But it's perfectly acceptable to do it
this way and just have it in the path twice.
| | 05:27 | Let's use Ctrl+X to exit.
| | 05:28 | we'll save the changes to our bashrc file.
| | 05:31 | If we just do echo $PATH now, we won't
have that value because we have to run
| | 05:37 | the bashrc file again or we have to
close our window and open a new one.
| | 05:41 | Now we try echo $PATH and now you can see
that it made my changes at the beginning.
| | 05:45 | So here's my directories
followed by the regular path.
| | 05:50 | Now at this beginner level it might
not be really obvious to you why you
| | 05:53 | would want to do this.
| | 05:54 | But once you start actually working in
Unix a lot and you start having your own
| | 05:57 | programs that you start putting in these
folders, you'll understand that it's an
| | 06:00 | essential thing to be able
to control this path variable.
| | 06:03 | Probably the most important
environment variable that you can set.
| | 06:06 | So it's important that you
understand how it works and how to set it.
| | Collapse this transcript |
| Configuring history with variables| 00:01 | Environment variables can be very
helpful in allowing us to customize how
| | 00:04 | the Unix history works.
| | 00:05 | There are five different
variables that we can configure.
| | 00:08 | Before we start looking at them, let's
first start by just using history -c to
| | 00:13 | clear out our existing history so that
everything we work with we'll be able to
| | 00:16 | easily identify what's new and what's
been added to the history, and then let's
| | 00:20 | do nano .bashrc and right below where
we set our path I am just going to paste
| | 00:26 | in the lines for the history and then we
can talk to them and that way you don't
| | 00:30 | have to sit and watch me type.
| | 00:32 | So notice that on each of them I'm
exporting it and here is the variable name.
| | 00:36 | HISTSIZE, HIST being short for
history, HISTSIZE= and then a number.
| | 00:41 | This is the number of
commands that history will remember.
| | 00:44 | By default it will remember 500.
| | 00:45 | If you want more or less, well this is how
you change it. You set the variable here.
| | 00:50 | Once we get to that maximum number then
the oldest command will drop off the top
| | 00:55 | and it will still just add the
newest command at the bottom.
| | 00:57 | So essentially what we are looking at
would be the 500 most recent commands.
| | 01:02 | In my case the 10,000 most recent commands.
| | 01:05 | I have given a very large number so
it just keeps growing and growing and
| | 01:08 | growing until I manually clear it.
| | 01:09 | We can also set the maximum size
that the history file has allowed to
| | 01:12 | become, using HISTFILESIZE.
| | 01:15 | Here I've said that it can be one
million k that's a very large number.
| | 01:18 | But you could restrict it to
something smaller if you wanted.
| | 01:21 | If you said I never want that file
to get larger than this certain amount,
| | 01:24 | once it gets to that amount well then
it will truncate the oldest entries in
| | 01:28 | the file and leave the newest ones, so
that it gets back under that file limit again.
| | 01:33 | The next one is HISTTIME format.
| | 01:36 | What this one does is it provides a
timestamp next to each of your history
| | 01:40 | entries, letting you know when they were added.
| | 01:42 | It does that by using a string here
and a set of special format codes.
| | 01:46 | These are known as the strftime format.
| | 01:51 | I call that string from time.
| | 01:52 | Other people may pronounce it as string f time.
| | 01:54 | I have given you a reasonable example
here, but you can also go out on the Internet
| | 01:58 | and look up these formatting codes,
if you want to use something different.
| | 02:02 | The next one is HISTCONTROL and you
can see the value for that is ignoreboth.
| | 02:06 | Well what does that mean ignoreboth?
| | 02:08 | Where there are actually three
values that we could put there.
| | 02:11 | We could put ignoredups, ignorespace or
ignoreboth and I'm telling it I wanted
| | 02:16 | to do both, both dups and space.
| | 02:18 | ignoredups means that we don't want
history to record the same line multiple times.
| | 02:23 | Try to ignore the
duplicates when you can.
| | 02:25 | ignorespace tells history not to
record any line that begins with a space.
| | 02:31 | Why would we need that?
| | 02:32 | Well it's a little trick that
a lot of people like to use.
| | 02:35 | Let's say that there is a single Unix
command that you're about to enter, but it
| | 02:38 | has something in it like a password
that you don't want to be saved in your
| | 02:41 | history where someone
might see the password later.
| | 02:44 | Well if you just start the line with a
space and then type the command, Unix
| | 02:48 | will still interpret the command as
normal but history will ignore it and it
| | 02:52 | won't show up in your history file.
| | 02:54 | So it's a very convenient way just be
able to hide a single line from your history.
| | 02:57 | We also can ignore certain commands all
the time and we do that with HISTIGNORE.
| | 03:03 | Here we provide a colon-separated list
of all the commands that we want history
| | 03:07 | to ignore all the time.
| | 03:08 | For example history. I don't need to record
in my history the fact that I typed history.
| | 03:14 | Another common one is PWD.
| | 03:16 | Every time I've checked to see what
directory I am in I don't need to have that
| | 03:20 | in my history, so I can leave that out.
| | 03:22 | You typically want to leave the things
to get ignored to be a very simple list.
| | 03:25 | Either put nothing in there at
all or keep it very very simple.
| | 03:28 | I've put mostly here informational things.
| | 03:31 | Things that are just sort of I might
check you know just to see where I am at,
| | 03:34 | to see what's going on.
| | 03:36 | I don't want to record those in my histories.
| | 03:37 | But things like copying and moving and
stuff, I want to have those in my history
| | 03:41 | because I may want to go back and use
them again or edit them or to see the fact
| | 03:45 | that I took those actions.
| | 03:46 | All right remember that you can
pause the movie to copy all of this down.
| | 03:49 | Once you've get it in there, let's
use Ctrl+X to exit, save the changes, and
| | 03:55 | let's also then run, using source
.bashrc will read in those changes as well.
| | 04:01 | So now all those variables have
been defined and let's just type a few
| | 04:04 | different commands.
| | 04:05 | Let's try echo "Hello" and let's try ls.
| | 04:10 | Let'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:24 | Okay, now that we have done all those,
let's clear the screen. Let's type
| | 04:27 | history and let's see what's in there.
| | 04:30 | My nano that I did to edit the bashrc,
the source where I ran those changes, and
| | 04:34 | the first echo made it in there and that's it.
| | 04:38 | That's because ls, ls -a, pwd and
history were all hidden those are all things
| | 04:44 | that I told them that they
should ignore those commands.
| | 04:47 | The duplicates were kept out, right,
so I only saw echo "Hello" one time and
| | 04:52 | also the echo 'secret' that I did with a
space at the beginning got left out as well.
| | 04:55 | So you can see how we are
able to leave those out.
| | 04:57 | You also can see that now I have this
time format here at the beginning, telling
| | 05:01 | me when each of these different
commands was entered. I have a timestamp there.
| | 05:04 | As far as the history size and history
file size, you'll just have to sort of
| | 05:08 | take my word for it or you can set
those to really low numbers so that you can
| | 05:12 | actually see how those work as well.
| | 05:14 | Now I want to show you want other thing though.
| | 05:15 | Let's try ls -al, right.
| | 05:19 | I added ls -la in that ignore list,
but now let's type our history and notice
| | 05:25 | that it did get added there.
| | 05:27 | So the things that we type under
HISTIGNORE have to be exact, even the options.
| | 05:32 | For example if I do history, pipe it
through tail -8, now I see the last 8
| | 05:38 | commands that were run including this
history command that I just ran, right.
| | 05:42 | So history by itself doesn't get added,
but history piped through this other
| | 05:46 | thing does get added.
| | 05:48 | So again all of these changes are
really just about configuring your working
| | 05:52 | environment to be the
way that you like it to be.
| | 05:54 | We have now seen a few of the most
useful configurations that you can make with
| | 05:57 | the environment variables, but beyond
these I think it's more important for you
| | 06:00 | to understand the overall concept of
how you go about setting environment
| | 06:04 | variables and making them available
every time that you log in, by putting them
| | 06:08 | in your bashrc file.
| | 06:09 | Other Unix programs you encounter in
the future are also going to make use of
| | 06:13 | shell variables and when they
do, now you know how to set them.
| | Collapse this transcript |
| Customizing the command prompt| 00:00 | In this movie we're going to learn how
to customize the Unix command prompt.
| | 00:04 | And we talked about long time ago the
prompt is this bit of text right here that
| | 00:08 | occurs before every line, right,
to prompt you to enter something.
| | 00:10 | I don't know why, but it's very,
very satisfying to customize that to
| | 00:14 | something that you like.
| | 00:15 | Maybe it's because it's the part of
Unix that you see most often, but it's
| | 00:19 | really useful to be able to customize that
and make it look exactly the way that you want.
| | 00:22 | The way that we're going to do that is
with a shell variable. And we saw shell
| | 00:25 | variables in the last movie.
| | 00:27 | The shell variable we want here is PS1.
| | 00:30 | That's for the main command prompt.
| | 00:32 | There's also a PS2, 3 and 4.
| | 00:34 | those are for other prompts that
occur in other contexts and we don't need
| | 00:37 | to worry about those.
| | 00:38 | PS1 is the one we want.
| | 00:39 | PS1= and then we can make it
equal to whatever we want, so -->.
| | 00:45 | It's always a good idea to put a
space after it just so that it separates the
| | 00:48 | prompt from what you're about to type.
| | 00:50 | Notice the space that it gave me.
| | 00:52 | We could try it without the
space and see the difference.
| | 00:54 | See now when I type it's right up against that.
| | 00:57 | So if you like that, you certainly can do it.
| | 00:58 | I prefer to have the space there. So that's it.
| | 01:01 | You can see that it took effect right-
away but like the shell variables that
| | 01:04 | we were setting before, if we close this
window and log out, it won't be set anymore.
| | 01:09 | In order to have shell variables set so
that they're there all the time, we need
| | 01:12 | to put those in either our
.bash_profile or our .bashrc file.
| | 01:16 | If we define them there, then as soon
as we log in, then our prompt will be
| | 01:20 | defined the way we want it.
| | 01:21 | So we're going to do that.
| | 01:22 | We're going to move it over there,
but this is a great way to play with it.
| | 01:24 | Just play with it here, get it to
something that you like, and then we'll move
| | 01:28 | over to .bashrc and we'll
actually plug it in there permanently.
| | 01:31 | So let's try a few more.
| | 01:32 | You can have it be whatever you want.
| | 01:33 | You're going to be What now?, space,
and that's what you'll get every time, or
| | 01:38 | you can have it just be something
like your username, all right. kevin.
| | 01:42 | Now that's the string kevin.
| | 01:44 | That's not my username kevin.
| | 01:46 | In 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:53 | The other one was just a string of characters.
| | 01:55 | It could have just as easily been bob.
| | 01:57 | It wouldn't have mattered.
| | 01:58 | \u is actually pulling my username from Unix.
| | 02:01 | Let's look at what these other
formatting codes are so that you'll be able to
| | 02:03 | use them when customizing your prompt.
| | 02:06 | So as we just saw there's u for username.
We also can use a lowercase s for the
| | 02:10 | current shell that we're in. bash in this case.
| | 02:13 | There's lower case w and upper W
which will show the working directory.
| | 02:16 | The difference is that w shows you the
full path to get to the working directory
| | 02:21 | whereas capital W just shows you
essentially the name of the folder that you're in,
| | 02:24 | just that current directory only.
| | 02:26 | w is the same thing as if you typed pwd.
| | 02:29 | Then there's \d which will show you the date.
| | 02:32 | There's \D which allows you to then
provide curly braces with a format, a
| | 02:36 | string from time format.
| | 02:38 | You can look up with those
formatting codes or other places.
| | 02:40 | I've just given you a real simple one to
show you the year, the month, and the day.
| | 02:43 | Then we have four different ones that'll
show the time, depending on the format you want.
| | 02:47 | There's capital A, lowercase t,
there's the at sign, and then there's the
| | 02:52 | capital T. There's capital H for
hostname, lowercase h for just the first
| | 02:56 | part of the hostname.
| | 02:57 | There's exclamation point which will
be the history number of this command.
| | 03:01 | If you use your history a
lot, that might be useful.
| | 03:04 | And then there's the special dollar
sign. If you're logged in as root, that
| | 03:08 | will then display a pound sign;
otherwise it's going to display a dollar sign .
| | 03:12 | So essentially it gives you a nice
visual cue for whether or not you're logged
| | 03:16 | in as root at the moment.
| | 03:17 | And then \\ would be a literal backslash,
if you want to use the actual backslash
| | 03:22 | character instead of using it
for one of these formatting codes.
| | 03:25 | So before we launch into a lot of these
let me just show you what the Mac default.
| | 03:29 | The Mac default is the
\h:\W \u$, space, and then we close the quotes.
| | 03:39 | So that's the default setting.
| | 03:41 | If you decide oh, you know
what, I liked most of that.
| | 03:42 | I just wish it didn't show me
the hostname at the beginning.
| | 03:45 | Well then, no problem.
| | 03:46 | You can take that out.
| | 03:46 | Let's try some other ones.
| | 03:48 | So let's say we have t that'll show us
the time. We have capital T was another
| | 03:54 | one, shows the time. Capital A. We
have a at sign that we can plug in there.
| | 03:59 | So you see the different
versions the time that it'll give you.
| | 04:04 | Let's do a lowercase d for the date.
| | 04:06 | If we want capital D then we need
to provide some formatting as well.
| | 04:10 | Capital D and then inside the curly
braces we'll use %Y-%m-%d. The percents are
| | 04:19 | the special characters.
| | 04:20 | These dashes could just as easily be
swapped in with something like this if
| | 04:23 | you like that better.
| | 04:24 | So let's just do the dashed
version for now. There we go.
| | 04:26 | And you can combine these of course.
| | 04:28 | So if you want that, then you also
wanted to have a space followed by a slash.
| | 04:33 | Let's pick the at sign. there you go.
| | 04:36 | So now you get the date and time.
| | 04:37 | Now I just want to show you the
difference between the w version.
| | 04:41 | Go up here and let's pick out w,
lowercase w. Notice that in my current
| | 04:46 | directory it's just the tilde. If I
switch into the unix_files directory into
| | 04:50 | test1, now I get that full path, right?
| | 04:53 | Exactly the same thing as if I did pwd,
except it abbreviates it for the fact
| | 04:58 | that I am in my user directory.
| | 04:59 | So it tries to shorten it when it can,
but it does still show me the full path.
| | 05:03 | If I instead put the prompt with a
capital W, then now I get just the current
| | 05:07 | directory that I am in.
| | 05:09 | As I move backwards I see
just the current directory.
| | 05:12 | So again, you can customize it to
be absolutely anything you want.
| | 05:15 | This is actually one of the
ones that I find to be most useful.
| | 05:17 | So I am just going to copy that using
Command+C and then I will back up into my
| | 05:23 | user directory, nano .bashrc, and
then we'll just put that in there.
| | 05:27 | Let's drop it right below
all the exports we do here.
| | 05:31 | We'll do export PS1.
| | 05:34 | Now remember export we need to do to
make sure that it's available all the
| | 05:36 | time, so go ahead and make sure you
add that. export PS1 and then whatever
| | 05:41 | prompt you decided you liked.
| | 05:42 | Ctrl+X. save it, and then just to make
sure that it takes effect, you want to
| | 05:47 | say source .bashrc and
that'll read that file then.
| | 05:51 | So now that's my prompt and as I
move around we'll just see the current
| | 05:54 | directory that I am in. unix_files.
| | 05:57 | That's the file we'll see.
| | 05:58 | So on your own, play around with those
different format codes, come up with a
| | 06:01 | prompt that works well for you, and
then put that in your .bashrc file.
| | Collapse this transcript |
| Logout file| 00:00 | So far all of our configurations for
our Unix working environment have been
| | 00:04 | done in either the .bash_profile
configuration file, or in .bashrc and that's
| | 00:09 | typically the two places that you'll
locate them, because those are the files
| | 00:12 | that get read in when you first start a new
session and they set things up the way you like.
| | 00:16 | But there is one other configuration
file that I don't want us to forget about,
| | 00:19 | which is the logout file. That's in
bash_logout, and anything that's in there will
| | 00:24 | get executed whenever we log out of
Unix. Let's take a look at how it works.
| | 00:27 | Let's create a new file. You see I
don't have a bash_logout file right now,
| | 00:32 | So let's do nano .bash_logout and in
it let's just put echo "See ya later!",
| | 00:40 | which is nice and simple.
| | 00:41 | That's the command that we
get run whenever we log out.
| | 00:44 | Now on a Mac, let me just show you first
of all that there is the file .bash_logout.
| | 00:50 | Now on a Mac, it's not that useful
because if we close this window, if we just
| | 00:55 | go up here and close it, well then
we'll never see that echo statement.
| | 00:58 | If we do it this way, exit, then
you see it run, "See ya later!"
| | 01:02 | Now you may be thinking, "Well, if I normally
just close the window, why have this file at all?"
| | 01:06 | Well, immediately you won't use it very
much, but I just want you to know that
| | 01:09 | that's there. You could use it.
| | 01:10 | It could alert you if there's some
unfinished business before you log out.
| | 01:14 | It could start a database
backup as soon as you logout.
| | 01:17 | It could notify friends who are on the
same server that you've left the server.
| | 01:21 | It could record some things in a log
file or maybe clear out some temporary
| | 01:25 | files that you left behind.
| | 01:27 | All of those types of things could be
put into a bash_logout file and even
| | 01:31 | though you aren't seeing them, you
certainly won't see anything that's echoed,
| | 01:35 | it does get executed.
| | 01:36 | So for example, if you had a clearing
out some temporary files, Unix would
| | 01:40 | gracefully close out, run everything in the
bash_logout file as the window was closing.
| | 01:45 | So again, it's certainly the least
useful of the configuration files, but it's
| | 01:49 | still important not to forget that it's there.
| | Collapse this transcript |
|
|
8. Unix Power Toolsgrep: Searching for matching expressions| 00:01 | In this chapter we're going to take
a look at some of the most useful and
| | 00:04 | powerful Unix commands.
| | 00:05 | Up until now we've really been
covering the fundamentals and getting familiar
| | 00:08 | with Unix, but now we are at the point
where we can start doing some serious
| | 00:11 | work, and the most powerful tool
in the Unix toolbox by far is grep.
| | 00:16 | Grep is a powerful way for us to search
for text which matches patterns that we
| | 00:20 | specify, and we aren't just talking
about searching for simple text strings.
| | 00:24 | We can define complex matching patterns by
using regular expressions, and regular
| | 00:28 | expression are actually
part of how grep gets its name.
| | 00:31 | Grep stands for Global Regular Expression Print.
| | 00:34 | If you think back to the movie where we
talked about Unix text editors, I told
| | 00:38 | you that the earliest Unix text
editor was called ed or Ed, and if you were
| | 00:42 | working inside the ed text editor and
you wanted to search for something, the
| | 00:45 | way you did it was by typing a g,
then a forward slash and then a regular
| | 00:49 | expression that described what you
were going to search for, another forward
| | 00:53 | slash and then p, for print.
| | 00:55 | And G stood for global, meaning
globally search for it, and p for print meant
| | 00:59 | to output the results, and the
shorthand that described that pattern, global
| | 01:04 | regular expression print, become
shortened to just be rep and it is these
| | 01:10 | regular expressions often just
called regex or regexp for short
| | 01:13 | that give grep its power.
| | 01:15 | Grep and regular expressions have been
around since the earliest days of Unix.
| | 01:18 | You might have guessed that since it
was available in this early text editor,
| | 01:21 | ed, but they were actually created for Unix.
| | 01:24 | By now though, grep and
regular expression was spread to most
| | 01:26 | programming languages, into many
text editors as well, so they're useful
| | 01:30 | in a lot of context.
| | 01:31 | It's really worth trying to learn how they work.
| | 01:33 | We'll focus on regular
expressions in the next movie.
| | 01:35 | First, let's get familiar with grep by
using simple text strings. So to begin
| | 01:39 | with, notice that I'm in my user
directory and I'll just change into my Unix
| | 01:43 | files directory and let's
take a look at what's in there.
| | 01:46 | File that we're going to start
out working with is fruits.txt.
| | 01:50 | That's just a simple list of fruit that
we created earlier and what we're going
| | 01:54 | to do be doing is search
inside this file for an expression.
| | 01:57 | We're going to start with just
searching by a simple text string to begin with.
| | 02:00 | And what you also might say is
that you're going to grep that file.
| | 02:04 | It is perfectly
acceptable to use grep as a verb.
| | 02:06 | So we're going to grep
this file for text string.
| | 02:08 | The way that we will do that is with
grep, space, then the expression that we
| | 02:13 | want to search for, we're
going to search for Apple.
| | 02:16 | Now this is still an expression,
even though it's the simplest
| | 02:19 | expression possible.
| | 02:20 | It's the literal characters, a-p-p-l-e,
but it is still a regular expression
| | 02:24 | that it's passing in.
| | 02:25 | So grep for Apple inside the file fruit.txt.
| | 02:30 | That's all there it is to it.
| | 02:31 | We hit Return and it comes back and
what it returns to us are the results.
| | 02:35 | The results that we're seeing are
the three lines that contain a match.
| | 02:40 | Not just the three words, the three
lines. What I mean by that is in our case
| | 02:45 | each of our lines only had one word on
it, but if that first line had been Tom
| | 02:49 | likes apples and the second line was
Mary likes pineapples, then it would
| | 02:54 | return the entire line,
not just the single word apple.
| | 02:57 | It would have returned the line that
had a match, so don't be confused by that.
| | 03:00 | We're actually seeing the
entire line that gets returned.
| | 03:03 | It's also a case-sensitive match.
| | 03:06 | So for example, if we search for
apple with a capital A, well, then it comes
| | 03:10 | back and it says no matches, because
it checked for case-sensitive search and
| | 03:14 | looked for apple exactly as we
specified it and it wasn't there.
| | 03:17 | If we wanted to be insensitive, we can
pass an option for that, I. That's a case
| | 03:22 | insensitive search and now it doesn't care.
| | 03:25 | It doesn't care if I have upper or lower
case a in my expression and it doesn't
| | 03:29 | care whether it's upper or
lowercase a in the results.
| | 03:31 | It will still match.
| | 03:33 | I is probably the single most common
option that you would use with grep, but
| | 03:36 | there are a number of others.
| | 03:37 | You can use the man pages
to look through all of them.
| | 03:39 | Let me show you a few of the more useful ones.
| | 03:42 | Notice for example that it matched both Apple
and pineapple. If we were to use the -w option,
| | 03:47 | that will match only on whole words.
| | 03:49 | I'm going to take away the capital A
to make sure that that still matches.
| | 03:52 | Now notice it didn't find the pineapple.
| | 03:54 | It only found the two whole word
matches apple and apple and it's smart
| | 03:58 | about how it finds words.
| | 03:59 | It uses spaces and punctuation and line
returns to know where the word breaks are.
| | 04:04 | Now we also could write a more
complex regular expression that would do the
| | 04:07 | same thing and we'll that little bit later,
but this is sort of the quick and easy way.
| | 04:11 | Another nice one is the V option. So
with the V, we'll get our lines that don't match.
| | 04:16 | I is the reverse, the
inversion of our regular search.
| | 04:21 | So notice now apple and
pineapple are missing from the list.
| | 04:24 | We've essentially filtered them out.
| | 04:26 | So all lines that do not
match is what we're looking for.
| | 04:29 | We could do the in option.
| | 04:31 | In option is nice because it gives us
line numbers. So it numbers the line, so
| | 04:36 | it says, all right, I found a
match in line 5 and here's the line.
| | 04:40 | I found the match in line 6 and here's the line.
| | 04:42 | A match in line 13 and here's the one.
| | 04:45 | Now you may not actually be
interested in seeing those results. Maybe what
| | 04:48 | you're interested in is actually just
counting them and you can do that with
| | 04:52 | the C option. grep c, it comes back
and says ah, I found it three times.
| | 04:56 | An example of a place that where that
might be more useful, if we do grep inside
| | 05:01 | lorem_ipsum, let's search for lore.
So it's our long fake Latin text.
| | 05:06 | It comes back and it says, ah, the
word lorem occurs in there 44 times.
| | 05:10 | Now notice also how fast that was.
| | 05:13 | It's really, really fast about being able
to search in there looking for this text.
| | 05:16 | That's part of the power of Unix, is
being able to do these things and do them
| | 05:19 | in a really, really fast way.
| | Collapse this transcript |
| grep: Multiple files, other input| 00:00 | What if we want to grep from
multiple files or search inside directories?
| | 00:04 | There are a couple of ways to do it.
| | 00:05 | One is we could just say well, grep for
Apple inside and then provide the file
| | 00:10 | path, a directory in this case.
| | 00:12 | So let's just say we're
going to use the current path.
| | 00:14 | The dot represents the current path.
| | 00:16 | We could just as easily type Users/Kevin
/unix_files, same thing. It comes back and says
| | 00:22 | I didn't find anything.
| | 00:24 | It's because we've given it a directory.
| | 00:27 | If we want it to do it in this way
then we need to put the -R option in front
| | 00:32 | of it for recursive.
| | 00:33 | Recursively look inside here, meaning
check all of the files that are in this
| | 00:38 | directory and then go into the directories
below that and look inside those and so on.
| | 00:42 | The same option that we used with
copy, move, and remove earlier. So grep
| | 00:47 | recursively for apple inside this directory.
| | 00:50 | So whenever we use a directory, we
want to use -R just like we did when we
| | 00:53 | were doing copying.
| | 00:54 | So now it tells me not only the
matches it made but the file where it occurs.
| | 00:59 | We could also provide the -n option
after that and now it gives me the line
| | 01:04 | numbers in each of those files.
| | 01:05 | Notice that it does this by default. It says ah,
| | 01:08 | now because I'm searching multiple
files I'm not just going to give you the
| | 01:10 | results. I'm going to tell
where I found them as well.
| | 01:13 | You can suppress that. Let's just
erase all of this and I'll just use the dot
| | 01:18 | from now on just because it's a little
bit shorter and instead of the n option
| | 01:22 | let's use the h option.
| | 01:24 | h option will suppress the file name
and still show you just the line itself
| | 01:29 | or if you wanted the other way around
and you wanted just the file name, you
| | 01:32 | can use the l option.
| | 01:33 | Now it tells me ah,
| | 01:34 | I've found apple in three files.
| | 01:36 | Fruit, sorted fruit, and unique sorted fruit.
| | 01:38 | That can be really handy.
| | 01:39 | All right you could say all right, here
are the relevant files I found them, and
| | 01:43 | now we could presently pipe those
results into something else, right.
| | 01:46 | Maybe we want to now
concatenate these three files.
| | 01:49 | After we find them then we could
potentially concatenate them together.
| | 01:52 | If we want to do the opposite of that
and find the files that don't have a match
| | 01:56 | it's the capital L.
| | 01:57 | So that returns all files that did not match.
| | 02:00 | This is very similar to the -v option
that we did earlier but that returned the
| | 02:04 | lines that didn't match.
| | 02:05 | This is returning whole files that don't match.
| | 02:08 | So it's similar but not the same.
| | 02:09 | Now this recursive approach using this R
option is searching all files in all directories.
| | 02:16 | Don't forget that bash also lets us
use wildcards to trend pick more specific
| | 02:20 | files and that might speed up our searches.
| | 02:22 | For example, just using ls we can do ls*.txt.
| | 02:27 | And we can see just the text files that
are in the current folder. Or even more
| | 02:31 | specific, we can say all right
let's check just the fruit files.
| | 02:33 | All right, so now I'm saying just the
fruit files. That wildcard lets us do that.
| | 02:37 | That's handy with grep because now
for example we can say we'll grep for
| | 02:42 | apple inside just the files that end
in fruit.txt. It's the same results that we
| | 02:47 | got but it's just searching these three files
instead of searching every file and directory.
| | 02:53 | If you have a lot of files and
directories this can be a lot faster.
| | 02:55 | But the main point is don't just
choose recursive by default. Think about what
| | 02:59 | you're really trying to accomplish and
try and find the best way to do that.
| | 03:02 | Now these are working with files and
directories. We also can change our standard input.
| | 03:06 | We can use piped input.
| | 03:08 | We could for example do cat
fruit.txt and pipe that into grep for Apple.
| | 03:13 | That's still just working
with a file though, right.
| | 03:15 | It's just as easy to do it the other way.
| | 03:17 | Well it's really more useful as with
something like ps aux. You remember that
| | 03:20 | that returns a list of all of our
currently running processes. Well, we can grep
| | 03:24 | that list. Let's grep for Terminal.
| | 03:26 | There is my entry for Terminal.
| | 03:28 | I can now see what its ID number is.
| | 03:31 | I can see how long it's been running.
| | 03:32 | I can see how much memory it's
taking up, all of that information just by
| | 03:36 | grepping for only the
line that contains Terminal.
| | 03:38 | Or we could say all right, show me
everything that has applications in it.
| | 03:44 | So now all of my Mac applications will
show up and I can see just those and not
| | 03:47 | have to sort to that whole
list of running processes.
| | 03:51 | Another place it is useful with the history.
| | 03:53 | So for example we'd say history
and let's grep that for Unix files.
| | 03:58 | Every time that I've made reference to
the Unix files directory, we also could
| | 04:02 | then use this as shortcuts to
be able to run those commands.
| | 04:05 | We saw how to do that when
working with our history.
| | 04:07 | Let's try history and let's pipe that
through grep nano and then pipe that into less.
| | 04:15 | Now I only have a few but if you
have a lot of these we now would have a
| | 04:18 | paginated list of all the
times that we've ever used nano.
| | 04:21 | So piper history, filter out so that we
get only lines that have nano in it, and
| | 04:25 | then use less pagination.
| | Collapse this transcript |
| grep: Coloring matched text| 00:00 | You can see how grep is
a really power full tool.
| | 00:03 | There is one more thing that I want to show you.
| | 00:04 | Let me just do grep for lorem
ipsum again. lorem inside lorem ipsum.
| | 00:10 | Now here's the content there. So you
see that this line has lorem in it and
| | 00:15 | this line over here has lorem in it and
here it says lorem again, so I'm seeing
| | 00:18 | each of those lines.
| | 00:19 | But it's a little hard to find those matches.
| | 00:22 | Grep also offers an option
called color which is really useful.
| | 00:26 | Color and now you can see that it colors those.
| | 00:29 | Color also can have a couple of
variations. color equals auto, color equals
| | 00:34 | always, or color equals never.
| | 00:37 | Always of course will always provide
the coloring. Never will not do it. What
| | 00:42 | auto does is auto says I want you to
color it if you're showing it to me on
| | 00:46 | the Terminal, but not when you're sending
it somewhere else like to a file or a pipe.
| | 00:50 | And the reason why it is, there is
actually some special characters around the
| | 00:53 | word lorem there every time
so that it knows to display it.
| | 00:56 | Otherwise how would it find those?
| | 00:57 | What we don't want is those special
characters to accidentally slip into a file.
| | 01:01 | So auto is the option
that'll take care of that for us.
| | 01:04 | Now by default this color is going to
be red, but if we want to change that we
| | 01:08 | can do it using a shell variable.
| | 01:10 | We saw how to work with shell
variables. Let's just go back into our user
| | 01:13 | directory and we'll open up our bashrc
file and I'm just going to scroll down
| | 01:19 | here to the bottom of these exports
that I've done before and I'm just going to
| | 01:23 | paste in some text that
we will then take a look at.
| | 01:25 | Let me just scroll up here
so you can see the top of it.
| | 01:28 | So these are the grep color codes.
| | 01:29 | That's the name of the environment
variable, grep_color, and I've given you the
| | 01:33 | different numbers, so you
know what they all mean.
| | 01:35 | But here's the command down here,
export GREP_COLOR and then in quotes put the
| | 01:41 | colors that you want separated by
semicolons. So you can pick different
| | 01:44 | attributes. You want to pick only one
text color and only one background, but you
| | 01:48 | can pick several attributes.
| | 01:49 | So for example this is 34 blue. 47 is white.
| | 01:53 | So it's going to be blue text on a white
background and that's how it will show me those.
| | 01:57 | We'll just take a look at that in a second.
| | 01:59 | There's also another one down here which
is export GREP_OPTIONS and then you can
| | 02:04 | provide options that you
want grep to use all the time.
| | 02:06 | So if you always want to do a case
insensitive search, well you can put the -I option here.
| | 02:11 | I've put --color equals auto
and there is my grep option.
| | 02:14 | That's probably the one people use most often.
| | 02:16 | So feel free to use these or don't use them.
| | 02:18 | But I just wanted you to see them and
know how they work, because a lot of
| | 02:21 | people really like them.
| | 02:22 | So let's just save that file and one
last time I'll go up and let's rep.
| | 02:27 | I actually have to specify it's
inside the Unix files directory now.
| | 02:31 | Oh! It didn't work.
| | 02:32 | I need to run that file source.bashrc.
| | 02:35 | All right, and now that I've loaded
that file or if you close the window
| | 02:40 | and reopen it, now that bashrc file is run and
now you can see the results of the coloring.
| | 02:44 | So that's the basics of working with grep.
| | 02:47 | As you can see it's really complex tool.
| | 02:49 | So far we've just been using a regular string.
| | 02:52 | Where it really gets powerful is when
we start working with regular expressions
| | 02:55 | and that's what we'll do in the next movie.
| | Collapse this transcript |
| Introduction to regular expressions| 00:00 | In the previous movie we learned to use
grep to search by using a simple text
| | 00:04 | string as our search expression.
| | 00:06 | Grep becomes even more powerful once
we learn to use regular expressions.
| | 00:10 | Now there is a lot to learn
about regular expressions.
| | 00:12 | We can have an entire course on them,
and we won't too in-depth here, but in
| | 00:15 | this movie we will cover the most
essential concepts so that you can apply them
| | 00:19 | and use them with grep.
| | 00:20 | To begin with notice that in the
Terminal I'm inside my user directory and I'll
| | 00:24 | just change it to my unix_files directory.
| | 00:26 | And the file that we're going to start
by working with is the same fruit.txt
| | 00:29 | file that we're working with in the last movie.
| | 00:31 | It's just a simple list of fruit
that we can search inside of and see how
| | 00:34 | regular expressions work.
| | 00:36 | To begin with we saw that we can
grep for apple inside fruit.txt.
| | 00:40 | First thing I want to tell you about
regular expressions is that it's a good
| | 00:43 | idea to put quotes around it.
| | 00:45 | Now they are not strictly required,
you saw it worked just fine without them,
| | 00:49 | but it's a good idea.
| | 00:50 | And the reason why is because when we
start working with regular expressions
| | 00:53 | we're going to be using some
special regular expression symbols.
| | 00:56 | And some of those regular expression
symbols also have a meaning to the Unix
| | 00:59 | command line and by putting it in quotes
we're helping the Unix command line to see ah,
| | 01:04 | this is the expression.
| | 01:06 | The symbols that I see in there they
are not for me to interpret and for me to
| | 01:09 | do things with. They're meant to
be part of the regular expression.
| | 01:12 | So the single quotes are good habit to be in.
| | 01:16 | Now the string that we've been
searching for, apple, that is a
| | 01:19 | regular expression.
| | 01:20 | It's the simplest regular expressions
that you can have, because it matches only
| | 01:24 | the literal characters apple.
| | 01:27 | Instead what we want to do is be
able to provide some variation to that.
| | 01:30 | So for example, we can
replace those p's with periods.
| | 01:34 | The period is a wild card
character for regular expressions.
| | 01:37 | It says any character could match here.
| | 01:39 | So now we're not just looking for
apple we're looking for a followed by any
| | 01:43 | character, followed by any other
character, followed by l and e. It has to be
| | 01:47 | 2 character. It can't be 1, but 2 characters,
but we don't care what those characters are.
| | 01:51 | So now it matches not just
apple, but it matches ankle.
| | 01:54 | Now it's not that useless in this case,
because it really just does match apple
| | 01:57 | just the same, but let's try another example.
| | 01:59 | Let's do grep that file for .a.a.a inside fruit.
| | 02:05 | Do you know what it's going to return?
| | 02:08 | Now we get back both banana and papaya.
| | 02:11 | The reason why is because each of them
matches the pattern that we've described.
| | 02:15 | Any character followed by an a, any
character followed by an a, and then any
| | 02:19 | character followed by an a, and
both of them match this pattern.
| | 02:21 | And that's really what we're doing
with regular expressions. We're defining
| | 02:24 | patterns that we want to use for searching.
| | 02:27 | So you can think of that.
| | 02:27 | You can think of regular expressions
as ways of defining patterns that we
| | 02:31 | want to search for.
| | 02:32 | Let's try another one let's just grep
the file for e and a together, all right
| | 02:36 | so, anytime that we have ea together.
| | 02:38 | So it returned pear, peach, pineapple, and pear.
| | 02:43 | Now if we wanted to just get pear
and peach, we can just put a p at
| | 02:46 | the beginning, right?
| | 02:47 | And that would rule out pineapple.
That would no longer match our pattern, but if
| | 02:51 | we wanted to get peach and pineapple
and omit pear how we go about doing that?
| | 02:56 | Well, we can say for example that we
want the third character after the e and a
| | 03:01 | to match either c or p, but not the r.
Now you can see it returns peach and it
| | 03:07 | returns pineapple, but it didn't return pear.
| | 03:10 | We're going to talk more about the
square brackets, but I want you to just
| | 03:12 | understand the concept that what we're
doing here is we're defining a pattern
| | 03:16 | that we wanted to match and both
eac and eap match our pattern.
| | Collapse this transcript |
| Regular expressions: Basic syntax| 00:01 | Now I'm not going to go through all of
these item by item, but you can pause the
| | 00:04 | movie if you want to take a look.
| | 00:05 | Really get a feel for what they do.
| | 00:07 | First a simple period, that's a wild
card. The character set, so that's any
| | 00:11 | character listed inside those square brackets.
| | 00:13 | So in the example on the far right you
can see that that would match both grey
| | 00:16 | and gray, both spellings
of gray could get matched.
| | 00:21 | Then we have a negative character set.
We'd have the same square brackets but we
| | 00:25 | put a caret as the first character
inside of them and that would say any
| | 00:28 | character that's not included there.
| | 00:30 | So for example, if we wanted to match
everything that was not an A, E, I, O or U,
| | 00:33 | then we would use that negative character set.
| | 00:37 | Next we have the range indicator which
is a minus sign and that's typically just
| | 00:41 | used inside a character set to
indicate a range of characters.
| | 00:44 | You don't have to actually spell out
every single character you mean. You can
| | 00:47 | just say capital A to capital Z, A
dash Z, lowercase a to z, 0 to 9, and those
| | 00:53 | are the typically the cases you would
use them with is with the alphanumeric
| | 00:56 | characters and with digits.
| | 00:58 | The next three items the asterisk, the
plus, and the question mark all have to do
| | 01:02 | with how often element repeats.
| | 01:04 | Does it occur zero or more times,
one or more time, or zero or one time?
| | 01:08 | So to take the first one as an example,
file_name, that underscore may or may not exist.
| | 01:15 | If it's not there it still matches.
| | 01:16 | If it exist one time it still matches.
If it exists two times, three times, it
| | 01:20 | doesn't matter. It still matches.
| | 01:22 | Below those we have the alternation operator.
| | 01:24 | This is the upright pipe that we've
already seen we were piping commands.
| | 01:28 | Notice that this is one of the
symbols that has a meaning to Unix that's
| | 01:31 | different than the
meaning in regular expressions.
| | 01:33 | Here it's the OR operator.
| | 01:35 | So what it means is for example I asked
for JPG or GIF or PNG. That's very common
| | 01:42 | if you want all three file types to be matched.
| | 01:45 | Next we have the start and end of line anchors.
| | 01:47 | The start of line anchor is the caret symbol.
| | 01:49 | That's the same thing we used up on
the negative character set, but it's in a
| | 01:52 | different context here. Don't get them confused.
| | 01:54 | If it's inside the square brackets,
it's the negative characters set.
| | 01:57 | Otherwise, we're really talking about
this start of line anchor and that means
| | 02:01 | that a line beginning with hello
would match, but if hello was somewhere in
| | 02:06 | the line, not at the beginning, it would not
match. Same thing for the end of line anchor.
| | 02:11 | If you need an actual literal
character and you want to escape one of these
| | 02:13 | regular expression characters, you
would use the backslash and then there's a
| | 02:17 | number of them at the bottom that I
won't go over in detail, but they match
| | 02:20 | either any digit, anything not a
digit, anything that's an alphanumeric
| | 02:24 | character or not, anything that's
white space or not and so on.
| | 02:28 | Notice though that three of
those I've marked with an asterisk.
| | 02:31 | These are extended regular expression syntax.
| | 02:33 | We'll talk more about that.
| | 02:34 | But for now, just notice that there are
a couple of these that are part of the
| | 02:38 | extended set and some of them
that are part of the basic set.
| | 02:41 | There're also some predefined
character classes that can help you to find
| | 02:44 | certain types of things easier.
| | 02:46 | The symbols for these are a little bit longer.
| | 02:48 | All of them have square brackets and
colons on either side of a keyword.
| | 02:52 | So for example, alpha would match
any of those alphabetic characters.
| | 02:56 | Digit would match any numeric characters.
| | 02:59 | Alnum would match both,
alphabetic or numeric, and so on.
| | 03:02 | Now you could just as easily use
those other symbols to write these out
| | 03:06 | yourself. These just provide useful shortcuts.
| | 03:08 | Notice here that the square bracket
is not indicating a character set.
| | 03:11 | This is talking about a single character.
| | 03:14 | If you want to say this is the
character set, we put another set of square
| | 03:17 | brackets around that.
| | Collapse this transcript |
| Using regular expressions with grep| 00:01 | So we've already gotten a look at the
wildcard, the period, and we've also seen
| | 00:04 | character sets when we were working
with finding both peach and pineapple.
| | 00:08 | Let's try the beginning Of
line and end Of line anchors.
| | 00:11 | So for example grep and the beginning
of line anchor followed by P inside fruit
| | 00:17 | will find every line that begins
with P. Notice it did not match apple.
| | 00:21 | Apple has a P in it, but it's
not at the beginning of the line.
| | 00:24 | I am going to do the same thing.
| | 00:25 | We can find everything with berry at
the end and that will return every line
| | 00:30 | that has berry at the end.
| | 00:31 | Now in this case actually all
occurrences of berry were also at the end of the line,
| | 00:35 | but it would find it if it
were only at the end of the line.
| | 00:39 | Let me show you that.
| | 00:40 | One way that you can work with grep
that's really useful is instead of working
| | 00:43 | with a file, let's say we have berry
bush, we can use pipes from echo, so
| | 00:48 | we'll pipe-in the string, and
then we'll grep for 'berry$'.
| | 00:53 | Now, it didn't find it, because
it's not at the end of the line.
| | 00:56 | If we instead we're looking for
berry bush berry, now it does find it.
| | 01:01 | It matched the last one, not the first one.
| | 01:03 | You can see I have my colorization
turned on, so it just colorized the last one.
| | 01:07 | If we did the same thing but we take
away that end Of line anchor, now it
| | 01:10 | finds it both times.
| | 01:11 | Let's try another one.
| | 01:13 | Let's do echo, and let's do
AaBbCcDdEe. There we go!
| | 01:21 | And let's pipe that into grep, and this
time I'm going to make sure that color is on.
| | 01:25 | If you don't have your color on,
this will turn your color on.
| | 01:28 | Let's search for just upper.
| | 01:31 | We're looking for uppercase.
| | 01:32 | We're going to use that character class.
| | 01:35 | Notice what happened here.
| | 01:36 | It didn't match the uppercase letter
that you might have expected it to, because
| | 01:41 | it's not interpreting this
as being a character class.
| | 01:44 | It's interpreting this as being a character set.
| | 01:47 | It thinks that we want to match
anything that is in this character set.
| | 01:51 | So E is in this
character set, so it got matched.
| | 01:54 | There is no U, P, and R. That's
why the lowercase E got matched.
| | 01:58 | If we instead put double-brackets
around this, and we actually should probably
| | 02:02 | put quotes around it too.
| | 02:04 | It's always a good practice, remember.
| | 02:05 | Now, it returns what we expect it to match.
| | 02:08 | So I just point that out to you to
make sure that you see with the single
| | 02:11 | brackets we're actually
referring to the class of characters.
| | 02:14 | But if we wanted to actually work
inside grep, we use the double ones so that
| | 02:18 | it's saying a character set
made up of the character class.
| | 02:23 | I'm just going to paste-in another example,
so you don't have to watch me type it.
| | 02:27 | I've just got a bunch of punctuation
here and I'm going to search that to find
| | 02:29 | all the punctuation.
| | 02:30 | I think that gives you the
basics for regular expressions.
| | 02:33 | Again, it's a very deep subject, and
this is really only the surface of it.
| | 02:36 | There are even sites that
catalog regular expressions.
| | 02:39 | So if you're looking for a regular
expression that will match every phone number
| | 02:43 | or every email address, all those
different combinations for how those might be
| | 02:47 | formatted, those exist.
| | 02:48 | People have written them and they've
shared them on different web sites and you
| | 02:51 | can make use of them without
having to reinvent the wheel.
| | 02:53 | But what I do want to show you is there
are a couple of things to watch out for
| | 02:57 | when using regular expressions with grep.
| | 03:00 | The first of these is that if we were
to grep for 'ap*le' inside *fruit.txt,
| | 03:09 | that the two asterisks
here have different meanings.
| | 03:11 | This is the regular expression asterisk.
| | 03:13 | It means that the P is
repeated zero or more times.
| | 03:16 | This would match ALE, APLE,
APPLE, APPLE, and so on.
| | 03:22 | This one is a wildcard for the file
system, totally different meaning.
| | 03:27 | So I want to make sure that you
see those and realize the difference.
| | 03:29 | That's part of why we want to make
sure that we put these inside quotes, is to
| | 03:32 | help keep that separate.
| | 03:34 | The second is I'll take away the
asterisk here and I'll run the command and
| | 03:37 | you'll see that it does return
the results that we would expect.
| | 03:40 | If we run this other version with the
plus sign instead, that's the operator
| | 03:46 | that means one or more times.
| | 03:48 | That would match APLE and APPLE, but not ALE.
| | 03:52 | It has to occur at least once.
| | 03:54 | That's what the plus symbol means.
| | 03:56 | If we run it now, we get nothing back.
The reason why is it's taking that plus
| | 03:59 | to be the literal plus sign.
| | 04:01 | This is what I was talking
about with the basic and extended
| | 04:04 | regular expressions.
| | 04:05 | So it's an important sort of gotcha
when working with regular expressions, that
| | 04:09 | there are the basic ones that work all
the time, and then there is this extended
| | 04:13 | set of few things that only work in some cases.
| | 04:16 | If we want to use the extended set in grep,
we need to use the -E option. Now it works!
| | 04:23 | Now, it finds it exactly as we would expect.
| | 04:25 | So if you want to use a few of these extra
features, you're going to need to use that -E.
| | 04:30 | There are more of them than
just the three I showed you.
| | 04:32 | Those are the most basic and the ones that
come up and cause the problem most often.
| | 04:36 | Could you use -E all the time? Absolutely.
| | 04:38 | Another spot where this basic and
extended regular expressions causes problems
| | 04:42 | is when we're working with
that OR operator as well.
| | 04:44 | So for example, if we grep for Apple or
Pear inside fruit.txt, so it feels like
| | 04:51 | a very simple operator.
| | 04:52 | It feels like something
we ought to be able to do.
| | 04:54 | It says oops, it's not there.
| | 04:55 | All we've got to do is put that -E in
front of it and now it does find every
| | 05:01 | occurrence of either Apple or pear.
| | 05:02 | So regular expressions, especially when
combined with rep, is really great for
| | 05:06 | finding exactly what you want.
| | 05:08 | But what it doesn't do is it doesn't
allow you to change or manipulate anything.
| | 05:12 | All it does is allow you to find it.
| | 05:14 | So in order to make changes to
manipulate the data, we're going to need to take
| | 05:17 | a look at a few more Unix tools.
| | Collapse this transcript |
| tr: Translating characters| 00:01 | In this movie, we're going to take a
look at another simple but powerful
| | 00:03 | Unix tool called TR.
| | 00:05 | TR is short for Translate and what
it does is it copies from the input it
| | 00:09 | receives to output, but with the
substitution of selected characters, according
| | 00:15 | to translation rules that we give it.
| | 00:17 | So at its simplest we would have echo a,
b, c, and we'll pipe that in to tr, and
| | 00:23 | the first argument of tr is going to be the
thing we want to search for. Our search string.
| | 00:27 | So we're going to search for all of the
commas, and the second argument will be
| | 00:30 | the replacement string.
| | 00:31 | So we'll replace it all with dashes.
So as you might guess, it takes all the
| | 00:35 | commas and it translates them into being dashes.
| | 00:38 | Now this is a totally valid use.
| | 00:40 | But translating one character to one
character doesn't really reveal how TR works.
| | 00:45 | So let's try another better example.
| | 00:47 | Let's say we have echo and for this
string let's use the numbers 1 to 6, so
| | 00:51 | you can just peck away through your
keyboard, just pick a random set of
| | 00:54 | numbers, but not going any higher
than 6. And then we're going to pipe that
| | 00:57 | into tr, and this time we're going to
say that our search string is the numbers
| | 01:01 | in sequence 1 to 6.
| | 01:02 | 1, 2, 3, 4, 5, 6, and the replacement
string that we'll use for that, I am
| | 01:07 | going to use the letters E, B, G, D, A, E.
Those map to the tunings on a six string guitar.
| | 01:13 | When we hit Return, now notice what it did.
| | 01:16 | It took all of the 1s and it turned
them into Es and it took all of the 2s and
| | 01:21 | it turned them into Bs and
all the 3s became Gs and so on.
| | 01:25 | Everything that it found in the search
string, not only did it find it, but then
| | 01:28 | it said "Oh, what position
are you in the search string?
| | 01:31 | Let me map that to the replacement
string and find the item that's in the
| | 01:35 | same position there."
| | 01:36 | It's a mapping, right?
| | 01:38 | So the number 6 gets mapped to E,
number 4 gets mapped to D, right?
| | 01:43 | Do you see how that works?
| | 01:44 | Notice also that if we had
another number, let's put in a 9 or any
| | 01:48 | other character really,
| | 01:49 | it wouldn't get matched, right?
| | 01:52 | It doesn't get translated, because
it's not in our translation string.
| | 01:54 | Therefore it gets left alone.
| | 01:56 | It's only those selected characters
and those selected characters get
| | 02:00 | translated based on their position in
the search string and in the replacement
| | 02:05 | string, so position does matter.
| | 02:07 | This translation is a lot like the code
breaking that you might have done as a kid.
| | 02:11 | In order to find out a secret message,
you'd have to swap all the As for another
| | 02:14 | letter and all the Bs for another letter.
| | 02:16 | When you were finally done, you would
have decoded the message by doing this
| | 02:19 | simple alphabetic translation.
| | 02:21 | We can actually do this with TR.
| | 02:23 | A simple version would be to encode a
string using something called ROT-13.
| | 02:26 | ROT-13 is just rotating
all the characters 13 places.
| | 02:31 | So the A becomes N, B becomes O, C
becomes P, and so on. Let's try it.
| | 02:37 | echo 'This is ROT-13 encrypted.'
A single quote and we'll pipe that into tr and
| | 02:46 | now for tr's search string, we're
actually going to use character sets.
| | 02:49 | We could write out A, B, C, D, and all
that, but we can shorthand that with A-Z,
| | 02:55 | all the letters A to Z in that order,
followed by the lowercase letters, a-z in
| | 02:59 | that order, because it is case-sensitive.
| | 03:02 | Then in the replacement string, what we
want to do is rotate all of those 13 positions.
| | 03:06 | So N-Z would be take care of letters A
through M, and then A-M capital M would
| | 03:14 | be the second half of that.
| | 03:16 | Same thing for the lowercase letters.
| | 03:17 | n-z, a-m, and we translate it.
| | 03:20 | Now what we get is something that is ROT
-13 encoded. And we can just take that
| | 03:24 | and we can decode it by just
rotating it 13 places again.
| | 03:28 | Since there are 26 letters in the
alphabet we can do that, and now we can decode
| | 03:33 | the message just as well.
| | 03:34 | So that's probably the simplest
kind of encoding and encryption there is.
| | 03:37 | It is just that simple rotation cipher.
| | 03:40 | But hopefully now you start to see the
idea of how translate does its thing.
| | 03:43 | Just to make it really clear,
let me show you what it's not for.
| | 03:46 | If we have for example something like
already daytime and we want to swap out
| | 03:52 | tr 'day' for 'night',
| | 03:56 | it doesn't replace the
word day with the word night.
| | 03:59 | That's not what it's doing.
| | 04:00 | What it's doing is it translates the d into n
and the a into I and the y into g, right?
| | 04:06 | Do you see that?
| | 04:08 | So already also got affected by that.
| | 04:10 | We're translating each of those
characters that we find, not doing a Find &
| | 04:14 | Replace like you might do
with the word processor.
| | 04:17 | Now we don't have to provide a
simple one-to-one replacement.
| | 04:21 | For example, let me just
paste in an example here.
| | 04:23 | I have a long string and then have
as my search string bedf and then the
| | 04:28 | numbers 5 through 9 in that order.
| | 04:30 | I'm replacing them all with x,
just by itself. See what it did?
| | 04:34 | It found those specific items
and replaced them all with x.
| | 04:38 | To see what it's actually doing, a good
way to do that is just let's put another
| | 04:41 | character there. Let's say z. Now
notice that the b got replaced with the x,
| | 04:46 | but everything else got replaced by the
z. That's because it's repeating the z.
| | 04:51 | It's the same thing as if I've
done something like that, right.
| | 04:54 | Whatever the last item is, it just
gets repeated multiple times until we have
| | 04:58 | enough characters to match our search string.
| | 05:01 | So if the replacement set is
smaller, then the last item repeats.
| | 05:05 | If the replacement set were bigger than
the search set, well then those items
| | 05:09 | will just never get reached.
| | 05:10 | There would be nothing that ever
would map to those remaining characters.
| | 05:13 | So now that you understand what it is
and how it works, what are some real-world
| | 05:17 | use cases where you would use this?
| | 05:19 | Well, one example might be, if you
remember we have a file here called people.txt.
| | 05:24 | I am in unix_files folder already.
| | 05:27 | It just has names of people.
| | 05:29 | Well, what if I wanted to make all
of those lowercase? We could do that.
| | 05:32 | Let's say tr and we'll do capital A-Z
should be translated into a-z and we
| | 05:41 | would not say people.txt, right?
| | 05:44 | There are only two
arguments allowed. Let's try that.
| | 05:47 | You'll see that it comes up and
says no, I only want two arguments.
| | 05:49 | Redirecting our input from the
file, and remember how we did that.
| | 05:53 | So we're now taking the contents of the
file and passing those in as input to tr
| | 05:58 | and then it gives us the output.
| | 05:59 | It now made those all lowercase.
| | 06:01 | You also can use those same regular
expression classes that we just saw in the
| | 06:06 | regular expression movie.
| | 06:08 | upper and then here would be lower, right?
| | 06:17 | Does the exact same thing.
| | 06:18 | So we can use upper and lower
because those are well ordered sets.
| | 06:22 | Using some of those other classes like
punctuation, they would work but the
| | 06:26 | order might not be obvious to you,
what order those characters come in.
| | 06:30 | Another example where it might be useful
is let's imagine that we have something
| | 06:33 | that's in a foreign language, right,
and has lots of accented characters in it.
| | 06:36 | We need to get all those accents out of there.
| | 06:38 | For whatever reason, we aren't going
to be able to print those accents so we
| | 06:41 | need to just take all of the
accented Es and turn them into regular Es.
| | 06:44 | That will do it for us.
| | 06:44 | It will strip them out and
turn them into its equivalent.
| | 06:49 | Another really useful case is let's
imagine we have a file. I have a file here,
| | 06:53 | a new file I've added.
| | 06:54 | It's us_presidents.csv.
| | 06:57 | CSV is for Comma Separated Values.
| | 07:00 | If we take a look at the head of that
file, us_presidents, you'll see that I've
| | 07:04 | got values here that are separated by
commas. so George Washington, 1789, and so on.
| | 07:10 | That's comma-separated Values.
| | 07:12 | It might actually be a little easier to
understand the data by looking at it in a table format.
| | 07:16 | So I've essentially got the number of
the President, their name, the year they
| | 07:20 | started, and finished their term,
their party, their state, and then where
| | 07:23 | their Wikipedia entry is.
| | 07:25 | So that's what I've got.
| | 07:26 | I've just got all these values not in a
table, but separated by comments, right,
| | 07:30 | and then I could import them
into things and that kind of thing.
| | 07:32 | Well, in addition to comma-separated
values, a very common format is to have tab-
| | 07:36 | separated values, and you put
tabs in between each of them.
| | 07:39 | So using tr, that's a cinch, right?
| | 07:41 | We just say tr, take all of the commas
and let's replace those with tabs.
| | 07:46 | Tabs, the special character is
this /t, and so we'll pipe in our
| | 07:50 | us_presidents.csv and
you can see the difference.
| | 07:54 | Now you see those tabs and we can just
take that same thing and let's output
| | 07:57 | it now to us_presidents, and we'll do TSV
for tab-separated values. So there we go.
| | 08:05 | Now I have my comma-separated values
and I have a tab-separated version with
| | 08:08 | just one simple command.
| | 08:09 | So it's the basics if I had to work with tr.
| | 08:12 | There are a couple of options
to give you some nice features.
| | 08:14 | So let's look at those in the next movie.
| | Collapse this transcript |
| tr: Deleting and squeezing characters| 00:00 | In the previous movie we saw how to use tr
by looking at its standard translate usage.
| | 00:05 | The tr has several options that we
can specify that will allow us to work
| | 00:08 | with it in other ways.
| | 00:10 | Instead of translating, it can also
allow you to delete or filter out characters
| | 00:14 | into dedupe repeating characters,
a process that it refers to as squeezing.
| | 00:18 | The 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:25 | So it'd just simply remove them. Instead of
translating them that will just get removed.
| | 00:28 | The -s option will squeeze
repeats that are in the listed set.
| | 00:32 | So it won't necessarily squeeze
everything, but the things that we've told it to,
| | 00:35 | it will compress down.
| | 00:36 | If we had for example five Xs in a row,
it would turn it into one X instead, and
| | 00:42 | then the -c option says Use the
complementary set. It's the opposite.
| | 00:45 | So you would use this option either
with either the -d or the -s option.
| | 00:50 | With the -d option I mean delete characters
that are not in the listed set, right.
| | 00:53 | It's the opposite of that, the reverse.
| | 00:55 | It will become clear once we
actually look at some examples.
| | 00:58 | So let's say that I have a simple string.
| | 00:59 | I have "abc1233deee567f".
| | 01:04 | The threes and the Es are repeated.
| | 01:06 | I am going to take that string.
| | 01:07 | I am going to pipe it into tr, I am
going to use the -d option, and I am going
| | 01:10 | to say the set that I want to specify, the
thing I'm looking for, is this class called digit.
| | 01:14 | So what I am doing is deleting all of
the digits and you can see the output
| | 01:18 | there to the far right.
| | 01:19 | It would then just give me the letters.
| | 01:20 | If use the -c that's the complementary set.
| | 01:23 | So now I'm saying delete
everything that is not a digit.
| | 01:26 | So what I'm left with is just the digits.
| | 01:28 | Now that's not just stripping out the letters.
| | 01:31 | That's also stripping out tabs, line
returns, anything else that might be in
| | 01:35 | that file at all, anything that's not a digit.
| | 01:37 | So a lot of times it's easier to
specify not the thing you're looking for,
| | 01:40 | the thing that you don't want, right.
| | 01:42 | We are filtering out the opposite version.
| | 01:44 | It can be very handy.
| | 01:45 | Notice that in both of those cases there is
only one argument tr now. Before we had to.
| | 01:50 | We were saying translate from a to b;
now we are just saying we want to delete
| | 01:54 | from set a. We don't need to
translate to the other set.
| | 01:57 | So there is no reason to have a second argument.
| | 01:59 | Squeeze works the same way.
| | 02:00 | We just have one argument.
| | 02:02 | This time I have tr -s digit
and that squeezes all the digits.
| | 02:06 | So you notice in the result there to
the far right that now the threes have
| | 02:10 | just become a single 3. It's just 1, 2, 3.
| | 02:13 | If we use the complementary set, well
then it's the opposite. Squeeze everything
| | 02:17 | that's not a digit, so that squeezes the es.
| | 02:20 | So now there's only one e.
| | 02:21 | There are still two 3s in there. It's 1233.
| | 02:22 | It's the es that got compressed.
| | 02:25 | Now again that's the complementary
set, so everything that's not a digit.
| | 02:28 | That would be line returns, that would be
tabs, anything else that's in that file
| | 02:32 | that's not a digit would get squeezed.
| | 02:35 | We can use these two options together.
| | 02:36 | So for example, if I have tr -ds, the
first argument you are going to specify is
| | 02:41 | going to be the thing you want to delete.
| | 02:43 | The second argument is going to be
the thing that you want to squeeze.
| | 02:46 | So in the first example I have there,
it's going to delete all the digits and
| | 02:49 | then it's going to squeeze all of the
letters, and then we come up with abcdef.
| | 02:54 | Now 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:00 | So what the example I have there will do
is it will delete everything that's not
| | 03:04 | a digit and then squeeze the digits.
| | 03:07 | It's not squeezing the
opposite of the digits, right.
| | 03:09 | -c just applies to the first argument to the -d.
| | 03:12 | So that's how it works, but you may be
thinking well why in the world would I
| | 03:15 | need something like this?
| | 03:16 | I mean if you have something like a
file that has an essay in it, why would you
| | 03:20 | want to transform your letters
so that the word sweet became swet?
| | 03:24 | That's not that useful.
| | 03:26 | Well, admittedly you won't use it that
often, but it is a good tool to have it
| | 03:29 | in your toolbox for a couple of special cases.
| | 03:32 | Let's say for example that you want to
remove certain characters from the file.
| | 03:36 | We can remove all no-nprintable
characters from the file for example, right.
| | 03:39 | So remove everything that's not a
printable character out of the file and then
| | 03:43 | presumably I'd be able
to print the file, right?
| | 03:46 | You could also remove other things.
| | 03:47 | You can remove all of the tabs from a file.
| | 03:49 | You can remove all the line returns from a file.
| | 03:51 | You can remove all the punctuation, right.
| | 03:53 | Any of those things are possibilities,
but essentially we are saying go through
| | 03:56 | the file and remove the
things that I don't want in there.
| | 03:59 | One common use case that I want to
give you just so that you have it and can
| | 04:02 | refer to it is that you can remove the
surplus carriage returns and end of file
| | 04:06 | characters that Windows files often have.
| | 04:08 | For the end of line in a Windows file it
has both a carriage return and the line
| | 04:13 | feed, but on Mac and Unix,
that's just a line feed character.
| | 04:17 | So what we want to do is get rid of
extra carriage return character and then
| | 04:20 | Windows must also have an end of file
character to indicate the end of the file,
| | 04:25 | which Unix sometimes doesn't like as well.
| | 04:26 | So we can strip those out and
essentially take something that was a Windows file
| | 04:30 | and make it a happy Unix file by stripping out
the hex codes that I have there. That /015/032?
| | 04:37 | There is no way you would know this.
| | 04:38 | You would have to look those up to know
what they are, but I wanted to give them
| | 04:41 | to you because this is a handy
tool to have in your toolbox.
| | 04:43 | And an example where you might you
squeeze? Well, you might want to remove all the
| | 04:47 | double spaces from your file, right.
| | 04:49 | So every time you hit a period, if you
accidentally hit the spacebar twice, you
| | 04:53 | really wish you'd only done it once,
well, we can squeeze all those spaces out
| | 04:56 | and not just double spaces, but if
you accidentally hit triple spaces or
| | 04:59 | however many more there was, this would squeeze
them all down, so that they would only be one space.
| | 05:04 | There are all sorts of variations on
these. There are ways that you can combine
| | 05:07 | these. You could for example remove
all characters that are not printable and
| | 05:10 | squeeze the spaces in the
file, do those together.
| | 05:13 | The main thing I want you to see
is that tr doesn't just translate.
| | 05:17 | It also has these other special features
which almost could be programs on their
| | 05:20 | own, but they're really just options to tr.
| | 05:22 | So tr -d and tr -s.
| | 05:24 | So make sure that you have
tr -d and tr -s in your toolbox.
| | Collapse this transcript |
| sed: Stream editor| 00:00 | In this movie, we will take a
look at a Unix program called sed.
| | 00:04 | sed is short for Stream Editor.
| | 00:05 | What that means is that sed modifies a
stream of input according to a list of
| | 00:09 | commands before passing it on to the output.
| | 00:11 | It's very similar to the way that tr
works, but with sed, we can modify the
| | 00:15 | stream of input in many more ways.
| | 00:16 | sed is a complex tool that offers
several modes of working and mini features.
| | 00:21 | In this movie, we are just going to focus on
the basics and get to see the most common usage.
| | 00:25 | It's most common usage is for doing
substitution and so what we'll be doing is
| | 00:29 | sed, space, and then providing
an expression for substituting.
| | 00:34 | That will always begin with the letter s
inside the quotes to indicate that it's
| | 00:38 | going to be doing substitution.
| | 00:39 | Then we have got some delimiters, the
forward slash in my example, and then we've
| | 00:43 | got patterns of searching and
replacing, just like we had with tr.
| | 00:47 | So we are going to search for whatever is
in a and replace it with whatever is in b.
| | 00:51 | Notice that the big difference between
this and tr though is that tr actually
| | 00:55 | had two different arguments.
| | 00:58 | It had tr, then the first
argument, and then the second argument.
| | 01:01 | Here, they're all in one argument
and they are inside those delimiters.
| | 01:05 | sed is really based on this one expression.
| | 01:07 | So everything that we wanted to do, the
sort of command that we are sending to
| | 01:10 | sed, is going to be inside those quotes.
| | 01:13 | Let's take a look at some examples.
| | 01:15 | So, for example, let's take some
input, echo upstream, and we are going to
| | 01:20 | pipe that into sed.
| | 01:22 | So then what we needed here is an
expression inside quotes for substitution.
| | 01:27 | I am going to go ahead and put my delimiters.
| | 01:29 | Now, inside each of these, we are going to
need to fill in what we are searching for.
| | 01:33 | I am going to search for up and
we want to replace it with down.
| | 01:37 | So, upstream becomes downstream,
which is appropriate since we are doing
| | 01:41 | stream modification.
| | 01:43 | Upstream, it was upstream.
| | 01:44 | Once it gets downstream, it's downstream.
In between is sed doing the stream editing.
| | 01:49 | Now, notice how this is different
from what we were doing with tr.
| | 01:52 | tr was translating each of those.
| | 01:54 | What we are doing here is we are
searching for one thing and we are replacing it
| | 01:59 | with something else.
| | 02:00 | It's much more like the find and
replace that you might do in a word
| | 02:02 | processing program.
| | 02:03 | Now there is one important thing I need to
show you about sed, which is that by default
| | 02:07 | it doesn't search globally.
| | 02:09 | I will show you what I mean.
| | 02:10 | So let's say we have upstream and
upward and then let's just hit Return.
| | 02:14 | Notice now it changed the first
occurrence of up, not both of them, just the
| | 02:19 | first one got changed.
| | 02:20 | That's different from what
we were doing with grep.
| | 02:22 | When we were greping for something, it
was finding all occurrences of it and
| | 02:26 | that's because in grep,
the g stands for global.
| | 02:29 | It's globally searching.
| | 02:30 | So, just like grep, we need to
provide a g modifier here to say that we want
| | 02:34 | to do this globally.
| | 02:36 | So after this pattern of s and then a
replace string, then we put a g to say
| | 02:42 | that we would like to do that globally.
Downstream and downward now does both of them.
| | 02:46 | Now, the delimiters that we are using here,
these forward slashes, actually are modifiable.
| | 02:51 | It doesn't actually matter which one we have.
| | 02:53 | Let's do, for example, colons. What
sed does is it says, all right, what's the
| | 02:59 | first thing that comes after the s?
| | 03:00 | All right, I see the s, I know we
are going to do a substitution, what
| | 03:03 | delimiters should I use? And whatever we
have is that next character is what it
| | 03:07 | will use as the delimiter.
| | 03:08 | So, we can use pipes like that. It still works.
| | 03:13 | I will just paste in an example to
show you why you might want to do this.
| | 03:17 | The forward slashes are really
the most traditional usage of it.
| | 03:20 | But let's say, for example, that
I had a phrase like MacOS/Unix:
| | 03:22 | awesome and I wanted to change that.
| | 03:27 | Then the search string that I'm
looking for has a forward slash and a colon in it in it,
| | 03:32 | so I don't wan to use either of those first two
choices. Therefore I'm using the upright pipes.
| | 03:37 | Now, I could still use the forward slashes. I
will just have to escape each of those values.
| | 03:42 | Every time it occurred, I would have
to put a backslash before the forward
| | 03:45 | slash to escape it.
| | 03:47 | By switching to a different delimiter,
I can just make it a little cleaner
| | 03:49 | and easier to read.
| | 03:50 | So far, the stream that I've been
using has been texted that I'm piping into
| | 03:54 | sed, but sed also works with files.
| | 03:56 | So, let's say we have sed s and we will
use pear to mango inside our fruit file.
| | 04:03 | Now, I am already inside my Unix files here.
| | 04:05 | Inside my user directory. We
have a file of just fruit there.
| | 04:09 | Now, notice that I did not put
the pipe here. I certainly can.
| | 04:13 | That still works too.
| | 04:14 | This is different from tr.
| | 04:15 | In tr, you had to put this there.
| | 04:17 | with sed, you don't have to.
| | 04:19 | It will take a second argument, which
is the file that you want it to do, and
| | 04:23 | then we can take that and we can pipe that
and we can have something like mango_fruit.txt.
| | 04:28 | Now, we've redirected the
output into this other file.
| | 04:31 | Now, notice here that we
did not use the g modifier.
| | 04:35 | We did that, we didn't use the g
modifier, but at the same time it replaced pear
| | 04:38 | here and it replaced pear down
here, two times. Why is that?
| | 04:43 | It was because each line
gets treated as a stream.
| | 04:47 | Let me show you an example where you
can really see this in action. s/a/x and we
| | 04:51 | will do that with the fruit file.
| | 04:54 | Now, notice here when we had, for example,
banana, it only changed it the first time.
| | 05:00 | It didn't change it the second time
but it did do it on the next line.
| | 05:02 | It found the first occurrence and
then the first occurrence and so on.
| | 05:05 | But then it got on papaya.
| | 05:07 | It only used the first one.
We didn't do it globally.
| | 05:09 | If we switch that and you do the g
in front of our global, now notice the
| | 05:13 | papaya all got changed.
| | 05:16 | So, it's line by line.
| | 05:17 | So when we say global, we are saying
per stream and each one of these lines is
| | 05:21 | being treated as a new stream that's coming in.
| | 05:23 | Now, you can search for just about
anything inside a file and replace it.
| | 05:27 | It's useful for things like let's say
we had a file that was written in Britain.
| | 05:32 | So it has the word colour
in it, spelled with o-u-r.
| | 05:33 | We want to change that to color, the
American spelling without the u. Then we
| | 05:39 | put that global and
whatever file we want to work with.
| | 05:41 | Now, obviously, I don't
have the word color in there.
| | 05:43 | That's the kind of thing you could do
globally to a file and sed would make it easy.
| | 05:48 | You can also provide multiple sed commands.
| | 05:50 | Let's say for example
that I have a phrase like echo.
| | 05:53 | During day time, we have sunlight
and let's take that and we'll do sed.
| | 06:03 | We want to first do s for day and night.
| | 06:09 | That will simply replace the day with nights
and that says during nighttime, we have sunlight.
| | 06:13 | If we also wanted to replace sun with
moon, we can provide a second command and
| | 06:18 | the way we do that is we put -e.
| | 06:22 | It's important that it's lowercase
and that says that we are going to
| | 06:24 | have multiple commands.
| | 06:25 | We are essentially going to build them up.
| | 06:26 | So, -e in the first one, -e, and
then next sed command, s/sun/moon.
| | 06:30 | There we go and now it does both of them.
| | 06:35 | So, all you have to do is prefix it
with that -e option and it says "Oh, what
| | 06:40 | comes next is an argument to -e.
| | 06:43 | Therefore, I am going to take that as
one of my sed arguments and I am going to
| | 06:46 | wait until I get another -e."
| | 06:47 | And I will take that and just sort of build
up the set of filters that we want to apply.
| | 06:52 | So, it's nice. You can actually put
together a five or six of these and change a
| | 06:55 | whole bunch of things all at one time.
| | 06:57 | And you can remember that
it's the e because e is for edit.
| | 07:00 | So we are providing the edits.
| | 07:02 | So each one of these is an edit that
we want for our stream editor to use.
| | 07:06 | Now, we won't go over it, but if you
have a lot of sed commands that you want to
| | 07:09 | run on a single input stream, what
you can do is you can put all these edit
| | 07:12 | commands into a file and you can use the
f option to run all of them at once and
| | 07:17 | that will run every sed
command that's in the file.
| | 07:19 | So you can have a set of regular things
that you want to change, sort of filters
| | 07:23 | you want to apply. Save them to a
file and they're available to you.
| | 07:27 | Anytime you want them, you can just
call up that sed file and say hey, run all
| | 07:31 | of these commands on this file for me.
| | 07:32 | Now, these will give you some examples
of just the simplest substitution that
| | 07:35 | you can do just by using literal characters.
| | 07:38 | Where sed really becomes powerful
was when we start working with regular
| | 07:41 | expressions and that's what we
will look at in the next movie.
| | Collapse this transcript |
| sed: Regular expressions and back-references| 00:00 | In the last movie, we got familiar
with the syntax of sed, but all of our
| | 00:04 | searching so far has been
with literal text strings.
| | 00:07 | Now we're going to learn to
use regular expressions with sed.
| | 00:10 | It may seem like sed is really similar to grep.
| | 00:12 | That's because it is.
| | 00:13 | All sed is, is grep and then substitute.
| | 00:16 | So put another way, anything that you
can find with grep, you can change with
| | 00:21 | sed, and that includes making
good use of regular expressions.
| | 00:24 | So as a simple example of this, let's
just have echo "Who needs vowels?" and
| | 00:31 | we'll pipe that into a sed expression
where we will look for anything that is in
| | 00:37 | a, e, i, o, or u, inside a
character set and we'll replace that with an
| | 00:41 | underscore. We'll do it
globally. So there you go.
| | 00:44 | You see it took all of the vowels that
were in that character set and replaced
| | 00:48 | them with the underscore. So
we can use regular expressions.
| | 00:51 | Now, the regular expressions here work
exactly like they do with grep, meaning
| | 00:55 | that we also have an issue with basic
versus extended regular expressions.
| | 00:59 | So for example, if I put the plus
sign in there, it doesn't work anymore,
| | 01:02 | because the plus is part of the
extended regular expression set and just like
| | 01:07 | grep, we would use the -E option to
be able to use those extended features.
| | 01:14 | So let's try a couple.
| | 01:15 | Let's say we have for example our fruit file.
| | 01:19 | That's just cat fruit.txt and in that
let's writes a sed expression that will
| | 01:25 | take the first line that starts with p,
any line that begins with p, and we're
| | 01:29 | going to replace that with space, space, p.
| | 01:32 | Now, notice I had to repeat
the p again. So I'm finding it.
| | 01:36 | It's going to be part of what gets replaced.
| | 01:38 | So I want to make sure that I still
include it when I finally replace it.
| | 01:41 | So there we go, and we'll just
do our fruit.txt file for that.
| | 01:45 | So everything that had a
p got indented two spaces.
| | 01:49 | We could also leave out the p and
just indent everything two spaces.
| | 01:55 | A variation on that would be to,
instead of having two spaces, put the right
| | 02:00 | angle quote in there.
| | 02:01 | That does the same thing as when
we quote a mail message, right?
| | 02:03 | If we reply to a mail message, our mail
editor might stick those in front of the
| | 02:07 | reply that we're doing.
| | 02:08 | One important thing that you might run
into is you might think, well, instead of
| | 02:11 | spaces in front of every line,
what if I wanted to put a tab?
| | 02:14 | And we have this shortcut for the tab
character, which is the \t. That doesn't work here.
| | 02:20 | sed doesn't understand that \t, or at
least the Mac version of sed doesn't.
| | 02:24 | There are other versions that do, but
the Mac version doesn't understand it.
| | 02:27 | So in order to get it, the trick that
you need to know is that in bash if we
| | 02:32 | want to type a tab character, the
special tab character, the way to do it is to
| | 02:36 | type Ctrl+V and then the actual character.
| | 02:42 | So hit Ctrl+V and then tab, and then
we'll actually get that tab effect.
| | 02:45 | Ctrl+V works for other characters as well.
| | 02:47 | Ctrl+V and Enter, Ctrl+V and Escape.
| | 02:50 | It will type the actual character for you.
| | 02:53 | You won't need it most of the time,
but this is one of those cases where it
| | 02:55 | definitely comes in handy.
| | 02:56 | Let me give you one last
example in this before we move on.
| | 03:00 | Inside the directory I am in I've
added a new file called homepage.html.
| | 03:05 | What that is, is just a basic homepage for a
fake company. So you can use any HTML you have.
| | 03:11 | I just wanted to have some HTML to work with.
| | 03:13 | Let's construct a sed script that will
remove all of the tags from this, all the
| | 03:17 | HTML tags, so that what
we're left with is just text.
| | 03:20 | Well, the way we could that is with sed,
we'll use -E, capital E, so we can make
| | 03:26 | use of the extended sed substitute.
| | 03:28 | And we know we're going to want to
find everything that's inside those tags,
| | 03:32 | the angle brackets.
| | 03:33 | So I'll just do that for now.
| | 03:35 | And then we're going to remove
them globally inside homepage.html.
| | 03:39 | So now we just need to write a
bit more of our regular expression.
| | 03:43 | Inside those angle brackets,
what are we going to have?
| | 03:45 | What's allowed to be in there?
| | 03:46 | Well, you could say a lot of things.
| | 03:48 | I'm going to say that the thing that
defines it is that it is not those angle brackets.
| | 03:54 | It could be any other character
besides those, and I'm not going to be picky.
| | 03:57 | And then we'll put our plus sign after it to
show that there can be more than one of those.
| | 04:01 | So that then takes our HTML
and strips out those tags.
| | 04:04 | You certainly can come up with
better regular expressions certainly using
| | 04:07 | more advanced techniques.
| | 04:09 | You can see, for example, it didn't
filter out this first tag because it's
| | 04:12 | broken across two different lines.
| | 04:13 | It's not perfect, but you do get
the idea of what it can do for you.
| | 04:17 | Now let's talk about back references.
| | 04:18 | Back references are actually part of regular
expressions and sed makes good use of them.
| | 04:23 | Let me show you a good example.
| | 04:24 | Let's say we have echo 'daytime' and we
want to change that using sed, and
| | 04:30 | what we want to make is daytime,
| | 04:32 | it's going to be made into daylight.
| | 04:37 | We might be looking for things
that are much more complicated.
| | 04:39 | We might be looking for not
the literal daytime, right?
| | 04:43 | We might be constructing some
very fancy regular expression here.
| | 04:46 | We might be saying, well, look for anything
that is ... time, we don't care what it is.
| | 04:51 | And what we want to do is take that thing,
whatever that thing was, and use it again.
| | 04:56 | We don't know what it is ahead of time.
| | 04:58 | It's not necessarily day.
| | 04:59 | It might be some other three letters.
| | 05:01 | Well, the way that we do that is with a
back reference and a back reference is
| | 05:05 | the backslash and then the
number of the back reference.
| | 05:10 | So we can have more than one of
these defined in our search string.
| | 05:13 | In fact, I believe it supports up to
nine, so you can have up to nine of
| | 05:16 | these and they will then say "Ah, the first set
of parentheses, well, that corresponds to \1.
| | 05:21 | The second set of parenthesis,
that corresponds to \2, and so on."
| | 05:26 | Now, if we try and run this, it
doesn't work, and that's because these
| | 05:30 | parentheses here have to either be
escaped to work with basic regular
| | 05:36 | expressions or if we don't
escape them, we have to say this is an
| | 05:41 | extended regular expression.
| | 05:43 | So anytime we use those parenthesis in
there, it has to either be extended or
| | 05:47 | they have to be escaped.
| | 05:49 | And just to make sure that you
understand the difference here, let's say
| | 05:51 | instead of day, let's say that it said
something like, we'll put xxx for now.
| | 05:57 | So you can see that it took those
same three characters that it found.
| | 06:00 | It didn't care whether they
were day or something else.
| | 06:03 | It took those and dropped
them into the replacement string.
| | 06:06 | Let me give you a real world example.
| | 06:08 | I think that will make it
clear why this is really useful.
| | 06:11 | Let's say that I have a name like Dan Stevens.
| | 06:14 | I can pass that into a sed script that
will say all right, take any characters
| | 06:19 | that could be in the first name,
followed by a space, followed by any
| | 06:23 | characters, and then reverse
them with a comma between them.
| | 06:26 | Look at that. Dan Stevens
suddenly become Stevens, Dan.
| | 06:30 | So you can see how you can do this,
not just to this little bit of input that
| | 06:33 | I'm sending it, but you
could do it to an entire file.
| | 06:36 | Let's try something
similar by using our fruit file.
| | 06:39 | So I have a sed expression here that's
going to look for either apple, pear,
| | 06:43 | plum, or peach, and for any of
those it will append tree after it.
| | 06:47 | So we get pear tree, then we get
raspberry banana, then we get peach tree,
| | 06:52 | apple tree, pineapple tree.
| | 06:54 | So any of those that matched our
regular expression got reused in our output.
| | 06:58 | Now, there is a lot more that sed can
do, but this shows you some of its most
| | 07:02 | common uses, and I think it gives
you a solid foundation for exploring
| | 07:06 | further on your own.
| | Collapse this transcript |
| cut: Cutting select text portions| 00:00 | So far we've seen Unix tools that will
let you find, translate, and replace.
| | 00:05 | The next useful Unix tool I
want to introduce you to is cut.
| | 00:09 | Cut allows you to cut out
selected portions of each line of a file.
| | 00:13 | We could probably write a sed command
that would do something similar to what
| | 00:16 | cut does, but cut is much
simpler and easier to use.
| | 00:19 | The first thing you need to know
about cut is that it can cut three things,
| | 00:22 | characters, bytes, or fields, and we are
always going to need to pick one of those.
| | 00:27 | We are only going to be looking at
characters and fields, and the reason why is
| | 00:31 | that bytes in English is going to be
exactly equivalent to characters,
| | 00:34 | because every character takes up exactly 1 byte.
| | 00:37 | And really, bytes are there mostly for
when you are thinking about raw data,
| | 00:41 | not actual characters, but raw data, and you
want to grab a certain amount of bytes out of it.
| | 00:46 | We're working with text files here, so
we're really thinking about characters,
| | 00:48 | and most times
that's what you will be doing.
| | 00:50 | So characters and fields is
where we're going to focus.
| | 00:52 | So let's start with characters.
| | 00:54 | Notice that I am inside my user directory,
inside unix_files, and inside there I
| | 00:59 | have this file that I created
earlier called dir_content.txt.
| | 01:03 | If we take a look at that file you will
see that it's just a directory listing.
| | 01:08 | It looks very much like the output
from ls-la, because that's exactly what I
| | 01:12 | did, ls-la earlier, and I
directed that output into a file.
| | 01:16 | So this is not my current director listing.
| | 01:18 | It's a snapshot of what it was previously.
| | 01:20 | So here's the scenario. Imagine that
we have a file like this and we say, you
| | 01:24 | know what, I like the data here,
but I really wish I could just grab a
| | 01:28 | selected portion of it.
| | 01:29 | For example, I really wish that I
could grab these permissions out of here,
| | 01:33 | just that line and that part of this line,
all the way down, essentially grabbing a column.
| | 01:38 | Just wish I could grab that
column of these permissions out of it.
| | 01:42 | That's the purpose that cut serves.
| | 01:44 | So we say cut and then we
need to specify an option.
| | 01:47 | Always have to specify an option.
Either -c for characters, which is what we'll
| | 01:52 | be doing, or -b for bytes, or -f for
fields, which we will see in a moment.
| | 01:56 | And then we tell it what
characters we want to cut.
| | 01:58 | Well, out of each line we'd
like to cut characters 2-10.
| | 02:03 | That's what represents these characters,
starting with character 2, going until
| | 02:06 | we get to character 10.
| | 02:08 | And then we need to say the file that we want to
do that from, and there it is. It's that easy.
| | 02:13 | Now, notice that it also
grabbed this up here, total 144.
| | 02:17 | That was the top line here.
| | 02:19 | It went ahead and grabbed that as well.
| | 02:20 | So be aware of that.
| | 02:22 | You actually, I believe, can suppress
the output of this from your directory
| | 02:26 | listing if you needed to, but for
our purposes, we just really want to
| | 02:29 | illustrate the way that cut works.
| | 02:30 | Now, let's say we wanted to cut something
else out of this. We can cut more than one thing.
| | 02:35 | We can grab more data.
| | 02:36 | So in addition from cutting the
permissions, let's say that we also wanted
| | 02:40 | to keep the file size.
| | 02:41 | So we wanted the permissions, followed
by the file size, and then after that we
| | 02:45 | will grab the file name as well.
| | 02:47 | So what we'll be left with is
permissions, size, name. Everything else will
| | 02:51 | end up being removed.
| | 02:53 | So what we need to do is we look at
the data here and you can see that this
| | 02:56 | is the last column, so what we want
to do is we want to find out how many
| | 03:00 | characters is this?
| | 03:01 | How many do I need to skip over
essentially till I get to more data that I want?
| | 03:05 | So I am going to use Command+C to copy that.
| | 03:07 | Let's just do echo and we'll put that
in quotes and pipe it into Word Count.
| | 03:13 | Word Count tells me it's 21 characters.
| | 03:15 | So now I know I need to skip over 21
characters, so 2-10, and then we'll do a
| | 03:21 | comma, followed by skipping 21
characters starting with 10, so that would be 31.
| | 03:27 | Now, I would also like to leave a space
between them, so I am actually going to
| | 03:30 | keep the space in there before it.
| | 03:32 | So that would be going to 30
and then I'll go up to 35.
| | 03:36 | That will give me the space plus the
four characters here that represent the size.
| | 03:40 | So there we go, now I have the size.
| | 03:43 | Let's say I also want to
get the name, and it's here.
| | 03:47 | I want to leave a space here
so I'll keep this space in.
| | 03:51 | So I want to know how wide is that? Copy it.
| | 03:53 | echo it into Word Count.
| | 03:56 | That's 14 characters.
| | 03:58 | So now I go back up here and I add 14
onto this and I come up with 49 to the end.
| | 04:04 | If you want to go all the way to the end,
you can just put a dash with nothing after it.
| | 04:08 | You can do the same thing at the
beginning too if you knew you wanted to get
| | 04:10 | everything at the beginning.
| | 04:11 | So now, look at that. I have
sort of my own custom ls listing.
| | 04:15 | We can take this same thing, let's
copy it, and we can actually do that.
| | 04:20 | ll, pipe through this cut statement, and look
at that. I can see exactly the data I want.
| | 04:27 | I can leave everything else out of there.
| | 04:29 | You could take, for example, your
history file and let's grep that for
| | 04:34 | everything that has fruit in it and
then let's take that and let's pipe that
| | 04:37 | into cut, and I am going to cut
everything 24 characters to the end.
| | 04:42 | So I have got just then the command itself.
| | 04:45 | I left off everything that
was at the beginning of it.
| | 04:47 | Or we could take ps aux, your listing of all
your processes, and let's cut 11-15 and 72-end.
| | 04:55 | Now, I have just got the process ID and
what's actually running? Everything else
| | 05:02 | that's inside ps aux. pipe
it so we don't see everything.
| | 05:06 | Now, all this stuff in
the middle, that's all gone.
| | 05:08 | I can really condense it down to just
what I want to see, just the process ID
| | 05:13 | and what is running.
| | 05:14 | I leave out all those other stats.
| | 05:16 | So that's how cut works.
| | 05:18 | Cut also has this -f option that is really nice.
| | 05:21 | I have a file here that I created
earlier called us_presidents.tsv,
| | 05:27 | tab-separated values.
| | 05:28 | So it's just an
information about U.S. Presidents
| | 05:31 | that is tab separated values.
| | 05:33 | You could use any other tab
delimited file that you wanted to for this.
| | 05:37 | The tabs are important, and the
reason why I say that is because if we use
| | 05:41 | cut -f, by default what it's going
to do is use those tabs to figure out
| | 05:46 | where the columns are.
| | 05:48 | So if I say I want fields 2, 6 out of
us_presidents.tsv, look at that. I get
| | 05:56 | just the column that has the
President's name in it and then I get just the
| | 06:01 | state that they're from.
| | 06:02 | Notice that it also kept the tabs as
delimiters between these. So it's great!
| | 06:06 | You can have something that's like tab-
separated values from a spreadsheet and
| | 06:10 | you can just grab the
columns out of it that you want.
| | 06:13 | We have something here which we can
then save as another tab-separated file.
| | 06:18 | So we can save this as presidents_states.tsv.
| | 06:24 | And now we have this file saved
that we can then continue to work with.
| | 06:27 | We can now join that with other things,
we can use it in sed scripts, whatever
| | 06:31 | we want to do. We've just
zeroed in on the data that we want.
| | 06:34 | Now, I want to show you that we
have another example of a file.
| | 06:37 | I have us_presidents.csv and
that's comma-separated values.
| | 06:42 | We worked with that earlier as well.
| | 06:44 | It's the same thing, but instead of
tabs we have commas between the data.
| | 06:47 | If we tried the same thing that we
did before but on the csv file, let me
| | 06:52 | just clear this so you can see, using
csv this time, asking for columns 2 and
| | 06:57 | 6, it doesn't work.
| | 06:58 | It gives me everything back, because it
was expecting the tab to be the delimiter.
| | 07:03 | If you want to change the delimiter,
then you have to use the -d option. So -d
| | 07:09 | and then tell it what delimiter is.
| | 07:10 | So if it's a comma, now it does it.
| | 07:14 | Notice that it still kept the comma as
the delimiter in the output as well, but
| | 07:18 | the main thing is that this -d
followed by what it should be looking for.
| | 07:22 | By default it's tab.
| | 07:23 | There are a couple of
other options that it offers.
| | 07:25 | There is a -s option and that would do
nothing to lines that don't have delimiters.
| | 07:30 | It would just let them through on their own.
| | 07:32 | That's all there is to using cut.
| | 07:33 | It's a fairly simple tool to use, but
it's really quite powerful, especially
| | 07:37 | when used in conjunction with a
lot of the other Unix commands and
| | 07:40 | techniques that we've learned.
| | Collapse this transcript |
| diff: Comparing files| 00:00 | In this movie we're going to take a
look at a Unix tool called DIFF, which is
| | 00:03 | useful for comparing two files.
| | 00:05 | Imagine that a client sends
you revisions to a text document.
| | 00:08 | You still have the original document but
you can't tell where the client changed.
| | 00:12 | You could put the two documents side-by-
side and then scan each one looking for
| | 00:16 | the differences then every time you find
a difference you could take note of it.
| | 00:18 | That's exactly what DIFF does for
you, but much faster and with greater
| | 00:23 | precision than you could.
| | 00:24 | Using DIFF is easy.
| | 00:25 | Notice that I am inside my user
directory and inside unix_files and we're going
| | 00:29 | to be working with two files
that I've added to this directory.
| | 00:32 | The first one is original_file.txt,
the other one is revised_file.txt.
| | 00:39 | Both of these are included in the
Exercise Files but they're also easy for
| | 00:42 | you to create yourself.
| | 00:43 | The first one just has several lines
that say delete, several lines that say
| | 00:47 | change, and several lines that say append.
| | 00:49 | And then on the far left side I've
included the line numbers for reference.
| | 00:53 | The revised file has something very
similar except that now I've deleted one of
| | 00:57 | the lines that says delete, changed
one of the lines that says change, and
| | 01:01 | appended a line to the
section of lines that say append.
| | 01:04 | Now what we want to do is ask DIFF to
compare these two files and report the
| | 01:08 | differences between them.
| | 01:09 | We do that with diff and then as
the two arguments we pass in the two
| | 01:14 | filenames we want to compare.
| | 01:15 | Typically, you want to put the
old or original file on the left.
| | 01:19 | You don't have to but that's sort of
typical. original_file.txt revised_file.txt.
| | 01:25 | DIFF compares the two files
and reports the changes to us.
| | 01:29 | Now the way that it reports those
changes might seem a little cryptic at first.
| | 01:32 | So let's understand what it's telling us.
| | 01:34 | It found three changes altogether.
| | 01:36 | The first change is
represented by these two lines.
| | 01:38 | The d in the first line is letting
it know that it detected a deletion.
| | 01:42 | Something was deleted.
| | 01:43 | The numbers on either side of the d let
us know the line number where this would
| | 01:48 | occur in each of the two files.
| | 01:50 | The number on the left corresponds
to the file that was passed in on the
| | 01:54 | left, the first argument.
| | 01:55 | The number on the right of
corresponds to what I call the right file.
| | 01:59 | That's the second argument.
| | 02:00 | Notice also then that it tells us the
text what was deleted and it has an angle
| | 02:05 | bracket at the beginning that's acting
as an arrow letting us know that this
| | 02:08 | occurs in the left file, original file.
| | 02:11 | Second change that it found is
described by these four lines.
| | 02:15 | Now instead of a d we have a c
letting us know that there was a change.
| | 02:19 | Once again, we have the line
numbers and you can see why it's useful to
| | 02:22 | have those line numbers.
| | 02:23 | Because the position of these two
lines, even though they're being
| | 02:26 | compared, it's changed.
| | 02:27 | And if we had a lot of deletions,
it might have changed a lot.
| | 02:30 | This line might have jumped
up a hundred lines in the file.
| | 02:33 | Having the line number helps us to be
able to locate the change regardless of
| | 02:36 | what else has happened.
| | 02:37 | Notice now instead of one line, it
gives us two lines with dashes in between
| | 02:42 | the two and the first one has an
arrow pointing to the left letting us know
| | 02:45 | that this is the text as it exists in
the left file, and then we have an arrow
| | 02:49 | pointing to the right thing, letting us know that
this is the text as it exists in the right file.
| | 02:53 | And then last of all, we have append
which uses an a. So we have d, c, and a.
| | 02:58 | Once again, it tells us the line numbers.
| | 03:00 | Don't let it throw you.
| | 03:00 | This says 11 and this says 12.
| | 03:02 | That's because we deleted line 2.
| | 03:05 | The arrow this time points to the
right letting us know that this is the text
| | 03:08 | that exist in the right file.
| | 03:10 | So deletes will always point to the
left, appends will always point to the right,
| | 03:14 | and changes will always have
arrows that point in each direction and show
| | 03:17 | us the text from both.
| | 03:19 | So even though it may have seemed
cryptic at first, once we understand how to
| | 03:22 | read it, it's actually a very
efficient way to describe the changes.
| | 03:26 | There are a number of other formats
that we can output these results in.
| | 03:28 | This is just the default one.
| | 03:30 | We'll take a look at those but before
we do that, let's take a look at some
| | 03:33 | of the options that we can pass to DIFF for
how it goes about comparing the two files.
| | 03:37 | If we give DIFF the i option, then it
performs a case insensitive comparison.
| | 03:42 | A capital A and a lowercase a
would be considered exactly the same.
| | 03:45 | It would not bother
reporting that difference to us.
| | 03:48 | If we want it to ignore changes to the
blank characters like Space and Tab or
| | 03:52 | all of the white space or the number of
blank lines that are in the file, well,
| | 03:55 | then we could use the b, w, or capital B options.
| | 03:59 | DIFF doesn't just compare files.
| | 04:00 | It'll actually compare
whole directories of files.
| | 04:03 | We can use the lowercase r option to
recursively compare two directories.
| | 04:07 | So then it will look for files that
have the same name in each of those two
| | 04:10 | directories and tell us what the
differences are going down each and every
| | 04:14 | one of those files.
| | 04:15 | Now if two of those files are
identical, it won't report it.
| | 04:18 | It'll just ignore it and just tell us
about the ones that actually have differences.
| | 04:22 | If we also want it to tell us about the files
that are identical, we have to use the s option.
| | 04:27 | These are the primary options for
controlling how DIFF does this comparison but
| | 04:30 | there are a few others and you can
read the Man Pages to see what those are.
| | 04:33 | But these are the most common ones.
| | Collapse this transcript |
| diff: Alternative formats| 00:00 | In this movie we're going to learn to
control the format the DIFF uses for output.
| | 00:04 | We can have an output that's something
called copied context, which it would be c
| | 00:08 | option. We can use the u option for
unified context, and then we can have y for
| | 00:13 | side-by-side comparison.
| | 00:14 | There's a q option that will tell us
only whether the two files are different.
| | 00:18 | It won't report what that
actual changes were to us.
| | 00:21 | It'll just give us a single line
saying yes, the two files are different.
| | 00:24 | There are also a couple of other output
formats but I think these are going to
| | 00:27 | be the most common and the most useful for you.
| | 00:29 | Let's take a look at them.
| | 00:30 | So let me just clear my screen and
let's do that same thing but now let's pass
| | 00:35 | in that -c for copied context.
| | 00:38 | Now notice what it does is it gives us
the whole first section, lines 1 through 11.
| | 00:43 | It lets you know that here at
the beginning. And here it is.
| | 00:45 | That's everything that
was in the first document.
| | 00:48 | And then below that is everything
that was in the second document.
| | 00:50 | And it gives us a minus here letting us
know that something got deleted and it
| | 00:54 | gives us an exclamation point letting us
know when something changed, and a plus
| | 00:58 | down here letting us know
when something was added.
| | 01:01 | So this gives us essentially one set
of changes and then right below it is
| | 01:04 | another set of changes.
| | 01:05 | Let's take a look at the y option next
because that does essentially the same
| | 01:10 | thing but it puts them side-by-side.
| | 01:12 | Now in my screen it can be widened a
little bit but if you used a smaller font
| | 01:16 | in Terminal you would be able
to see them really side-by-side.
| | 01:19 | What it does here is it shows us one
file on the left, one file on the right,
| | 01:22 | and then puts some symbols here in the
middle to let us know if something was
| | 01:25 | deleted, changed, or added.
| | 01:27 | Now let's look at the u option.
| | 01:29 | That's actually the one that I prefer.
| | 01:31 | This gives us unified context.
| | 01:33 | What that means is it essentially
is smashing the two files together.
| | 01:36 | What we're seeing is not what's in
either file one or file two, but if we
| | 01:40 | merged them together, you'll see that
we have line 2 here with a minus next to
| | 01:43 | it letting us know that this is something
that's going to get deleted from the original file.
| | 01:47 | And down here we can see that the
line is added, has a plus on it, and then
| | 01:51 | the lines that have been changed here
have a minus and a plus. The minus lets
| | 01:55 | you know up here corresponds to original file
while the plus corresponds to the revised file.
| | 01:59 | Let's take a look at the q option
so you could see what that does.
| | 02:03 | If we compare the two files with q, it just
comes up and says yes, these two files differ.
| | 02:08 | That's it.
| | 02:09 | it doesn't tell us what the changes are.
| | 02:10 | Now let's just try it with two
files that are the same such as the file
| | 02:14 | compared with itself.
| | 02:15 | Original file compared to original file.
| | 02:18 | Now we get no output back.
| | 02:19 | That's because they are the same.
| | 02:20 | If we wanted to report the fact that
they're the same, we need to use that s option.
| | 02:24 | And then it comes back and
says yes, they are identical.
| | 02:27 | Now let me just show you a few more things.
| | 02:28 | Let's go back up and let's pick
this -u option version comparing original
| | 02:32 | file with revised file.
| | 02:33 | What I want to do now is I want to
output that into a file. We'll call it
| | 02:38 | original_revised and the file
extension that want to use would be diff.
| | 02:43 | That's the standard for things
which are an output from DIFF.
| | 02:46 | So now we have what are the changes
between these two documents saved as a document.
| | 02:50 | Let me also just show you that in a text editor.
| | 02:53 | I have TextMate installed and I can
just use the pipe it into mate and that'll
| | 02:58 | pop it open in TextMate.
| | 02:59 | I just want to show you that when you
view that diff file it's something like a
| | 03:02 | text editor that offers code coloring.
| | 03:04 | It may offer you this kind of coloring for
each of the lines which can be really handy.
| | 03:08 | So now I can really easily see this is
what was deleted, the green line down
| | 03:11 | here is what was added, and the two
lines here side-by-side highlights the
| | 03:15 | two changes for me.
| | 03:16 | Last of all, I want to show you that
you can take this -u version and instead
| | 03:19 | of piping it into my text editor, I am going to
pipe it into another Unix tool called diffstat.
| | 03:24 | And it has to be the -u option for this to work.
| | 03:27 | Doing the default one won't work.
| | 03:28 | But if we pass -u into diffstat, it
will come back and it'll tell you what the
| | 03:33 | changes look like. Just
a summary of those changes.
| | 03:35 | Now a change in a file, neither
delete or an append, but a change will be
| | 03:40 | described as both an insertion and a deletion.
| | 03:43 | So it lets you know here that it basically
has two pluses and two minuses in this file.
| | 03:48 | Four lines changed altogether.
| | 03:50 | If we were looking at a comparison
of directories, well, then we'd have a
| | 03:52 | whole list of these.
| | 03:53 | That might help you to very quickly
identify where the bulk of the changes
| | 03:56 | occurred between the two.
| | 03:58 | Now at the risk of stating the obvious,
let me say that of course DIFF works
| | 04:01 | best on files that are similar, such as
our example where we have an original
| | 04:05 | file and a file that's been modified.
| | 04:07 | It's not very useful to compare
two things that are really different.
| | 04:10 | Let's say the Declaration of
Independence with the Magna Carta.
| | 04:12 | Well, then it would give you back every
line as a difference. That's not useful.
| | 04:17 | Of course, they're
different on every single line.
| | 04:19 | But if we're really trying to just
track the changes where we know that
| | 04:22 | something is similar but has a few
differences, we really want to just identify
| | 04:26 | those differences, then DIFF
becomes an indispensable tool.
| | Collapse this transcript |
| xargs: Passing argument lists to commands| 00:00 | In this movie we'll learn to use
xargs to pass argument lists to commands.
| | 00:05 | xargs is kind of a funny name.
| | 00:06 | It's short for execute as arguments.
| | 00:09 | What xargs does is it parses an
input stream into items and then it loops
| | 00:13 | through each item in that
list and passes it to a command.
| | 00:16 | I think it's easier to understand
if we actually see it in action.
| | 00:19 | So you remember that we had the
wc command before for word count.
| | 00:23 | So if we do word count on our
lorem_ipsum.txt file that's inside our Unix files
| | 00:28 | directory, you see that it comes back
and it tells us the number of lines,
| | 00:32 | words, and characters that are in lorem_ipsum.
| | 00:33 | Now, let's try a variation on this.
| | 00:35 | Let's say if we want to echo lorem_ipsum
.txt and we want to pipe that into wc.
| | 00:43 | Now what it's doing is it's saying
not the file lorem_ipsum.txt, but this
| | 00:47 | string, this actual text lorem_ipsum.txt,
how many characters does that have? There is 16.
| | 00:53 | So that's what it's doing.
| | 00:54 | It's not looking at the file.
| | 00:55 | It's looking at the string.
| | 00:57 | If we instead want to pass that string
to wc as an argument, then we use xargs.
| | 01:03 | That's what it does.
| | 01:04 | So now we get the exact
same result as we did before.
| | 01:08 | It's the exact same thing.
| | 01:10 | It took the string and the input
and it passed it to the command wc.
| | 01:15 | The man pages refer to
this as the utility commands.
| | 01:18 | So it passes it to its utility command.
| | 01:19 | xargs gives us a nice way to see what
it's actually doing using the -t option.
| | 01:24 | So now we can actually see the
command right before it runs.
| | 01:27 | It will output what its doing.
| | 01:28 | So it's running wc lorem_ipsum.txt.
| | 01:32 | Now, if we don't supply a utility command
at all, let me just show you what it does.
| | 01:36 | It actually just echoes.
| | 01:37 | it calls bin echo and then the file.
| | 01:39 | So as a default setting, it will
just echo that argument back to you.
| | 01:42 | Now, that explains what xargs does.
| | 01:44 | It passes its input as an
argument to a utility command.
| | 01:48 | But the power of xargs is in
looping through a list of arguments.
| | 01:52 | Let's imagine that instead of just
this that now instead we are going to have
| | 01:55 | lorem_ipsum.txt us_presidents.csv.
| | 02:01 | And let's go ahead and put that
again with the word count at the end.
| | 02:04 | Now you can see that it passes both
of these as arguments to word count.
| | 02:10 | So it's the same as if we had typed
we lorem_ipsum.txt us_presidents.csv.
| | 02:15 | But sometimes you don't want xargs to
run its command with all of the arguments.
| | 02:20 | What we want is for it to loop.
| | 02:22 | And so we can do that by using the -n
option to limit how many arguments it passes.
| | 02:26 | So we'll just say 1 to start with.
| | 02:28 | We can also take that space away to
make it a little more clear that the 1 goes
| | 02:31 | with the n, and now it will take one
argument, pass it to wc, and then it will
| | 02:37 | loop and it will take the
next argument and pass it to wc.
| | 02:39 | So n is the number of arguments
that we use on each loop through.
| | 02:43 | So now that you see the difference
between what it did before. Now it's actually
| | 02:47 | calling wc two times, not once, but twice.
| | 02:50 | And to see an example of this using
higher numbers of n, let's say that we had
| | 02:54 | echo 1 2 3 4, and let's pipe that into
xargs, and then -n2, and we don't have to
| | 03:02 | specify anything. We're
just going to echo those back.
| | 03:04 | Now you see what it does.
| | 03:05 | 1 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:10 | It's passing in the first two, then it
goes through again. A second loop and
| | 03:14 | passes in the next two.
| | 03:16 | If there had been an uneven number,
well, then that last one would just get
| | 03:19 | passed in by itself.
| | 03:21 | So for example, let's say that we have ls to
list our directory. We can pipe that into xargs.
| | 03:26 | That will be all of the file names in
our directory, and we'll do -n 3 and
| | 03:31 | echo, and now you see we get three
file names and then we get a new line.
| | 03:35 | We get three more file names, because it's just
echoing each of those file names broken up.
| | 03:39 | xargs also has a -L option
which limits what it takes in.
| | 03:43 | Let me show you that.
| | 03:45 | If I just had echo 1 2 3 4 and I
pipe that into xargs with the capital L 2
| | 03:53 | option, you don't really notice a
difference, because it's all just one line.
| | 03:57 | But let's instead use a multiline
document and it will become a lot clearer.
| | 04:00 | Let'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:08 | That's the default. What we're
seeing is the first two lines of the file
| | 04:13 | echoing back to us.
| | 04:14 | If I instead use -n, you'll see that
we get the first two words of each line.
| | 04:19 | Now, the n and L options are mutually
exclusive, so you don't want to use both.
| | 04:23 | And they can be a little bit tricky,
because xargs can find its arguments by
| | 04:26 | splitting on either spaces or on lines,
and it depends a little bit on the
| | 04:30 | format that the input was sent in and whether
or not you've specified the n or L the option.
| | 04:35 | It can be a little tricky, so you may
have to play with it a bit to get it to do
| | 04:38 | exactly what you want.
| | 04:39 | Let me show you another example.
| | 04:40 | Let's say we have cat, our file
fruit.txt, and we'll pipe that into xargs.
| | 04:46 | So what xargs is going to do is just
echo each one of those back in a line.
| | 04:50 | But here is what I want to show you.
| | 04:51 | We can actually instead of just
saying we want to echo this, we can actually
| | 04:55 | put a placeholder. We'll specify it
with the capital I option, then we can use
| | 04:59 | the two curly braces and then we can
actually place this somewhere in position.
| | 05:04 | So for example, I can have buy more:
| | 05:07 | and then it will drop in the value where
I've put this placeholder. See what it does?
| | 05:14 | So now we have the ability to actually
not just take that argument, but to take
| | 05:17 | that argument and put it inside
something, to put in a specific place rather
| | 05:21 | than just forcing it to just
use the argument by itself.
| | 05:25 | You don't have to use these curly braces.
| | 05:26 | That's the most common one, but you can
actually specify absolutely anything you want here.
| | 05:31 | So I could have fruit. You just want to
make it something that's unique. :FRUIT.
| | 05:39 | Right? And it will drop it in there.
| | 05:40 | So all you're doing is saying all right,
with the -I option here is what I am
| | 05:44 | going to declare as my placeholder and
wherever you see that, that's where the
| | 05:47 | argument ought to go.
| | 05:48 | There is one last catch to using xargs.
And that's that xargs, when it tries to
| | 05:53 | figure out where the arguments are,
it will split things up based on either
| | 05:56 | spaces or control characters.
| | 05:58 | This can cause some problems.
| | 05:59 | So for example, let's say I do ls of
my Library directory. So inside Library,
| | 06:05 | let's take a look at what's in there.
| | 06:07 | Let's just grab the first few of
those by piping this through grep.
| | 06:11 | So in grep I'll look for A, followed by
anything that comes after it, and so it
| | 06:15 | will just show me those first four.
| | 06:18 | Now, let's take that list and let's
pipe that into xargs and let's just have
| | 06:21 | it echo it back to us.
| | 06:22 | That's all we're going to do. We'll
use the n1 option, so it will break it.
| | 06:25 | Each item that it finds, each argument,
will get sent through to echo one time.
| | 06:30 | See what happened?
Application Support got broken in half.
| | 06:34 | Autosave Information got broken in half.
| | 06:36 | It thought they were each different
arguments, because it split it up on the space.
| | 06:39 | So you see why this causes the problem.
| | 06:42 | In order to get around this what we need
do is the -0 option and that will tell
| | 06:48 | it to split on null characters as
separators instead of on spaces and new lines.
| | 06:53 | So now when I hit Return, now
it correctly does break them up.
| | 06:58 | The main place that you are going to
use that -0 option is with file names.
| | 07:02 | If you're working with file names, you
probably want to use that -0 option to be safe.
| | 07:07 | So it's a bit of a quirk, but it is
something that you have to keep in mind as
| | 07:10 | you're passing things to it is, is
there a chance that there will be a space in
| | 07:14 | the data that I'm passing in
that would screw things up?
| | 07:16 | That gives you a good
explanation of what xargs does.
| | 07:20 | Next, 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:00 | Now that we have seen how xargs works,
I want you to help you to see the power
| | 00:03 | that it gives by demonstrating some of its uses.
| | 00:06 | There's no limits what you can do with
xargs, but these example should give you
| | 00:09 | an idea of how other people typically
use it and get you thinking about ways to
| | 00:12 | make it work for you.
| | 00:13 | The first example I want to show you is how
to use xargs with what I call File Manifest.
| | 00:18 | Notice that I am inside my user
directory and inside UNIX files and in there,
| | 00:22 | there is a file called file_manifest.txt.
| | 00:25 | It's just simply a list of file names of
files that are in the same current directory.
| | 00:30 | The idea here is that I have selected
the files that I care about and have
| | 00:32 | arranged them in a certain order.
| | 00:34 | Now I can use xargs to do something with those.
| | 00:37 | Now what you do with them is up to you.
| | 00:38 | You can open them, you might grep them,
you might print them, or you might just
| | 00:43 | want to concatenate their contents together.
| | 00:45 | They might represent file templates
that we want to copy over whenever we
| | 00:48 | start a new project.
| | 00:50 | For now, I'm just going to use cat
just to show you the contents of them and
| | 00:53 | then pipe it through less, so that we
just don't get a huge stream of output.
| | 00:56 | So there you go, you can see
that that's exactly what it did.
| | 00:59 | It took each of these files and put
them together in the order I specified.
| | 01:03 | Imagine that you had 20 different
files that all belonged to one project and
| | 01:06 | you wanted to assemble those together in a
certain order for presentation or something.
| | 01:11 | You could use this file manifest
technique to be able to manage them.
| | 01:14 | It's not just file names that
we put in a file that we can use.
| | 01:17 | We could for example use
our fruit file, fruit.txt.
| | 01:20 | That's just going to be a simple list of fruit.
| | 01:22 | Let's take that and let's
pipe it through sort and unique.
| | 01:25 | So we have the unique list of fruit.
And then we will send that to xargs.
| | 01:30 | And in xargs, we will use our
placeholder specifier, make a directory, including
| | 01:35 | the parent directories, and we will
put it on our desktop, a folder called
| | 01:40 | fruits and in that we will put a
folder for each of these fruits.
| | 01:45 | There it is, we have fruits now that
appeared on our desktop. I am going to
| | 01:47 | open that folder up.
| | 01:48 | I have got a folder for each one of my fruits.
| | 01:51 | Imagine that we have got a class of 30
students and we need to create a folder
| | 01:55 | to put each student's
work in for all 30 students.
| | 01:58 | We take the list of their first names
and last names, pass it to xargs and boom,
| | 02:02 | we have created 30 folders
with just one single command.
| | 02:05 | You can see how it can really speed
up repetitive tasks that we normally
| | 02:08 | would do in the Finder.
| | 02:09 | Let me give you another example. Let's
say that we have our process list and we
| | 02:13 | want to pipe that through grep and we
are going to look for some process, some
| | 02:17 | set of processes that have gone bad
somehow, we need to get rid of. There are
| | 02:21 | bad processes we'll call them and if I
just hit Return on that, you will see
| | 02:25 | that it comes up and if finds the
grep that I am doing for. bad process.
| | 02:28 | That's the only process that matches.
| | 02:30 | So this is just a demo, and what we
will do is we will pass that in to cut and
| | 02:34 | we will grab, 11-15, which will
represent this process ID here, and then we will
| | 02:39 | take all of those process IDs that it
finds, pass them in to xargs with kill -9.
| | 02:45 | Boom, we just killed all of our bad processes.
| | 02:48 | Now it came up and told me there were
no such processes and that's because
| | 02:51 | this process that if did find, grep
bad processes, is gone by the time it gets
| | 02:55 | over here to kill it.
| | 02:57 | It started and it stopped before
kill could ever get a hold of it.
| | 03:00 | But you see the concept and it shows you
how you can use xargs in some unusual places.
| | 03:04 | In general xargs works really well with grep.
| | 03:06 | So for example, we had grep for apple
inside all the files called fruit.txt and
| | 03:12 | it gives us back all of those matches.
| | 03:15 | Remember we used the -l option in
front of it, dash lowercase L. Now it gives us
| | 03:20 | just the file names themselves.
| | 03:22 | So now these files are
ready to be passed into xargs.
| | 03:25 | We have found everything that had a
match. Now we can do whatever we want with
| | 03:28 | those files that have a
match. We can take them and
| | 03:30 | we can pipe them into xarg's word count.
| | 03:33 | We can find out the word counts of
those. Or we can concatenate them altogether.
| | 03:37 | It also works really well with find.
| | 03:39 | So for example, we could find
everything that's in my test directory and
| | 03:43 | everything that's typed as a standard
file, and pass that to xargs and change
| | 03:48 | the permissions on that to be 755.
| | 03:51 | Now remember, I told you that sometimes we
need to use this -0 as an option to xargs.
| | 03:57 | Well, dind is one of the places where
we really want to use it and find even
| | 04:01 | has a special option of
its own called print0.
| | 04:04 | So print0 will make sure that the null
character is used to separate them and
| | 04:08 | then the -0 on xargs will make sure
the xargs uses that null character.
| | 04:13 | So it's a way for the two of
them to communicate effectively.
| | 04:15 | We can go ahead and hit Return on that.
| | 04:17 | Let's try another example.
| | 04:18 | Let's do find and we will look
everything in the current directory whose
| | 04:22 | name matches fruit.
| | 04:24 | We will do that one again and we are
going to use print zero, pipe it through
| | 04:28 | xargs with -0, and then let's use our
placeholder specifier and what we are going
| | 04:34 | to do is copy each one of those
arguments to Desktop and we are going to use
| | 04:40 | that placeholder again, but
also append.backup to the end.
| | 04:44 | So I hit Return and guess what? It went
and found each one of those files that
| | 04:49 | had a match, send it to xargs, and then
copied it as a backup file to my desktop.
| | 04:54 | Now, let's say that we want to
remove all of those backup files.
| | 04:57 | So we have Find and Desktop.
| | 05:00 | Everything on the Desktop whose name has
backup at the end and we will do the print0.
| | 05:07 | Pipe it through xargs with
-0 and then we can use rm.
| | 05:11 | Now you might think well,
let's use rm-i for interactive.
| | 05:15 | Actually xargs doesn't
allow you to do interactive.
| | 05:18 | If you want xargs to be interactive,
you have to use the -p option for prompt.
| | 05:23 | And this will work for any kind of
Xarg's thing and basically it will prompt
| | 05:26 | you before it actually does it.
| | 05:27 | It gives you a chance to say yes or no.
| | 05:29 | There is one other thing that I want
to caution you about here, which is that
| | 05:32 | when we are doing a find, especially
if you are doing a find that is going to
| | 05:35 | then do copy or remove or anything like
that, it's a good idea to make sure that
| | 05:39 | you are not searching too deeply in the folders.
| | 05:42 | If I really just want to get rid of
these things that are on my Desktop, then I
| | 05:45 | need to specify the depth
is going to be equal to one.
| | 05:48 | Just look in the current immediate
folder, don't go any deeper, or if I want it
| | 05:52 | to allow to go two folders deep,
then you could specify depth 2.
| | 05:55 | You want to make sure that you don't
accidentally grep more files than you intend to.
| | 05:58 | All right, let's hit Return on this.
| | 05:59 | You will see xargs give you a prompt saying
hey, do you want to remove all these files?
| | 06:03 | First I'll start with saying n
and then Return, so that's no.
| | 06:06 | I am just going to make a change to it now.
| | 06:08 | I am going to say -n1 as an option.
| | 06:11 | So it will take each one of
these as a separate argument.
| | 06:13 | Now, it comes up and says, ah,
do you want to remove this one?
| | 06:16 | Y, boom, it's gone.
| | 06:18 | Y. We get a chance to approve each
one of them before they disappear.
| | 06:23 | The -n option makes sure that they are
sent in, each one as its own argument.
| | 06:26 | And I have shown you that xargs works
well with grep and it works well with find
| | 06:30 | and it works really well if we use both.
| | 06:31 | Let me just paste in a fake example here.
| | 06:34 | Imagine that I am finding everything
in the current directory whose filename
| | 06:37 | has invoice anywhere in it.
| | 06:39 | Pass that to xargs and then tell
grep, hey, look inside that subset of
| | 06:43 | files and see if you find the word
programming and tell me what those files names are.
| | 06:47 | Of course, it is fake example.
I don't have any files called invoice.
| | 06:50 | Imagine that we are a web developer
and we are looking for all files in our
| | 06:54 | website that have .html at the end,
pass those off, and grep those to find a
| | 07:00 | certain tag that we are looking for, like h3.
| | 07:01 | Now again, this is just a demo
example, but you get the idea.
| | 07:05 | Now not only would I be able to use
find and then pass it to xargs and grep it
| | 07:09 | but what I am getting back is a list
of file names because I used that -l option.
| | 07:13 | So guess what?
| | 07:14 | I can use xargs again on the file
names that it returns from that.
| | 07:18 | Let me show you a real example.
| | 07:19 | Here is one where we say all right,
find everything whose file has fruit.txt,
| | 07:23 | grep it for mango inside the file and
then pass that to xargs a second time,
| | 07:29 | and this time we are going to copy
it to our desktop. So there we go.
| | 07:33 | Boom, it searched for the file name, looked
for the right file content, and then copied
| | 07:38 | the file to our desktop.
| | 07:39 | These examples are just a small
sample of what you can do with xargs.
| | 07:42 | I want you to think of using xargs,
whenever you want to perform a command on
| | 07:46 | many items in the list. That list can
be recorded in a file, as we saw with
| | 07:50 | the file manifest, or it can be
dynamically generated by another command, as
| | 07:53 | we did with grep and find.
| | 07:55 | And if you learn to use xargs effectively,
it will really speed up your workflow.
| | Collapse this transcript |
|
|
9. Useful Mac-Only Commands and TechniquesFinder integration| 00:00 | Almost everything we've learned so
far has been portable, meaning that it
| | 00:03 | will work in any version of Unix,
not just in Mac OS X. Of course, we did
| | 00:07 | discuss a few Mac idiosyncrasies
along the way, but the commands and
| | 00:10 | techniques are fundamental Unix.
| | 00:12 | In this chapter, we're going to look at
some techniques that will work only on
| | 00:15 | Mac OS X. We'll begin by looking at
how we can integrate our work in the Unix
| | 00:19 | environment with our work in the Finder.
| | 00:21 | The simplest of these is how we can take a
Finder path and be able to use it in Unix.
| | 00:26 | So let's say we're inside the Finder,
we're working, we've got windows open,
| | 00:29 | and we suddenly want to take this folder here
and reference it inside Unix for some reason.
| | 00:35 | Well, all we have to do is
grab it, drag it in, and let go.
| | 00:37 | In Mac OS X, we'll then paste the path
that Unix needs to reference that same thing.
| | 00:42 | Now I switch to Unix.
| | 00:44 | I'll hit Ctrl+A to go to the
front of the line. cd space.
| | 00:47 | Now I've moved into that Sites directory.
| | 00:49 | You can use this for any command that
takes a path, whether it's copying or
| | 00:53 | moving or making symbolic links. For
anything that uses a path you can simply
| | 00:57 | drag the object from the Finder into the
Terminal window and then Mac will give you the path.
| | 01:01 | This is really useful if you need a
path to a deeply nested directory.
| | 01:05 | Instead of typing the seven or eight
parts of the path to get there, if it's
| | 01:09 | already open in your Finder window,
well then just drag it in there.
| | 01:12 | Save yourself the typing.
| | 01:13 | Now you can also do the reverse, that is,
take a Unix path, and use it in the Finder.
| | 01:18 | Mac OS X has a Unix command called
open that will open things in the Finder.
| | 01:21 | folders, files, applications, even URLs.
| | 01:25 | So let's change to our Home directory,
and from here if we just say open
| | 01:30 | unix_files, then it opens that folder.
| | 01:33 | If we want to open a specific file,
we say open unix_files/lorem_ipsum.txt and
| | 01:38 | now it opens up that text file in
whatever our default text editor is.
| | 01:42 | For me, that's TextMate.
It may be something else for you.
| | 01:45 | Essentially, it's just like if we
double-click on that file or that folder.
| | 01:49 | One technique that comes in really
handy is that if you're moving around inside
| | 01:53 | Unix and you get to a certain
directory and you really wish you had that
| | 01:56 | directory open in the Finder, well
open period. That references the current
| | 01:59 | directory so it pops it up in the Finder.
| | 02:01 | Then anything that you're more
comfortable doing inside the Finder, maybe
| | 02:05 | renaming files, throwing things in the trash,
all those things can be done in the Finder.
| | 02:09 | And anything you're more comfortable
doing in Unix, you can then do in Unix.
| | 02:12 | You can do the same thing with
the parent directory with dot dot.
| | 02:15 | Open doesn't open files and folders.
| | 02:17 | It'll also open the applications.
| | 02:19 | So for example, open -a
calculator will open up the calculator.
| | 02:25 | The -a option basically says go to
the Applications folder and find a Mac
| | 02:30 | application there with the name that matches.
| | 02:32 | If you want to open a specific file in
a specific application, well, then you
| | 02:35 | just say open -a TextEdit and
then let's use our lorem_ipsum.txt.
| | 02:41 | Now it opens up inside TextEdit.
| | 02:43 | One time-saving tip is that you can
create aliases for some of these that you
| | 02:46 | use often, so like alias
firefox= 'open -a firefox'.
| | 02:53 | Now whenever you type firefox,
it opens up Firefox for you.
| | 02:57 | Remember if you want to use these aliases,
you'll need to put them in your bashrc file.
| | 03:01 | open will also accept input from
standard n if we use the f option.
| | 03:05 | So for example, ls -la can be piped
into open with the f option, and then it
| | 03:10 | will open up the contents.
| | 03:12 | Instead of outputting it to the screen
here in our Terminal, it will open it up
| | 03:16 | in TextEdit in this case.
| | 03:18 | This is nice because it allows us to use
a lot of the power of Unix to construct
| | 03:21 | exactly what we want and then we can
send it to a regular Mac application.
| | 03:26 | So for example, I can find the fields in
us_presidents.tsv that I want and I can
| | 03:31 | send them to Numbers or Excel
or another program like that.
| | 03:34 | And it will open it up, and there it is,
and you can see that it's got them in
| | 03:38 | nice columns for me.
| | 03:39 | Another nice trick is that we can
look at the man pages in Unix in preview.
| | 03:43 | So if we type man -t and then
whatever we want to search for.
| | 03:47 | The t is just going to say format this
as PostScript and then it will pass that
| | 03:52 | off to fa Preview, so that
takes a second while it composes it.
| | 03:57 | And the last thing that open will do that's
really useful is open will open URLs for you.
| | 04:02 | Basically, what it is doing is that it
says "All right, if what you're sending me
| | 04:05 | is http:// well, then I know it's a
URL and I know I need to open it up with
| | 04:10 | whatever your default browser is."
| | 04:12 | So for example, if we want to open
lynda.com then it will open up my default
| | 04:16 | browser which is Firefox and
open up the lynda.com web site.
| | 04:19 | Again, this is a place where you can
create aliases for things that you use often.
| | 04:23 | So if there are web sites that you
frequent often, you can make aliases for them
| | 04:27 | or, you could keep a file that has a
list of many URLs and you could use xargs
| | 04:32 | to open them all at once for you, or
you could grep that file for a subset of
| | 04:36 | URLs and then use xargs on it.
| | 04:38 | I think you can see how we've learned
to combine some of the Unix techniques
| | 04:41 | that we've learned to speed
up our workflow in the Finder.
| | Collapse this transcript |
| Clipboard integration| 00:00 | On the Mac you're probably
with the concept of the Clipboard.
| | 00:03 | You're inside a document in one
application, you select some text, you copy it,
| | 00:07 | you switch to another document or
another application, and you paste it there.
| | 00:11 | Well, in Unix we don't have
a global clipboard like that.
| | 00:13 | But we can make use of the
same one that the Mac Finder uses.
| | 00:16 | And we can use it within Unix or we
can use it to ferry data back and forth
| | 00:20 | between Unix and the Finder.
| | 00:22 | To do it we'll use a pair of Mac-only
Unix commands called pbcopy and pbpaste.
| | 00:27 | The pb stands for Pasteboard.
| | 00:29 | Now when you're going to select
something and copy it in the Finder, you use
| | 00:32 | your mouse to select it.
| | 00:34 | But in Unix we don't have a mouse.
| | 00:36 | I mean of course I have the mouse here.
| | 00:37 | That's the Mac OS allowing
me to work with Terminal.
| | 00:41 | But in Unix itself, it's all command line.
| | 00:43 | So in order to select something
we're going to need to have input.
| | 00:48 | And we've already seen how
to work with input in Unix.
| | 00:50 | So let's say for
example that I hit ls -lah.
| | 00:53 | If I just hit Return,
that'll go to standard out.
| | 00:56 | But instead what I can do is pipe
that output as input into pbcopy.
| | 01:01 | Now that same text that would've
been output is on my Clipboard.
| | 01:04 | We can see that if we just
switch into TextMate real quick.
| | 01:07 | We'll open a new document and paste it in there.
| | 01:09 | This can be really handy when we're
working in Unix and let's say we're trying
| | 01:12 | to pull out a couple of columns out of
our us_presidents tab separated values file.
| | 01:16 | Well, we can find everything that we want.
| | 01:19 | Now we certainly could select this and
then we could scroll back up the page,
| | 01:22 | but that might be a lot.
| | 01:24 | Instead, a much simpler way would just be
to simply take that and send it to pbcopy.
| | 01:28 | Now without any output and without
having to switch and use our mouse to copy
| | 01:32 | the text, we've got it on our Clipboard.
| | 01:34 | We can switch to the Finder
and we can paste it in there.
| | 01:37 | And we can use our other techniques
for directing input, so pbcopy and let's
| | 01:41 | pipe in our lorem_ipsum.txt file.
| | 01:44 | Now the entire contents of
that file are on the Clipboard.
| | 01:48 | We didn't open it, we didn't have to
scroll up at the Terminal output and select
| | 01:52 | it and then copy it,
fill up our Terminal window.
| | 01:54 | All we did was take the contents of the
file, put it on our Clipboard so we're
| | 01:58 | ready to do something with it.
| | 01:59 | Now the companion to pbcopy is pbpaste.
| | 02:02 | So we just do pbpaste by itself.
| | 02:04 | It outputs whatever is on the Clipboard
to the standard out. So there we are.
| | 02:09 | We see it in the Terminal window.
| | 02:10 | We can instead direct that output. We can for
example, send it to a file, so clipboard.txt.
| | 02:17 | Now that content is in a file.
| | 02:19 | One nice trick that I like to use
with directing output from pbpaste is
| | 02:23 | to create an alias.
| | 02:24 | I'll call it pbsort and from that what
we'll do is we'll say take whatever is on
| | 02:27 | the Clipboard and paste it, pipe it into
sort, and then pipe the results of that
| | 02:32 | back onto the Clipboard.
| | 02:34 | Replace what's on the Clipboard
essentially with now the new sorted version.
| | 02:37 | Now remember, aliases that you want to keep
around you'll need to put in your bashrc file.
| | 02:41 | But this will serve our purposes.
| | 02:43 | Let's say that I have a list:
monkey, zebra, lion, tiger, and bear.
| | 02:46 | I'll take that, send it in to translate
to convert the commas into new lines so
| | 02:50 | each one will get its own line,
and then I'll put it on my Clipboard.
| | 02:54 | Now I'll just call my
pbsort and now it's sorted.
| | 02:57 | pbpaste, you can see it, or we can
switch into another document somewhere else
| | 03:02 | and paste it in there.
| | 03:03 | Now the original list doesn't
have to start in Unix either.
| | 03:06 | We could, for example, copy this, put
it on our Clipboard, switch over to Unix,
| | 03:11 | and regardless of what directory we
are in, we just type pbsort. Boom!
| | 03:14 | My Clipboard is now sorted.
| | 03:16 | We come back into whatever
application we were working in and we paste the
| | 03:19 | sorted version back in.
| | 03:21 | The last trick that I want to show
you is how to use multiple clipboards.
| | 03:25 | Have you ever wished that you had
more than one clipboard when you were
| | 03:27 | working on the Mac?
| | 03:28 | Well, it may surprise you to learn that
Mac OS X actually has four clipboards.
| | 03:32 | It has the general Clipboard which
we've been using here and then it has one
| | 03:35 | called find, one called
font, and one called ruler.
| | 03:39 | The find one you may have bumped into
before because it's whenever you do a
| | 03:42 | find in something like your web
browser for example. You do a find there, it
| | 03:47 | puts it on the Clipboard, and then
when you switch to another application,
| | 03:50 | you do a find there, you may see
that same value pop up, and you may have
| | 03:53 | wondered how did it get that, how
did it have that same value that was in
| | 03:56 | that other application?
| | 03:57 | Well, it's because it was on the Find Clipboard.
| | 04:00 | From Unix we can actually directly access these.
| | 04:03 | So for example, let's say we have First,
and we'll send that to pbcopy, and then
| | 04:08 | we'll specify the clipboard we want
with pboard followed by either the words
| | 04:12 | general, font, find, or ruler.
| | 04:14 | So general is the one we've been using.
| | 04:16 | Let's now echo "Second" and let's
pipe this to pbcopy, but this time the
| | 04:23 | Pasteboard that we'll use will be the find.
| | 04:27 | Now if we just say pbpaste,
which one will we get?
| | 04:30 | Well, we get the contents of the general one.
| | 04:32 | But if we say pbpaste with pboard find,
now we get the contents of the second one.
| | 04:37 | It can be a really handy
way for you to juggle data.
| | 04:39 | If there is something on your Clipboard
that you don't want to lose, well, you
| | 04:42 | can just switch it to another Clipboard.
| | 04:43 | Just pipe it off of one Clipboard onto
the Ruler Clipboard let's say, and then
| | 04:48 | go ahead and do the
regular copy/paste you need.
| | 04:50 | When you're done you can issue another
command that will pipe it back from the
| | 04:54 | Ruler Clipboard onto your general
Clipboard and you won't lose the data.
| | 04:58 | I think you'll find that using pbcopy
and pbpaste will really enhance the way
| | 05:02 | you work with the Clipboard. | | Collapse this transcript |
| Screen capture| 00:00 | In this movie we're going to learn to
take screen captures from the command line.
| | 00:04 | Do you already know how to
do screen captures in the Mac?
| | 00:06 | It's simple. You just hold down the
Command key, the Shift key and hit the number
| | 00:10 | 3 key and it will take a screenshot of
your screen and put it on your Desktop.
| | 00:15 | There is another version of that. If you
hold down Command+Shift+4 you'll get an
| | 00:20 | interactive screen capture
which means that you get crosshairs.
| | 00:23 | You select exactly the portion of the
screen that you wanted to capture and when
| | 00:27 | you let go just that portion of the
screen gets turned into a capture as a file
| | 00:31 | on your Desktop. We can do the exact
same thing from the command line plus
| | 00:35 | there are a few extra options that we
don't normally have available to us.
| | 00:38 | Let's see at its most basic first.
| | 00:40 | From the command line you just simply
type screencapture. It's that easy. Followed
| | 00:46 | by the name of the file where you want it to go.
| | 00:48 | So screencapture and let's put it on
our Desktop and then I'm going to call
| | 00:52 | it screen_capture.png.
| | 00:56 | By default the screen
captures are going to be PNG files.
| | 00:59 | Now already this is a slight
improvement over the regular Mac version, because
| | 01:03 | we get to specify our own
location and our own filename.
| | 01:06 | Whereas we can do the other way the Mac
gives it a filename based on the time
| | 01:10 | that the picture was taken
and puts it on your desktop.
| | 01:12 | So let's hit Return and it takes the
screenshot and puts it on the Desktop.
| | 01:16 | Now in addition we have a number
of options that we can specify.
| | 01:19 | There is the lowercase -i option and
that's put us in that interactive capture
| | 01:23 | mode exactly, the same as if we done
Command+Shift+4. Once you have those
| | 01:27 | crosshairs and you decide you don't want
to take the screen picture you can just
| | 01:30 | hit Escape key to cancel out of it.
| | 01:32 | There is a lowercase -m option if you
wanted to capture your main monitor only.
| | 01:36 | That's of course only if
you have multiple monitors.
| | 01:38 | By default the Mac will capture all monitors.
| | 01:41 | Then there is a capital
-C for showing the cursor.
| | 01:44 | By default the cursor is not shown in
the screen capture. If you did want the
| | 01:48 | cursor for some reason, you couldn't
do that from the Mac, but you can do it
| | 01:51 | from the command line.
| | 01:53 | You can use the -t option followed by a
Format if you wanted it in a different
| | 01:56 | format than png. You can use pdf, jpg, tiff.
| | 02:01 | There are number of others as
well, but these are the main ones.
| | 02:04 | The capital -T allows you to specify a Delay.
| | 02:07 | So you put the -T option followed by a
number of seconds that you wanted it to wait
| | 02:11 | before snapping the picture.
| | 02:13 | Once it finishes snapping the picture
you can use the -P Option to open that
| | 02:16 | file with Preview or the -M option to
send it directly to Mac Mail and put it
| | 02:21 | in a mail message. Or if you don't
want to even have a file you can use the
| | 02:24 | lowercase -c option and
capture it directly to the clipboard.
| | 02:27 | Let's try a few to these out.
| | 02:28 | So this time instead of the simple
version I'll do screencapture, but I'll
| | 02:34 | specify some options.
| | 02:35 | Let's say I'm going to use lowercase
-m to get my main monitor, capital C to
| | 02:40 | include the cursor. We're going to
open in preview when we're done with the
| | 02:43 | capital P, and then let's put a delay
on it capital -T 3 and wait 3 seconds.
| | 02:48 | Then let's also change the format using
lowercase -t, make it a jpg file, and this
| | 02:53 | time I'm going to save it in my
current directory which is the Unix files
| | 02:57 | directory inside my user directory.
| | 02:59 | So I'll just save it there and I'll
call it screen_capture.jpg since the
| | 03:05 | JPEG file this time.
| | 03:07 | So there we go. I hit Return.
| | 03:08 | It waits 3 seconds and then it snaps the
picture and it opens it up in Preview for us.
| | 03:13 | So here is the actual screen capture.
| | 03:15 | So again all of the special
configuration options are available in the man pages
| | 03:19 | if you need to look them up.
| | 03:20 | Now if is the kind of thing you find
yourself doing often, then create an alias for it.
| | 03:23 | That has all the configurations that
you want. Put that in your bashrc file and
| | 03:27 | then you can just type something
simple like screencap and voila!
| | 03:30 | You'll get your screen capture
configured exactly the way you wanted.
| | 03:34 | So again this is the exact same
screen capture you can normally do on the
| | 03:36 | Mac, but we just have these extra
enhancements available to us through the power of Unix.
| | Collapse this transcript |
| Shut down, reboot, and sleep| 00:00 | In this movie I'm going to show you how
you can shut down, reboot, and sleep your
| | 00:04 | computer from the Unix command line.
| | 00:06 | Not only can we perform these actions just
like we can in the Finder, but from the
| | 00:10 | command line we get an extra cool
bonus we can schedule these actions.
| | 00:14 | Now before we get started I want you
to proceed carefully. I don't want to be
| | 00:17 | responsible if you put your computer to
sleep or reboot and you lose data in a
| | 00:20 | file that you're in the process of editing.
| | 00:22 | So take a moment and make sure that
all your files are saved and everything
| | 00:26 | so that if you did accidentally trigger a
shutdown you wouldn't lose anything important.
| | 00:30 | Now the command that we're going to
use for all three of these, for shut down,
| | 00:33 | reboot and sleep, is going to be the
shutdown command. That may not seem
| | 00:37 | intuitive, but that's the command that
we use for all three of them and in order
| | 00:40 | to use the shutdown command we
always have to use it with sudo.
| | 00:43 | So sudo shutdown and then immediately
after we have to specify one of three
| | 00:48 | options. We have to tell it we want to
shut down, reboot or sleep, and we do that
| | 00:52 | with the h, r or s option. h stands
for halt, so halt would be to shut down.
| | 00:58 | All three of these options then after
that need a time. They want you to specify
| | 01:01 | what time you should shut down and the
time format can be one of three things.
| | 01:06 | You can either say shut down now,
shut down in a certain number of minutes from
| | 01:10 | now and this case use the plus sign
followed by a number, or at a specified date
| | 01:15 | and the date format will be the two
last digits of the year followed by two
| | 01:19 | digits for the month, two digits for
the date, two digits for the hour, and two
| | 01:23 | digits for the minute.
| | 01:24 | Now it's a little difficult for me to
demonstrate shutting down my computer, so
| | 01:28 | let's just take a look at what
some of these might look like.
| | 01:30 | So if we wanted to just do shutdown
now the same way that the Finder does it,
| | 01:34 | well then you would do sudo shutdown
with the H option for halt and then the
| | 01:38 | word now. If we wanted to schedule it
for 45 minutes from now then you could do
| | 01:42 | the same thing but use +45.
| | 01:45 | And if we wanted to shut down at a
specified date and time well then you use the
| | 01:48 | date, remember for the year you leave
off the first two digits, so it would be
| | 01:52 | 110601 and then zeroes to represent midnight.
| | 01:57 | Reboot works exactly the same way the
only difference is that now we're using
| | 02:00 | the r option instead of the h option.
| | 02:03 | So the first one, reboot now, is the
exact same behavior as the Finder has or we
| | 02:07 | can schedule in the future at
either a relative or an absolute date.
| | 02:10 | And then for sleep we have the
same thing but with the s option.
| | 02:14 | One place that I find this especially
useful is that if I'm working late at
| | 02:18 | night, it's almost bed time and I'm doing
some long-running process. I'm FTPing a
| | 02:22 | whole lot of files up to a server and
its going to take another 30 minutes.
| | 02:26 | Well I can go in to Unix and I can say
all right, one hour from now I want my
| | 02:30 | Mac to go to sleep. Then I can go to
bed, my file transfer are finish, and then
| | 02:34 | when I'm done my computer will just go
to sleep, ready for me to then come back
| | 02:37 | in the next morning.
| | 02:38 | Now in addition to the shutdown
command there also is a Unix command for halt
| | 02:43 | and reboot, but they're not
preferable. It's better to use shutdown.
| | 02:47 | So even though you may see halt and
reboot in there, shutdown is a better choice.
| | 02:51 | Now for sleep there is a sleep
command in Unix, but it's totally different.
| | 02:56 | Because in the days when Unix was
embedded computers didn't go to sleep.
| | 02:59 | They were either on or they were off.
They didn't have energy-saving modes.
| | 03:03 | So the sleep command in Unix actually
just tells Unix to wait for a specified
| | 03:08 | amount of time, so if I say sleep 3,
then it waits for three seconds and then it
| | 03:13 | pops back up and give me another
command prompt. You can use this with commands.
| | 03:17 | So sleep 3 and then we could do
something hello and wait three seconds and it
| | 03:23 | will do the thing that I asked it to do.
| | 03:25 | So it does have a use especially once
we start doing scripting in Unix, but
| | 03:29 | it's completely unrelated from
sleeping your computer the way that you're
| | 03:32 | normally used to doing it.
| | Collapse this transcript |
| Text to speech| 00:00 | In this movie we'll learn how to use
the Mac's text-to-speech functionality
| | 00:04 | from the command line.
| | 00:05 | We can type something and
then our Mac will save it for us.
| | 00:08 | Now before we start making our Mac talk,
be sure to check your computers volume
| | 00:12 | level make sure it's not
set a something too high.
| | 00:14 | The way that we are going to do it
is using the Mac only Unix command say,
| | 00:19 | and then after that we'll tell it
what we wanted to say, so we're going to
| | 00:22 | have "Unix is awesome".
| | 00:25 | (Computer voice: Unix is awesome.)
| | 00:26 | That's it there is all there is to it.
Now the voice that is talking in is the
| | 00:30 | voice that set as the default voice for
your Mac. There are also other voices as well.
| | 00:35 | We go into the Finder hold down
Command+Shift+U it will open up the
| | 00:39 | Utilities folder and then right
below Terminal you see there something
| | 00:42 | called VoiceOver Utility.
| | 00:44 | If you open that up and you click on
the Speech option, you'll see that what the
| | 00:47 | default voice is set out and you can
click on this and see list of all the
| | 00:51 | choices for all the voices that you can pick.
| | 00:54 | If you want to use a different voice
from the command line you can do it on a
| | 00:57 | per command basis so we could have
say hello and then use the -v option to
| | 01:03 | specify the voice, so let's say with Vicki.
| | 01:05 | (Female computer voice: Hello.)
| | 01:07 | Or Fred. (Male computer voice: Hello.)
| | 01:09 | Or Whisper. (Male computer voice whispering: Hello.)
| | 01:14 | Or Zarvox. (Computer voice: Hello.)
| | 01:18 | So you can have fun playing
with that, trying all those out.
| | 01:21 | In addition to saying a single string we
also have the ability to pipe in input.
| | 01:25 | So here I'm taking a string from echo.
| | 01:27 | But it could really come out of any
command at all. Pipe it in to say and we can
| | 01:32 | have it them say it.
| | 01:32 | (Computer voice: Dumm dee dee dum dee dee dum.)
| | 01:36 | Or you can have a read from a file.
I've got a file on here just called
| | 01:40 | shakespeare.txt, just a simple
Shakespeare sonnet. I can say that with the -f
| | 01:45 | option, 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:52 | And Ctrl+C will exit out, if you want or
need to break out of that. Remember that
| | 01:56 | you don't have to listen to the whole thing.
| | 01:58 | In addition to reading in content
from a file, you can also send the
| | 02:02 | text-to-speech output to a
sound file by using the -o option.
| | 02:06 | It's not exactly books on tape
quality, but for someone who is visually
| | 02:10 | impaired this could be really useful.
Another handy technique is to use say as
| | 02:14 | a notification that'll long-
running process has completed. Just put it
| | 02:18 | immediately after the command using a
semicolon then go to work on something
| | 02:21 | else while your process runs. When
it's done you'll get an audio message
| | 02:25 | letting you know that its finished.
| | 02:27 | And then lastly you can have some fun with this.
| | 02:29 | Wait until someone steps away from
their computer then ran a command like the
| | 02:33 | last one to make them think that
their computer is talking to them.
| | Collapse this transcript |
| Spotlight integration: Searching metadata| 00:00 | Spotlight is a fast and powerful
Macintosh find tool that's based on file metadata.
| | 00:05 | We can access Spotlight from the command
line too and I'm going to be honest with you.
| | 00:09 | This isn't my first
choice for finding files.
| | 00:11 | Most times if I'm working in the
Finder I need to find something based on
| | 00:14 | metadata and typically going to go up
here to the Spotlight menu in the upper
| | 00:18 | right corner and type my query there.
And most times if I'm working in Unix in
| | 00:22 | the command line, I'm going to tend to
use find and rep, but there are times
| | 00:26 | when you really need Unix to have access
to the metadata of the Spotlight keeps.
| | 00:30 | So let's see how we can
use it from the command line.
| | 00:33 | The command that we're going to using
is mdfind. The md stands for metadata.
| | 00:37 | Now the metadata for file contains much
more information about the file than we
| | 00:41 | normally have access to from the command line.
| | 00:43 | As an example, let's say that you have
an MP3 or a song that you've purchased on
| | 00:47 | iTunes. Well that has a file name. It's
probably the same name as the song and we
| | 00:52 | can see that in the file system. We
could search it for using find or grep, but it
| | 00:56 | also has a lot more
information associated with it.
| | 00:59 | It has the artists name, the album name,
the track number, the running time, the
| | 01:04 | bit rate that it is encoded at.
| | 01:05 | All of that information is stored in
the files metadata and we have the ability
| | 01:09 | to access it by using
Spotlight or by using mdfind.
| | 01:12 | The way that we use it is just simply
to tell mdfind what our query is.
| | 01:16 | So mdfind and then new and then we hit
Return and it does the exact same find as if
| | 01:22 | we'd come up here and typed New into Spotlight.
| | 01:25 | Now you could add more to your search
the same ways as you can type other terms
| | 01:29 | into the Spotlight window, so I reduced
the number that I got here. But really
| | 01:33 | whenever possible I really recommend
very strongly that you use -onlyin option
| | 01:39 | and then tell it where you want it to
look and then just give it a regular path.
| | 01:43 | This will massively limit the
amount of searching that it does.
| | 01:46 | So it'll make it a faster search and it'll
also give you less results that you don't want.
| | 01:50 | Now if you really do want to search
your entire hard drive, then by all means
| | 01:54 | go ahead and leave it out, but most
times you at least know roughly where you
| | 01:58 | want to be looking.
| | 01:59 | So you can go ahead and say ah this is
in my documents folder, this is in my
| | 02:02 | music folder, this is in my sites
folder, and limit it that way. I also want to
| | 02:06 | show you that you could
include negatives in there.
| | 02:08 | So for example, I got three files back
I could say well I don't want anything
| | 02:12 | that has join in it.
| | 02:14 | Well now left out the one joins.txt.
It just gave me the other two files results back.
| | 02:18 | So again it's just like if we typed
in the Spotlight menu. The documentation
| | 02:22 | actually says that it's a really good
idea to use the interpret option passed
| | 02:27 | as well to make sure that it really does
interpreted exactly like you typed in there.
| | 02:31 | Now my experience is that most
times you don't need it. Most times that
| | 02:35 | interpret option is overkill, but I
just want to mention it just in case you're
| | 02:38 | wondering why a certain search here
isn't coming up. You may need to force it to
| | 02:43 | strictly interpret the same with
the spotlight query window would.
| | 02:46 | In addition to asking for a query
string we can also use the name option and
| | 02:52 | specify something that has
exactly a file name that matches.
| | 02:55 | So you see we got back something
slightly different this time. Now the hits were
| | 02:59 | based on the file name new and new.
| | 03:02 | If you remember when we're using the
regular find it also had a print O option
| | 03:07 | that allowed it use null as a separator
between each of the results. That made it
| | 03:11 | really easy than the pass off to xargs
and xargs was enable to use it reliably.
| | 03:15 | Well, we have the same thing here and it's
the -o option, so then we can type that
| | 03:19 | into xargs and then in the xargs we
would need to use the -o option there as
| | 03:24 | well and then we can do something with it.
Let's say we'll use our open command.
| | 03:28 | So now we'll pop off those
open in text made for me.
| | 03:31 | Now this is the simple usage of it and
probably the way that you'll use it most
| | 03:35 | of the time, but I do want to show you
that you can go even deeper and we'll
| | 03:38 | take a look at that in a next movie.
| | Collapse this transcript |
| Spotlight integration: Metadata attributes| 00:00 | I want to look at another command
related to metadata, which is mdls.
| | 00:04 | So we're looking at metadata
listing and we can look at a file, like
| | 00:08 | our lorem_ipsum file.
| | 00:09 | And we can peek at the metadata for this file.
| | 00:11 | So now we can see all the metadata for
it, which can help us to figure out what
| | 00:14 | we're trying to search on.
| | 00:15 | And you can see that there is a
lot more stuff stored in here that we
| | 00:18 | normally have access to.
| | 00:19 | Let's just look at another one, just as
contrast, mdls. We'll look at this JPEG
| | 00:24 | we created, the screen_capture.
| | 00:26 | Notice here it's got all sorts of
things like in the color ProfileName.
| | 00:29 | It's got the PixelWidth, the PixelHeight,
and all this metadata that's in here
| | 00:33 | we can actually search on as well.
| | 00:36 | So you could find images that match
certain width and height parameters.
| | 00:39 | You can be really specific.
| | 00:40 | And those for each one of these we've got
the key over here and the value on this side.
| | 00:44 | So the key is in the name of the
attribute that we want to look for.
| | 00:48 | So let's say for example this one,
this is the Metadata Item File System Name
| | 00:53 | that we're looking for. That's the key.
| | 00:55 | So let's just copy that and we're going to
now construct a new query using that to search.
| | 01:00 | mdfind and I'm going to use my
-onlyin option again. unix_files and then in
| | 01:07 | single quotes I'm going to put that,
equals, and what do I want it to equal, and I put
| | 01:12 | it in double quotes so they can tell
the difference, and I'm going to search for
| | 01:15 | lorem_ipsum as our first example.
| | 01:17 | So lorem_ipsum, I've got an * on either side.
| | 01:20 | That is used as a wild card for any
characters that might be on either side of it.
| | 01:24 | Then I hit Return and it
found our lorem_ipsum file.
| | 01:28 | Now notice if I take the wild card
characters away, now it doesn't find it.
| | 01:32 | It is looking for an exact match, right?
| | 01:34 | That's what this == here is telling
it that it should be an exact match.
| | 01:37 | And notice that I said put
single quotes around this.
| | 01:40 | Now it would work fine in this case if
you reversed them and you did the double
| | 01:44 | quotes in the outside and the single
quotes in the inside, but when we start
| | 01:47 | working with time, it doesn't always work.
| | 01:50 | And so once you start working with
time, you want to make sure that you're
| | 01:52 | working with single quotes.
| | 01:53 | A lot of people run into problems and
wonder why it's not working for them.
| | 01:57 | Now you'll notice that I told you to
use single quotes around the outside and
| | 02:01 | the double quotes on the inside.
| | 02:02 | It would have worked if we've swapped them
and done the other way around. In this case.
| | 02:07 | But in some cases that's not true,
especially like the ones we're about to see
| | 02:10 | when we're working with time.
| | 02:12 | So let's say that we have the exact same
thing we had before, but now instead of
| | 02:17 | KMD, we're going to look at ItemFSCreationDate
and we're going to say greater than or equal to
| | 02:25 | and now we can use the special time object,
time.today to start with. Single quotes.
| | 02:32 | Again, single quotes are important when
working with all of these times. Oops!
| | 02:35 | I left a space here. That's
why it didn't find anything.
| | 02:38 | Let's remove that space and try again.
| | 02:41 | And now it finds the Shakespeare
file that I created earlier today.
| | 02:44 | In addition to using this keyword today,
you can also use yesterday, this_month,
| | 02:49 | or this_year, and there is
also a one that's for now.
| | 02:53 | Now it's not very useful to search for
things that are now, but we can provide
| | 02:57 | an argument to that that is a
relative time to now in seconds.
| | 03:02 | So -36000, let's say, inside parentheses.
| | 03:06 | Let me just widen my window so that's not
wrapping and you can see a little bit better.
| | 03:10 | So now -36000 seconds and now it
shows me any files that meet that.
| | 03:15 | I didn't have any. If I just broaden
it by another 0, now you can see the
| | 03:19 | ones that come back.
| | 03:20 | Now searching in seconds might not be
the most useful. We can also provide that
| | 03:24 | same kind of option to today as well.
| | 03:26 | So today(-2) is everything in the last 2 days.
| | 03:29 | Now we're not specifying seconds anymore.
Wwe're specifying in days. Or if we did
| | 03:34 | something like this_week(-1).
| | 03:37 | Well now we'll see all the files that
we were created this week, sending for
| | 03:41 | this month and this year, and if
you do this year, then the number you
| | 03:44 | specify is in years.
| | 03:46 | In addition to having relative time, you
can also specify absolute time by using ISO.
| | 03:52 | So, time.iso. Again, this is where it
is important to make sure that you're
| | 03:56 | using single quotes. The double quotes
won't work in this case. And then you put
| | 03:59 | a time in the ISO format.
| | 04:02 | So this is what the ISO format looks
like the year, the month, the date with
| | 04:06 | dashes between it, space and then the
time, the hour, minute, and seconds with
| | 04:10 | colons, and then the time
zone offset after that.
| | 04:13 | So now it will come back and tell me all the
files that we were created since that time.
| | 04:18 | So again, let me just say that
searching by all of these attributes is the most
| | 04:22 | powerful lowest level you can do. You
don't have to do that. You can just simply
| | 04:27 | put those simple query strings in
there and have it find everything.
| | 04:31 | If you really do know that you want to
target something really specific though,
| | 04:34 | I just want you to know that you have
the ability to go in and find exactly the
| | 04:38 | metadata that you want.
| | Collapse this transcript |
| Using AppleScript| 00:00 | In this movie I'll show you how to
use the Unix command line to communicate
| | 00:03 | with the AppleScript.
| | 00:05 | I'm not going to try to teach you to
write AppleScript. I'm certainly no expert
| | 00:08 | on it myself, but I will
demonstrate a few useful basics.
| | 00:11 | But if you already know how to write
scripts on your own, well, then you'll be
| | 00:14 | able to quickly link the power of
AppleScript with the power of Unix and do
| | 00:17 | some really cool things.
| | 00:18 | Now the way we're going to call
AppleScripts is by using the Mac only
| | 00:22 | Unix command osascript.
| | 00:25 | OSA stands for Open Scripting Architecture.
| | 00:27 | And after that, we can either provide the
file name of the script that we want to
| | 00:31 | run, or rather the path to that file,
or we can use the -e option and actually
| | 00:35 | give our instructions right here in the string.
| | 00:38 | So I can say set volume output muted true.
| | 00:43 | And that will mute our computer.
Or I can say output muted false.
| | 00:47 | These two are handy because
we can put them in an alias.
| | 00:50 | So for example, I could just jump to the
beginning of the line here. alias mute=
| | 00:55 | and I'll put double quotes around it,
jump to the end of the line and put double
| | 00:59 | quotes at the end of that.
| | 01:00 | and then let's do the same thing this
time with false. We'll make it unmute.
| | 01:06 | Now I can say mute, unmute.
| | 01:09 | Now remember, if you want to keep those
aliases around, you'll need to put them
| | 01:12 | in your bashrc file.
| | 01:14 | We can also use AppleScript to talk to
different applications, and the Finder is
| | 01:18 | actually considered application.
| | 01:20 | So we can have an AppleScript that
looks like this, osascript -e 'tell
| | 01:26 | application "Finder" to
display dialog "Hello"'. Hit Return.
| | 01:34 | You see it starts bouncing down here.
I click on it, and there is my dialog box.
| | 01:39 | Once I click OK, it comes back and it
actually returns a value to me, which I
| | 01:43 | could also catch and do
something within Unix if I needed to.
| | 01:47 | Now once we start talking the applications,
very quickly one line stops being enough.
| | 01:52 | Usually we don't just want to tell an
application to do one simple thing, but we
| | 01:55 | want to give it a set of instructions.
| | 01:57 | So that's when you really go about
writing scripts and putting them in files.
| | 02:00 | I've created a sample script
that you can take a look at.
| | 02:03 | It's going to be inside the Unix files
folder that's included with the exercise files.
| | 02:08 | And I'll just let you see what it looks like.
| | 02:09 | It's pretty small script.
| | 02:10 | It just says tell application iTunes
and now instead of just a single thing,
| | 02:14 | we're going to have a list of
things that are activate, set the sound
| | 02:17 | volume, play, wait 10 seconds, switch to the
next track, wait 5 seconds, and then finally quit.
| | 02:23 | Now if we want to run the script we just
type osascript and then the name of it,
| | 02:28 | control and then itunes.scpt.
| | 02:31 | That's typically what you would name the
end of your scripts. So it would be .scpt.
| | 02:35 | Hit Return.
| | 02:37 | Wait one moment while iTunes opens up.
| | 02:38 | Then it will play this song for 10 seconds.
| | 02:43 | (Music playing)
| | 02:48 | Switches to the next track.
| | 02:51 | That should play this for 5
seconds and then it shuts down.
| | 02:54 | Now you can look up more information
about iTunes and you can find out there is
| | 02:57 | a lot more that you can do.
| | 02:58 | You can manage your playlist, things like that.
| | 03:00 | You can search for things. All that
you can then do from the command line by
| | 03:04 | integrating Unix with AppleScripts.
| | 03:06 | Let me give you another example so you
can see how we can actually talk to an
| | 03:10 | application and have it return data to
us that we can then work within Unix.
| | 03:14 | Let's take a look at another sample
script that I've included in the exercise files.
| | 03:17 | You can control the address
book, so control_address_book.scpt.
| | 03:23 | And there is another very simple script.
| | 03:24 | It says tell the application Address
Book create an empty list called emailList,
| | 03:29 | then count the people that are in the
address book, and then for each one of
| | 03:33 | them go through and get the value of
that person's email and put it into the
| | 03:37 | emailList and at the end return
that item to us. So let's try it.
| | 03:41 | So osascript control_address_book.
| | 03:47 | You can see that it opens up, then it
comes back and it gives me a list of all
| | 03:50 | the email addresses that are in there.
| | 03:52 | Now this is data, this is data that we
can use, so we can take that and we can
| | 03:55 | pipe it into something.
| | 03:56 | Let's pipe that into seed. We have a
comma-delimited list, but I want to take out
| | 04:00 | the space that is right
there. That should do it.
| | 04:05 | So now it's a comma-delimited
list without that extra space.
| | 04:09 | Now let's take that and
let's put that through tr.
| | 04:12 | Tell that comma it should turn into a line return.
| | 04:16 | Now it's at each line and now let's
take that and pipe it into sort and finally
| | 04:22 | let's go ahead for good
measure and put it through uniq.
| | 04:25 | And then if we wanted at the end of
that we could pipe that then into a file
| | 04:28 | and we would have an export, a dump, of all
of the email addresses that were in there.
| | 04:32 | Now we could get more creative about
the kinds of things that we pull out by
| | 04:36 | using a script. Maybe you want to pull
everyone's address out or you want to
| | 04:39 | pull the first name and the last name.
| | 04:41 | The idea here is to show you that you
can tell these applications to send data
| | 04:46 | back to you that you can then work within Unix.
| | 04:50 | You can put on your clipboard.
| | 04:51 | You can send it to another script that
will then send it to another application.
| | 04:54 | There is almost no limit to what you can
do here, because now you've the ability
| | 04:58 | to use AppleScript not just to work in
Unix, but actually control programs and
| | 05:03 | have them do your bidding.
| | 05:04 | Now if you are a hard-core AppleScript
user, I also just want to mention that
| | 05:08 | there is one other command in Unix that
maybe useful, which is called osacompile
| | 05:11 | and that will actually
compile scripts for you as well.
| | 05:15 | So for most of you, osascript will be
enough just be able to run scripts, but
| | 05:19 | for some people you may want to make
use of the osacompile command as well.
| | Collapse this transcript |
| System configurations: Viewing and setting| 00:00 | Mac OS X has hundreds of
configurations and settings to control your
| | 00:04 | environment that you work in.
| | 00:06 | Some of these can be set in your
system preferences but others are actually
| | 00:08 | inaccessible to most users.
| | 00:10 | But through Unix we can access those
configurations and even modify them.
| | 00:14 | In this movie we'll learn how
to do that. Before we begin,
| | 00:17 | keep in mind two things
about setting configurations.
| | 00:20 | The first is you want to be super
careful that you don't accidentally change a
| | 00:23 | configuration that you didn't mean to change.
| | 00:25 | You might set something
into an undesirable state.
| | 00:28 | The second thing is that some changes
are going to take effect immediately, but
| | 00:32 | others are going to require either
that you log out and back in or that you
| | 00:36 | completely restart your Mac.
| | 00:37 | So just because you don't see a change
right away doesn't mean that it didn't
| | 00:40 | actually take effect.
| | 00:41 | It really just depends at what
point the Mac loads in that particular
| | 00:45 | preference, and that's going to
vary from preference to preference.
| | 00:48 | Let's start by seeing where the
configurations are stored. I'll use Command+N to
| | 00:52 | get a new window in the Finder and I'll
open another Finder window here to put
| | 00:55 | next to it, and then this one I'll use Command
and click on the bar to go up to Macintosh HD.
| | 01:02 | Now we have a Library folder here and
we have a Library folder here. These are
| | 01:06 | where the different configurations are stored.
| | 01:08 | These are the configurations which are
global and apply to every user on the system.
| | 01:12 | these are the ones that are
just for me in my particular User.
| | 01:15 | Inside the Library for each of them,
there is also a Preferences folder.
| | 01:19 | That's where these are typically stored.
| | 01:21 | Now they may exist in other places
in some cases. Here is the Preferences for
| | 01:24 | this one, there we are.
| | 01:25 | But most of the time these are the
two places that you'll find them and
| | 01:28 | they really depends on whether the
preferences are global preference or user preference.
| | 01:32 | Let's go inside the user one here and
let's take a look at this. I'll actually
| | 01:35 | close up the global one.
| | 01:36 | And you notice almost all of these have
the same format. The name is always com, period,
| | 01:42 | the company name, followed by the
application name, and then typically it ends
| | 01:45 | in plist at the end.
| | 01:47 | Remember this format because
we're going to need to use it.
| | 01:49 | Plist let's you know that it's a
preference list. We can open that up in a text
| | 01:53 | document but if we double-click on one,
we'll open up the Apple Property List
| | 01:57 | Editor that they give you with your Mac.
| | 01:59 | I've opened up the one is for the Finder.
| | 02:00 | So do the same, just so
you can follow along with me.
| | 02:02 | Now what we she here are the keys and
values for all the configuration options.
| | 02:08 | So for example the Preferences window
location has the following coordinates.
| | 02:13 | My EmptyTrashProgressWindowLocation
here will be placed at the following coordinates.
| | 02:17 | It remembers where we've moved that
progress window as the trash is emptied.
| | 02:21 | If we move it someplace else,
it keeps track of those coordinates in its
| | 02:24 | preference list and that's why it
opens up in the same place every time.
| | 02:28 | Now you can open up a lot of these,
your SearchViewSettings, you see what's
| | 02:31 | inside there. RecentFolders,
DesktopVolumePositions,
| | 02:35 | ComputerViewSettings, BrowserWindowState, and
open up you can dig around and see what's there.
| | 02:39 | If you double-click on something here
you can actually change these values.
| | 02:43 | So you can change them directly
via the Property List Editor.
| | 02:46 | We're going to do instead is learn to
read and write these values directly
| | 02:49 | from Unix without using the Property List Editor,
and I'll show you why that's a better solution.
| | 02:54 | The way that we're going to do that is by
using this Unix command called Defaults.
| | 02:58 | It's a program that's a Mac only
Unix program that will manage these
| | 03:02 | default settings for us.
| | 03:04 | One benefit is that it takes care of
whether or not these are user preferences
| | 03:08 | or global preferences.
| | 03:09 | It goes and finds it in the right
place and makes the change for us and we
| | 03:12 | don't have to dig around at all.
| | 03:15 | And after the defaults command we
can put several other commands that we
| | 03:18 | want defaults to run.
| | 03:19 | We're going to be looking at read
and write. There are few others and you
| | 03:23 | can look at the man pages to see what those
are, but read and write are the most useful.
| | 03:26 | So read the domain and the key that we
want to read. That will return the value
| | 03:31 | to us so we know what the current setting is.
| | 03:34 | Or we can set a value by saying defaults
write, the domain, the key, and the value.
| | 03:39 | And that domain is typically in the
format com.companyname.appname. Remember we
| | 03:44 | just saw that, com.apple.Finder.plist.
Well that's typically the domain and
| | 03:49 | it's the same thing as the file name.
| | 03:52 | So for example we can do defaults read
com.apple.finder and then we can read
| | 03:58 | CopyProgressWindowLocation and that
gives us that same string that we saw in
| | 04:05 | the plist editor right here,
169, 270, and it is a string.
| | 04:10 | Now we can change that value using write.
| | 04:13 | So now instead of that let's use
Ctrl+A to shoot to beginning of line, we'll
| | 04:18 | change this from read to write, and use
Ctrl+E to go to end of the line, and now
| | 04:23 | we need to provide a value. So the
value here is just going to be a string,
| | 04:27 | which means it's in quotes, and
inside there we'll just put 168, 270.
| | 04:33 | Put a space between so it's really formatted the
exact same way. Make sure we don't mess anything up.
| | 04:37 | Now we just change that first value from
being 169 to 168, essentially one pixel
| | 04:42 | of difference for where
that window will be located.
| | 04:44 | If we go back to the Property List
Editor and let's close that up, just open
| | 04:49 | that again, Finder, and now you see
that the value is changed. 168, 270.
| | 04:54 | So that's really all there is to it.
| | 04:56 | It's just targeting the preference
that you want with the right key name and
| | 05:00 | then either reading or writing a value to it.
| | 05:02 | Now the other thing that's nice about
defaults though is that some settings
| | 05:06 | won't be listed. Sometimes even
the Config file itself won't exist.
| | 05:10 | It won't even be in the Preferences folder.
| | 05:13 | Defaults will take care of creating
those files for you as needed and make sure
| | 05:17 | that they are in the
right format and everything.
| | 05:19 | Now how do you know about these
settings and what to change, if you can't go in
| | 05:22 | and see them in the plist files?
| | 05:24 | Well quite honestly, you typically
learn about them from the Internet.
| | 05:27 | Word gets out from Mac software
developers who know the Mac API really well and
| | 05:32 | they know what these available options
are and so then word gets out that this
| | 05:36 | is the way you change something.
| | 05:37 | It spreads on the Internet until people
find about the sort of mysterious hidden
| | 05:41 | options that they can then type the
commands here into defaults and it will set
| | 05:45 | it for them and create the files as needed.
| | 05:47 | In the next movie we'll take a
look at some popular changes.
| | Collapse this transcript |
| System configurations: Examples| 00:00 | In this movie, I want to show you
a few of the most popular system
| | 00:03 | configuration changes.
| | 00:05 | The first one will show all
the dot files in the Finder.
| | 00:07 | Remember that by default the Finder
hides all our dot files so that we can't see them,
| | 00:12 | but we could change that behavior.
If we want to make them visible we'd
| | 00:15 | say defaults write com.apple.
Finder and then ShowAllFiles true.
| | 00:20 | If you ever decide you want to
turn it off again, ShowAllFiles false.
| | 00:22 | Or if you want the Unix path to
display in the Finder windows up at the top,
| | 00:28 | well its default is write com.apple.
Finder, and then _FXShowPosixPathInTitle
| | 00:35 | using all the correct capitalization, and then
-bool TRUE, setting a Boolean to true.
| | 00:42 | If you wanted to go away then
you'd just set it to bool False.
| | 00:46 | We can also configure some of the
defaults for screen capturing. We saw a little
| | 00:49 | bit about how to trigger
screen capturing from Unix.
| | 00:52 | We can change how the Mac does its
screen capturing permanently even in the
| | 00:56 | Finder by setting defaults write com.
apple.screen capture. Type is we can set
| | 01:02 | a different file type.
| | 01:03 | I have listed off most of them there for
you. You could pick one of these and if
| | 01:06 | you say, oh you know what I wish it
would capture it and save it as a PDF file
| | 01:09 | every time, well you can have it do that.
| | 01:12 | You also change the location
where it places that screen capture.
| | 01:15 | By default it's going to put it on your
user's desktop, but if you use defaults
| | 01:19 | write com.apple.screen capture
location, then you can provide the path to where
| | 01:24 | your 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:36 | The one caveat with that is that you do
want to use a full file path. You don't
| | 01:40 | use any kind of shortcuts like the
till date, represent your user directory.
| | 01:43 | Give it the full entire working
path so that it can reliably find it.
| | 01:47 | And last of all you can change the
background image for when you log into your
| | 01:51 | Mac by using the defaults as well.
| | 01:54 | You know the picture that I'm talking about?
| | 01:55 | It's not the desktop picture
that you get after you've logged in.
| | 01:58 | This is the image that's
behind that log-in screen.
| | 02:01 | Currently there's not a way to change
that image inside System Preferences.
| | 02:05 | Maybe they'll make that possible in
the future, but right now that's a hidden
| | 02:08 | option. You can't change that picture.
| | 02:10 | Well let's say you want to put your
company logo in the background or something
| | 02:13 | or pictures of your kids there,
right behind the login screen,
| | 02:16 | as soon as your computer comes up.
You can. All you have to do is use defaults to
| | 02:20 | change com.apple.loginwindow
DesktopPicture to the path to the new picture
| | 02:25 | that you want to change it to.
| | 02:27 | Notice that I've also used
sudo in front of this one.
| | 02:30 | That's because these defaults live in
that system configuration, every user will
| | 02:34 | get this preference, and so we need
sudo to make that change system wide.
| | 02:38 | Once again you want to provide a full path.
| | 02:40 | Don't have any shortcuts to it.
| | 02:42 | Now a lot of people have resorted to
a low-tech version of making the same
| | 02:46 | change and that is they go into their
library of desktop pictures and they move Aqua
| | 02:50 | Blue.jpg out-of-the-way and they put
whatever image they want in its place
| | 02:55 | within name Aqua Blue.jpg.
| | 02:56 | That's bad for a number of reasons.
| | 02:58 | First of all now Aqua Blue is no longer
there if you need it for something else
| | 03:02 | and your other image is mislabeled.
| | 03:04 | It's better to do at this way with the
preferences. And so you can just change it
| | 03:07 | to this, and if you want to change it
back to Aqua Blue later, you just change
| | 03:10 | the preference again.
| | 03:11 | So you can see the increase level of power
and control that Unix gives you over your Mac.
| | 03:15 | Not only can you do really powerful cool
things in Unix but you actually can get
| | 03:19 | under the hood of your Mac and make
changes to the way that it works as well.
| | Collapse this transcript |
|
|
ConclusionConclusion| 00:00 | I want to thank you for
taking Unix for Mac OS X Users.
| | 00:03 | Over the course of this training
title we've come a long way, from simply
| | 00:06 | changing directories in the command
line all the way to power user techniques.
| | 00:10 | And don't worry if it takes a while for
some of these commands become a routine.
| | 00:13 | Remember that you always have the man
pages to remind you of the command syntax and
| | 00:17 | its available options.
| | 00:18 | I've been using Unix for 20 years and I
still look up things all the time, and
| | 00:23 | those built-in man pages? They're also
one of the best resources for you to
| | 00:26 | continue advancing your Unix skills.
| | 00:29 | One point that I hope you've picked
up on while we we're learning is that
| | 00:31 | Unix is as much a part of your Mac
as the Finder is. In fact Unix is what
| | 00:35 | makes the Finder possible.
| | 00:37 | The Finder is a layer of
convenience that sits on top of Unix.
| | 00:40 | So when you want convenience, use the
Finder. When you want power, use Unix.
| | 00:44 | And when you can switch back and forth,
and integrate both in your workflow then
| | 00:48 | you can work faster and get
more power out of your computer.
| | 00:51 | And you may not appreciate it at first
| | 00:53 | but the Unix is the most fundamental building
block of the Internet and all modern computers.
| | 00:58 | Concept such as shells, user ownership
or permissions, directing standard input
| | 01:02 | and output, controlling processes,
regular expressions, all these things show up
| | 01:06 | in other operating systems,
applications, and programming languages.
| | 01:10 | Now that you know about them, you'll
begin to notice how often they come up.
| | 01:14 | Unix was first and everyone built on
its solid foundation, so by becoming
| | 01:18 | comfortable in Unix you've not
only empowered yourself as a Mac user,
| | 01:21 | you've given yourself a computer
literacy that can be applied to hundreds
| | 01:24 | of other uses.
| | Collapse this transcript |
|
|