The Unix Shell
Creating Things
Learning Objectives
- Create a directory hierarchy that matches a given diagram.
- Create files in that hierarchy using an editor or by copying and renaming existing files.
- Display the contents of a directory using the command line.
- Delete specified files and/or directories.
We now know how to explore files and directories, but how do we create them in the first place? Let’s go back to Nelle’s home directory, /Users/nelle
, and use ls -F
to see what it contains:
$ pwd
/mnt/home/nelle
$ ls -F
avida/ notes.txt
We’re going to repeat a classic Avida experiment on the origin of complex features. Let’s set up a directory structure to keep track of everything.
$ mkdir complex_features_experiment
As you might (or might not) guess from its name, mkdir
means “make directory”. Since thesis
is a relative path (i.e., doesn’t have a leading slash), the new directory is created in the current working directory:
$ ls -F
avida/ notes.txt complex_features_experiment/
However, there’s nothing in it yet:
$ ls -F complex_features_experiment
Let’s change our working directory to complex_features_experiment
using cd
, then run a text editor called Nano to create a file called notes.txt
:
$ cd complex_features_experiment
$ nano notes.txt
Let’s type in a few lines of text, then use Control-O to write our data to disk:
Once our file is saved, we can use Control-X to quit the editor and return to the shell. (Unix documentation often uses the shorthand ^A
to mean “control-A”.) nano
doesn’t leave any output on the screen after it exits, but ls
now shows that we have created a file called draft.txt
:
$ ls
notes.txt
Let’s tidy up by running rm draft.txt
:
$ rm notes.txt
This command removes files (“rm” is short for “remove”). If we run ls
again, its output is empty once more, which tells us that our file is gone:
$ ls
Let’s re-create that file and then move up one directory to /mnt/home/nelle
using cd ..
:
$ pwd
/mnt/home/nelle/complex_features_experiment
$ nano notes.txt
$ ls
notes.txt
$ cd ..
If we try to remove the entire thesis
directory using rm thesis
, we get an error message:
$ rm complex_features_experiment
rm: cannot remove `complex_features_experiment': Is a directory
This happens because rm
only works on files, not directories. The right command is rmdir
, which is short for “remove directory”. It doesn’t work yet either, though, because the directory we’re trying to remove isn’t empty:
$ rmdir complex_features_experiment
rmdir: failed to remove `thesis': Directory not empty
This little safety feature can save you a lot of grief, particularly if you are a bad typist. To really get rid of thesis
we must first delete the file draft.txt
:
$ rm complex_features_experiment/notes.txt
The directory is now empty, so rmdir
can delete it:
$ rmdir complex_features_experiment
Let’s create that directory and file one more time. (Note that this time we’re running nano
with the path complex_features_experiment/notes.txt
, rather than going into the complex_features_experiment
directory and running nano
on notes.txt
there.)
$ pwd
/mnt/home/nelle
$ mkdir complex_features_experiment
$ nano complex_features_experiment/notees.txt
$ ls complex_features_experiment
notes.txt
notes.txt
isn’t a particularly informative name, so let’s change the file’s name using mv
, which is short for “move”:
$ mv complex_features_experiment/notes.txt complex_features_experiment/experiments.txt
The first parameter tells mv
what we’re “moving”, while the second is where it’s to go. In this case, we’re moving complex_features_experiment/notes.txt
to complex_features_experiment/experiments.txt
which has the same effect as renaming the file. Sure enough, ls
shows us that complex_features_experiment
now contains one file called experiments.txt
:
$ ls complex_features_experiments
experiments.txt
Just for the sake of inconsistency, mv
also works on directories — there is no separate mvdir
command.
Let’s move experiments.txt
into the current working directory. We use mv
once again, but this time we’ll just use the name of a directory as the second parameter to tell mv
that we want to keep the filename, but put the file somewhere new. (This is why the command is called “move”.) In this case, the directory name we use is the special directory name .
that we mentioned earlier.
$ mv complex_features_experiment/experiments.txt .
The effect is to move the file from the directory it was in to the current working directory. ls
now shows us that complex_features_experiment
is empty:
$ ls complex_features_experiment
Further, ls
with a filename or directory name as a parameter only lists that file or directory. We can use this to see that quotes.txt
is still in our current directory:
$ ls experiments.txt
experiments.txt
The 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 parameters — like most Unix commands, ls
can be given thousands of paths at once:
$ cp experiments.txt complex_features_experiment/planned_experiments.txt
$ ls
experiments.txt complex_features_experiment/planned_experiments.txt
To prove that we made a copy, let’s delete the experiments.txt
file in the current directory and then run that same ls
again.
$ rm experiments.txt
$ ls experiments.txt complex_features_experiment/planned_experiments.txt
ls: cannot access experiments.txt: No such file or directory
complex_features_experiment/planned_experiments.txt
This time it tells us that it can’t find experiments.txt
in the current directory, but it does find the copy in complex_features_experiment
that we didn’t delete.
To get ready for the next step, let’s make a few more directories:
$ cd complex_features_experiment
$ mkdir configs
$ mkdir results
$ mkdir results/all_rewarded
$ mkdir results/equ_rewarded
Now let’s copy the Avida config files into our configs directory, so that we always know what configs we used to run this experiment:
$ cp ~/avida/cbuild/work/*.cfg configs
$ cp ~/avida/cbuild/work/*.org configus
$ cp ~/avida/cbuild/work/avida configus
Renaming files
Suppose that you created a .txt
file in your current directory to contain a list of the statistical tests you will need to do to analyze your data, and named it: statstics.txt
After creating and saving this file you realize you misspelled the filename! You want to correct the mistake, which of the following commands could you use to do so?
cp statstics.txt statistics.txt
mv statstics.txt statistics.txt
mv statstics.txt .
cp statstics.txt .
Moving and Copying
What is the output of the closing ls
command in the sequence shown below?
$ pwd
/Users/jamie/data
$ ls
proteins.dat
$ mkdir recombine
$ mv proteins.dat recombine
$ cp recombine/proteins.dat ../proteins-saved.dat
$ ls
proteins-saved.dat recombine
recombine
proteins.dat recombine
proteins-saved.dat
Organizing Directories and Files
Jamie is working on a project and she sees that her files aren’t very well organized:
$ ls -F
analyzed/ fructose.dat raw/ sucrose.dat
The fructose.dat
and sucrose.dat
files contain output from her data analysis. What command(s) covered in this lesson does she need to run so that the commands below will produce the output shown?
$ ls -F
analyzed/ raw/
$ ls analyzed
fructose.dat sucrose.dat
Copy with Multiple Filenames
What does cp
do when given several filenames and a directory name, as in:
$ mkdir backup
$ cp thesis/citations.txt thesis/quotations.txt backup
What does cp
do when given three or more filenames, as in:
$ ls -F
intro.txt methods.txt survey.txt
$ cp intro.txt methods.txt survey.txt
Listing Recursively and By Time
The command ls -R
lists the contents of directories recursively, i.e., lists their sub-directories, sub-sub-directories, and so on in alphabetical order at each level. The command ls -t
lists things by time of last change, with most recently changed files or directories first. In what order does ls -R -t
display things?