Force sort command to ignore folder names

I ran the following from the base folder. /

find . -name *.xvi.txt | sort 

Returns the following sort order:

 ./LT/filename.2004167.xvi.txt ./LT/filename.2004247.xvi.txt ./pred/2004186/filename.2004186.xvi.txt ./pred/2004202/filename.2004202.xvi.txt ./pred/2004222/filename.2004222.xvi.txt 

As you can see, the file names follow the usual structure, but the files themselves can be located in different parts of the directory structure. Is there a way to ignore folder names and / or directory structure so that sorting returns a list of folders / file names based ONLY on the file names themselves? For instance:

 ./LT/filename.2004167.xvi.txt ./pred/2004186/filename.2004186.xvi.txt ./pred/2004202/filename.2004202.xvi.txt ./pred/2004222/filename.2004222.xvi.txt ./LT/filename.2004247.xvi.txt 

I tried several different switches under the find and sort commands, but no luck. I could always copy everything into one folder and sort from there, but there are several hundred files, and I hope there is a more elegant option.

Thanks! Your help is appreciated.

+4
source share
3 answers
 find . -name *.xvi.txt | sort -t'.' -k3 -n 

sorts it as you wish. the only problem is that if the file name or directory name will contain extra dots.

To avoid this, you can use:

 find . -name *.xvi.txt | sed 's/[0-9]\+.xvi.txt$/\\&/' | sort -t'\' -k2 | sed 's/\\//' 
0
source

If your find has -printf , you can print both the base file name and the full file name. Sort by the first field, and then turn it off.

 find . -name '*.xvi.txt' -printf '%f %p\n' | sort -k1,1 | cut -f 2- -d ' ' 

I chose a space as a separator. If your file names include spaces, you must choose a different delimiter, which is a character that is not specified in your names. If any file names include newlines, you will have to change this because this will not work.

Note that glob in the find must be specified.

+5
source

If your find does not have printf , you can use awk to do the same:

 find . -name *.xvi.txt | awk -F / '{ print $NF, $0 }' | sort | sed 's/.* //' 

The same warnings about spaces that Dennis Williamson mentioned are applicable here. And for a change, I use sed to remove the sort field instead of cut .

+1
source

All Articles