複制和粘貼是計算機上最常用的操作之一。使用Ctrl+ C和Ctrl+ V鍵盤快捷鍵很容易做到,但是在Linux終端上卻不是那麼簡單。您有幾種選擇可以完成工作。這是在Linux終端中複制和粘貼文本,檔案和目錄的方法。

複制和粘貼文字
如果您隻想在終端中複制一段文本,您要做的就是用滑鼠突出顯示該文本,然後按Ctrl+ Shift+ C進行複制。
要将其粘貼到光标所在的位置,請使用鍵盤快捷鍵Ctrl+ Shift+ V。
當您從Word文檔(或任何其他應用程式)複制一段文本并希望将其粘貼到終端時,粘貼快捷方式也适用。例如,您可以從浏覽器中的網頁複制指令,然後使用Ctrl+ Shift+ V快捷方式将其粘貼到終端中。
複制并粘貼單個檔案
每當您想在Linux指令行中複制檔案或檔案夾時,上述鍵盤快捷鍵将不起作用。您必須使用cp指令。cp是複制的簡寫。文法也很簡單。使用,cp後跟要複制的檔案以及要将其移動到的目的地。
cp your-file.txt ~/Documents/
當然,這假定您的檔案位于要處理的目錄中。您可以同時指定。
cp ~/Downloads/your-file.txt ~/Documents/
您還可以選擇在複制檔案時重命名檔案。在目的地中指定新名稱。
cp ~/Downloads/your-file.txt ~/Documents/new-name.txt
複制和粘貼檔案夾及其内容
為了複制檔案夾及其内容,您将需要告訴cp指令以遞歸方式複制。使用-r标志就足夠簡單了。
cp -r ~/Downloads/pictures-directory ~/Pictures/family-vacation-picsLinux Cli Copy FolderAll the rest of your syntax is exactly the same. The -r flag serves to tell cp that it’s working with a directory and should copy its contents.If you want the paste action to overwrite existing files, you can add the -f flag:cp -rf ~/Downloads/pictures-directory ~/Pictures/family-vacation-picsCopy and Paste Multiple FilesYou can also copy multiple files. The Linux command line lets you target multiple items at once with brackets {}. You can use them to list the names of each file to be copied separated by commas.cp ~/Downloads/{file1.txt,file2.jpg,file3.odt} ~/Documents/Linux Cli Copy MultipleAll three files of differing file types will be copied to the Documents directory.Copy and Paste All Files of the Same TypeIf you have a ton of files of the same type to copy, you can use the wildcard character *. The asterisk/wildcard tells the Linux command line to accept absolutely anything in that place. So, if you tell Linux to copy *.jpg, it’ll copy all JPG files, regardless of the name or whatever comes before the .jpg part.cp ~/Downloads/*.jpg ~/Pictures/Linux Cli Copy All File TypeIf you want to use multiple file types, say JPG and PNG, you can use the brackets from before.cp ~/Downloads/*.{jpg,png} ~/Pictures/Move a File or FolderIf you came here looking to move a file from one place to another without making a duplicate, you can do that easily too, but moving a file requires the mv command. The syntax is very similar to cp.mv ~/Downloads/your-file.txt ~/Documents/Similarly, you can also rename it.mv ~/Downloads/your-file.txt ~/Documents/renamed.txtThere is one major difference, though. You don’t need the -r flag to move a whole folder.mv ~/Downloads/downloaded-folder ~/Pictures/vacation-picsThat’s all there is to it. You’re ready to start copying and moving your files from the command line. You can see that the command line way can be very efficient in some situations.Want more pointers on the Linux command line? Here’s how to check sudo history or find out what the chmod 777 command does to your file permission.Image credit: Copy – Paste by DepositPhotosIs this article useful? Popular PostsXfce Review: A Lean, Mean Linux MachineHow to Hide the Top Bar and Side Panel in Ubuntu 20.04How to Share Files Between Android and Ubuntu on Your NetworkHow to Fix High CPU Usage in LinuxHow to Speed Up Your Linux Desktop with ComptonAffiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.Never Miss OutReceive update of our latest tutorials.Your email addressSee all newsletters | Privacy Policy
您其餘所有文法都完全相同。該-r标志用于告訴cp它正在使用目錄,并且應該複制其内容。
如果希望粘貼操作覆寫現有檔案,則可以添加-f标志:
cp -rf ~/Downloads/pictures-directory ~/Pictures/family-vacation-pics
複制并粘貼多個檔案
您也可以複制多個檔案。Linux指令行使您可以使用括号同時定位多個項目{}。您可以使用它們列出要複制的每個檔案的名稱,并用逗号分隔。
cp ~/Downloads/{file1.txt,file2.jpg,file3.odt} ~/Documents/
具有不同檔案類型的所有三個檔案都将被複制到Documents目錄中。
複制和粘貼相同類型的所有檔案
如果要複制大量相同類型的檔案,則可以使用通配符*。星号/通配符告訴Linux指令行在該位置絕對接受任何内容。是以,如果您告訴Linux複制*.jpg,它将複制所有JPG檔案,而不管名稱是.jpg部分之前是什麼。
cp ~/Downloads/*.jpg ~/Pictures/
如果要使用多種檔案類型(例如JPG和PNG),則可以使用之前的括号。
cp ~/Downloads/*.{jpg,png} ~/Pictures/
移動檔案或檔案夾
如果您來這裡是想将檔案從一個位置移動到另一個位置而不進行複制,那麼您也可以輕松地做到這一點,但是移動檔案需要mv指令。文法與cp非常相似。
mv ~/Downloads/your-file.txt ~/Documents/
同樣,您也可以重命名它。
mv ~/Downloads/your-file.txt ~/Documents/renamed.txt
但是,有一個主要差別。您不需要-r标志來移動整個檔案夾。
mv ~/Downloads/downloaded-folder ~/Pictures/vacation-pics
這裡的所有都是它的。您已經準備好從指令行開始複制和移動檔案。您會看到指令行方式在某些情況下可能非常有效。
在Linux指令行上需要更多指針嗎?這是檢查sudo曆史記錄或查明chmod 777指令對檔案權限的作用的方法。