![](/img/trans.png)
[英]Move multiple files in same directory without typing directory name each time (Linux)
[英]Linux Bash: Move multiple different files into same directory
作為一個相當新手的 Linux 用戶,我似乎無法找到如何做到這一點。 我正在嘗試將一個目錄中的所有唯一文件移動到另一個目錄中。 例子:
$ ls
vehicle car.txt bicycle.txt airplane.html train.docx (more files)
我想要車內的 car.txt、bicycle.txt、airplane.html 和 train.docx。
現在我通過單獨移動文件來做到這一點:
$ mv car.txt vehicle
$ mv bicycle.txt vehicle
...
我怎樣才能在一行中做到這一點?
你可以做
mv car.txt bicycle.txt vehicle/
(注意上面的/
是不必要的,我包含它只是為了確保vehicle
是一個目錄。)
您可以按如下方式進行測試:
cd #Move to home directory
mkdir temp #Make a temporary directory
touch a b c d #Make test (empty) files ('touch' also updates the modification date of an existing file to the current time)
ls #Verify everything is there
mv a b c d temp/ #Move files into temp
ls #See? They are gone.
ls temp/ #Oh, there they are!
rm -rf temp/ #DESTROY (Be very, very careful with this command)
移動所有 .txt 文件的速記命令
您可以嘗試使用通配符。 在下面的代碼中, *
將匹配所有名稱以.txt
或.docx
結尾的文件,並將它們移動到車輛文件夾。
mv *.txt *.docx vehicle/
如果要將特定文件移動到目錄
mv car.txt bicycle.txt vehicle/
編輯:如評論中所述,如果您手動移動文件,我建議使用mv -i ...
如果目標文件已經存在,它會警告您,讓您選擇不覆蓋它。 其他“文件銷毀器”命令(如 cp 和 rm)也有-i
選項
linux 中的mv
命令允許我們將多個文件移動到另一個目錄中。 您所要做的就是寫下要移動的每個文件的名稱,並用space
分隔。
以下命令將幫助您:
mv car.txt bicycle.txt airplane.html train.docx vehicle
或者
mv car.txt bicycle.txt airplane.html train.docx vehicle/
他們都將工作。
您可以使用mv命令將多個文件移動到特定目錄。 在您的場景中,它可以通過以下方式完成,
mv car.txt bicycle.txt airplane.html train.docx vehicle/
您必須注意的一點是,最后一個條目是目的地,其余的除了mv是源之外的所有內容。
另一種情況是目的地不在我們的目錄中,那么我們必須選擇絕對路徑來代替車輛/。
注意:絕對路徑總是從/開始,這意味着我們從根目錄遍歷。
我已經編寫了一個小的 bash 腳本,它將使用 bash 中的 mv 和 find 命令將存在於多個目錄(使用模式匹配)中的多個文件(使用模式匹配)移動到單個位置
#!/bin/bash
for i in $(find /path/info/*/*.fna -type f) # find files and return their path
do
mv -iv $i -t ~/path/to/destination/directory # move files
done
注意:以上標志不是強制性的
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.