So if you enter cp -i file1 Documents and there’s already a file1 in Documents, you’ll see: If the current directory contains files file1 and file2, entering cp file1 file2 overwrites the old file2 file with a copy of file1!įortunately, you can turn on an optional warning that appears if you’re about to overwrite an existing file, using the -i (“interactive”) flag. When you enter cp file1 Documents, the command happily overwrites the file that’s already in Documents without any warning! The same goes for duplicating files in the same directory. Let’s say there’s already a file in Documents that’s called file1. It won’t overwrite a directory with a file.īut, in fact, cp is not quite as smart as you might like.
The command assumes that if the second argument is the name of an existing directory, you want to copy the file to that directory otherwise, it copies the file in the current directory, giving it the name of the second argument. Likewise, if you want to copy the file to another location and give the copy a new name, specify the new name in addition to the destination:Ĭp file1 Documents/file2 Avoid Overwriting Files When CopyingĪnything strike you as suspicious about that? We know there’s a file called file1 and a directory called Documents in the current directory, so will this command copy file1 into Documents or make a copy in the current directory and name the copy Documents (potentially overwriting the existing directory)? The answer is: cp is smart. If you want to duplicate a file and keep the duplicate in the same directory, enter the name you want the duplicate to have: For instance:Ĭp /Users/jk/Documents/file1 /Users/Shared The location of the file you’re copying, and the location you’re copying it to, can be expressed as relative or absolute paths. For example, if you’re in your home directory ( ~) and want to make a copy of the file file1 and put it in the Documents directory, you can do it like this: It takes two arguments: the first is the file you want to copy, and the second is the destination for the copy. To duplicate a file (in the same location or another location), use the cp (“copy”) command. Spaces, apostrophes, and quotation marks in directory names must be escaped (see Spaces in Paths). If you want to create a hierarchy of directories-for example, you want to create a directory called oranges inside ~/Documents/fruit/citrus/ and the fruit and citrus directories don’t already exist-add the -p flag (for example: mkdir -p ~/Documents/fruit/citrus/oranges). You can create a new directory in some other location than your current one (for example, you could enter mkdir ~/Documents/apples). That’s it! A few other potentially useful things to be aware of: To make a directory called apples, you’d enter the following: To create a directory (which, of course, appears in the Finder as a folder), use the mkdir (“make directory”) command. When applied to an existing file, touch doesn’t affect its contents at all, only its modification date. You use exactly the same syntax, supplying the name of the existing file:
Note: Remember, if you want to create a file with a space in the name, put it in quotation marks ( touch "my file") or escape the space character ( touch my\ file).Īs for the other use of touch-marking a file as modified-you might do this if, for example, the program that saved it failed to update its modification date for some reason and you want to make sure your backup software notices the new version. So try creating a few files right now with touch. Since you’re creating the files, you can rename, move, copy, and delete them without worrying about causing damage. Using touch is the quickest way to create such a file.īut for the purposes of this book, the reason to know about touch is so you can create files for your own experiments. What’s in the file doesn’t matter-just that it’s there. Why would you do this? There are occasionally situations in which a program behaves differently based solely on the existence of a file with a certain name in a certain place. It’s just a marker, though you could use a text editor, for example, to add to it.
It doesn’t have an extension, or a type, or any contents. This file that you’ve just created is completely empty. Now use ls -l to list the contents of your current directory.
When supplied with the name of an existing file or folder as an argument, touch updates its modification date to the current date and time, marking it as modified. When supplied with the name a nonexistent file as an argument, touch creates an empty file. I want to begin by mentioning a curious command called touch that serves two interesting functions: This chapter covers the essentials of interacting with files and directories. Much of what you’ll need to do on the command line involves working with files in some way-creating, deleting, copying, renaming, and moving them.