[英]How to save a list of all files in a directory in a single text file and add prefixes and suffixes?
我正在嘗試使用將目錄中的文件列表保存到單個文件中
ls > output.txt
假設我們在目錄中:
a.txt
b.txt
c.txt
我想將output.txt
中這些文件的名稱修改為:
1a.txt$
1b.txt$
1c.txt$
另一種簡單的方法是使用 AWK 更改內容並保存到文件 via.tmp
該腳本將按照您的需要打印內容。 只需相應地在開頭和結尾添加“1”和“$”。
cat output.txt | awk '{print "1"$1"$"}'
然后您可以通過擴展命令 && (如果成功則下一步)根據需要保存到原始文件
cat output.txt | awk '{print "1"$1"$"}' > output.txt.tmp && mv output.txt.tmp output.txt
#!/bin/sh -x
for f in *.txt
do
nf=$(echo "${f}" | sed 's@^@1@')
mv -v "${f}" "${nf}"
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.