Copy & Move files with Terminal | cp & mv commands – Learning How to Use Unix Terminal (Shell) [Tutorial]

In the previous article I showed you how to navigate the filesystem with absolute and relative paths and change the directory with the cd  command.

In this article I’m going to show you how to copy files with the cp  command and how to move files with the  mv  command.

The cp  command in Unix / Linux

Definition: It’s used  to copy files and directories.

Syntax: $ cp [options] source dest

> Copy a file with a certain name and extension to the same dir but with a different name and extension.

If file2.html exists than file.css will replace its content. If it doesn’t exist, the file will be created with the content of the first.
> Copy a file from a directory to another directory:

> Copy all files from a directory to another directory:

Copies all files drom dir to dir2.

> Copy 2 or more files from one directory to another directory:

> If the files are in the working directory (the one that you are in currently), than you can use the command as follows:

> An interesting application in bulk actions is copying all files with a certain extension to a certain directory:

> Copy all files and directories in Source recursively to subdirectory destinationDir:

cp  command main Options:

The mv  command in Unix / Linux

Definition: It’s used to move files and directories.

Syntax: $ mv [options] source dest

You use the move command similar to how you use the cp  (copy) command.

mv  command main Options:

Also another application for the mv command is renaming files or directories. You do this by specifying a different name for the destination file or directory:

$ mv /Users/username/file1 /Users/username/dest/file1renamed

If you want to learn more about terminal, visit the previous tutorial I made, in which you will find how to change the working directory in Terminal with absolute and relative paths.

Hope you enjoyed this tutorial, and found it useful! And I’ll be doing more on this subject in the near future therefore don’t forget to subscribe to my Newsletter to get the latest posts! Also follow me on Social Media!




Comments 0

Leave a Reply