Issue
I have a folder with a lot of CSV files. There are subfolders also. For each such file I need to extract the first 200 lines (to make a db sub sample) creating another file with these lines. So I would have several new files with 200 lines within each of them. I need to complete this task in Linux Ubuntu and using OS shell commands. Is there a way to do this? Thanks in advance.
Solution
This line could respond to your need :
find . -type f -name "*.csv" -print|xargs -I@ sh -c 'head -n200 @ > @.headed'
This line is made with different part :
- Find the files that must be "headed"
- The output of the find command is just a list of files
- xargs command that take the list of files and run on it the command pass with the sh -c option
- The command that display the 200 first line of a file and put it in a filename with "headed" extension
With this line you have a solution that could be extensible for another usecase near the one you've written.
Answered By - YLR