Episode 2 Viewing files
We look at viewing files using commands like cat
, more
, less
, head
, and
tail
, including using those in short pipelines. We also try opening files in
external applications using open
or xdg-open
(and live dangerously by
dumping a binary file with cat
).
4 August 2015
•
[Rhythmic, dark electronic intro music] | |
League |
•
Hi, welcome back to Command Line TV. I’m Chris League, with me is my co-host Chris Lopes. •Last time we learned how to list files and navigate the directory structure. •And today we’ll cover the basics of viewing and manipulating files from the Unix terminal. •First are there any questions or issues you’d like to address from last time? |
Lopes |
•
Actually I had one. I remember last time we
navigated using We did that in two different ways. •We did it the relative way from the directory we were currently in, •And then we also did it using the tilde ( |
League |
•
Not really, it’s just a matter of what’s convenient, but •there is terminology about them. You said relative for one, which is correct. •So it’s relative if it matters where you’re
starting from. And when you started with then it’s called an absolute path because then it doesn’t matter where you start from, •it always goes to your home directory first. •So one way to think of that is that
an absolute path starts with a and the So I’m going to do here a command called
echo ~• an abbreviation for |
Lopes |
•
So this terminal actually looks a little different as well. •I guess is that because we’re using Linux as opposed to the Mac this time? |
League |
•
Yeah, last time we were on a Mac terminal, and this is Linux. •There are some minor differences. One of them
is that the actually has color in it. •The prompt also looks a little different, but it shows the same stuff. •It’s got your username and host name (the name of the machine), current directory – •so if I go to Downloads, then it shows up there. And then the dollar sign is my prompt. •And one cool thing about the colors is that it’s showing us directories in blue, •and it sometimes has other colors for other file types. •So that helps us distinguish things in a
similar way to doing that ls -F• that we learned last time, which adds a slash. •That’s a little redundant now because the color gives it away. |
Lopes |
•
Can the |
League |
•
Not directly, but you can install a different
version of So the version of ls --version• – it needs to be exactly like that, no extra spaces. •This shows me that I’m using the
GNU version of which has this feature of adding colors. •And on the Mac you’re using the
BSD version of these are just two different variants of the Unix operating system. •And so you can install the GNU version on the Mac, •but it’s not the version that comes with it by default. •The other thing that GNU supports is a lot of
commands have a double-dash ls --help• So if we do I have to scroll up to see the rest. •But this gives you a quick reference for the different options that it supports. •And we can find things that we learned last time, like reversing the order of the sort. •So we can see those details. |
Lopes |
•
So the |
League |
•
I would say it works on most commands, certainly all commands from the GNU project, •but not every command is from GNU. •But it’s always worth trying, because a lot of commands do support it. |
Lopes |
•
In future episodes we also hope to address questions and comments from our viewers |
League |
•
Yeah I hope you’ll get in touch with us with your questions, corrections, comments, •and we’ll be happy to respond on the show. How will they get in touch with us? |
Lopes |
•
Uh right now we have two options:
on Twitter or via email |
League |
•
Alright, thanks. •So now we’ll learn a little about viewing and manipulating files from the command line. •So I’m here in my Downloads folder. |
Lopes |
•
So now that we learned how to navigate through the directories, •how can we actually view the files that are within those directories? |
League |
•
Right, so there’s a text file here about languages, and the most basic command •to dump out the contents of a file is called
it doesn’t matter, but that’s what it’s called. •So i can type cat languages.txt• dump out the contents right onto my terminal, like that. •Now that was a really long file so I have to scroll up to see more of the content of it. •But another way so that I don’t have to scroll in my terminal is •I can replace more languages.txt• And what and it gives me this prompt at the bottom with a percentage of how much of the file I’ve seen. •And I’ve got a couple options here. If I just want to see the next full page, •I can hit the space bar. So now I’m looking at page 2, and so on. •It pages through the file that way. •And if I’m done looking I can hit the |
Lopes |
•
Now does |
League |
•
Yeah, so And I can try to do cat hello-2.10.tar.gz• 2.10, okay – •but I’m going to regret it [laughs]. I’ll show you what it does anyway. •So if I do dumped to my terminal. So it really is just dumping out the contents of that file, •but that’s not something that I can see on my terminal in a reasonable way. •So what I have to do here to fix this is hit control-C to stop it – •and sometimes that’ll take a moment. And once it actually stops, •what you might want to do is reset your
terminal. There’s a command called reset• sometimes dumping the contents of a binary file will mess with your terminal settings •and things might not come out quite right. •Sometimes when you start to type after that, it will actually show you funny characters •instead of the keys you pressed. But typing
Now my terminal is back to its default settings. |
Lopes |
•
Now that we learned is it the same process to open images as well, and PDFs? |
League |
•
So images and PDFs you might want to be able to view from the command line, •but you can’t actually see the content directly. •However, you can open them in another program by typing a command. •So I’ve got here – down in some sub-directory – I’ve got some figures •that are part of a book. And a lot of these are in Postscript format, •which we’re not going to worry about right now. •But I do have down here an image file, in PNG format. ls *.png• And if I want to open that on the command line,
on Linux the command would be What this does is it connects to your desktop environment that you have installed, •and chooses the appropriate viewer for the file type. •So you can use this for both images and PDFs, and some other things as well. •So I xdg-open gridworld.png• to show you the image. And then you can close that and switch back to the command line. •On a Mac, the command that does the same thing – that hooks into the graphical environment •on the Mac – it’s just called open gridworld.png• and that won’t work on Linux but it works fine on Mac. |
Lopes |
•
And I noticed at the top you did |
League |
•
Yeah, so this asterisk, or ‘star’ as I will usually say, is a wildcard. •It just matches anything. So this is a way of
showing – so if I just do it shows all the files in this directory.
ls *.png• where there is anything followed by a And so you can use that in a nice way to limit what files you’re seeing, •to just the files that end with it doesn’t have to have a boundary with the dot, you can use it elsewhere. •Like I can show all files that have So this ls *ren*• And that We’ll say a lot more about wildcards in another episode, but that’s a good introduction. |
Lopes |
•
Now I see that after the earlier when we did So is there a difference between those options and what we’re seeing right now? |
League |
•
Yeah, sort of… •the syntax of these commands is usually that you have the name of the command •(which is if you have any. So and then space, and then you do any other arguments that you would have. •So for example, names of files or wildcard patterns for files, or whatever. •So I can do that to get a detailed listing of just those files that I selected before. ls -l *ren*• Some commands also support putting the options after the arguments, •so you could do ls *ren* -l• And for and it’s more typical to put the options before the arguments. •When you do of how to use it, including the order that the options and arguments should go in. •We did from scrolling way off the page by using the
So previously we said and it’s usually, on most keyboards it’s shift-backslash. •So we call it ‘pipe’ or ‘vertical bar’, but Unix people will just call it ‘pipe’. •What this means is take the output of the thing on the left •and feed it to the program on the right. So we’re combining two programs together ls --help | more• and then we get the help message from the where after And then or hit |
Lopes |
•
I noticed that when you did but we couldn’t go back up. •What if I wanted to go back up to view the options I already passed? |
League |
•
Yeah, exactly, so That’s a serious limitation to it, so a lot of people actually use a newer command •called
So it’s just a silly name. •But it does essentially the same stuff. The prompt looks a little different, ls --help | less• so it just has this colon But it allows me to page forward in the same way using the space bar. •But I can also go back up, and I can do that with the arrow keys to go up a line at a time, •or you can use page-up and page-down to go up toward the top and down toward the bottom. •There are a couple other commands that it supports too. •If you want to go all the way to the top with one keystroke, that’s the less-than key. •So And then just like However on if you page all the way to the bottom, it just exits by itself when it gets to the bottom. •
it stays there still running until you hit So it’s really a matter of preference which one you want to use, •but |
Lopes |
•
So if I didn’t want to view the entire file, •but I just want to know what’s at the beginning or end of it, do I have options for that? |
League |
•
Yeah, very good. There are two commands, one
called that show the beginning and end of files. •
So I’ve got a log file down here in this
There’s a to configure this software. And if I just want to review the last couple of lines •of that file, to see if it was successful or there was an error. •You can see tail config.log• looks like it’s 1, 2, 3, … 10 lines that it’s showing. •And the bottom-most line just says You can configure So if I only want to see that last one line, •I can say tail -1 config.log |
Lopes |
•
So when we were talking about the syntax before |
League |
•
Yeah, that’s one of the options
that So you put that, and then you put the filename afterwards. •How about So this can allow you to identify files, for example. •Like a lot of times there is authorship information or copyright information •at the top of the file. So I’ve got here a file called Changelog. •And the Changelog probably has information about what changes have been made •to this software. And generally when we keep those files, •we put the most recent stuff at the top. •So if I want to see the most recent stuff in the Changelog, I see here – head ChangeLog•
and it’s for version 2.10. •So that’s an example of using let’s say four lines of that file, using the
head -4 ChangeLog• One other cool way to use the same way that I showed you to use Well instead of that we can pipe to head, and what do you think that’s going to do. |
Lopes |
•
I’m assuming it will only show the first few lines for the help |
League |
•
Yeah, the first few lines of that help message, and then just return me to the terminal. ls --help | head• So if all I really wanted to see was the synopsis at the top, •
|
Lopes |
•
Now could we do combine |
League |
•
Yup, so and you put its option out here, so let’s say we want to see 3 lines. ls --help | head -3• Then there’s just the 3-line synopsis. |
Lopes |
•
Nice. |
League |
•
So this is what I think is the really cool thing about the terminal. •You have these little commands that you can then mix and match •in a bunch of different ways to get things done. •And we’re only seeing the glimpse of that right now but we’re going to do •much more powerful pipelines with some other commands in future episodes. |
Lopes |
•
Thanks for joining us today for episode 2. Professor, what’s the topic for next time? |
League |
•
So I think next time we’ll dig into wildcards a little bit more. •And we’re going to use pipes for simple text manipulation with a few more commands •we’ll learn. But we’ve already started to see the power of pipes to some extent, •just with commands like We’re going to see even more sophisticated ways to combine small commands •together in the next episode. |
Lopes |
•
Sounds good. |
League |
•
See you then! |
•
[Dark electronic beat] •[Captions by Christopher League] •[End] |