Lesson 4: Copying, Moving and Renaming
Now that you know how to create and delete (empty) folders, and the importance of case sensitivity, let's learn how to create files and do some basic file manipulation like copying, moving and renaming.
This lesson only covers regular files. Since working with folders is trickier, we'll leave that for the next lesson.
Creating new files
For the sake of practice, we'll create and work with some empty files using the touch
command:
> mkdir ~/Hogwarts/Wizards
> cd ~/Hogwarts/Wizards
> touch Harry Ron Hermione Draco
> ls
Draco Harry Hermione Ron
> file Draco Harry Hermione Ron
Draco: empty
Harry: empty
Hermione: empty
Ron: empty
Explanation:
After creating and entering the Wizards
directory, we ran the touch
command with multiple arguments to create some empty files, and the usual ls
to verify.
The file
command is new, too -- it analyses the contents of a file or folder, and prints out what kind of file it is (image, video, PDF, folder etc.). In this case, file
assures us that all these files are, indeed, empty.
Copying files
The command for copying files is the appropriately named cp
-- run cp
, followed by the file that you want to copy, followed by the name you want for the copied file. Let's copy Harry
to Harry Potter
:
> cp Harry "Harry Potter"
> ls
Draco Harry 'Harry Potter' Hermione Ron
You'll notice that I put "Harry Potter" in quotes -- that's because the new file has a space in its name. If you don't use quotes, the system takes "Harry" and "Potter" as two different arguments.
Single quotes (
'...'
) instead of double quotes ("..."
) work, too.
To copy a file to another folder, run cp
followed by the file you want to copy, followed by the path of the folder you want to copy the file to:
> cp Harry ../Houses/Gryffindor/
> ls ../Houses/Gryffindor
Harry
As usual, you can use multiple arguments to copy several files to a folder:
> cp Draco "Harry Potter" Hermione Ron ../Houses/Gryffindor/
> ls ../Houses/Gryffindor
Draco Harry 'Harry Potter' Hermione Ron
Moving files around
The astute reader will notice that we mistakenly moved Draco to the Gryffindor house. Let's correct our mistake by moving him to where he belongs with the mv
command, and add his surname while we're at it:
> mv ../Houses/Gryffindor/Draco ../Houses/Slytherin/"Drayco Malfoy"
> ls ../Houses/Slytherin/
'Drayco Malfoy'
Note: For files with spaces in the name, both
../Houses/Slytherin/"Drayco Malfoy"
(only the name of the file in quotes), and"../Houses/Slytherin/Drayco Malfoy"
(the entire file path in quotes) work.
That's it for the basics of file manipulation. In the next lesson, we'll learn how to extend commands using command options, and use that newfound knowledge to deal with folders.
Exercises
- You might've noticed that I mentioned file renaming in the title, but didn't cover the topic. In addition, I made a typo in Draco's name when moving the file (pay attention to the first name). Try to figure out how to rename files using the
mv
command and fix the typo. - Create 10 new empty files and 5 new folders (including ones with spaces in their names). Practice copying and moving these files to and from the new folders.
Doubts and explanations
So what's the difference between moving and renaming files? Why does the same command work for both?
On the command line, there is no difference. The command mv "Drayco Malfoy" "Draco Malfoy"
roughly translates to, "copy the contents of the source file Drayco Malfoy
to a new file called Draco Malfoy
, then delete the source file."