Last Updated: 2023-10-11
Before beginning this lesson, if you do not already have the shell software installed, you will need to download and install it
This session will be geared around building a directory hierarchy, and so we will be following a series of interactive steps exercises as we learn new Unix commands.
To begin, use the cd
command to move to the Desktop
directory on your computer.
mkdir
On your Desktop, create a new personal directory (*choose whatever name you like)
After creating this new directory, move from your Desktop
to the new directory using the cd
command.
Nano is a text editor that can be used to create and edit plain text files via the command line. When we say ‘plain text’, it can only work with plain character data, and doesn’t work with tables, images, or any other media. There are many different text editors to choose from, but for the purpose of this lesson, we are going to use Nano because it’s one of the least complex. As you move forward in your Unix journey, feel free to explore and play with different brands of text editors. For the purpose of this lesson, we will be working on .txt files, but there are other plain-text file types that you are also free to explore.
Nano syntax:
nano
(opens text editor)nano + new-file-name
(opens text editor and creates a new file with a specified name)nano + existing-file-name
(opens text editor to an existing file)Exercise 1
On your desktop, in your personal directory:
- Create 3 .txt files containing:
- Your 3 favourite musical artists
- Your 3 favourite movies
- Your 3 favourite books
- Name each .txt file as you see fit
touch
touch + new-file-name
cp
Copies a file. There are a few different ways that this command can be used:
cp {file-path/file-name} + {target-path}
front-porch.wav
from the Country
directory to the Party
directory:
cp Country/Kenny-Chesney/front-porch.wav Party
Starting in the position illustrated in top tree diagram, this command will result in the what’s presented the bottom diagram. Feel free to create this hierarchy on your own machine and play around!
Exercise 2
- In your personal directory, create a new directory called “music”
- Copy the file with your 3 favourite songs to this new directory, keeping the same file name
cp
cp {file-path/file-name} + {target-path/new-file-name}
front-porch.wav
from the Country
directory to the Party
directory, changing the file name to fp.wav
:cp Country/Kenny-Chesney/front-porch.wav Party/fp.wav
Exercise 3
- In your personal directory, create a new directory called “movies”
- Copy the file containing your 3 favourite movies to this new directory, giving the file a new name
cp
cp {file-path/file-name} + {target-path/new-file-name}
front-porch.wav
within the Country
directory (its current directory), changing the name to fp.wav
:cp Country/Kenney-Chesney/front-porch.wav Country/Kenney-Chesney/fp.wav
Exercise 4
- Copy the file containing your 3 favourite books it is current directory, giving the file a new name.
We’re now moving on to commands that are a bit more dangerous than those that have already been discussed, in that once they’re executed, there’s no way to undo them. Considering this, always ensure that you’ve backed up all your important files so you won’t have to deal with any unnecessary headaches.
mv
Move a file from one destination to another. This command works with the same syntax as the cp
command, noting that files are being moved rather than being copied. The one unique function that mv
offers in that it can be used to change a file’s name, using the following pattern:
mv {file-name} {new-file-name}
Exercise 5
In your personal directory, create a new directory called “books” Move one of the files containing your 3 favourite books to this new directory, keeping the same file name
The mv
command can also be used to change a file’s name while moving it to a new directory.
mv {file-path/file-name}{/targer-path/new-file-name}
Now we’re going to move to a command that contains some danger, in that it will delete a file, with no undo button. Be cautious with this command, as you won’t be able to retrive files that are deleted this way.
rm
rm {file-name}
In addition to the commands that we’ve discussed, there are a number of “flags” in Unix that allow us to change the behaviour of a command. For the purpose of this lesson we’ll only be discussing one flag, but for a comprehensive list of Unix flags, see the “Resources” section at the bottom of the page.
-r
flag
This is called the “recursive” flag, and allows commands to operate on multiple files or whole directories (the previous commands only work on single files on their own).
To use the -r
flag, and any other Unix flag, simply place if after a command, and before an argument:
Syntax: {command} + {-flag} + {directory/files}
Erase a directory
rm -r {directory-name}
Copy a directory
cp -r {directory-name}
Moving back to the directory structure you’ve created on your personal machine, we’ve made a of a mess! But that’s ok, because it’s very common for our files to get messy when working on projects. With that said, let’s finish things up cleaning our files up and putting together a clean structure.
Exercise 8
Create three new .txt files:
- one that contains your favourite breakfast food,
- one that contains your favourite lunch food,
- one that contains your favourite dinner food.
Recreate the following directory structure with the files you’ve been working with. The
my-movies
,my-music
,my-books
, andmy- foods
directories contain the .txt files of your favourites.