8.4. Other frequently used commands for file manipulation

cp command

Syntax: cp source target

Copy the file /Users/MyUser/Desktop/test.txt into folder /Volumes/MyExternalDrive $ cp /Users/MyUser/Desktop/test.txt /Volumes/MyExternalDrive

Copy file /Users/MyUser/Desktop/test.txt into folder /Volumes/MyExternalDrive, but save it with a different name in the new directory (i.e. test2.txt instead of test1.txt) $ cp /Users/MyUser/Desktop/test.txt /Volumes/MyExternalDrive/test2.txt

As you can observe in the previous two examples, if you specify a folder as a target in the cp command, then it will copy the file with the same name. If you specify a regular file as a target, then it will rename the file in the destination (not in the source, the source file will stay untouched). The source file need not have the same extension. For example, the following command will copy the file /Users/MyUser/Desktop/subjectFolder/534534.bvals into the folder /Volumes/MyExternalDrive, will rename it and delete the extension (now it has no extension).
$ cp /Users/MyUser/Desktop/subjectFolder/534534.bvals /Volumes/MyExternalDrive/bvals

Caution! If a file with the same name exists in the source, it will be re-written and this action cannot be undone. For example, if you run the following command:
$ cp /Users/MyUser/Desktop/test.txt /Volumes/MyExternalDrive/test2.txt

And a file with the same source path (/Volumes/MyExternalDrive/test2.txt) already existed, then that file will be replaced by test.txt. You will not be able to recover the replaced file.

mv command

Syntax: mv source target

The mv command works the same way was the cp command. The difference is that the source file is moved instead of copied.

Renaming a file Rename file test1.txt to test2.txt in the current directory.

$ ls
Applications
Library
Volumes
bin
test1.txt
$ mv test1.txt test2.txt
$ ls
Applications
Library
Volumes
bin
test2.txt

rm / rmdir commands

mkdir command

touch command

find command

Moving a file from one directory to another Move test1.txt from directory /Users/MyUser/ to directory /Volumes/MyExternalDrive.

$ ls /Volumes/MyDrive

$ mv /Users/MyUser/Desktop/test1.txt /Volumes/MyDrive
$ ls /Volumes/MyDrive
test1.txt