Read file names from directory in Bash

后端 未结 2 500
渐次进展
渐次进展 2021-01-13 08:50

I need to write a script that reads all the file names from a directory and then depending on the file name, for example if it contains R1 or R2, it will concatenates all th

相关标签:
2条回答
  • 2021-01-13 09:38

    Simple hack:

    ls -al R1 | awk '{print $9}' >outputfilenameR1

    ls -al R2 | awk '{print $9}' >outputfilenameR2

    0 讨论(0)
  • 2021-01-13 09:44

    To make the smallest change that fixes the problem:

    dir="path to the files"
    for f in "$dir"/*; do
      cat "$f"
    done
    

    To accomplish what you describe as your desired end goal:

    shopt -s nullglob
    dir="path to the files"
    substrings=( R1 R2 )
    for substring in "${substrings[@]}"; do
      cat /dev/null "$dir"/*"$substring"* >"${substring}.out"
    done
    

    Note that cat can take multiple files in one invocation -- in fact, if you aren't doing that, you usually don't need to use cat at all.

    0 讨论(0)
提交回复
热议问题