4 Ch. 1.4: Working with Files and Directories
Creating Directories
We now know how to explore files and directories, but how do we create them in the first place?
Step one: see where we are and what we already have
Let’s go back to our data-shell
directory on the Desktop and use ls -F
to see what it contains:
$ pwd
/Users/nelle/Desktop/data-shell
$ ls -F
creatures/ data/ molecules/ north-pacific-gyre/ notes.txt pizza.cfg solar.pdf writing/
Create a directory
Let’s create a new directory called thesis
using the command mkdir thesis
(which has no output):
mkdir thesis
As you might guess from its name, mkdir
means ‘make directory’. Since thesis
is a relative path (i.e., does not have a leading slash, like /what/ever/thesis
), the new directory is created in the current working directory:
$ ls -F
creatures/ data/ molecules/ north-pacific-gyre/ notes.txt pizza.cfg solar.pdf thesis/ writing/
Two days of doing the same thing |
Using the shell to create a directory is no different than using a file explorer. If you open the current directory using your operating system’s graphical file explorer, the thesis directory will appear there too. While the shell and the file explorer are two different ways of interacting with the files, the files and directories themselves are the same. |
Good names for files and directories |
Complicated names of files and directories can make your life painful when working on the command line. Here we provide a few useful tips for the names of your files.
If you need to refer to names of files or directories that have spaces or other special characters, you should surround the name in quotes ( |
Since we’ve just created the thesis
directory, there’s nothing in it yet:
$ ls -F thesis
Create a text file
Let’s change our working directory to thesis
using cd
, then run a text editor called Nano to create a file called draft.txt
:
$ cd thesis
$ nano draft.txt
Which Editor? |
When we say, ‘nano is a text editor’” we really do mean ‘text’: it can only work with plain character data, not tables, images, or any other human-friendly media. We use it in examples because it is one of the least complex text editors. However, because of this trait, it may not be powerful enough or flexible enough for the work you need to do after this workshop. On Unix systems (such as Linux and macOS), many programmers use Emacs or Vim (both of which require more time to learn), or a graphical editor such as Gedit. On Windows, you may wish to use Notepad++. Windows also has a built-in editor called notepad that can be run from the command line in the same way as nano for the purposes of this lesson.
No matter what editor you use, you will need to know where it searches for and saves files. If you start it from the shell, it will (probably) use your current working directory as its default location. If you use your computer’s start menu, it may want to save files in your desktop or documents directory instead. You can change this by navigating to another directory the first time you ‘Save As…’ |
Let’s type in a few lines of text. Once we’re happy with our text, we can press Ctrl+O (press the Ctrl or Control key and, while holding it down, press the O key) to write our data to disk (we’ll be asked what file we want to save this to: press Return to accept the suggested default of draft.txt
).
Once our file is saved, we can use Ctrl+X to quit the editor and return to the shell.
Control, Ctrl, or ^ Key |
The Control key is also called the ‘Ctrl’ key. There are various ways in which using the Control key may be described. For example, you may see an instruction to press the Control key and, while holding it down, press the X key, described as any of:
In nano, along the bottom of the screen you’ll see |
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
draft.txt
Creating Files a Different Way
We have seen how to create text files using the |
Solution
|
What is in a Name?
You may have noticed that all of Nelle’s files are named ‘something dot something’, and in this part of the lesson, we always used the extension This is just a convention, albeit an important one. Files contain bytes: it’s up to us and our programs to interpret those bytes according to the rules for plain text files, PDF documents, configuration files, images, and so on. Naming a PNG image of a whale as |
Moving Files and Directories
Returning to the data-shell
directory,
cd ~/Desktop/data-shell/
In our thesis
directory we have a file draft.txt
which isn’t a particularly informative name, so let’s change the file’s name using mv
, which is short for ‘move’:
$ mv thesis/draft.txt thesis/quotes.txt
The first argument tells mv
what we’re ‘moving’, while the second is where it’s to go. In this case, we’re moving thesis/draft.txt
to thesis/quotes.txt
, which has the same effect as renaming the file. Sure enough, ls
shows us that thesis
now contains one file called quotes.txt
:
$ ls thesis
quotes.txt
One has to be careful when specifying the target file name, since mv
will silently overwrite any existing file with the same name, which could lead to data loss. An additional option, mv -i
(or mv --interactive
), can be used to make mv
ask you for confirmation before overwriting.
Note that mv
also works on directories.
Let’s move quotes.txt
into the current working directory. We use mv
once again, but this time we’ll use just the name of a directory as the second argument 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 thesis/quotes.txt .
The effect is to move the file from the directory it was in to the current working directory. ls
now shows us that thesis
is empty:
ls thesis
Further, ls
with a filename or directory name as an argument only lists that file or directory. We can use this to see that quotes.txt
is still in our current directory:
$ ls quotes.txt
quotes.txt
Moving Files into a New Directory |
After running the following commands, Jamie realizes that she put the files sucrose.dat and maltose.dat into the wrong folder. The files should have been placed in the raw folder.
Fill in the blanks to move these files to the
Solution$ mv sucrose.dat maltose.dat ../raw Recall that |
Copying Files and Directories
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 arguments — like most Unix commands, ls
can be given multiple paths at once:
$ cp quotes.txt thesis/quotations.txt
$ ls quotes.txt thesis/quotations.txt
quotes.txt thesis/quotations.txt
We can also copy a directory and all its contents by using the recursive option -r
, e.g. to back up a directory:
$ cp -r thesis thesis_backup
We can check the result by listing the contents of both the thesis
and thesis_backup
directory:
$ ls thesis thesis_backup
thesis:
quotations.txt
thesis_backup:
quotations.txt
Renaming Files |
Suppose that you created a plain-text 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?
Solution
|
Moving and Copying |
What is the output of the closing ls command in the sequence shown below?
|
SolutionWe start in the
|
Removing Files and Directories
Returning to the data-shell
directory, let’s tidy up this directory by removing the quotes.txt
file we created. The Unix command we’ll use for this is rm
(short for ‘remove’):
$ rm quotes.txt
ls
:
$ ls quotes.txt
ls: cannot access 'quotes.txt': No such file or directory
Deleting is forever |
The Unix shell doesn’t have a trash bin that we can recover deleted files from (though most graphical interfaces to Unix do). Instead, when we delete files, they are unlinked 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 recycle the file’s disk space right away. |
Using |
What happens when we execute rm -i thesis_backup/quotations.txt ? Why would we want this protection when using rm ?
Solution
The |
If we try to remove the thesis directory using rm thesis, we get an error message:
$ rm thesis
rm: cannot remove `thesis': Is a directory
This happens because rm
by default only works on files, not directories.
rm
can remove a directory and all its contents if we use the recursive option -r
, and it will do so without any confirmation prompts:
$ rm -r thesis
Given that there is no way to retrieve files deleted using the shell, rm -r
should be used with great caution (you might consider adding the interactive option rm -r -i
).
Operations with multiples files and directories
Oftentimes one needs to copy or move several files at once. This can be done by providing a list of individual filenames, or specifying a naming pattern using wildcards.
Copying with Multiples Filenames |
For this exercise, you can test the commands in the data-shell/data directory.
In the example below, what does
In the example below, what does
cp do when given three or more file names?
Solution
|
Using wildcards for accessing multiple files at once
Wildcards |
* is a wildcard, which matches zero or more characters. Let’s consider the data-shell/molecules directory: *.pdb matches ethane.pdb , propane.pdb , and every file that ends with ‘.pdb’. On the other hand, p*.pdb only matches pentane.pdb and propane.pdb , because the ‘p’ at the front only matches filenames that begin with the letter ‘p’.
Wildcards can be used in combination with each other e.g. When the shell sees a wildcard, it expands the wildcard to create a list of matching filenames before running the command that was asked for. As an exception, if a wildcard expression does not match any file, Bash will pass the expression as an argument to the command as it is. For example typing |
List filenames matching a pattern
When run in the molecules
directory, which ls
command(s) will produce this output?
ethane.pdb methane.pdb
ls *t*ane.pdb
ls *t?ne.*
ls *t??ne.pdb
ls ethane.*
Solution
The solution is 3.
1. shows all files whose names contain zero or more characters (*) followed by the letter t, then zero or more characters (*) followed by ane.pdb. This gives ethane.pdb methane.pdb octane.pdb pentane.pdb.
2. shows all files whose names start with zero or more characters (*) followed by the letter t, then a single character (?), then ne. followed by zero or more characters (*). This will give us octane.pdb and pentane.pdb but doesn’t match anything which ends in thane.pdb.
3. fixes the problems of option 2 by matching two characters (??) between t and ne. This is the solution.
4. only shows files starting with ethane..
More on Wildcards |
Sam has a directory containing calibration data, datasets, and descriptions of the datasets:
Before heading off to another field trip, she wants to back up her data and send some datasets to her colleague Bob. Sam uses the following commands to get the job done:
Help Sam by filling in the blanks. The resulting directory structure should look like this
Solution
|
Organizing Directories and Files |
Jamie is working on a project and she sees that her files aren’t very well organized:
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?
|
Solution
Jamie needs to move her files
fructose.dat and sucrose.dat to the analyzed directory. The shell will expand *.dat to match all .dat files in the current directory. The mv command then moves the list of .dat files to the ‘analyzed’ directory. |
Reproduce a folder structure
You’re starting a new experiment, and would like to duplicate the directory structure from your previous experiment so you can add new data. Assume that the previous experiment is in a folder called ‘2016-05-18’, which contains a
|
Which of the following set of commands would achieve this objective? What would the other commands do?
SolutionThe first two sets of commands achieve this objective. The first set uses relative paths to create the top level directory before the subdirectories. The third set of commands will give an error because The final set of commands generates the ‘raw’ and ‘processed’ directories at the same level as the ‘data’ directory. |
Key Points |
|