偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

終端基礎(chǔ):在 Linux 中復(fù)制文件和目錄

系統(tǒng) Linux
在終端基礎(chǔ)知識(shí)系列的這一部分中,學(xué)習(xí)如何在 Linux 中使用命令行復(fù)制文件和目錄。

復(fù)制文件是你經(jīng)常執(zhí)行的最基本但最重要的任務(wù)之一。

Linux 有一個(gè)專門的 cp 命令用于復(fù)制文件和目錄(文件夾)。

在終端基礎(chǔ)知識(shí)系列的這一部分中,你將學(xué)習(xí)在終端中復(fù)制文件和文件夾。

?? 回想一下,以下是你迄今為止在本終端基礎(chǔ)知識(shí)系列中所學(xué)到的內(nèi)容:

讓我們繼續(xù)該系列的第七章。

在 Linux 命令行中復(fù)制文件

讓我向你展示一些復(fù)制文件的示例。

將文件復(fù)制到另一個(gè)目錄

要將一個(gè)文件復(fù)制到另一目錄,你所要做的就是遵循給定的命令語法:

cp 源文件 目標(biāo)目錄

例如,在這里,我將名為 Hello.txt 的文件復(fù)制到名為 Tux 的目錄中:

copy file to another directory in linux command linecopy file to another directory in linux command line

正如你所看到的,文件已成功復(fù)制到 Tux 目錄中。

復(fù)制文件但重命名

你可以選擇在復(fù)制文件時(shí)重命名該文件。只需為“目標(biāo)文件”指定一個(gè)不同的名稱即可。

cp 源文件 改名的文件

作為參考,在這里,我將名為 Hello.txt 的文件復(fù)制到同一目錄,并將其重命名為 Renamed_Hello.txt

rename a file while copying in a same directory in linux terminalrename a file while copying in a same directory in linux terminal

為什么要這么做? 比如說,你必須編輯配置文件。一個(gè)好的做法是在編輯配置文件之前在同一位置對(duì)其進(jìn)行備份。這樣,如果事情沒有按計(jì)劃進(jìn)行,你可以恢復(fù)到舊配置。

將多個(gè)文件復(fù)制到另一個(gè)位置

要將多個(gè)文件復(fù)制到另一個(gè)目錄,請(qǐng)按以下方式執(zhí)行命令:

cp File1 File2 File3 FileN Target_directory

在這里,我將多個(gè)文件復(fù)制到新位置。

copy multiple files using the cp command in linuxcopy multiple files using the cp command in linux

?? 當(dāng)你復(fù)制多個(gè)文件時(shí),僅使用 cp 命令無法重命名它們。

復(fù)制時(shí)處理重復(fù)文件

默認(rèn)情況下,如果目標(biāo)目錄中存在同名文件,cp 命令將覆蓋該文件。

為了避免覆蓋,你可以在 cp 命令中使用 -n 選項(xiàng),它不會(huì)覆蓋現(xiàn)有文件:

cp -n 源文件 目標(biāo)目錄

例如,在這里,我嘗試復(fù)制目標(biāo)目錄中已有的兩個(gè)文件,并使用 -v 選項(xiàng)來展示該命令正在執(zhí)行的操作:

cp -n -v itsFOSS.txt LHB.txt LU.txt ~/Tux

how not to override files while copying in linux using the cp commandhow not to override files while copying in linux using the cp command

交互式復(fù)制文件

但是,當(dāng)你想要覆蓋某些文件,而某些文件應(yīng)該保持不變時(shí)該怎么辦?

好吧,你可以使用 -i 選項(xiàng)在交互模式下使用 cp 命令,它每次都會(huì)詢問你是否應(yīng)該覆蓋該文件:

cp -i 源文件 目標(biāo)目錄

how to use cp command in interactive modehow to use cp command in interactive mode

??? 自己練習(xí)上述所有示例。你已經(jīng)了解如何創(chuàng)建文件和文件夾,因此請(qǐng)重新創(chuàng)建所有內(nèi)容。

在 Linux 命令行中復(fù)制目錄

mkdir 命令用于創(chuàng)建新目錄,rmdir 命令用于刪除(空)目錄。但沒有用于復(fù)制目錄的 cpdir 命令。

你必須使用相同的 cp 命令,但使用遞歸選項(xiàng) -r 將目錄及其所有內(nèi)容復(fù)制到另一個(gè)位置:

