Join Kevin Skoglund for an in-depth discussion in this video Listing files and directories, part of Unix for Mac OS X Users.
In the last movie we became familiar with the concept of the working directory and we saw how we could use pwd to show us our present working directory. Now what I want us to do is see how to see what files and folders are inside that present working directory, and we do that with the ls command. We got a peek at it earlier. ls on a line by itself will just list the contents of our present working directory. Desktop, Documents, Downloads, Library and so on. If we look over here that exactly matches what we see in the Finder window. Desktop, Documents, Downloads and so on.
In addition we can pass in some options to ls. We could do man ls and see a list of all of those, but I am just going to show you the couple that I think are the most important. ls -l is going to give us a different kind of listing. ls -l option, now we get them in a vertical format. Notice the last column there still has the file names, but going top to bottom and then over to the left, all those other columns give us information about the file, the size of it, the permissions on it, the owner of it, the time, all those things we'll talk about lot later on, we'll get into those, but just ls -l will show you this different version of it.
If we add another option to it, we can do it with or without the l, but I like doing it with it. ls -la and hit Return. Now we get that same list, but notice that we got a few extra files there. There is ., .., .CFUserTextEncoding, .DS_Store, .Trash and .bash_history. Now you may or may not have exactly those same ones. Don't worry about it. The important thing to understand is that the dot represents the current directory. That's all it is. It's that sort of symbol or a placeholder that means this directory that I'm in right now.
Dot dot refers to the parent of this directory. It's the directory that's right above this, and in this case because our pwd returns users kevin, .. is a reference to Users. But we are always going to have dot and dot dot to refer to those two directories. The other four files there are what we call dot files, because they begin with the dot and dot files are invisible config files. Notice that they don't show up here. The Finder is set to hide dot files by default.
We don't need to see them. They are just configuration files. So for example .Trash is files that are in the trash. That's where my trash is. If you ever wondered how does it know when files are in the trash, well, it moves them from let's say your desktop into this folder and it sits there waiting in this hidden folder until you throw them away, and then it empties out that folder. That's where those trash files are stored. Then we have DS_Store. That's actually for the Mac desktop, the Finder, to store different options about how we're viewing this folder.
Whether it is in this sort of layout, the size of the window, the position of the icons, all that gets stored in this little configuration DS_Store. Bash_history we'll talk about later, but that's where we store history of those commands that we have been typing and then user text encoding is another just configuration file that the Mac uses. Notice that there is also another difference here. Some of these have Ds at the front of the line while some of them just simply have dashes. The dashes is a file. The d is a directory. That's what it's letting you know. That's your tip-off of whether something is a directory or not, is whether that line begins with the D.
There is a third possibility that sometimes shows up there that we'll talk about later. Instead of D and dash you could have an L, which will be for a link or a shortcut. It's like an alias in Unix. So that's how we list the files. That's how we see the files that are there. We can pass in just ls, if we just want a very simple list. We can pass in ls -la if we want that longer list and actually one other thing I like to add as an option is ls -lah, and that returns the size of these different things in human readable format. 510 bytes, 15 kB, 1.1 k. That just gives us a nicer size that I think is a little more pleasant to read.
- Moving around the file system
- Creating and reading files
- Copying, moving, renaming, and deleting files and directories
- Creating hard links and symbolic links
- Understanding user identity, file ownership, and sudo
- Setting file permissions with alpha and octal notation
- Changing the PATH variable
- Using the command history
- Directing input and output
- Configuring the Unix working environment
- Searching and replacing using grep and regular expressions
- Manipulating text with tr, sed, and cut
- Integrating with the Finder, Spotlight, and AppleScript
Skill Level Beginner
Q: The exercise files for the following movies appear to be broken:
Is there something wrong with them?
These exercises include one or more "dot files", whose file names start with a period. These files are normally hidden from view by the Finder. So that they would show up in the Finder, the period has been removed from the file names. Additionally, "_example" has been added at the end of the file name to make it clear that the file will not work as-is.
To make the dot files usable, either:
1) Open the file in a text editor to view its contents. Note that it may not be possible to double-click the file to open it because there is no file extension (such as .txt).
2) Resave the file under a new name (usually by choosing File > Save As), adding a "." to the beginning of the file name and removing "_example" from the end.
1) Copy and rename the file from the Unix command line using the techniques discussed in this course. Rename the file by adding a "." to the start and removing "_example" from the end. Include the "-i" option to prevent overwriting an existing file unexpectedly.
Example: cp -i ~/Desktop/Exercise\ Files/Chapter_07/07_02_files/bashrc_example ~/.bashrc
The instructor uses the UNIX program 'units' to convert 72° Fahrenheit to degrees Celsius. The returned value of 40 is incorrect. The correct result should be 22°C. What's the reason for this discrepancy?
The problem is that units does the 5/9 calculation but does not have the ability to subtract 32. So you'll need to subtract (or add) the 32 degrees yourself.