At this point in the course, you can:
see where you are in your system (
list the contents of a directory (
change directories (
make directories (
make files (
use keyboard shortcuts to avoid command line typing hassles
Now, you might want to move around or copy content that you've created. There are a series of commands that will allow you to do this.
Let's say you've accidentally created a file in the wrong place, like putting your biology research spreadsheet in your art history folder. 😖
If you use a visual interface like Finder (or another visual interface), you would have to click and drag this file into its correct location. In Terminal, you don't have a visual interface, so you'll have to know the
mv command to do this!
mv , of course stands for move. The
mv command requires several pieces of information.
the original file we want to move.
the new destination for the original file.
My first piece of information is therefore
research-findings.csv , and my second piece of information is therefore the file path to the Biology folder.
Absolute and relative file paths
I could type this in two different ways. Let's explore the difference between absolute and relative file paths.
Option 1 (absolute path):
mv research-findings.csv ~/Desktop/Second\ Semester/Biology
Option 2 (relative path):
mv research-findings.csv ../Biology
Option 1 contains the absolute path towards the Biology folder. Each folder is listed in the path from top to bottom.
Option 2 is a relative path . It is the path relative to where we are now.
.. indicates "move up one level" (to "Second semester"), and from there, move into the Biology folder.
Rename content using mv
You can also rename files and folders using the mv command. To rename
biology-final-report.csv , you could run:
mv research-findings.csv biology-final-report.csv
Technically, you're "moving" the one file to another and renaming it in the process. 👍
Copying content works similarly to moving content. Let's start with copying a file because it's simpler.
The same arguments are true for
cp (copy) as for
The first argument is the original file we want to move.
The second argument is the new destination for the copy of the original file.
Let's run through an exercise to put together a few different things we've worked on.
Scenario: you want to copy the final coursework from your second semester folder to a new folder called "Final papers". You don't want to move the files; just copy them to a different folder so you can have copies of your final papers in one centralized place.
From within "Second semester" folder, make a directory to contain the end of semester work.
mkdir Final\ papers
Now, you can either change directories (
cd) into the Art history folder and copy the final paper from there, or just copy it without changing directories.
cd Art\ history
cp term-paper.text ../Final\ papers
cp Art\ history/term-paper.txt Final\ papers
Now change directories (
cd) into the Final papers folder and list its contents (
ls) to confirm the file is now copied here:
cd Final\ papers
Optional study: copying folders
Copying folders is more tricky. You will need to use something called an option. Options exist for many terminal commands. They are a way for you to tell the computer that a command should run in a specific, non-default way.
To copy folders, you'll use the recursive option. Recursive is similar to iteration, in that the command will run in small increments in order to achieve its final goal. This is not worth worrying about right now, though you can read more about recursiveness here.
For this example, we'll hop back to our code project. I want to duplicate my code folder in order to use the code I've written as a base for another project. The original directory is called code-project, so I'll name my copy
To copy this folder from one place to another, you'll run:
cp -R ~/Desktop/code-project ~/Desktop/code-project-2
This copied my code-project folder (including its contents) into a new folder called
-R is an option that specifies you want the command to run with recursion.
Now you can move and copy files and folders. Nice! In the next chapter, you'll see how to search for content among all the folders and files you've created in this course so far.