1

I am looking to concatenate a list of files in a directory in the reverse order that they appear in the list.

This is different from tac as tac will concatenate the files with reversed line order.

Basically I have a folder with 3 files, file1, file2 and file3.

cat f* > newfile will merge these files like so:

file1
file2
file3

However, I want to merge the files like this

file3
file2
file1

whilst maintaining the correct line order.

Jim L.
  • 7,188
  • 1
  • 13
  • 25

3 Answers3

5

This is different to tac as tac will concatenate the files with reversed line order

… and not in the desired reverse sequence of files. Surprisingly this is good, because an additional tac acting on the whole output will give you exactly what you want:

tac f* | tac
Kamil Maciorowski
  • 19,242
  • 1
  • 50
  • 94
2

Try "Brace Expansion" (cf. man bash):

set -vx
cat file{3..1}
+ cat file3 file2 file1
RudiC
  • 8,889
  • 2
  • 10
  • 22
1

You can ask ls to sort them out and then concatenate with cat:

ls -1r | xargs -d'\n' cat

If you ever want to use another sort order, or shuffle the entries, you change the left part of the pipe.

In this case -1 will list a filename in each line and the -r does inverts the order (since it is not specified, it is alphabetical, according to your locale).

Eduardo Trápani
  • 12,032
  • 1
  • 18
  • 35