Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's the situation:

My file A.csv needs to be concatenated individually with all the other *.csv's in the folder and each of the outputs needs to have the same name as the particular *.csv.

How can I do this at the terminal? I have to do this 50 times and the other csv's have no ordered naming convention so typing manually is a pain.

The data is sensitive so I can't fool around with checking if cat A.csv *csv >*.csv works. (I'm guessing it won't)

share|improve this question
FYI > will overwrite the file with the terminal output. >> will append the terminal output to the file. – justbrowsing Aug 14 '13 at 5:18
up vote 3 down vote accepted

Use a for-loop in bash. You can make yourself a harmless test first:

for f in *.csv; do
    if [ "$f" != 'A.csv' ]; then
        echo cat A.csv "$f" \> "$"

This allows you to see what will be executed but not actually execute it.

Notice that I have called the output file $ You can't guarantee that the cat will be performed before the shell starts overwriting the file. So I recommend you do this, or even better write the files into another directory:

mkdir combined
for f in *.csv; do
    if [ "$f" != 'A.csv' ]; then
        echo cat A.csv $f \> "combined/$f"

And of course, once you're happy, remove the echo (and the escaped > character):

cat A.csv "$f" > "combined/$f"
share|improve this answer
If you want to handle spaces properly all you have to do is double-quote your variables, simple as that :) – slhck Aug 14 '13 at 5:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.