Bummer! You have been redirected as the page you requested could not be found.
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!
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
workspacedirectory:cd ~/workspace - First up is the
cpcommand, which lets you copy files and directories.- If we list the files in this directory, we'll see a
bird.txtfile:ls - Suppose I want another bird.
- I run the
cpcommand. 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
lscommand again, we'll see both the originalbird.txtfile, and the newpigeon.txtfile: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.
-
cpis 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
cpto copy files into other directories, too.- I can copy
bird.txtinto themalldirectory 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
malldirectory:cd mall/ - And list its contents:
ls - You'll see a
bird.txtfile 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
workspacedirectory:cd .. - And now I'll copy the
bird.txtfile 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.txtfile 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
cpto copy directories, too.- Let me change back to the
workspacedirectory:cd workspace ls- Suppose I want to make a copy of the
officesdirectory. - But I can't just type
cp offices/ more_offices:cp offices/ more_offices - Normally, if you ask
cpto copy a directory, it will skip it. - But if I add the
-roption tocp, it will work:cp -r offices/ more_offices ls- You can see there's a
more_officesdirectory here now. - The
-roption 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
officesdirectory be copied, its contents and all of the contents of its subdirectories will be copied, too. - Let me list the contents of the
more_officesdirectory: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
mvcommand, which stands for "move".- I can move the
bird.txtfile tosparrow.txtwithmv 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.txtfile 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.txtandsparrow.txtfiles into themalldirectory with:mv pigeon.txt sparrow.txt mall/ ls- If I change into the
malldirectory and list it's contents, you can see that both thepigeon.txtandsparrow.txtfiles 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
mvcommand 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
*.txtto find all of them:echo *.txt -
mv *.txt ..will take all of the.txtfiles 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
malldirectory: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
workspacedirectory is getting a little crowded. Let's remove some of these files. - We do this with the
rmcommand, 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
rmcommand, be sure you're removing the correct files! - Let's try removing the
bird.txtfile:rm bird.txt - If I list the directory contents again, you can see the
bird.txtfile 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
rmfile can also remove directories.- Let's try deleting this
more_officesdirectory we copied. - Just like with the
cpcommand, thermcommand won't work on directories ordinarily:rm more_offices/ - But just like the
cpcommand, thermcommand has a-roption 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_officesdirectory 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
mkdircommand to make directories.- I can make a
parkdirectory 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
-poption 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
playgrounddirectory inside myparkdirectory, and atoysdirectory inside that. - I can run
mkdir -p park/playground/toys:mkdir -p park/playground/toys ls- The
parkdirectory already exists, so that's unchanged. - But there was no
playgrounddirectory inside theparkdirectory, so that's been created for us. -
cd park/,ls - And inside the
playgrounddirectory, atoysdirectory has been created. -
cd playground/,ls - We're now free to fill these
playgroundandtoysdirectories 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
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