The command line – the world of hidden codes behind the nice face of your Mac – sometimes provides a faster way to do everyday tasks, plus it’s just a cool way to establish your technical cred. You have learned how to navigate files and folders, as well as delete files and folders with the command line and get help when you need it from man pages. Here I will show you how to copy and move files, common operations that are often useful. I also show you how to create directories (it’s Unix-speak for folders) so you can move files to new locations.
Why bother with the command line?
Copying and moving files in the Finder is certainly easy, but there are several reasons why you might want to do this from the command line instead:
- You can copy or move files from one place to another without opening Finder windows.
- You can copy or move files hidden in the Finder. These files, which may contain settings for specific apps or parts of your Mac, contain a dot (.) In front of their names, and the Finder does not display them.
- You can copy or move several files using wildcards.
- You can rename a file quickly.
- If you have lost access to the Finder because your Mac is blinking, you may be able to use the command line to troubleshoot the issue.
The difference between copying and moving files
If you are in the Finder and you drag a file from, for example, the desktop to the Documents folder or any other folder on the same disk or volume, move the file. The file is no longer on the desktop and can only be found in the Documents folder. However, if you drag a file from the desktop to an external hard drive, you will see that the file remains in its original location. this file has been copied. (You may know that you can copy a file in the Finder, even on the same hard disk, by holding down the Option key when you drag it.)
The same is the case from the command line. There are two commands for moving and copying:
cp. The first does the same thing as dragging a file to a new location on the same hard drive; the other does what an Option drag does, or what happens when you drag a file to another disk or volume.
How to copy files
Copy files with
cp the command is simple. First start Terminal (in the folder / Programs / Tools). Then use the following syntax to create your command:
cp source destination
For example, to copy a file named MyFile.rtf from the desktop folder to the Documents folder, type the following command in Terminal, and then press Enter:
cp ~/Desktop/MyFile.rtf ~/Documents
You now have a file called MyFile.rtf on your desktop, and a copy of the file in the Documents folder.
You will remember from the “Master command line: Navigate in files and folders” that the tilde (~) symbol is a shortcut to your home folder, which contains the Documents folder. This command takes the file in the exact path you specify as the source argument, and moves it to the directory (folder), which is the destination. Note that if there is no file there, or if you enter the name incorrectly, Terminal will give you an error message “No such file or directory”.
You can also copy directories, including all the files they contain. This uses a special “flag” or “alternative” with
cp command: den
-R or recursive flag. When using command options, this extra letter – always in front of a hyphen (-) – tells you to do something a little different. The recursive option tells
cp command to copy each item in the folder: each subfolder, each file and folder in each subfolder, and then one, all the way down, to the new location. So you can copy a directory from your desktop to the Documents folder like this:
cp -R ~/Desktop/MyFolder /Documents
How to move files
You’re probably guessed that
mv the command works in the same way. But there are two ways you can use it
mv command. The first moves a file to another disk or volume; remember, just like in the Finder, copying a file to another volume will not delete the original, while moving will. So you can issue this command to move a file from your desktop to a folder on a backup disk:
mv ~/Desktop/MyFile.rtf /Volumes/Backup/MyFolder
You can also move directories with
mv command. The syntax is the same and you do not need it
-R flags that you do with cp command.:
mv ~/Desktop/MyFolder /Volumes/Backup
How to copy or move multiple files
One of the good things about the command line is the way you can use wildcards to simplify commands. For example, if you want to copy all the .rtf (Rich Text Files) files from the desktop to the Documents folder, you can use the star
cp ~/Desktop/*.rtf ~/Documents
mv You can use the same wildcard character with
command to move multiple files.
How to rename files
mv the command also allows you to quickly rename files. What you do is move a file to the same location, but change the name. If you enter a name for the destination,
mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf
the command renames the file when it moves the file. You can change a file name like this:
This is a valuable tool for troubleshooting; You can use this to back up a file, such as a preferences file, in case you need it again. But you can also use this renaming method just because you want to rename a file.
cp You can also copy a file with
cp ~/Desktop/MyFile.rtf ~/Documents/MyFile1.rtf
and change the name. In this case, you need to specify not only a destination directory, but also a name of the file:
How to create directories (aka folders)
mkdirHere is one last command that can be useful: , den create catalog
cd command. This is very useful when you need to create a bunch of folders at once, say for a new project you are starting. Use first(change directories
) command to move into the directory where you want to create a new directory. Once there, run this command:
mkdir MyDirectory1 MyDirectory2 MyDirectory3
You can use any directory name (such as “Hot Project” or “TPS Reports”), and you can create multiple directories with a single command:
mvWith these three simple commands –
Note: When you buy something after clicking on links in our articles, we can earn a small commission. Read our affiliate link policy for more information.