Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash cat all files that contains a certain string in file name

In bash, how would you cat all files in a directory that contains a certain string in its filename. For example I have files named:

test001.csv
test002.csv
test003.csv
result001.csv
result002.csv

I want to cat all .csv that contains the string test in the file name together, and all .csv that contains the string result in the file name together.

like image 394
GameOfThrows Avatar asked Dec 19 '22 00:12

GameOfThrows


2 Answers

Just:

cat *test*.csv
cat *result*.csv

For all files with test (or in case of the second one result) in their name.

like image 50
chaos Avatar answered Feb 22 '23 23:02

chaos


The shell itself can easily find all files matching a simple wildcard.

cat *test*.csv >testresult

You want to take care so that the output file's name does not match the wildcard. (It's technically harmless, but good practice.)

The shell will expand the wildcard in alphabetical order. Most shells will obey your locale, so the definition of "alphabetical order" may depend on current locale settings.

like image 28
tripleee Avatar answered Feb 23 '23 00:02

tripleee