Home > Software engineering >  Sort files in different directories based on file name
Sort files in different directories based on file name

Time:02-17

I have several different directories within my working directory, In each of these subdirectories, I have a file that ends in ".breaks.bed" that look like this:

1       93961   93962   sample_1           1
1       115757  115758  sample_2           5
1       115796  115797  sample_3           68
1       713966  713967  sample_4           7

I want to select these files based on this extension above, and then sort them by the 5th column and rank from highest to lowest (without having to create a new output file) like so:

1       115796  115797  sample_3           68
1       713966  713967  sample_4           7
1       115757  115758  sample_2           5
1       93961   93962   sample_1           1

So far, my inital attempt has been to try and use find and then sort. I think this is close, but not quite there.

find . -name "*.break.bed" -type f sort -o,-k5,5nr "*.break.bed"{,}

As always, any help is always appreciated!

CodePudding user response:

You seem to be looking for

find . -name "*.break.bed" -type f -exec sort -k5,5nr -o {} {} \;
  • Related