There are two cases in which you're going to need
mv: One is that a file is in the wrong directory. The other is that the file itself has the wrong name.
Suppose you've decided that
hello.txt doesn't really belong in your notes directory.
In this example, we say "move hello.txt up one directory level".
Instead of using
.., we could have specified
/home/pi. It's just faster to type a couple of dots. It would have been even faster to type
Now suppose you've decided that
hello.txt itself is called the wrong thing, and should be
Remember that a directory is just a special kind of file.
mv will work to move entire directories just the same as individual files.
Always make sure you're not going to overwrite something important when you move a file. There are a couple of good flags to know in order to avoid this:
For interactive - mv will ask what you want to do before overwriting any files.
For "no clobber". Will skip overwriting any files.
Make a backup of any files that get replaced.
In order to duplicate a file, use
Trying to copy a directory, however, behaves oddly:
cp skips over directories. Why? Well, there's some good discussion about this over on Stack Exchange, but the short answer is probably that copying everything in a directory could be a very expensive operation. (Imagine if a directory contained many thousands of files.)
In order to copy a directory, we need to give
-r flag to enable recursion. This is part of a broader pattern: In order for many commands to work on entire directories full of files, recursive operations must be specified with a flag, usually
mv, always make sure you're not going to clobber anything important by copying over it. The
-b flags work here too.