We now know how to explore files and directories, but how do we create them in the first place?
In this episode we will learn about creating and moving files and directories, using the IntroShell/data/shell-lesson-data/exercise-data/writing
directory as an example.
We should still be in the shell-lesson-data directory on the Desktop, which we can check using:
jupyter-user:~$pwd
/jupyter/jupyter-user/IntroShell/data/shell-lesson-data
Now lets change to the exercise-data/writing directory:
jupyter-user:~$cd excercise-data/writing
Ans check what is in the directory:
jupyter-user:~$ls -F
LittleWomen.txt haiku.txt
Let’s create a new directory called thesis using the command mkdir thesis
(which will print no output):
jupyter-user:~$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:
jupyter-user:~$ls -F
LittleWomen.txt haiku.txt thesis/
Since we’ve just created the thesis directory, there’s nothing in it yet:
jupyter-user:~$ls -F thesis
Note that mkdir is not limited to creating single directories one at a time. The -p
option allows mkdir
to create a directory with nested subdirectories in a single operation:
jupyter-user:~$mkdir -p ../project/data ../project/results
The -R option to the ls
command will list all nested subdirectories within a directory. Let’s use ls -FR
to recursively list the new directory hierarchy we just created in the project directory:
jupyter-user:~$ls -FR ../project
../project/:
data/ results/
../project/data:
../project/results:
Let’s change our working directory to thesis using cd, then run a text editor called Nano to create a file called draft.txt:
jupyter-user:~$cd thesis
jupyter-user:~$nano draft.txt
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
:
Lets tidy up our home dirctory by removing the file we have just created:
jupyter-user:~$rm my_file.txt
The rm
command removes files (rm
is short for "remove"). If we run ls
again, it tells us that our file is gone.
Lets move back to our writing
directory:
jupyter-user:~$cd -
jupyter-user:~$pwd
/jupyter/jupyter-user/IntroShell/data/shell-lesson-data/exercise-data/writing
And remove the thesis
folder we created:
jupyter-user:~$rm thesis
rm: cannot remove 'thesis': Is a directory
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
:
jupyter-user:~$rm -r thesis
Recreate the thesis
folder and draft.txt
file again.
draft.txt
isn't a particularly informative name, so let's change the file's name using mv
, which is short for "move":
jupyter-user:~$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
:
jupyter-user:~$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 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.
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:
jupyter-user:~$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:
jupyter-user:~$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:
jupyter-user:~$cp quotes.txt thesis/quotations.txt
jupyter-user:~$ls quotes.txt thesis/quotations.txt
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.
jupyter-user:~$rm quotes.txt
jupyter-user:~$ls quotes.txt thesis/quotations.txt
ls: cannot access 'quotes.txt': No such file or directory
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:
jupyter-user:~$cp -r thesis thesis_backup
and check the result by listing the contents of both the thesis
and thesis_backup
directories:
jupyter-user:~$ls thesis thesis_backup
thesis:
quotations.txt
thesis_backup/:
quotations.txt
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
.