简体   繁体   中英

bash : sort “find” output files first

I'm trying to get a relative path's list of all my *.tex files to get a proper compilation list.

I structured my directories the following way: each *.tex file containing other sections has an eponymous folder, which makes the structure looking this way :

./section1/ 
./section2/
./section1.tex
./section2.tex

Inside of each folder there is thus other *.tex files and eventually other directories in order for me to keep a clean hierarchy.

I'd like to get the following output and print it into a file (let's say "toc.txt') :

./section1.tex

./section1/subsection1_1.tex
./section1/subsection1_2.tex
./section1/subsection1_3.tex


./section2.tex

./section2/subsection2_1.tex
./section2/subsection2_2.tex
./section2/subsection2_3.tex

I tried the following command :

find . -name '*.tex' -print | sort

But it gives me the following result :

./section1/subsection1_1.tex
./section1/subsection1_2.tex
./section1/subsection1_3.tex
./section1.tex
./section2/subsection2_1.tex
./section2/subsection2_2.tex
./section2/subsection2_3.tex
./section2.tex

I have to say I really don't want to struggle manually with reordering every line to get this toc file...

Would you have an idea ? :-)

It has to do with collation order. This will work.

find . -name '*.tex' -print | LC_COLLATE=C sort

And, just for fun, this is a bit crazy, but if you just replace your . with something that sorts higher up in the alphabet and is not found in your filenames, sort, then reverse the transform, it works. eg

find . -name '*.tex' -print | sed -e 's/\./aaaa/g' | sort | sed -e 's/aaaa/\./g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM