Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Copying and Moving Files - Command Line Tutorial

Introduction

Welcome to the tutorial on copying and moving files using the command line. This guide will help you understand the basic commands needed to manage files effectively. By the end of this tutorial, you will be able to copy and move files and directories with ease.

Copying Files

To copy files in the command line, you can use the cp command. The syntax for the cp command is as follows:

cp [source] [destination]

Here, [source] is the file you want to copy, and [destination] is the location where you want to copy the file.

Example

Copying a file named file1.txt to a directory named backup:

cp file1.txt backup/

This command copies file1.txt into the backup directory.

Copying Directories

To copy a directory and its contents, you need to use the -r (recursive) option with the cp command:

cp -r [source_directory] [destination_directory]

Example

Copying a directory named docs to a directory named backup:

cp -r docs/ backup/

This command copies the docs directory and all its contents into the backup directory.

Moving Files

To move files, you use the mv command. The syntax for the mv command is similar to cp:

mv [source] [destination]

Here, [source] is the file you want to move, and [destination] is the location where you want to move the file.

Example

Moving a file named file1.txt to a directory named backup:

mv file1.txt backup/

This command moves file1.txt into the backup directory.

Moving Directories

The mv command can also be used to move directories. The syntax remains the same:

mv [source_directory] [destination_directory]

Example

Moving a directory named docs to a directory named backup:

mv docs/ backup/

This command moves the docs directory and all its contents into the backup directory.

Renaming Files and Directories

The mv command can also be used to rename files and directories. The syntax is:

mv [old_name] [new_name]

Example

Renaming a file from oldname.txt to newname.txt:

mv oldname.txt newname.txt

This command renames oldname.txt to newname.txt.

Summary

In this tutorial, we covered the basic commands for copying and moving files and directories using the command line. The cp command is used for copying files and directories, while the mv command is used for moving and renaming them. Practice these commands to enhance your command line proficiency.