Skip to content

Instantly share code, notes, and snippets.

@nishidy
Last active August 18, 2017 01:19
Show Gist options
  • Select an option

  • Save nishidy/6ab1d28f380d7b9d6809cfd3a291ee80 to your computer and use it in GitHub Desktop.

Select an option

Save nishidy/6ab1d28f380d7b9d6809cfd3a291ee80 to your computer and use it in GitHub Desktop.
Convenient Search command
#!/bin/bash
if [ -z "$*" ]; then
echo "cs [OPTIONS] DIR"
echo " ORDER BY OPTIONS:"
echo " -f : file count"
echo " -s : file size"
echo " -m : file modification time"
echo " -j : total size of files just under a directory"
echo " -a : total size of files all under a directory"
echo " -r : sort by ascending order (default is by descending order)"
echo " -h : show in human readable format"
echo " -nN : show the top N"
exit 0
fi
topdir=$(pwd)
os=$(uname)
num=10
AWK_NOTHING='{ print $1,$2 }'
AWK_FILE_HUMAN='{
if (1000*1000*1000<$1)
printf("%d,%03d,%03d,%03d %s\n",$1/(1000*1000*1000),($1/(1000*1000))%1000,($1/1000)%1000,$1%1000,$2);
else if (1000*1000<$1)
printf("%d,%03d,%03d %s\n",$1/(1000*1000),($1/1000)%1000,$1%1000,$2);
else if (1000<$1)
printf("%d,%03d %s\n",$1/1000,$1%1000,$2);
else
printf("%d %s\n",$1,$2);
}'
AWK_SIZE_HUMAN='{
if (1024*1024*1024<$1)
printf("%5.1fGB %s\n", $1/(1024*1024*1024), $2);
else if (1024*1024<$1)
printf("%5.1fMB %s\n", $1/(1024*1024),$2);
else if (1024<$1)
printf("%5.1fKB %s\n", $1/1024, $2);
else
printf("%5.1fB %s\n", $1,$2);
}'
AWK_TIME_HUMAN='{ c=sprintf("date -r %s",$1); c | getline r; close(c); print r, $2;}'
for opt in $@;do
case $opt in
-f) ope=file;;
-s) ope=size;;
-m) ope=time;;
-j) ope=just;;
-a) ope=all;;
-r) reverse=true;;
-h) human=true;;
-n*) num=${opt:2};;
*) topdir=$opt;;
esac
done
[[ $reverse = true ]] && sorto="-n" || sorto="-rn"
_IFS=$IFS
IFS=$'\n'
case $ope in
file)
[[ $human = true ]] && awkc="$AWK_FILE_HUMAN" || awkc="$AWK_NOTHING"
for dir in $(find $topdir -type d | grep -vxF .); do
echo "$(ls -1 $dir | wc -l) $dir"
done | sort $sorto | head -n $num | awk $awkc
;;
size)
[[ $human = true ]] && awkc="$AWK_SIZE_HUMAN" || awkc="$AWK_NOTHING"
[[ $reverse = true ]] && lso="-Slr" || lso="-Sl"
for dir in $(find $topdir -type d | grep -vxF .); do
#echo $dir >&2
for file in $(ls $lso $dir | grep '^-' | awk '{print $NF}' | head -n $num); do
if [ $os = "Darwin" ]; then
stat -f "%z %N" "$dir/$file"
else
stat "$dir/$file" --format="%s %n"
fi
done
done | grep -v "/$" | sort $sorto | head -n $num | awk $awkc
;;
time)
[[ $human = true ]] && awkc="$AWK_TIME_HUMAN" || awkc="$AWK_NOTHING"
[[ $reverse = true ]] && lso="-tlr" || lso="-tl"
for dir in $(find $topdir -type d | grep -vxF .); do
for file in $(ls $lso $dir | grep '^-' | awk '{print $NF}' | head -n $num); do
if [ $os = "Darwin" ]; then
stat -f "%m %N" "$dir/$file"
else
stat "$dir/$file" --format="%Y %n"
fi
done
done | grep -v "/$" | sort $sorto | head -n $num | awk $awkc
;;
just)
[[ $human = true ]] && awkc="$AWK_SIZE_HUMAN" || awkc="$AWK_NOTHING"
for dir in $(find $topdir -type d | grep -vxF .); do
s=0
for i in $(ls -l $dir | grep '^-' | awk '{print $5}'); do
s=$((i+s))
done
echo "$s $dir"
done | sort $sorto | head -n $num | awk $awkc
;;
all)
[[ $human = true ]] && awkc="$AWK_SIZE_HUMAN" || awkc="$AWK_NOTHING"
for dir in $(find $topdir -type d | grep -vxF .); do
du -d0 -b $dir
done | sort $sorto | head -n $num | awk $awkc
;;
*)
;;
esac
IFS=$_IFS
@nishidy
Copy link
Author

