linux - Bash : Adding thread-capability to this bash function, so it's faster -


i have bash-function regularly use list files , directories in nice format. problem is, everytime execute function, takes 2-3 seconds till data listing finished.

as calling find command 2 times, takes double time. wanted ask, how can start both find commands thread in parallel , output. presume make function output load faster. wrong?

script :

function lsa {  # function stat files , folders in current dir  # takes first argument directory stat  # if no directory supplied, current dir assumed  if [ -z "$1" ];then    dir="."  else    dir="$1"  fi   # print directories first  printf "*** directories *** \n"  find "$dir"  -maxdepth 1 -type d ! -name "."   -printf "%m %u %g "  -exec du -sh  {} \; 2> /dev/null   # print non-directories second  printf "*** files *** \n"   find "$dir"  -maxdepth 1 ! -type d ! -name "."   -printf "%m %u %g "  -exec du -sh  {} \; 2> /dev/null   } 

kindly let me know. thank you.

update

with changes :

*** directories ***  [1] 6882 *** files ***  [2] 6883 [1]-  done                    find . -maxdepth 1 -type d ! -name "." -printf "%m %u %g " -exec du -sh {} \; 2> /dev/null > dirs [2]+  done                    find "$dir" -maxdepth 1 ! -type d ! -name "." -printf "%m %u %g " -exec du -sh {} \; 2> /dev/null > non-dirs // , files , dirs 1 after other 

without changes :

borg@borg-cube:~$lsa *** directories ***  // directories *** files ***  // files 

you cannot have multi-threads multi-processes running 2 find commands in background , saving outputs different files. wait both find commands completion , concatenate outputs (dirs first).

this way...

find .  -maxdepth 1 -type d ! -name "."   -printf "%m %u %g "  -exec du -sh  {} \; 2> /dev/null > dirs & ... find "$dir"  -maxdepth 1 ! -type d ! -name "."   -printf "%m %u %g "  -exec du -sh  {} \; 2> /dev/null > non-dirs & ... wait cat dirs non-dirs .... 

** update **

replace cat dirs non-dirs @ end with:

printf "*** directories *** \n" cat dirs printf "*** files *** \n" cat non-dirs 

Comments