簡體   English   中英

Bash如何對兩個目錄中的文件執行命令

[英]Bash How to execute a command for files in two directories

我知道此bash代碼可對一個目錄中的所有文件執行操作:

for files in dir/*.ext; do 
cmd option "${files%.*}.ext" out "${files%.*}.newext"; done

但是,現在我必須對一個目錄中的所有文件以及另一個目錄中的所有文件具有相同文件名但具有不同擴展名的所有文件執行操作。 例如

directory 1 -> file1.txt, file2.txt, file3.txt
directory 2 -> file1.csv, file2.csv, file3.csv

cmd file1.txt file1.csv > file1.newext
cmd file2.txt file2.csv > file2.newext

我不能比較兩個文件,但是我必須執行的腳本需要兩個文件才能生成另一個文件(尤其是我必須執行bwa samsa path_to_ref/ref file1.txt file1.csv > file1.newext

你可以幫幫我嗎?

謝謝您的回答!

在bash中使用變量操作:

$ for f in test/* ; do t="${f##*/}";  echo "$f" test2/"${t%.txt}".csv ; done
test/file1.txt test2/file1.csv
test/file2.txt test2/file2.csv
test/file3.txt test2/file3.csv

編輯:

實施@ DavidC.Rankin的保險建議:

$ touch test/futile
for f in test/*
do 
  t="${f##*/}"
  t="test2/${t%.txt}".csv
  if [ -e "$t" ]
  then 
    echo "$f" "$t"
  fi
done
test/file1.txt test2/file1.csv
test/file2.txt test2/file2.csv
test/file3.txt test2/file3.csv

嘗試:

for file in path_to_txt/*.txt; do 
   b=$(basename $file .txt)
   cmd path_to_txt/$b.txt path_to_csv/$b.csv 
done
  • 如果此命令不需要對“ cmd”的調用,請不要包含這些路徑。
  • 如果“。”語句在.txt文件目錄中運行,則不能包含該路徑
  • 如果需要執行時間,則可以用posix regexp代替基本名稱。 看到這里https://stackoverflow.com/a/2664746/4886927

暫無
暫無

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

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