0

I have the following command to gather all files in a folder and concatenate them....but what is held in the variable is only the file names and not the directory. How can I add 'colid-data/' to each of the files for cat to us?

cat $(ls -t colid-data) > catfiles.txt
Barmar
  • 741,623
  • 53
  • 500
  • 612
B_Miner
  • 1,840
  • 4
  • 31
  • 66
  • 1
    Does this answer your question? [Add a prefix string to beginning of each line](https://stackoverflow.com/questions/2099471/add-a-prefix-string-to-beginning-of-each-line) – KamilCuk Jan 30 '20 at 22:43

2 Answers2

0

List the filenames, not the directory.

cat $(ls -t colid-data/*) > catfiles.txt

Note that this will not work if any of the filenames contain whitespace. See Why not parse ls? for better alternatives.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If you want to concatenate them in date order, consider using zsh:

cat colid-data/*(.om) >catfiles.txt

That would concatenate all regular files only, in order of most recently modified first.

From bash, you could do this with

zsh -c 'cat colid-data/*(.om)' >catfiles.txt

If the ordering of the files is not important (and if there's only regular files in the directory, no subdirectories), just use

cat colid-data/* >catfiles.txt

All of these variations would work with filenames containing spaces, tabs and newlines, since the list of pathnames returned by a filename globbing pattern is not split into further words (which the result of an unquoted command substitution is).

Kusalananda
  • 14,885
  • 3
  • 41
  • 52