Created
May 1, 2009 03:53
-
-
Save hirose31/104861 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # SYNNOPSIS | |
| # ---------------- | |
| # leave_n_files N PATTERN | |
| # leave_n_dirs N PATTERN | |
| # return PATTERN matched files/directories but the latest(mtime) N files/directories. | |
| # EXAMPLE | |
| # ---------------- | |
| # for i in $(leave_n_dirs 7 '/var/backup/db/2???????'); do # y3k problem | |
| # echo rm -fr "$i" | |
| # done | |
| # | |
| # leave_n_files 3 '/var/backup/db/*.dmp' | ( IFS=''; \ | |
| # while read -r i; do | |
| # echo rm -f "$i" | |
| # done) | |
| _leave_n_objs() { | |
| n=$1 | |
| pat="$2" | |
| type=$3 | |
| [ $# = 3 ] || { echo "missing args" 1>&2; return 1; } | |
| target_dir=${pat%/*} | |
| filename_pat=${pat##*/} | |
| find "$target_dir" -maxdepth 1 -name "$filename_pat" -a -type "$type" -print0 | \ | |
| xargs -r0 ls -trd | \ | |
| head -n -${n} | |
| } | |
| leave_n_files() { _leave_n_objs "$@" 'f'; } | |
| leave_n_dirs() { _leave_n_objs "$@" 'd'; } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment