Bash: find CSV files, count their rows, and print modification time; all in one line!
Recently I needed a quick and dirty way to
Recently I needed a quick and dirty way to
- Find all CSV files in a directory
- Print their modification time
- Print their row count
I used the following one linerfind /home/kpatronas -name '*.csv' | xargs -I {} bash -c 'date -r {} "+%m-%d-%YT%H";cat {} | wc -l;echo {}' | paste -d " " - - -
How it works
- find: find files based on patterns and options
- xargs: pass the filename as the variable {}, bash -c will execute commands inside single quotes
- date -r {}: will print the modification time of the file in the specified time format (i care only for hours, but it can easily modified to include minutes and seconds)
- cat {} : will pass the contents of the file to wc -l which will print the number of rows
- echo {} : will print the filename
- | paste -d “ “ — — — : The paste command will print each three lines into separates columns
Example