Skip to content

Instantly share code, notes, and snippets.

@nishidy
Last active August 18, 2017 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishidy/6ab1d28f380d7b9d6809cfd3a291ee80 to your computer and use it in GitHub Desktop.
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 May 28, 2017

user:[~]$ which cs
/usr/bin/cs
user:[~]$ cs
cs [OPTIONS] DIR
   ORDER BY OPTIONS:
   -f : file count
   -s : file size
   -t : file modification time
   -j : total size of file just under a directory
   -r : total size of file recursively under a directory
   -nN : show the first N
user:[~]$ du -s --inodes /usr/
200905	/usr/
user:[~]$ time cs /usr/ -f
3295 /usr/share/app-install/desktop
2103 /usr/share/app-install/icons
1977 /usr/share/man/man3
1910 /usr/lib/x86_64-linux-gnu
1894 /usr/share/doc
1826 /usr/share/man/man1
1730 /usr/bin
1568 /usr/src/linux-headers-4.8.0-52-generic/include/config
1568 /usr/src/linux-headers-4.8.0-36-generic/include/config
1180 /usr/src/linux-headers-4.8.0-52/include/linux

real	0m31.259s
user	0m1.588s
sys	0m3.048s
user:[~]$ cs /usr/ -s
103581280 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
75839136 /usr/lib/thunderbird/libxul.so
72949536 /usr/lib/firefox/libxul.so
58131384 /usr/lib/libreoffice/program/libmergedlo.so
41587032 /usr/lib/x86_64-linux-gnu/libLLVM-3.8.so.1
40680536 /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37.14.10
39688256 /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitPluginProcess2
37857816 /usr/lib/x86_64-linux-gnu/libQt5WebKit.so.5.5.1
26068668 /usr/share/tegaki/models/zinnia/handwriting-ja.model
25913104 /usr/lib/x86_64-linux-gnu/libicudata.so.55.1
user:[~]$ cs /usr/ -t
1495976676 /usr/bin/cs
1495553004 /usr/lib/python3.5/xml/dom/__pycache__/xmlbuilder.cpython-35.pyc
1495553004 /usr/lib/python3.5/xml/dom/__pycache__/minidom.cpython-35.pyc
1495553004 /usr/lib/python3.5/xml/dom/__pycache__/minicompat.cpython-35.pyc
1495553004 /usr/lib/python3.5/xml/dom/__pycache__/domreg.cpython-35.pyc
1495553004 /usr/lib/python3.5/xml/dom/__pycache__/__init__.cpython-35.pyc
1495553004 /usr/lib/python3.5/xml/dom/__pycache__/NodeFilter.cpython-35.pyc
1495553004 /usr/lib/python3.5/json/__pycache__/scanner.cpython-35.pyc
1495553004 /usr/lib/python3.5/json/__pycache__/encoder.cpython-35.pyc
1495553004 /usr/lib/python3.5/json/__pycache__/decoder.cpython-35.pyc
user:[~]$ cs /usr/ -j
678711001 /usr/lib/x86_64-linux-gnu
188644591 /usr/lib/libreoffice/program
152053906 /usr/bin
126005804 /usr/share/fonts/opentype/noto
114280108 /usr/lib/x86_64-linux-gnu/dri
110737485 /usr/lib/thunderbird
100040982 /usr/lib/firefox
81083082 /usr/lib/gcc/x86_64-linux-gnu/5
43228891 /usr/share/mythes
39806264 /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0
user:[~]$ cs /usr/ -r
3277225709	/usr/
1846131747	/usr/lib
1059178007	/usr/share
1010776575	/usr/lib/x86_64-linux-gnu
248093867	/usr/lib/libreoffice
247811476	/usr/share/fonts
195975336	/usr/lib/libreoffice/program
188611511	/usr/src
152115347	/usr/bin
128825452	/usr/share/fonts/opentype
user:[~]$ cs /usr/ -s -n3
103581280 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
75839136 /usr/lib/thunderbird/libxul.so
72949536 /usr/lib/firefox/libxul.so

@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