Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introduction to the Terminal!
You have completed Introduction to the Terminal!
Preview
With the commands we've shown you so far, you can only look at files and directories that already exist on the system. Now, we're going to show you commands that will let you move, copy, and delete those files and directories.
- Let me make sure I'm in my
workspace
directory:cd ~/workspace
- First up is the
cp
command, which lets you copy files and directories.- If we list the files in this directory, we'll see a
bird.txt
file:ls
- Suppose I want another bird.
- I run the
cp
command. I give it two arguments: the name of the file I want to copy, and the name of the file I want to copy it to:cp bird.txt pigeon.txt
- If I run the
ls
command again, we'll see both the originalbird.txt
file, and the newpigeon.txt
file:ls
- And if I print the contents of
pigeon.txt
, we'll see those have been copied over, too:cat pigeon.txt
- If we list the files in this directory, we'll see a
treehouse:~/workspace/library/non-fiction$ cd ~/workspace
treehouse:~/workspace$ ls
bird.txt cart.txt library mall offices statue.txt
treehouse:~/workspace$ cp bird.txt pigeon.txt
treehouse:~/workspace$ ls
bird.txt cart.txt library mall offices pigeon.txt statue.txt
treehouse:~/workspace$ cat pigeon.txt
There is a bird here, looking up at the statue with interest.
-
cp
is an important command. It's one of those commands you'll be using a lot. That's why its name is abbreviated, so you can type it more quickly. - You can use
cp
to copy files into other directories, too.- I can copy
bird.txt
into themall
directory by giving the directory name as the second argument tocp
:cp bird.txt mall/
- As always, the directory name will work with or without a slash following it.
- Now let me change into the
mall
directory:cd mall/
- And list its contents:
ls
- You'll see a
bird.txt
file in this directory as well.
- I can copy
treehouse:~/workspace$ cp bird.txt mall/
treehouse:~/workspace$ cd mall/
treehouse:~/workspace/mall$ ls
bird.txt dullards map.txt starbunks
- You can also copy files to the parent directory.
- Let me change back to the
workspace
directory:cd ..
- And now I'll copy the
bird.txt
file again, specifying the parent directory as the target:cp bird.txt ..
- If I change to the parent directory and list its contents, you'll see another
bird.txt
file there.
- Let me change back to the
treehouse:~/workspace/mall$ cd ..
treehouse:~/workspace$ cp bird.txt ..
treehouse:~/workspace$ cd ..
treehouse:~$ ls
bird.txt workspace
- You can use
cp
to copy directories, too.- Let me change back to the
workspace
directory:cd workspace
ls
- Suppose I want to make a copy of the
offices
directory. - But I can't just type
cp offices/ more_offices
:cp offices/ more_offices
- Normally, if you ask
cp
to copy a directory, it will skip it. - But if I add the
-r
option tocp
, it will work:cp -r offices/ more_offices
ls
- You can see there's a
more_offices
directory here now. - The
-r
option stands forrecursive
, as in "copy recursively". - To do something recursively means to do it in a recurring or repeating fashion.
- In this case, it means that not only will the
offices
directory be copied, its contents and all of the contents of its subdirectories will be copied, too. - Let me list the contents of the
more_offices
directory:ls more_offices/
- You can see its subdirectories have been copied over, too.
- Any files will get copied over too.
- If I list the contents of the
more_offices/web_agency/mcgavren/
directory, you'll see a copy of the script file we saw earlier in the course:ls more_offices/web_agency/mcgavren/
- Let me change back to the
treehouse:~$ cd workspace
treehouse:~/workspace$ ls
bird.txt cart.txt library mall offices pigeon.txt statue.txt
treehouse:~/workspace$ cp offices/ more_offices
cp: omitting directory 'offices/'
treehouse:~/workspace$ cp -r offices/ more_offices
treehouse:~/workspace$ ls
bird.txt cart.txt library mall more_offices offices pigeon.txt statue.txt
treehouse:~/workspace$ ls more_offices/
dentist lawyer web_agency
treehouse:~/workspace$ ls more_offices/web_agency/mcgavren/
hello.sh
- What if you want to change a file's name, without copying it? In that case you'd use the
mv
command, which stands for "move".- I can move the
bird.txt
file tosparrow.txt
withmv bird.txt sparrow.txt
:mv bird.txt sparrow.txt
ls
- You can see there's no longer a file under the name
bird.txt
, but there is a file namedsparrow.txt
.
- I can move the
treehouse:~/workspace$ ls
bird.txt cart.txt library mall more_offices offices pigeon.txt statue.txt
treehouse:~/workspace$ mv bird.txt sparrow.txt
treehouse:~/workspace$ ls
cart.txt library mall more_offices offices pigeon.txt sparrow.txt statue.txt
- If you specify a directory as a target, you can move a file into that directory.
- Let's say we want to move the hot dog cart to the mall.
- We'd run
mv cart.txt mall/
(again, the trailing slash is optional):mv cart.txt mall/
- If I change to the mall directory and list its files, you can see the
cart.txt
file has been moved there. cd ..
ls
treehouse:~/workspace$ ls
cart.txt library mall more_offices offices pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ mv cart.txt mall/
treehouse:~/workspace$ cd mall
treehouse:~/workspace/mall$ ls
bird.txt cart.txt dullards map.txt starbunks
- You can move multiple files into a single directory by giving multiple file names.
- For example, I can move both the
pigeon.txt
andsparrow.txt
files into themall
directory with:mv pigeon.txt sparrow.txt mall/
ls
- If I change into the
mall
directory and list it's contents, you can see that both thepigeon.txt
andsparrow.txt
files have been moved here.
- For example, I can move both the
treehouse:~/workspace/mall$ cd ..
treehouse:~/workspace$ ls
library mall more_offices offices pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ mv pigeon.txt sparrow.txt mall/
treehouse:~/workspace$ ls
library mall more_offices offices statue.txt
treehouse:~/workspace$ cd mall
treehouse:~/workspace/mall$ ls
bird.txt cart.txt dullards map.txt pigeon.txt sparrow.txt starbunks
- The
mv
command is one of many commands where wildcard expansion comes in handy.- Suppose I want to move all these text files back to the parent directory.
- I could use the wildcard
*.txt
to find all of them:echo *.txt
-
mv *.txt ..
will take all of the.txt
files in this directory, and move them to the parent directory:mv *.txt ..
- If I list the files here, you can see they've all been moved out of this directory.
- And if I change to the parent directory and list the files, you can see they've all been moved here.
- Whoops! It looks like I also moved the mall map to this directory. Let me move that back to the
mall
directory:mv map.txt mall
treehouse:~/workspace/mall$ ls
bird.txt cart.txt dullards map.txt pigeon.txt sparrow.txt starbunks
treehouse:~/workspace/mall$ echo *.txt
bird.txt cart.txt map.txt pigeon.txt sparrow.txt
treehouse:~/workspace/mall$ mv *.txt ..
treehouse:~/workspace/mall$ ls
dullards starbunks
treehouse:~/workspace/mall$ cd ..
treehouse:~/workspace$ ls
bird.txt cart.txt library mall map.txt more_offices offices pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ mv map.txt mall/
ls
- This
workspace
directory is getting a little crowded. Let's remove some of these files. - We do this with the
rm
command, which stands for "remove".- Before we use this command, let me give you a word of warning: there is no undo for removing files!
- The files don't go to a "Trash" folder or anything like that; they're simply gone.
- And on Unix-like systems, their data is usually scrubbed from the disk immediately, meaning there's no such thing as a file recovery program.
- So when using the
rm
command, be sure you're removing the correct files! - Let's try removing the
bird.txt
file:rm bird.txt
- If I list the directory contents again, you can see the
bird.txt
file is gone.
treehouse:~/workspace$ ls
bird.txt cart.txt library mall more_offices offices pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ rm bird.txt
treehouse:~/workspace$ ls
cart.txt library mall more_offices offices pigeon.txt sparrow.txt statue.txt
- The
rm
file can also remove directories.- Let's try deleting this
more_offices
directory we copied. - Just like with the
cp
command, therm
command won't work on directories ordinarily:rm more_offices/
- But just like the
cp
command, therm
command has a-r
option that causes it to recursively remove a directory, all its subdirectories, and all their files:rm -r more_offices
- If I list files again, you can see the
more_offices
directory is gone now, along with all the directories and files it contained.
- Let's try deleting this
treehouse:~/workspace$ ls
cart.txt library mall more_offices offices pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ rm more_offices/
rm: cannot remove 'more_offices/': Is a directory
treehouse:~/workspace$ rm -r more_offices
treehouse:~/workspace$ ls
cart.txt library mall offices pigeon.txt sparrow.txt statue.txt
- One last command. You can use the
mkdir
command to make directories.- I can make a
park
directory withmkdir park
:mkdir park
ls
- I can change into the new directory, list its contents, make new subdirectories, anything I can do with any other directory.
cd ..
- I can make a
treehouse:~/workspace$ ls
cart.txt library mall offices pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ mkdir park
treehouse:~/workspace$ ls
cart.txt library mall offices park pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ cd park
treehouse:~/workspace/park$ cd ..
- If you pass the
-p
option tomkdir
, it will make parent directories for the directory you want to create, if they don't exist.- For example, let's say I want a
playground
directory inside mypark
directory, and atoys
directory inside that. - I can run
mkdir -p park/playground/toys
:mkdir -p park/playground/toys
ls
- The
park
directory already exists, so that's unchanged. - But there was no
playground
directory inside thepark
directory, so that's been created for us. -
cd park/
,ls
- And inside the
playground
directory, atoys
directory has been created. -
cd playground/
,ls
- We're now free to fill these
playground
andtoys
directories with files, or whatever else we want.
- For example, let's say I want a
treehouse:~/workspace$ mkdir -p park/playground/toys
treehouse:~/workspace$ ls
cart.txt library mall offices park pigeon.txt sparrow.txt statue.txt
treehouse:~/workspace$ cd park/
treehouse:~/workspace/park$ ls
playground
treehouse:~/workspace/park$ cd playground/
treehouse:~/workspace/park/playground$ ls
toys
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
With the commands we've shown you so
far, you can only look at files and
0:00
directories that already
exist on the system.
0:03
Now we're going to show you commands
that will let you to move, copy,
0:06
and delete those files and directories.
0:09
Let me make sure I'm in my workspace
directory, cd ~/workspace.
0:12
First step is the CP command which
lets you copy files and directories.
0:18
If we list the files in this directory,
we'll see bird.txt file.
0:23
Suppose I want another bird.
0:28
I run the cp command and
I give it two arguments,
0:30
the name of the file I wanna copy and
the name of file I wanna copy it to.
0:33
cp bird.txt pigeon.txt.
0:37
If I run the ls command again we'll
see both the original bird.txt file and
0:41
the new pigeon.txt file.
0:45
And if I print the contents of pigeon.txt,
0:48
we'll see that those have
been copied over too.
0:51
cp is an important command.
0:53
It's one of those commands
you'll be using a lot.
0:55
That's why its name is abbreviated so
you can type it more quickly.
0:57
You can use cp to copy files
into other directories too.
1:01
I can copy bird.txt into the mall
directory by giving the directory name as
1:04
the second argument to cp.
1:09
Now let me change into the mall
directory and list its contents, and
1:12
you'll see a bird.txt file
in this directory as well.
1:17
You can also copy files
to the parent directory.
1:21
Let me change back to the workspace
directory cd.. , and now I'll
1:24
copy the bird.txt file again specifying
the parent directory as the target.
1:28
cp bird.txt.. ,
if I changed to the parent directory and
1:33
list its contents,
you'll see another bird.txt file there.
1:37
You can use cp to copy directories too.
1:43
Let me change back to
the workspace directory.
1:45
Suppose I wanna make a copy
of the offices directory, but
1:52
I can't just type cp offices/ more_offices.
1:56
Normally if you ask cp to copy
a directory it will skip it,
2:00
but if I add the -r option to cp,
it will work, cp -r office/ more_offices.
2:05
You can see there's a
more_offices directory here now.
2:16
The -r options stands for
recursive as in copy recursively.
2:20
To do something recursively means to do
it in a recurring or repeating fashion.
2:25
In this case it means that not only
will the offices directory be copied,
2:31
its contents and all of the contents of
its sub directories will be copied too.
2:34
Let me list the contents of the
more_offices directory, ls more_offices.
2:39
You can see its subdirectories
have been copied over too.
2:47
Any files will get copied over as well.
2:50
If I list the contents of
the more_offices/web_agency/mcgavren
2:54
directory, you'll see a copy of the script
file we saw earlier in the course.
2:57
So that's how the cp command works.
3:03
Now what if you wanna change
a file's name without copying it?
3:05
In that case, you'd use the mv
command which stands for move.
3:08
I can move the bird.txt file to
sparrow.txt with mv bird.txt sparrow.txt.
3:13
You can see there's no longer
a file under the name bird.txt but
3:21
there is a file named sparrow.txt.
3:25
If you specify a directory as a target,
you can move a file into that directory.
3:27
Let's say we wanna move
the hotdog cart to the mall.
3:32
We'd run mv cart.txt mall/.
3:35
Again, the trailing slash is optional.
3:38
If I change to the mall directory and
list its files,
3:42
you can see that the cart.txt
file has been moved there.
3:45
And if I change back to the workspace
directory and list files,
3:49
you can see that the cart.txt
file is gone from this directory.
3:52
You can move multiple files into a single
directory by giving multiple file names.
3:55
For example,
I can move both the pigeon.txt and
4:00
sparrow.txt files into the mall directory
with mv pigeon.txt sparrow.txt mall/.
4:04
If I change into the mall directory,
and list its contents,
4:11
you can see that both the pigeon.txt and
sparrow.txt files have been moved here.
4:14
The move command is one of many commands
where wild card expansion comes in handy.
4:20
Suppose I wanna move all these text
files back to the parent directory.
4:25
I could use the wild card *.txt to find
all of them, mv *.txt .., will take
4:28
all of the .txt files in this directory
and move them to the parent directory.
4:35
If I list the files here you can see
they've all been moved out of this
4:41
directory.
4:45
And if I change to
the parent directory and
4:46
list the files you can see
they've all been moved here.
4:48
Woops, it looks like I also moved
the mall map to this directory.
4:52
Let me move that back to the mall
directory, mv map.txt mall.
4:55
This workspace directory is
getting a little crowded,
5:03
let's remove some of these files.
5:05
We do this with the rm command,
which stands for remove.
5:08
Before we use this command,
let me give you a word of warning,
5:12
there is no undo for removing files.
5:16
The files don't go to a trash folder or
anything like that, they're simply gone.
5:18
And on Unix-like systems,
their data is usually scrubbed
5:23
from the disk immediately, meaning there's
no such thing as a file recovery program.
5:26
So when using the rm command make sure
you're removing the correct files.
5:32
Let's try removing the bird.txt file,
rm bird.txt.
5:36
If I list the directory and contents again
you can see the bird.txt file is gone.
5:42
The rm command can also
remove directories.
5:47
Let's try deleting this more
offices directory we copied.
5:50
Just like the what the cp command,
5:54
the RM command won't work
on directories ordinarily.
5:56
But just like the cp command, the rm
command has a -r option that causes it to
6:02
recursively remove a directory, all its
sub-directories and all their files.
6:07
If I list files again, you can see
more_offices directory is gone now,
6:12
along with all the directories and
files it contained.
6:16
One last command, you can use
the mkdir command to make directories.
6:19
I can make a park
directory with mkdir park.
6:25
You can see there's a new
park directory here now.
6:30
I can change into the new directory,
make new sub directories,
6:33
anything I can do with
any other directory.
6:36
If you pass the -p option to make
directory, it will make parent directories
6:40
for the directory you wanna
create if they don't exist.
6:44
For example, let's say I want a playground
directory inside my park directory, and
6:48
a toys directory inside that.
6:52
I could run make directory
-p park/playground/toys.
6:54
The park directory already exists,
so that's unchanged.
7:00
But there was no playground directory
inside the park directory, so
7:04
that's been created for us.
7:07
And inside the playground directory,
a toys directory has been created.
7:11
We're now free to fill these playground
and toys directories with files or
7:15
whatever else we want.
7:19
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up