Edit:
I tried for 2 hours and this the best solution I got:
My basic idea was to make a hash function with key equals basename and it's value equals the first 3 characters.
#!/bin/bash touch file_s echo 'declare -A map' > file_s find . ! -name file_s ! -name sort_map -type f -exec grep -I -q . {} \; \ -exec sh -c 'i="$(basename "$0")";echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s;' {} \; | sort | while read -r line do source file_s echo -e ${map["$line"]} done
The name of the script where you should write this code is sort_map and file_s is a temp. file. So you should not include these two files in the find command . grep -I -q . {} \; will grep files which are only text, not binary files.
The second -exec command is as follows:
i="$(basename "$0")"; will get the basename and will write into variable i.
echo "map["$i"]=$(head -n 1 "$0"|cut -c 1-3)" >> file_s; will write hash function and it's value to temp file file_s.
sort will sort the file names.
while read -r line do source file_s echo -e ${map["$line"]} done
Will read line by line and source the file file_s. Then will print the first 3 characters.
You can't use head because head prints the content of the file not the file names.
You can use:
find . -type f -exec basename {} \; | sort | cut -c 1-3
Or you can also use b option instead of c but it will assume that all the characters are of 1 byte.
find . -type f -exec basename {} \; | sort | cut -b 1-3
It will get first three characters.
You can use:
find . -type f -exec basename {} \; | sort | sed -Ee 's/(^.{3})(.*)/\1/g'
It will sort the files and then will match starting three characters and will only print them.
Note: All of these commands will consider space and tab as one character.