簡體   English   中英

將具有唯一名稱的多個文件移動到新文件夾並追加到文件名

[英]Move multiple files with unique name to new folder and append to file name

我的一個文件夾中有大約2000個文件。

所有文件的名稱中都包含字符串test

我需要做的是將所有這些文件_scrap移到同一目錄中的稱為trash的文件夾,並將_scrap附加到每個文件的末尾。

 mv *test* trash/

我想要的是這樣的:

[root@server] ls
test1.txt  test2.txt  test3.txt trash  video1.txt  video2.txt  video3.txt
[root@server] mv *test* trash/*_scrap
[root@server] ls
trash  vidoe1.txt  video2.txt  video3.txt
[root@server] ls trash/
test1.txt_scrap  test2.txt_scrap  test3.txt_scrap

我可以移動所有文件,但是我不知道如何將_scrap附加到末尾。

由於我必須在多台機器上執行此操作,因此與一小段腳本相比,一個襯紙會更好。

$ touch test1.txt  test2.txt  test3.txt vidoe1.txt  vidoe2.txt  vidoe3.txt
$ mkdir trash
$ for file in *test*; do mv "$file" "trash/${file}_scrap"; done
$ ls
trash       vidoe1.txt  vidoe2.txt  vidoe3.txt
$ ls trash
test1.txt_scrap test2.txt_scrap test3.txt_scrap
$ 

您也可以使用xargs

$ ls *test* | xargs -t -I{} mv {} trash/{}_scrap
mv test1.txt trash/test1.txt_scrap
mv test2.txt trash/test2.txt_scrap
mv test3.txt trash/test3.txt_scrap
$ 

您可以使用find

$ find . -name '*test*' -maxdepth 1 -exec mv {} trash/{}_scrap \;

您可以使用rename避免shell循環。 這是一個perl腳本,但是它隨附了許多常見發行版(包括Ubuntu 14):

$ mv *test* trash/
$ rename 's/$/_scrap/g' trash/*
$ ls trash/
test1.txt_scrap  test3.txt_scrap  test2.txt_scrap

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM