简体   繁体   English

Bash如何对两个目录中的文件执行命令

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

I know this bash code for execute an operation for all files in one directory: 我知道此bash代码可对一个目录中的所有文件执行操作:

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

But now I have to execute an operation for all files in one directory with all files in another directory with the same filename but different extension. 但是,现在我必须对一个目录中的所有文件以及另一个目录中的所有文件具有相同文件名但具有不同扩展名的所有文件执行操作。 For example 例如

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

I must not to compare two files, but the script that I have to execute needs two file to produce another one (in particular I have to execute bwa samsa path_to_ref/ref file1.txt file1.csv > file1.newext ) 我不能比较两个文件,但是我必须执行的脚本需要两个文件才能生成另一个文件(尤其是我必须执行bwa samsa path_to_ref/ref file1.txt file1.csv > file1.newext

Could you help me? 你可以帮帮我吗?

Thank you for your answers! 谢谢您的回答!

In bash using variable manipulation: 在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

Edit: 编辑:

Implementing @DavidC.Rankin's insuring suggestion: 实施@ 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

Try with: 尝试:

for file in path_to_txt/*.txt; do 
   b=$(basename $file .txt)
   cmd path_to_txt/$b.txt path_to_csv/$b.csv 
done
  • do not include the paths in call to "cmd" if this command doesn't need them. 如果此命令不需要对“ cmd”的调用,请不要包含这些路径。
  • "for" statement can not include the path if it is run at directory of .txt files 如果“。”语句在.txt文件目录中运行,则不能包含该路径
  • if execution time is a requisite, you can replace basename by posix regexp. 如果需要执行时间,则可以用posix regexp代替基本名称。 See here https://stackoverflow.com/a/2664746/4886927 看到这里https://stackoverflow.com/a/2664746/4886927

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM