簡體   English   中英

如何在Linux中復制多個文件的開頭?

[英]How do I copy the beginning of multiple files in Linux?

我想在Ubuntu中將一堆文件(* .txt)從一個目錄復制到另一個目錄。 我想減小它們的大小,所以我使用head來獲取每行的前100行。

我希望新文件保留其原始名稱,但位於子目錄small/ 我努力了:

head -n 100 *.txt > small/*.txt

但這會創建一個名為*.txt文件。 我也嘗試過:

head -n 100 *.txt > small/

但這給出Is a directory錯誤。

這一定很容易,但是我對Linux很不好。 任何幫助深表感謝。

您必須改為創建一個循環:

for file in *.txt; do
    head -n 100 "$file" > small/"$file"
done

這將循環遍歷所有.txt文件,在所有.txt文件中執行head -n 100並輸出到small/目錄中的新文件中。

嘗試

for f in *.txt; do
  head -n 100 $f > small/$f
done

暫無
暫無

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

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