Home > Mobile >  Selecting only few files from large no of files in a directory/bash script
Selecting only few files from large no of files in a directory/bash script

Time:04-19

I have a very simple question, I have 10,000 files in a directory with the name simulations_0.root to simulations_9999.root.

I want to merge 500 files together to make them bigger in size. I need to run hadd, selecting simulations_0.root to simulations_499.root; simulations_500.root to simulations_999.root and so on.

hadd will take multiple input files as command line arguments:

Synopsis
hadd outputfile inputfiles ...

My question is how can I select those files from 0 to 500 at once in the hadd command?

CodePudding user response:

You can use brace expansions for that.

Example:

[user@host dir]$ bash -x
[user@host dir]$ echo foo{1..10}
  echo foo1 foo2 foo3 foo4 foo5 foo6 foo7 foo8 foo9 foo10
foo1 foo2 foo3 foo4 foo5 foo6 foo7 foo8 foo9 foo10

As you can see in the above example with bash -x, the expression foo{1..10} will get expanded into space separated arguments.

In your particular case the command would be:

hadd output_file.root simulations_{1..500}.root 

CodePudding user response:

Taking @mashuptwice method a little further:

#!/bin/bash

min=0 max=9999 incr=500

for (( i = min, j = min   incr - 1; j <= max; i  = incr, j = incr))
do
    declare -a "files=( simulations_{$i..$j}.root )"
    hadd "simulations_${i}to$j.root" "${files[@]}"
done
  •  Tags:  
  • bash
  • Related