cp -r 源目錄 目標(biāo)目錄

例如,在這里,我將名為 IF 的目錄復(fù)制到 LHB

how to copy a directory in linux command linehow to copy a directory in linux command line

但它復(fù)制了整個(gè)目錄。??

那么,當(dāng)你只想復(fù)制目錄內(nèi)容而不是目錄本身時(shí)該怎么辦?

你可以執(zhí)行以下操作:

僅復(fù)制目錄的內(nèi)容(不是目錄)

要僅復(fù)制目錄的內(nèi)容,而不復(fù)制目錄本身,請(qǐng)?jiān)谠茨夸浢Q的末尾附加 /.

cp -r 源目錄/. 目標(biāo)目錄

在這里,我想復(fù)制名為 IF 的目錄的內(nèi)容,其中包含以下三個(gè)文件:

check the file contents of directory using the tree commandcheck the file contents of directory using the tree command

我將執(zhí)行以下命令將 IF 目錄的文件內(nèi)容復(fù)制到 LHB

cp -r IF/. LHB

copy the file contents of directory not a directory itself in linux command linecopy the file contents of directory not a directory itself in linux command line

你還可以在此處使用 源目錄/* 。

復(fù)制多個(gè)目錄

要復(fù)制多個(gè)目錄,你必須按以下方式執(zhí)行命令:

cp -r 目錄1 目錄2 目錄3 目錄N 目標(biāo)目錄

例如,在這里,我將兩個(gè)名為 IF 和 LU 的目錄復(fù)制到 LHB

cp -r IF LU ~/LHB

copy multiple directories using the cp command in linux command linecopy multiple directories using the cp command in linux command line

當(dāng)你想要從多個(gè)目錄復(fù)制文件但不復(fù)制目錄本身時(shí),你可以執(zhí)行相同的操作:

cp -r 目錄1/. 目錄2/. 目錄3/. 目錄N/. 目標(biāo)目錄

copy files from multiple directories but not directories their self using the cp commandcopy files from multiple directories but not directories their self using the cp command

??? 你還可以像重命名文件一樣重命名目錄。

測(cè)試你的知識(shí)

現(xiàn)在,讓我們看看你對(duì)到目前為止所學(xué)到的知識(shí)還記得多少。

  • 創(chuàng)建一個(gè)名為 copy_practice 的目錄。
  • 將文件 /etc/services 復(fù)制到這個(gè)新創(chuàng)建的文件夾。
  • 在此目錄下創(chuàng)建一個(gè)名為 secrets 的文件夾,并將文件 /etc/passwd 和 /etc/services 復(fù)制到其中。
  • 將 copy_practice 中的 services 文件復(fù)制到 secrets 文件夾中,但不要覆蓋它。
  • 將 secrets 文件夾復(fù)制到你的主目錄。
  • 刪除 secrets 和 copy_practice 目錄。

這會(huì)給你一些練習(xí)。

到目前為止進(jìn)展順利。你已經(jīng)學(xué)到了很多東西。在下一章中,你將了解如何使用 mv 命令移動(dòng)文件和文件夾。

責(zé)任編輯:龐桂玉 來源: Linux中國(guó)
相關(guān)推薦

2023-11-25 17:13:49

Linux重命名文件

2023-03-04 13:32:35

Linux創(chuàng)建目錄

2023-10-22 08:39:13

Linux目錄切換

2023-03-21 12:52:05

Linux創(chuàng)建文件

2021-08-13 15:00:40

Linux終端移動(dòng)文件

2023-05-13 17:43:17

Linux文件文件夾

2021-08-16 13:34:07

Linux終端刪除文件

2018-06-01 10:37:26

Linux歸檔文件壓縮文件

2021-07-04 19:48:07

LinuxFreeDOS復(fù)制文件

2016-12-20 09:47:17

Linux命令復(fù)制文件到多個(gè)目錄

2018-06-25 13:10:16

Linux復(fù)制重命名

2022-06-28 09:10:28

Linuxtree命令

2020-02-10 09:23:09

LinuxReddit命令

2022-04-11 15:30:39

LinuxGlowMarkdown

2017-03-14 09:45:24

2020-10-15 06:00:22

LinuxLinux終端幻燈片

2019-06-19 13:50:21

NeofetchLinux

2022-09-02 10:45:58

Micro編輯工具

2017-01-11 10:27:36

Linux終端自定義Bash

2022-09-30 12:55:14

Linux筆記
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)