繁体   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