Episode 6 File management part 2
In this episode, we continue looking at managing files and directories using
cp
, rm
, mkdir
, and rmdir
. We also show off the tree
command and
revisit creating an alias
in our .bashrc
.
1 September 2015
•
[Rhythmic, dark electronic intro music] | |
League |
•
Welcome back to Command Line TV. In this episode we’ll continue our look at •file management using |
Lopes |
•
So now that we’ve finished up on the Is there much difference between those two? |
League |
•
The main difference is just that them or put them somewhere else, so then the source files don’t exist anymore. •
so your source files will continue
to exist when you use Other than that, a lot of it is the same.
So if I look at the help for cp --help | less• you see the same formats here with either source and destination, •or multiple sources followed by a directory. And then a lot of the options are the same as well. •I can do interactive and no-clobber for
the same reasons with there are a few other things it
supports that So let’s try out the simplest form of it. I’ve got a web log here that’s marked as being July 11th. •If I just want to make a copy of that file in place, •I can specify cp weblog-2015-07-11.txt weblog-2015-07-12.txt• So maybe I just put the next day’s date
there. And now when I did would be like a rename but now it’s a copy so the original still exists. •And if we look at the detailed listing they have exactly the same size and ls -l• they have the same contents. So if I look at the first couple of lines in both of those files, head -3 weblog*• they’re exactly the same. So it’s just making a copy of the data under a different filename. •Now another form of into an existing directory even. So if I
go down into my cd thinkjava/figs• where I moved all of my So right now inside of the ls fig• “something” files within that cp fig/* .• The way we specify current directory is just with a single dot, •which makes it really easy to miss when you see this command like, •printed on a web page or something. A lot of times that dot will disappear •or you’ll think it’s a speck of dust on your screen or something. •But that dot is important because it specifies the destination directory. •So if I do that – now all of those but they also have copies in So that’s both formats of the One other option that So a lot of commands have a It means it should explain what’s going on as it happens. •And this is as opposed to usual – like most Unix commands will be very •quiet and they will only put out a message when there is something seriously wrong. •So we can do – let’s do the same
thing with the so cp -v eps/* .• And it will give me a transcript of all of the copies that it had to do in order to implement that. •So if you want to copy an entire directory worth of
files, the Let’s say I’m here where I’ve got this django project and I want to make a •copy of that to start new projects out of
it. So if I do then we’re going to call it cp django proj1• it says “omitting the directory”. So
there’s an option that will tell it’s okay to make copies of entire
directories and that’s cp -r django proj1• So has the same contents as the
original ls django ls proj1• And it’s fun to combine that with the verbose because then you get to see •lots and lots of files being created. So let’s
just try it again to create cp -rv django proj2• And you can see that even down to the sub-directories – •like it’s making a copy of this
same structure within the ls -l django ls -l proj2 |
Lopes |
•
I guess a good thing to point out is that its also copying the date as well – •the date and time as well. |
League |
•
Yeah, this date and time, it’s – like, the originals were June 1st and •then the copy actually gets a new timestamp. So it’s not preserving the timestamps currently. •There is a way for it to do that,
which is the If we go back to the help screen, it preserves all sorts of information. One of them is timestamps but it can •also be permissions and some other stuff. So
if I repeat that last instead of cp -av django proj3• so you can keep the So I make a copy of django into
ls -l django ls -l proj3 |
Lopes |
•
So professor, earlier in this episode we actually made a directory to do some work in it. •Can we go more in depth as to what the capabilities are of that? |
League |
•
Yeah, so you just type But there is one cool thing that it supports that’s very useful – which is, •imagine that you want to create a tree structure of a directory like, down a couple of levels. •So instead of just creating a single directory I might want to – let’s say, •create mkdir proj4/assets/js• files for my web project or something. The trouble with this is that •
So this would mean I want within that I want that it’s supposed to create and it will not do that. So one solution is to do it one at a time. •I could say mkdir proj4 mkdir proj4/assets•
This just means to create all of the parent directories necessary to •create what I said and so just mkdir -p proj4/assets/js• let’s see the top level first –
within within that I’ve got ls proj4 ls proj4/assets |
Lopes |
•
And if we did the |
League |
•
Yeah, I don’t know if we used tree proj4• So of line drawing characters. It doesn’t exist on every Unix system and you might •have to install it but it’s something definitely worth having. So it looks like that. tree django• Yeah, so we can see that that tree structure
that we created using Now for removing directories
there’s a command So if I decide I don’t want that
rmdir proj4/assets/js• The parent directories still exist, it only removed the bottom-most one that I specified. tree proj4• What if I want to remove the whole So if I tried rmdir proj4• this is unusual, most Unix commands will just do something awful without complaining! •But this one actually will prevent you from removing a directory unless it’s completely empty – •it cannot have any subdirectories or any files in it, even the hidden dot files. •So, that doesn’t work and I would have to do them one at a time, like, •
rmdir proj4/assets rmdir proj4• option that lets you shortcut that. However,
there’s another command which is just So It will delete lots and lots of stuff very quickly and if you mistype a •command a little bit you can really make things difficult on yourself. •So by default Let’s say I want to rm weblog*• Very quick and quiet, but those two
files are gone. So that’s Now if you want to remove a directory,
rm proj2• that doesn’t work because it’s a directory and
But it doesn’t take much to convince So the one option we’ll learn is rm -r proj2• And this means it will go into that directory, delete all of the files, •any subdirectories, and their files, and so on. The entire tree, it will just blow away. •And again it’s very fast and now all of those files are gone. •So is there a way we can protect
ourselves from |
Lopes |
•
Set up another alias on it. |
League |
•
Yeah, exactly. So rm --help|less• It supports the ‘interactive’ just
like which means before every removal it will prompt. Maybe you don’t want that •but I think it’s not a bad idea to have an alias for that. So let’s try that. •Where did I keep my aliases before? |
Lopes |
•
It was |
League |
•
nano ~/.bashrc alias rm='rm -i'• And I’ll put a little comment above this [laughs] okay. •So now I’ve got an alias for I would have to close the terminal and reopen
another one, or source ~/.bashrc• Now it’s there as an alias. And so now if I do rm -r proj1• First of all, “Do you want to descend into this directory” in the first place? Yes. •“Do you want to descend into templates?” Yes. “Do you want to remove this •file which is empty?” Okay. So this obviously slows you down if you type something by accident. •But if you really seriously want to remove a bunch of stuff, •one way to get around an alias is – by the way to stop that I just hit •control-C to cancel that command. Control-C wouldn’t undo any deletes that have already happened, •it just cancels it where it is in the process. So if I want to actually delete that whole directory, •one way to bypass an alias is to put a backslash before the command. \rm -r proj1• And now if I’ve got an go to the regular command. So that will now delete
all of the So it’s a little bit of a trade off, right? If then what you’re going to do when you really need
to delete something is just type And actually I’ve acquired that habit
personally, my own aliased to It’s just in my bones now, or in my fingers to type
so the alias doesn’t really do me any good. •One of the other options of So we see here something preventing deletion that can be resolved – so for example, •if I’ve set up a permission on a file so that I’m not allowed to write to it, •to delete it – it can try to actually modify the permissions in order to allow that again. •And so if I’m allowed to change the permission to allow me to delete it •then it will still delete it. So that’s a pretty serious thing. •When you combine recursive with forcing that really becomes quite dangerous •and so and it actually gives me chills a little bit even to type it. •And one thing you never ever want to do probably is – •I can’t even type this – I’ve got to separate the
r m -rf /• is if you put a slash here, slash is a way to refer to the top level of your file system, •the root directory. And so this means start at the top of my entire disk or •even multiple disks and try to delete everything it can. •So that’s a pretty awful command and you should probably try never to type it. |
Lopes |
•
Alright so today we covered a lot of file management using the copy, •the remove, the move, as well as the make and remove directories. |
League |
•
So next time I think we’re going to look at Image Magick which is a pretty •exciting suite of command line tools for doing image processing. •You can do things like convert images from one format to another. •You can automate things like applying filters to images, •shrinking and cropping them – all sorts of things. So I think that’s going •to be a pretty exciting one because we’re going to move beyond just the •simple commands that do things you already know how to do on a maybe desktop file manager. •And we’re going to start to see some things that we can do on the command •line that are pretty different. So see you then! |
•
[Dark electronic beat] •[Captions by Christian Lopes and Christopher League] •[End] |