What is the method of folder moving and copying under Linux?

10-19-2023

1. Copying a folder Copying a folder is the process of copying all the contents of a folder to other locations. The following are some basic command options and parameters:

Cp [Options] Source Directory Target Directory

Among them, the cp command is used to copy files or folders. You need to provide the source directory and destination directory address, and specify the location of the source folder and destination folder.

For example, if you want to copy everything in the/home/lihuanyu/data/mini _ imagenet/myimagenet directory to the/home/lihuanyu/code/06paper1/01model _ retraining/data/directory, you can use the following command:

cp -r /home/lihuanyu/Data/Mini_imagenet/MyImagenet /home/lihuanyu/code/06paper1/01model_pretraining/data/

In this example, the -r parameter means recursively copying the entire directory. You can also change this parameter to control the behavior of replication.

2. Moving Folders Moving Folders is the process of moving folders from one location to another. The following are some basic command options and parameters:

Mv [Options] Source Directory Target Directory

Among them, the mv command is used to move files or folders. You need to provide the source directory and destination directory address, and specify the location of the source folder and destination folder.

For example, if you want to move the /home/user/project directory to the /backup/project directory, you can use the following command:

mv /home/lihuanyu/Data/tiny-imagenet-200 /home/lihuanyu/code/06paper1/01model_pretraining/data/

In this example, the mv command will move the/home/lihuanyu/data/tiny-imagenet-200 directory to the/home/lihuanyu/code/06paper1/01model _ training/data/directory.

3. Common application cases Now let's take a look at how to use folder moving and copying operations in practical applications.

A. Copy backup data Suppose you have an important configuration file stored in the /etc directory. In order to back up the file, you can copy the whole directory to the backup directory. The following is an example of copying the /etc directory to the backup directory /backup/etc:

cp -r /etc /backup

B. Move the folder to another location If you want to move a completed project to another location to make room on the local disk, you can use the mv command to move the folder. The following is an example of moving the /home/user/project directory to the /backup directory:

mv /home/user/project /backup

C. archiving data when dealing with large folders, you may need to archive data to save disk space. The tar command can package multiple files into a single archive file. The following is an example of archiving the /home/user/project directory into the /backup/project.tar.gz file:

tar -czvf /backup/project.tar.gz /home/user/project

In this example, -c means to create a new archive file, -z means to compress the file using gzip algorithm, -v means to display the progress information and output it to the terminal, and -f means to specify the output archive file name.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us