An Introduction to the Command-Line (on Unix-like systems)

by Oliver; 2014

13. cp, mv, and rm

Finishing off our top 10 list we have cp, mv, and rm. The command to make a copy of a file is cp:
$ cp file1 file2 
$ cp -R dir1 dir2
The first line would make an identical copy of file1 named file2, while the second would do the same thing for directories. Notice that for directories we use the -R flag (for recursive). The directory and everything inside it are copied.

Question: what would the following do?
$ cp -R dir1 ../../
Answer: it would make a copy of dir1 up two levels from our current working directory.

To rename a file or directory we use mv:
$ mv file1 file2 
In a sense, this command also moves files, because we can rename a file into a different path. For example:
$ mv file1 dir1/dir2/file2
would move file1 into dir1/dir2/ and change its name to file2, while:
$ mv file1 dir1/dir2/
would simply move file1 into dir1/dir2/ or, if you like, rename ./file1 as ./dir1/dir2/file1.

Finally, rm removes a file or directory:
$ rm file     # removes a file
$ rm -r dir   # removes a file or directory
$ rm -rf dir  # force removal of a file or directory
	      # (i.e., ignore warnings)

<PREV   NEXT>