-
Copy a file. This creates the second file from the first:
$ cp harry.txt harry_stiles.txt
-
Rename a file. This also clobbers (or overwrites) any existing file with the same name:
$ cp harry_stiles.txt harry.txt
- clobber
- overwrite
-
Create, then remove a file.
$ touch file.txt $ rm file.txt
By default, the
rm
command does not warn you, so be careful! Also, any files you remove this way do not appear in the Mac's trash box. They're gone... forever! -
The
-i
(interactive) option allows you to typey
orn
when prompted to remove each file:$ rm -i *.txt
But suppose you accidentally tried to remove many, many files. Whenever you're in such a special mode outside of the shell itself, you can always type
CTRL-c
to cancel out of it. Think of it as an all-purpose escape hatch.- interactive
- mode
-
Create a subdirectory:
$ mkdir onedir
-
Remove a subdirectory, only possible with this command if it's empty:
$ rmdir onedir
-
Create a nested subdirectory. You need the
-p
(path) option when any of the upper directories don't exist yet.$ mkdir -p onedir/members
Otherwise, you'd have to run two separate commands:
$ mkdir onedir $ mkdir onedir/members
-
Copy files into a directory. Whenever the last argument is a directory, it becomes the target.
$ cp *.txt onedir
-
Move files from one directory to another:
$ mv onedir/*.txt onedir/members
-
Remove the entire directory structure, recursively prompting for each file:
$ rm -r onedir
WARNING: You can add the
-f
option to force-remove files and directory trees, but be very, very careful!- recursive
- force
-
Create a directory structure, create another directory, and copy it in there:
$ mkdir -p onedir/members $ mkdir -p ~/target $ cp -R onedir ~/target
-
Move and rename a directory:
$ mv ~/target/onedir ~/one_direction
-
Efficiently mirror a set of files. It's often hard to use
cp
to reflect a directory structure after it's already been copied. This works much better:$ mkdir -p onedir/members $ mkdir -p ~/target/ $ rsync -av --delete onedir ~/target
When you run that last command more than once, it only copies source files to the target that have been updated. The
--delete
option means any files removed from theonedir
directory also get deleted from thetarget
.- mirror
-
Now learn more about command history: Type an up-arrow to see previous commands, and down-arrow for newer ones. Type a carriage return to re-run any one of those commands.
- command history
-
Edit a previous command: Type
CTRL-a
to go to the start of the line, andCTRL-e
to go to the end. Type side-arrows to move by character, and add the OPTION key to move by word. TypeCTRL-k
to kill the text after the cursor andCTRL-y
to paste it back in. Position the cursor between two characters that you may have typed in too quickly, then typeCTRL-t
to transpose them. (These CTRL combinations are borrowed from the Emacs text editor.) -
View all history for this shell session:
$ history
-
Put an exclamation mark, otherwise known as a bang character, in front of any number from the history list to run it again:
$ !525
-
Run the most recent command starting with
rsy
(such asrsync
):$ !rsy
-
Two bangs runs the previous command in its entirety:
$ !!
Note that each command in the history may include several statements separated by semicolons, so you can keep modifying the previous command with others:
$ !! ; CHAIN_ANOTHER_COMMAND
-
Invoke the last argument from the previous command:
$ touch harry.txt $ open !$