Update: The problem is that $files
will include the directory name, which causes problems when you try to make an output filename from it. I originally thought it might be a spaces-in-filenames issue (only tested with in *
), but gniourf_gniourf found the real problem:
That's because index_$files
will expand to index_./stuff.gcount
and since the directory index_.
doesn't exist, Bash complains. Two possibilities to fix this: 1) use for files in *.gcount
, so that you don't have any slashes in files. 2) only keep the basename from file with, just after the do
keyword: files=${files#./}
. This will get rid of the leading ./
.
(Editing so that the post contains the real solution. rep ought to go to gniourf_gniourf, but we had a discussion where he was fine with an edit instead of posting his answer.)
Here's the original post, which might still demonstrate some good practice:
Works here, though there's a bug in that the "Index" line will be numbered too.
Check if any of your filenames contain spaces, as that will break it. To make scripts correctly handle such filenames, you should quote $files
to make sure it expands into a single word, like
paste -d "\t" index "$files" > index_"${files}"
, and similarly for other lines. (The {}
aren't needed there by the way, though they don't hurt.)
If you just want to add line numbers to your file, it's easier to use nl(1)
(which seems to be POSIX, so it should be reasonably portable). Here's an example you could use which matches the format you're after:
{ echo Index; nl -nln -ba "$files"; } > index_"${files}"
The brackets let you redirect the output from many commands to the same place.
Another (slightly nitpicky) thing that's worth being aware of is that you can use --
with many commands to turn off flag processing, which lets you handle filenames that start with -
. Here's an example: ls -- -weirdly_named_file
.
You could fix the numbering bug in the original version by doing the following by the way:
seq 1 1 $(wc -l < "$files") > index
{ echo "Index"; paste -d "\t" index "$files"; } > index_"${files}"
This question contains other variants for adding a line to the beginning of a file.