Up: The Shell

Creating and Deleting

slide 01 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.
slide 02 As we’ve seen in previous episodes, one way to interact with a computer…
slide 03 …is through a command-line shell.
slide 04 When we type in commands like these, the shell finds the corresponding programs, runs them on our behalf, and shows us their output.
slide 05 But how do we create files and directories for it to show us?
slide 06 Let’s go back to Vlad’s home directory, /users/vlad.
slide 07 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.
slide 08 Let’s create a new directory called tmp with the command mkdir tmp.
slide 09 As you might guess from its name, mkdir means “make directory”.
slide 10 Since tmp is a relative path (without a leading slash), the new directory is made below the current one.
slide 11 Let’s run ls again.
slide 12 There it is.
slide 13 Graphically, we started with this directory tree…
slide 14 …and created the new directory here.
slide 15 However, there’s nothing below it yet: tmp is empty.
slide 16 All right: we’re in /users/vlad
slide 17 …and tmp is empty…
slide 18 …which we can tell because ls doesn’t print any output.
slide 19 If we use ls -a to show directories whose names start with ‘.’, though, we see that . and .. are there, as they always are.
slide 20 The first name, ., refers to the directory itself, i.e., /users/vlad/tmp.
slide 21 The second, .., refers to its parent, which just happens to be the current working directory /users/vlad.
slide 22 Let’s change our working directory to tmp using cd, then run the command nano junk.
slide 23 nano is a very simple text editor that only a programmer could really love.
slide 24 And we really do mean “text”: it can only work with plain character data, not tables, images, or any other human-friendly media.
slide 25 This is what nano looks like when it runs.
slide 26 The cursor is the blinking square in the upper left; it shows us where what we type will be inserted.
slide 27 Let’s type in a short quotation…
slide 28 …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”.
slide 29 Once our quotation is saved, we can use Control-X to quit the editor and return to the shell.
slide 30 nano doesn’t leave any output on the screen after it exits.
slide 31 But ls now shows that we have created a file called junk.
slide 32 Running ls with the -s flag shows us how large things are.
slide 33 Unfortunately, by default Unix reports sizes in disk blocks…
slide 34 …which probably isn’t the least helpful option imaginable.
slide 35 If we add the -h flag, ls uses more human-friendly units for its output.
slide 36 Here, 512 is the number of bytes the file takes up.
slide 37 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.
slide 38 Let’s tidy up by running rm junk. “rm” stands for “remove”—this command deletes files.
slide 39 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.
slide 40 If we now run ls, its output is empty once again, which tells us that our file is gone.
slide 41 Let’s re-create that file…
slide 42 …and then move up one directory to /users/vlad using cd ...
slide 43 If we try to remove the tmp directory using rm tmp, we get an error message: rm only works on files, not directories.
slide 44 The right command is rmdir, which stands for “remove directory”.
slide 45 It doesn’t work yet either, though, because the directory we’re trying to remove isn’t empty.
slide 46 This little safety feature can save you a lot of grief, particularly if you are a bad typist.
slide 47 If we want to get rid of tmp we must first delete the file junk.
slide 48 The directory is now empty, so rmdir deletes it.
slide 49 Let’s create that directory and file one more time.
slide 50 junk isn’t a particularly informative name, so let’s change the file’s name using mv.
slide 51 mv is short for “move”: we use it to move a file from one place to another.
slide 52 It also works on directories: there is no separate mvdir command.
slide 53 The first argument tells mv what we’re moving.
slide 54 The second tells it where the thing we’re moving is to go.
slide 55 In this case, we’re moving tmp/junk to tmp/quotes.txt, which has the same effect as renaming the file.
slide 56 Sure enough, ls shows us that tmp now contains one file called quotes.txt.
slide 57 Let’s bring that file into the current working directory. Again, we use mv
slide 58 …but this time, the second argument is a directory.
slide 59 The effect is to move the file from the directory it was in to a different directory.
slide 60 Sure enough, ls shows us that tmp is now empty…
slide 61 …but we now have quotes.txt in our current directory.
slide 62 And notice, by the way, that ls with a filename or directory name as an argument lists only that file or directory.
slide 63 The cp command works very much like mv, except it copies a file instead of moving it.
slide 64 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.
slide 65 To prove that we made a copy, let’s delete the quotes.txt file in the current directory…
slide 66 …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.
slide 67 Let’s make one more copy.
slide 68 This time, though, we don’t specify the destination filename, just a directory, so the copy will keep the original’s filename.
slide 69 To summarize, here are the commands we’ve seen so far, along with the two special directory names.
slide 70 In the next episode, we’ll see how to operate on text files using pipes and filters.

  1. October 7th, 2010 at 17:01 | #1

    I guess that my undying love for nano makes me a programmer. Or is it the other way around? :)

    Great summary. I think the table at the end is very helpful.

  2. Sebastian Morkisch
    November 13th, 2010 at 08:34 | #2

    Thanks. I was wondering how to delete an !empty directory the right way without using a GUI Tool. Your Screencast was helpful.

  1. No trackbacks yet.