StatsSorceress
StatsSorceress

Reputation: 3099

Bash: find and concatenate files

I have the following structure:

/home/
├── DIR1/
│   └── file_ab.csv
├── DIR2/
│   └── file_cd.csv
└── DIR3/
    └── file3_ef.csv

Where file_**.csv contains rows of floats, different floats for each DIR.

I want to grab the contents of all of the file_**.csv files and concatenate them.

I found this answer here:

find /home -type f -name '*.csv' -exec cat {} \; > pl_parameters

But I get an empty file called 'pl_parameters'. Why is the file empty? How can I fix this?

Upvotes: 0

Views: 1214

Answers (2)

codeforester
codeforester

Reputation: 42999

With Bash 4.0+, you can use globstar and use a more straight forward command:

shopt -s globstar
cd /home
cat **/*.csv > pl_parameters

**/ expands to the entire directory tree underneath the current directory.


Your command:

find /home -type f -name '*.csv' -exec cat {} \; > pl_parameters

looks good to me - not sure why you got a zero by output file.

Upvotes: 0

Haifeng Zhang
Haifeng Zhang

Reputation: 31895

find /home/DIR* -name 'file*csv' |xargs cat > output.csv

find /home/DIR* -name '*csv' gives you the files absolute paths.

xargs cat will iterate the files and cat print the files content

Upvotes: 1

Related Questions