Reputation: 1
I'm attempting to use Bash to find all of the text files on my hard drive, sort them according to size, and export a CSV list of their paths.
This is similar to a few other threads on SO. Probably most closely to this: How sort find result by file sizes
But there are a few variations on the previously suggested code that I don't quite understand.
This line allows me to search by file type and export the results as path names, but I am unclear about how to combine this with the other necessary functions (namely, sorting path names by size ).
find / -type f -name '*.txt' > ~/Desktop/sorted.csv
I am doing this on MacOS and I am new to Bash, so the solution so far has been difficult to ascertain.
edit: Using bits of other code, I was able to piece this together. From what I understand, the "find" command finds all files with a txt extension and prints a full list of the file information, with path. "sort" picks the 5th "field", which is (always?) the file size, and sorts the information accordingly. "awk" is then used to print fields 9 through 13...which is a problem. this is printing bits and pieces of the path, potentially because each bit of the path occupies its own field at this point.
find / -type f -name '*.txt' -exec ls -al {} \;|sort -k 5 -n| awk '{ print $9, $10, $11, $12, $13 }' > ~/Desktop/sorts/sorted.txt
Just to add one last addition, thanks to RobC's comments: I would like the final CSV file to simply be a list of the sorted paths. Filesize no longer included. Ascending/Descending is not important.
Upvotes: 0
Views: 455
Reputation: 3183
Does this work:
find / -type f -name '*.txt' -exec du -k {} + | sort -rn > ~/Desktop/sorted.csv
Upvotes: 0