nishidy commented Aug 17, 2017

user$ uname
Darwin
user$ cs
cs [OPTIONS] DIR
   ORDER BY OPTIONS:
   -f : file count
   -s : file size
   -m : file modification time
   -j : total size of file just under a directory
   -a : total size of file all under a directory
   -r : sort by ascending order (default is by descending order)
   -h : show in human readable format
   -nN : show the top N
user$ sudo cs /usr -f -h
Password:
13,350 /usr/share/man/man3
4,284 /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula
1,634 /usr/share/man/man1
1,537 /usr/local/Cellar/openssl/1.0.2l/share/man/man3
1,532 /usr/local/Cellar/openssl/1.0.2k/share/man/man3
1,065 /usr/bin
979 /usr/share/zsh/5.2/functions
659 /usr/share/man/mann
586 /usr/share/vim/vim74/syntax
556 /usr/share/emacs/22.1/lisp
user$ sudo cs /usr -s -h
149.9MB /usr/bin/emacs
 68.5MB /usr/share/tokenizer/ja/matrix.bin
 43.2MB /usr/share/tokenizer/ja/sys.dic
 32.7MB /usr/local/Cellar/rust/1.19.0/lib/rustlib/x86_64-apple-darwin/lib/librustc_llvm-b6a648c73a523bba.dylib
 32.7MB /usr/local/Cellar/rust/1.19.0/lib/librustc_llvm-b6a648c73a523bba.dylib
 24.7MB /usr/share/icu/icudt57l.dat
 24.6MB /usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/058-73656-019.dmg
 24.5MB /usr/share/langid/langid.inv
 18.6MB /usr/standalone/firmware/iBridge1_1Customer.bundle/Contents/Resources/058-73666-019.dmg
 17.3MB /usr/share/tokenizer/ko/sys.dic
user$ sudo cs /usr -m -h -r
1999年 10月 3日 日曜日 21時14分08秒 JST /usr/share/emacs/22.1/etc/BABYL
1999年 10月 3日 日曜日 21時16分55秒 JST /usr/share/emacs/22.1/etc/celibacy.1
1999年 10月 3日 日曜日 21時34分48秒 JST /usr/share/emacs/22.1/etc/emacs.icon
1999年 10月 3日 日曜日 21時39分42秒 JST /usr/share/emacs/22.1/etc/copying.paper
1999年 10月 5日 火曜日 19時44分22秒 JST /usr/share/emacs/22.1/lisp/forms-d2.dat
2000年 10月12日 木曜日 18時14分12秒 JST /usr/share/emacs/22.1/etc/gnus.pbm
2001年 1月18日 木曜日 02時50分37秒 JST /usr/share/emacs/22.1/lisp/README
2001年 3月26日 月曜日 21時24分57秒 JST /usr/share/emacs/22.1/etc/splash.pbm
2001年 12月18日 火曜日 09時03分59秒 JST /usr/share/emacs/22.1/etc/sex.6
2003年 2月 4日 火曜日 23時30分40秒 JST /usr/share/emacs/22.1/etc/LINUX-GNU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment