30

In Linux if you type cat *, you will get something like this:

line1 from file1
line2 from file1
line1 from file2
line1 from file3
line2 from file3
line3 from file3

What I would like is to display a separator among files. Something like this:
line1 from file1
line2 from file1
XXXXXXXXXXXX
line1 from file2
XXXXXXXXXXXX
line1 from file3
line2 from file3
line3 from file3

Is that easily possible with a one-liner easy to type by heart?

41

If you're not too fussy about the appearance of the separator:

tail -n +1 *
| improve this answer | |
  • This is great! I am surprised how your command is different from tail -n 1 *. I can't either find any mention of the effect of +1 (rathen than 1) in the man page. – Daniele Aug 19 '10 at 11:21
  • 1
    tail -n X shows the last X lines. tail -n +X starts at line X beginning at 1, i.e., it shows all but the first (X-1) lines. – Gilles 'SO- stop being evil' Aug 19 '10 at 11:38
  • Ah, now I can see that in the man page. I overlooked it. Great, thanks a bunch – Daniele Aug 19 '10 at 11:51
  • Similar solutions are in this article: everythingsysadmin.com/2012/09/unorthodoxunix.html – TomOnTime Jun 4 '14 at 3:51
  • Sorry for the bad comment, but this is awesome. Thank you, @Gilles – Mike D Apr 8 '17 at 15:28
17

cd /to/your/directory; for each in *; do cat $each; echo "XXXXXXXXXXX"; done

| improve this answer | |
  • beat me to it :-) – Sirex Aug 19 '10 at 11:05
  • Still the solution provided by Gilles is impressively short! – Janne Pikkarainen Aug 19 '10 at 11:18
  • Wow. works wonder. Helped me concatenate a folder of JSONs – Mayank Chandak Apr 9 '19 at 16:34
11
awk 'FNR==1 && NR!=1 {print "XXXXXXXXXXXX"}{print}' *

Or

awk 'FNR==1 {print "XXXXXX", FILENAME, "XXXXXX"}{print}' *

Or

awk 'FNR==1 {print "XXXXXX File no. " ++count, "XXXXXX"}{print}' *

Using only Bash (no cat):

for file in *; do printf "$(<"$file")\nXXXXXXXXXXXX\n"; done

Edit:

In AWK 4:

awk 'BEGINFILE {print "XXXXXXXXXXXX"}{print}' *

You can use any separator such as the ones in the other examples in this answer. If you want the separator at the end of each file, change BEGINFILE to ENDFILE. It can still appear at the beginning of the script since it's a conditional (rather than implying execution order)..

| improve this answer | |
  • 2
    +1 for awk! I swear it can do anything... – brad Dec 19 '10 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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