Lesson 5: More on Copying and Moving Files
As promised in the previous lesson, we'll now learn about command line options, and how to use them for copying, moving, and deleting files and folders.
Copying folders
While copying files is rather straightforward, copying folders is (rather needlessly, I should add) trickier. If you try copying folders the way you copy files, the system refuses to copy anything and prints a very unhelpful and cryptic looking error:
> cd ~/Hogwarts
> cp Houses Groups
cp: -r not specified; omitting directory 'Houses'
As cryptic as the error is, it does provide a slight hint in the -r not specified
bit. Let's see how to put that into practice.
Commands can take options
You can extend the functionality of almost every command using command options. For example, an otherwise basic command such as ls
can reveal hidden files in a folder, display information like the file owner, and whether the file can be read or modified by users other than the owner (more on that later).
As for cp
, you need to use the "recursive" command option to copy non-empty folders, like so:
> cp --recursive Houses "Student Groups"
> ls Houses "Student Groups"
Houses:
Gryffindor Hufflepuff Ravenclaw Slytherin
'Student Groups':
Gryffindor Hufflepuff Ravenclaw Slytherin
Adding the --recursive
option tells cp
to copy over everything, even if the directory is not empty -- no cryptic errors involved.
Despite what some CLI fanboys would tell you, such a restriction has no reason to exist on modern systems, other than, "that's how some guy decided to code it some 40 years ago on Unix, the OS that the Linux / macOS command line is based on."
Abbreviations for command options
So if we're supposed to write --recursive
after the command, why does the error say "-r not specified"?
That's because -r
is shorthand for the --recursive
command, and can be used interchangeably:
> cp -r Houses Apartments
> ls Houses Apartments
Apartments:
Gryffindor Hufflepuff Ravenclaw Slytherin
Houses:
Gryffindor Hufflepuff Ravenclaw Slytherin
Yes, I just used "Apartments" to refer to Hogwarts' houses -- I have no creativity.
Notice how the shorthand uses a single hyphen (-
), but the full form uses a double hyphen (--
).
Heads up:
You'll be using abbreviations most of the time, so don't worry about remembering both long and short forms of command options.
Moving folders
Unlike cp
, the mv
command does not need any special options to move folders around, even if they contain files and subfolders:
> ls
Apartments Houses 'Student Groups' Wizards
> mv Apartments Maisons
> ls
Maisons Houses 'Student Groups' Wizards
So much for consistency between different commands.
Deleting files and folders
To delete files and folders, we use the rm
(think "remove") command. Like cp
, removing plain files does not require any special options. To delete folders, you need the -r
option:
> ls Houses/Gryffindor
Harry 'Harry Potter' Hermione Ron
> rm Houses/Gryffindor/"Harry Potter"
> ls Houses/Gryffindor
Harry Hermione Ron
> rm 'Student Groups'
rm: cannot remove 'Student Groups/': Is a directory
Unlike cp
, the rm
command does not even give a hint on how you can delete a directory. Moreover, the error text suggests that it isn't even possible to delete a directory with rm
.
Since we know better, let's try to delete the Maisons folder with the -r
option:
> rm -r 'Student Groups'
> ls
Houses Maisons Wizards
⚠ Alert
As pointed out in the course introduction, there is no safety net on the CLI -- if you delete the wrong file or folder, it's gone forever -- no Recycle Bin or Trash folder to recover files from.
Exercises
-
Go back to the new files and folders you created in the previous lesson and play around with deleting, copying and moving them around using the command options you just learnt.
-
Try out a new command option,
-v
withcp
,rm
, andmv
, for example,cp -r -v Wizards Magicians
and observe the output. What do you think the-v
option does? -
Try combining two command options -- observe and compare the outputs of
-r -v
,-v -r
,-rv
, and-vr
. Do you notice any difference?
Doubts and explanations
The -v
option
-v
stands for "verbose". As you've noticed, most commands don't bother to let you know when they're successful. This option comes in handy if you want to know exactly which files have been copied, moved, or deleted.
Like most other options, you can also use the expanded form, --verbose
instead of -v
.
Combining multiple command options
Most options can be combined in any order -- --verbose -- recursive
, --recursive --verbose
, -v -r
, -r -v
, -vr
, and -rv
give identical results.
Why does cp
and rm
need the recursive option to work with folders, but mv
doesn't? Why the inconsistency?
That's because all of these commands were originally written for Unix -- the predecessor of Linux and macOS, at least as far as the command line goes.
These commands were written by different people at different times, there were no strict user interface guidelines to be followed, and almost nobody writing the code knew that the programs they were writing in the 1970's would still be in use in the 2020's.
So why has nobody bothered to fix this?
While all the commands have had updates and new features built into them over the decades, it's hard to fundamentally change the way commands work -- any major changes in the behaviour or option names can break years-, or even decades-old scripts that are still in use.
Nobody wants to deal with that headache, so nothing has changed.