I have a command that needs to take multiple input files, which are originally from one directory and have certain file name patterns. For example:In directory/home/mydir/I have files:A.datB.datC.datreadme.docI would like to learn how to pass all files with ending of .dat to this command, which then should look like:command A.dat B.dat C.dat > /home/outputdir/output.datI could do it in python e.g. by storing the file names in a list, but how should I do it in shell please? Thanks a lot.
How to refer to multiple files from a directory to one command
shell script;files
A standard UNIX shell will do something called globbing; this uses special characters to mean, for example, one character (?) or any number of characters (*). To use your example, you could run (where the initial $ represents your command prompt and not something you'd type):$ command /home/mydir/*.dat > /home/outputdir/output.datYour shell will expand that to:$ command /home/mydir/A.dat /home/mydir/B.dat /home/mydir/C.dat > /home/outputdir/output.datbefore actually calling the command. The * says take any and all filenames in /home/mydir that end with .dat.For some variations of the command, given the same input files:# all of the sample input files have a single letter before the .dat$ command /home/mydir/?.dat > /home/outputdir/output.dat# the square brackets say any (one) of these characters$ command /home/mydir/[ABC].dat > /home/outputdir/output.dat