Skip to content

Instantly share code, notes, and snippets.

@horaciod
Created September 29, 2022 12:28
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 horaciod/8a43e4621aec7499f6733aa4b31acdd4 to your computer and use it in GitHub Desktop.
Save horaciod/8a43e4621aec7499f6733aa4b31acdd4 to your computer and use it in GitHub Desktop.
#!/bin/bash
num=10
short=0
timestyle='--time-style=locale'
#"+%d-%b-%Y%H:%M:%S "'
## number of files to print
## set to 1 for short listing
## GNU-specific time format
opts=Aadn:os
while getopts $opts opt
do
case $opt in
a|A|d) ls_opts="$ls_opts -$opt" ;;
n) num=$OPTARG ;;
o) ls_opts="$ls_opts -r" ;;
s) short=$(( $short + 1 )) ;;
esac
done
shift $(( $OPTIND - 1 ))
case $short in
0) ls_opts="$ls_opts -l -t" ;;
*) ls_opts="$ls_opts -t" ;;
esac
## options passed to ls
## number of files to display
## show oldest files, not newest
## long listing, use -l
## short listing, do not use -l
ls $ls_opts $timestyle "$@" | {
read
## In bash, the same as: IFS= read -r REPLY
case $line in
total*) ;;
## do not display the 'total' line
*) printf "%s\n" "$REPLY" ;;
esac
cat
} | head -n$num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment