Hello, and welcome to the third episode of the Software Carpentry lecture on the Unix shell. In this episode, we'll see how to create, rename, copy, and delete files and directories.
As we've seen in previous episodes, one way to interact with a computer…
…is through a command-line shell.
When we type in commands like these, the shell finds the corresponding programs, runs them on our behalf, and shows us their output.
But how do we create files and directories for it to show us?
Let's go back to Vlad's home directory,
As in the previous episode,
ls -F shows us the files and directories it contains, with a trailing slash after each directory to help us tell them apart.
Let's create a new directory called
tmp with the command
As you might guess from its name,
mkdir means "make directory".
tmp is a relative path (without a leading slash), the new directory is made below the current one.
There it is.
Graphically, we started with this directory tree…
…and created the new directory here.
However, there's nothing below it yet:
tmp is empty.
All right: we're in
tmp is empty…
…which we can tell because
ls doesn't print any output.
If we use
ls -a to show directories whose names start with '.', though, we see that
.. are there, as they always are.
The first name,
., refers to the directory itself, i.e.,
.., refers to its parent, which just happens to be the current working directory
Let's change our working directory to
cd, then run the command
nano is a very simple text editor that only a programmer could really love.
And we really do mean "text": it can only work with plain character data, not tables, images, or any other human-friendly media.
This is what nano looks like when it runs.
The cursor is the blinking square in the upper left; it shows us where what we type will be inserted.
Let's type in a short quotation…
…then use Control-O to write our data to disk. By convention, Unix uses the caret
^ followed by a letter to mean "control plus that letter".
Once our quotation is saved, we can use Control-X to quit the editor and return to the shell.
nano doesn't leave any output on the screen after it exits.
ls now shows that we have created a file called
ls with the
-s flag shows us how large things are.
Unfortunately, by default Unix reports sizes in disk blocks…
…which probably isn't the least helpful option imaginable.
If we add the
ls uses more human-friendly units for its output.
Here, 512 is the number of bytes the file takes up.
This is more than we actually typed in because the computer rounds sizes up: the smallest unit of storage on the disk is typically a block of 512 bytes.
Let's tidy up by running
rm junk. "rm" stands for "remove"—this command deletes files.
It's important to remember that there is no undelete. Unix doesn't move things to a trash bin: it unhooks them from the file system so that their storage space on disk can be recycled. Tools for finding and recovering deleted files do exist, but there's no guarantee they'll work in any particular situation, since the computer may reclaim the file's disk space right away.
If we now run
ls, its output is empty once again, which tells us that our file is gone.
Let's re-create that file…
…and then move up one directory to
If we try to remove the
tmp directory using
rm tmp, we get an error message:
rm only works on files, not directories.
The right command is
rmdir, which stands for "remove directory".
It doesn't work yet either, though, because the directory we're trying to remove isn't empty.
This little safety feature can save you a lot of grief, particularly if you are a bad typist.
If we want to get rid of
tmp we must first delete the file
The directory is now empty, so
rmdir deletes it.
Let's create that directory and file one more time.
junk isn't a particularly informative name, so let's change the file's name using
mv is short for "move": we use it to move a file from one place to another.
It also works on directories: there is no separate
The first argument tells
mv what we're moving.
The second tells it where the thing we're moving is to go.
In this case, we're moving
tmp/quotes.txt, which has the same effect as renaming the file.
ls shows us that
tmp now contains one file called
Let's bring that file into the current working directory. Again, we use
…but this time, the second argument is a directory.
The effect is to move the file from the directory it was in to a different directory.
ls shows us that
tmp is now empty…
…but we now have
quotes.txt in our current directory.
And notice, by the way, that
ls with a filename or directory name as an argument lists only that file or directory.
cp command works very much like
mv, except it copies a file instead of moving it.
We can check that it did the right thing using
ls with two paths as arguments. Like many other Unix commands,
ls can process up to thousands of paths at once.
To prove that we made a copy, let's delete the
quotes.txt file in the current directory…
…and then run
ls again. This time,
ls tells us that it can't find
quotes.txt in the current directory, but it does find the copy in
tmp, which we didn't delete.
Let's make one more copy.
This time, though, we don't specify the destination filename, just a directory, so the copy will keep the original's filename.
To summarize, here are the commands we've seen so far, along with the two special directory names.
In the next episode, we'll see how to operate on text files using pipes and filters.