1

I have aboout 100 .csv files with the same name in different sub directories. I need to copy all the data in these files into a single csv file but since they all have the same name they over write on each other. Can you please help me with the shell script code where i can rename all these files in order like say for example: All my files are in the name example.csv in different subfolders. I want them to be example1.csv in subfolder1, example2.csv in subfolder2 and so on...

This is the script that would over write on the csv files.

for c in *.csv
do
echo $c
cat $c >> fullreport.csv
done

Thanks a lot for your help :)

2 Answers 2

1

"I need to copy all the data in these files into a single csv file..."

cat */example.csv > fullreport.csv
Sign up to request clarification or add additional context in comments.

Comments

0

Do it this way instead:

for c in *.csv
do
    echo "$c"
    cat "$c"
done > fullreport.csv

Assuming fullreport.csv still doesn't exist. Delete it if needed.

To add more directories, just specify them space after space:

for c in *.csv a/*.csv b/*.csv
do
    echo "$c"
    cat "$c"
done > fullreport.csv

If you want to do it recursively, use find like:

while read c; do
    echo "$c"
    cat "$c"
done < <(find -type f -iname '*.csv') > fullreport.csv

You can place your output as well on a different directory instead to prevent conflicts like > ../fullreport.csv.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.