Reputation: 105
I have a list of files that I get by executing this: ls core_* | sort -n -t _ -k 2
which gives me something like this:
core_20171201142359.csv
core_20171202131548.csv
core_20171203141112.csv
The objective is to get a single file in which to append all the content of every single file in order.
So, I want to open every single file one by one, copy its content into another file, move the previous source file to another directory for safekeeping and move on.
To always get the very first file in order I use ls core_* | sort -n -t _ -k 2 | head -1
, and I need to cycle all of those files.
How can I know when there are no more files that I need to process?
Upvotes: 3
Views: 880
Reputation: 246764
For bash, you can store the filenames in an array:
files=(core_*)
Then the first entry is
first="${files[0]}"
And you can iterate with this (the quotes are absolutely required)
for file in "${files[@]}"; do
echo "$file"
done
Or, if you need to do something with all the files at once:
cat "${files[@]}" > core_all.csv
but if that's the case, you don't need to store them at all
cat core_* > core_all.csv
Upvotes: 3
Reputation: 17061
You can try this:
ls core_* | sort -n -t _ -k 2 | while read f; do cat $f >> total.csv; done
Also alongside with cat
you can perform move
etc.
Upvotes: 1
Reputation: 1838
for file in $(ls core_* | sort -n -t _ -k 2)
do
cat ${file} >> one_big_file.csv
mv ${file} /anywhere/you/want
done
Will read each file, copy all lines into one_big_file.csv, remove just read file to the /anywhere/you/want
Upvotes: 0