Working with files and directories
Overview
Teaching: 15 min
Exercises: 15 minQuestions
How do I create, copy, delete files and directories?
Objectives
How to create a directory hierarchy that matches a given diagram and how to navigate through it.
Delete, copy and move specified files and/or directories
Creating and removing directories
Before we start with creating new directories and doing other operations, we should find a place to work, otherwise material gets lost.
Cloning a repository
Online repositories contain written code in a directory. Github is a common place for people to upload their code in a safe place online, and so others can use it, comment and make changes. Many coding libraries are stored on GitHub, and it is where we store the materials for this lesson.
We are going to get you to clone this repo to your computer. Using the
cd
command, navigate to a directory which is easy for you to find (Desktop
, or aCourses
directory inDocuments
), and then type the following command in your terminal.git clone https://github.com/ICHEC-learn/intro-to-linux.git
This will clone the repository, which includes slides for the lesson into the directory that you are in. It may take a minute.
Then type
cd intro-to-linux/test
and you will arrive into that directory. Typepwd
so you know where you are andls
to confirm that there are files calledREADME.md
,hamlet.txt
and a directory,wildcards/
. We will get to these in later episodes.
Creating directories can be done very easily, with the mkdir
command, followed by the name of the directory that you
wish to make. So, let us create a new directory and then us our ls
command to show that is has been created
$ mkdir mydir
$ ls -F
README.md mydir/
Now we will move into it, then confirm the directory that you are in.
$ cd mydir
$ pwd
/Users/path/to/intro-to-linux/test/mydir/
Seems ok, doesn’t it? Now let us move back up one directory and then remove our newly created directory with our new
command rmdir
followed by the directory name.
$ cd ..
$ rmdir mydir
$ ls
On typing ls
you will notice that the directory mydir
has been removed. So let’s try this again with a challenge.
Creating and removing a directory
For this we are going to remove a directory that has a file in it, so.
- Create a new directory
- Change into the directory
- Create a new file
- Move back one directory
- Remove it
Solution
mkdir newdir
cd mydir
touch myfile
cd ..
rmdir mydir
You may have noticed that we get an error message;
rmdir: newdir: Directory not empty
We will now introduce the next command, which should be treated with care!
Getting lost with
cd
At the beginning, you may find that you get lost easily using
cd
, or more specifically,cd
on its own. There is a small shortcut that you can use,cd -
, which puts you back to the previous directory you were in.
Removing files
That last challenge was designed to catch you out, as the rmdir
command only works with directories that are empty.
We now introduce a command that should be handled with care. The rm
command.
Why “handled with care” you may ask! Because the shell has no recycle 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.
So when you delete files and directories in the shell… it’s gone!
Let us try removing the mydir
directory we created in the exercise.
$ rm mydir/
rm: mydir: is a directory
So as you can see, we cannot use rm
by itself to remove a directory, but let us move inside mydir
and delete the
file from there
$ cd mydir/
$ rm myfile
$ ls
The output here should be nothing, as we have deleted the file myfile
. And remember, there is no way of getting it
back! Let’s have a look at a way of using rm
safely.
Using
rm
safelyBy now you have seen the power of using
rm
, but we can add a useful flag,-i
which will request the user to confirm we want the file removed. So, create a new file usingtouch newfile
, and then try and remove it using the extra flag.Notice a difference? What are the different options should you try?
Solution
$ touch newfile $ rm -i newfile
The
-i
option will prompt before (every) removal (useY
to confirm deletion orN
to keep the file).
You can use rm
with multiple flags, but watch out as here be dragons! The -r
flag can be particularly useful when
trying to remove directories with a lot of files in it. Instead of removing each file one at a time, you can remove all
files in a directory and the directory itself by using;
$ rm -r mydir/
As with other commands, unless the directory/files can be seen by the program in the current working directory, the command will not run.
Below you can find a few of the different flags that rm
supports.
Flag | Action | Danger Level |
---|---|---|
-i |
Requests confirmation before removing a file | None |
-r |
Removes the file hierarchy, removing the directory and all files within it | Medium |
-f |
Removes files without confirmation, regardless of file permissions | High |
Death command
You may have been able to deduce that there is a command that will remove everything with no prompting. Combined with wildcards, (which will be discussed in the next episode) this can remove everything.
$ rm -rf *
So, a word of advice… don’t ever type this!
Moving and Copying
Now we move onto safer commands. The mv
command can do two things.
- Move a file from one location to another
- Change the name of the file.
Let’s have a look at this in action. Make sure you are in the test
directory. You should have a README.md
file,
hamlet.txt
and possibly a directory in there. Create a new directory if there is not one there.
First, let’s change the extension of the README file from .md
to .txt
. We need the original file name, as well as
the name/location that we want to rename it to.
$ mv README.md README.txt
$ ls README
README.txt
One must 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.
Now, lets move hamlet.txt
into a directory, let’s call it newdir
$ mv hamlet.txt newdir/
$ ls
You will see that hamlet.txt
is no longer there, as it has been moved to newdir
. So when we head in there, we can
see that our file has indeed been moved there.
$ cd newdir/
$ ls
hamlet.txt
Now let us introduce the copy command, cp
, which works in the same way as mv
, specify the file you want to copy,
and where you want to copy it to. This second argument, like mv
can be a new file, or a different directory.
So lets copy hamlet.txt
to the directory above our current one.
$ cp hamlet.txt ../
Heading up to that extra directory and we see that the file has indeed been copied.
Let’s finish this off with a bigger challenge, creating a directory hierarchy.
Creating a directory hierarchy
Look at the directory structure below, and recreate it. You should undertake the following steps.
- Ensure you are in the
test/newdir
directory- List all the files in the directory, then list all the hidden files
- Create a new directory called
01
and move into it- Create a new file called
README.txt
- Move back one directory
- Create two more directories
02
and03
. In02
, create a new directory calledsubdir
- In
03
, create a file calledscript.sh
- Copy
script.sh
to the02/subdir
folder, then move into it to confirm it is there- Remove this copied file, try using the
-i
flag
Use
pwd
andls
where needed so you can confirm where you are at any timeSolution
cd /path-to/test/newdir
ls
,ls -a
mkdir 01
,cd 01
touch README.txt
cd ..
mkdir 02 03
,cd 02
,mkdir subdir
cd ../03
,touch script.sh
cp script.sh ../02/subdir/
,cd ../02/subdir
rm -i script.sh
Key Points
Most files’ names are
something.extension
. The extension isn’t required, and doesn’t guarantee anything, but is normally used to indicate the type of data in the file.
cp [old] [new]
copies a file.
mv [old] [path]
moves a file into the specified path,mv [old] [new]
renames a file.
mkdir [path]
creates a new directory.
rmdir [path]
removes an empty directory.rm [path]
removes a file. These are irreversible as the shell does not have a recycle bin.