Episode 5 File management part 1
We use mv
to move and rename files, and create a shell alias
to improve the
safety of one of its sharp edges. We also introduce the .bashrc
configuration
script, and mkdir
to create directories.
25 August 2015
•
[Rhythmic, dark electronic intro music] | |
League |
•
Welcome back to Command Line TV. Last time we talked about text manipulation using •commands like |
Lopes |
•
Today we’ll be doing more file management.
We’ll be using the move ( the copy ( |
League |
•
Great. So the are two ways to use these commands. I’m going to get the usage screen – the help •screen for the mv --help | less•
one is just an alternative to another of them. But the first way that you use •
names and so what it means is that you are moving – actually, you’re renaming the •file to a different file name. So this version of the command could just be •called ‘rename’, and on other systems it might be called rename. So let’s take a •look at trying that version of the folder and I’ve got a couple of files here. Let’s say that I want to take this •
like that. So I can do which could be like mv weblog.txt weblog-2015-07-11.txt• command had a source and a destination.
when I do has changed but it’s exactly the same content. Now the destination doesn’t •have to be in the same directory. You can specify a different directory where •it will go. So then it feels like a little bit more than a rename. So let’s •say for example, if I want to move my
directory, but I might also rename it at the same time. so I could call it •“internationalization” or something like
that mv languages.txt thinkjava/internationalization.txt• combined with also moving it into a subdirectory. And that works fine. •So if I
ls thinkjava• and the name of that directory. You see here that internationalization •file is now in that directory. •The second way to use multiple source files. So the dots here mean I can specify multiple sources – you •might want to use a wildcard to specify multiple sources. And then the last thing •you specify is a destination directory. It’ll keep the names of all those files •but then move them all into that destination directory. So let’s say for •example – I’m going to go down into my
before to look at images and so forth. And there are different formats here. •So separate folders. So for example let’s
make a separate folder for for simplest commands there is – called
mkdir fig eps• a new directory and we can specify just one directory name or multiple directory •names and those would get created if they dont exist yet. So now I’ve got listed •here among all my other files I’ve got
an going to use mv *.fig fig• and the destination directory is called
mv *.eps eps• is in their various subfolders. |
Lopes |
•
Now professor, when I originally learned
about the especially dangerous to use it if files already existed and you might •overwrite something. |
League |
•
Yeah, it’s a real sharp edge of the way
already exists – if you’re using the first form where you’re just renaming – •if that destination file exists, it will just overwrite it and it doesn’t •even tell you that it did that. So let’s go back up here to my regular •downloads folder where I created this
to rename survey – let’s imagine that this survey actually is a weblog – so mv survey.tsv weblog-2015-07-12.txt• I want to rename it and give it a
different date, right. So I put something like that. Now that will be harmless because that file doesn’t •exist. But what if I make a typo or something and I put a file that does •exist? This will rename mv survey.tsv weblog-2015-07-11.txt• overwrote the old one. If I do this file contains the content from the
old from this weblog file is gone, it just doesn’t exist anymore. So if I do •
head weblog-2015-07-11.txt• weblog data. So yeah, that’s a really dangerous thing to do. It’s especially •dangerous if you’re using wildcards. So sometimes you will mean to use a •wildcard in order to move multiple files into a destination directory, right. So •let’s say I mkdir hello• use of a wildcard here will match both
of these files right, the well as the `tar.gz.sig’. So I want to move both of those files into the •destination directory mv hello-2.10.tar* hello• just forget to put the destination directory? Now what happens is this •wildcard happens to expand to two separate files, right? So that expansion is what’s •going to happen first. It’s going to
become the
this is just what the wildcard expansion will do. And so now instead of triggering •the second form where we’re moving to a directory, it’s triggering the first •form where i’m just renaming. And remember that that rename can clobber •something so now I’m just missing one of those files, it’s gone. So that’s a •real sharp edge. There are a couple of things that you can do to make that a •little bit more safe – a little easier to deal with. One thing that you can do •is, there are a couple of options to do –
like there’s something called Let’s take a look at the help page again. ‘-n’ stands for “no clobber” so do not mv --help | less• overwrite an existing file. So if I had
remembered to specify that – let’s actually put back – I’m just going to fake this by creating an empty •filename. So if I just create an empty
nano hello-2.10.tar.gz• – we covered least I’ve got two files with that name so I can try it again. So if I do •
mv -n hello-2.10.tar.gz*• It left both the files alone. Another
way besides interactive. So we’ll try that as well. What interactive does is it prompts you mv -i hello-2.10.tar.gz*• whether you really want to overwrite files or not. And you can type yes or no. •So I say “oh I didnt mean to
overwrite that file” so I’ll do So those are two options to Another technique is inherent in that third
format of thing as the second format, it moves a bunch of source files into a target or •destination directory, but by specifying the directory first, maybe you’re less •likely to forget to put it, right. So if I wanted to move both of these •
mv -t hello hello-2.10.tar.gz*• moving stuff into there, and then do
those are gone from this directory but they’re both present in the sub-directory. •So those are a couple of options for the move command. |
Lopes |
•
It seems like the move command should
automatically have the |
League |
•
embedded into it for safety reasons. Is there anything we can do about that? •Yeah, the philosophy of Unix is often that you know what you’re doing and if •you don’t, if you’re making a mistake, then it doesn’t do a lot to prevent it. •Unix commands in general are not meant to protect you from yourself, and in •fact one time when I was a teaching assistant, we had two different •sections of a programming course that we were teaching and we kept the grades •in two seperate spreadsheet files. And I wanted to move both of them into, or •maybe copy both of them – I don’t remember – into a different directory. •So I did like of those files and using that data to overwrite the other file, so we lost •the grades for one of our sections. We did have a backup of them but you •know, we lost a couple days of work. So the other TA’s were not very happy •with me. But there is a way if you would like to have greater defenses •for this kind of thing, to ensure that.
So we can make it so that the
So the way an alias works is you type
you want to create. So for example we
can make a brand new command. not a command that exists on Unix system generally. But I can make a command •called alias lt='ls -ltr'• quotes, usually single quotes but probably could be double quotes. And I •want that to stand for this because it shows me detailed listing in reverse chronological order. •So I declare that alias and now when I
type lt• the most recent stuff is at the bottom. So I can do that same thing for the •
alias mv='mv -i'• interactive one. And now that I’ve got that alias set up, if I go down into •
mv hello*• that would have worked by default is it takes one of those and overwrites •the other one. But because of my alias
with and so I can just say no. Now we would like to save that for •the future as well. An alias, if I just type it at the command line like that •will only be good for the current session and when I close this terminal •or log out, it’s gone. So I’d like to keep it longer than that. To do that •I’m going to go up to my home folder
which is cd ~• at what we call the ‘dot’ files.
So ls -a• default. Any file name or directory
that starts with a hidden out of the way, but the files here – well two of them – are
bash is your shell which it probably is then profile is a file where you can •put definitions and configurations that will get read every time you •log in and then or new shell. So you can probably put it in either one. Let’s go with the •
nano .bashrc• already have your work fine, you’ll just create a brand new file. Mine will show that that file •already exists because when I created a new user on this system it gave me a •default see here it’s already got an alias for
output on because of that alias. So I’m going to go just below that somewhere and do my •
alias mv='mv -i'• about copy – but its got the same sort of issues. So I create those aliases alias cp='cp -i'• and I’m going to save that with control-O and exit with control-X. •And now to get that to take effect in the current shell without restarting •the terminal I can type source .bashrc• If I go down to cd downloads/hello• to do the cp hello-2.10.tar.gz*• filename. It won’t remove the original which maybe is an improvement. •But since I added that alias for been able to use aliases to make those commands a little bit safer. •Today we learned to use copy files. We also learned how to make a
simple alias and to edit our customize settings beyond the current terminal. We’ll continue with more file •management next time. Thanks for joining us. |
•
[Dark electronic beat] •[Captions by Christian Lopes] •[End] |