We now know how to explore files and directories, but how do we create them in the first place? Let's go back to our data-shell
directory on the Desktop and use ls -F
to see what it contains:
%%bash2 --dir ~/data/data-shell
pwd
If after working through the exercises at the end of the previous lesson, you are not in your data-shell
directory, navigate to the correct location.
%%bash2
ls -F
Let's create a new directory called thesis
using the command mkdir thesis
(which has no output):
%%bash2
mkdir thesis
As you might 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:
%%bash2
ls -F
Since we've just created the thesis
directory, there's nothing in it yet:
%%bash2
ls -F thesis
Let's change our working directory to thesis
using cd
, then run a text editor called Nano to create a file called draft.txt
:
%%bash2
cd thesis
# nano draft.txt
# If you want to run nano, use the interactive terminal, it cannot be used in a notebook
# You can instead use the %%file magic if you want to continue working in the notebook.
# The '#' indicates a comment in bash so the command is not executred.
# When you run at the command line you should just type: 'nano draft.txt'
%%file draft.txt
This is my file
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.
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
:
%%bash2
ls
Returning to the data-shell
directory, let's tidy up the thesis
directory by removing the draft we created:
%%bash2
#cd thesis
rm draft.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:
%%bash2
ls
Let's re-create that file and then move up one directory to /Users/nelle/Desktop/data-shell
using cd ..
:
%%bash2
pwd
#nano draft.txt
%%file draft.txt
Some text
%%bash2
ls
%%bash2
cd ..
If we try to remove the entire thesis
directory using rm thesis
, we get an error message:
%%bash2
rm thesis
This happens because rm
by default only works on files, not directories.
To really get rid of thesis
we must also delete the file draft.txt
. We can do this with the recursive option for rm
:
%%bash2
rm -r thesis
Let's create that directory and file one more time. (Note that this time we're running nano
with the path thesis/draft.txt
, rather than going into the thesis
directory and running nano
on draft.txt
there.)
%%bash2
pwd
%%bash2
mkdir thesis
#nano thesis/draft.txt
%%file thesis/draft.txt
More text!
%%bash2
ls thesis
draft.txt
isn't a particularly informative name, so let's change the file's name using mv
, which is short for "move":
%%bash2
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
:
%%bash2
ls thesis
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 flag, mv -i
(or mv --interactive
), can be used to make mv
ask you for confirmation before overwriting.
Just for the sake of consistency, 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 just use 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.
%%bash2
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:
%%bash2
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:
%%bash2
ls quotes.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 arguments - like most Unix commands, ls
can be given multiple paths at once:
%%bash2
cp quotes.txt thesis/quotations.txt
ls quotes.txt thesis/quotations.txt
To prove that we made a copy, let's delete the quotes.txt
file in the current directory and then run that same ls
again.
%%bash2
rm quotes.txt
ls quotes.txt thesis/quotations.txt
This time it tells us that it can't find quotes.txt
in the current directory, but it does find the copy in thesis
that we didn't delete.
cp old new
copies a file.mkdir path
creates a new directory.mv old new
moves (renames) a file or directory.rm
path removes (deletes) a file.*
matches zero or more characters in a filename, so *.txt
matches all files ending in .txt
.?
matches any single character in a filename, so ?.txt
matches a.txt
but not any.txt
.Control
key may be described in many ways, including Ctrl-X
, Control-X
, and ^X
.