Skip to content

Instantly share code, notes, and snippets.

@KebabLord
Last active November 24, 2020 20:16
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 KebabLord/14c3e9ab66f1d721c29837042c435e5c to your computer and use it in GitHub Desktop.
Save KebabLord/14c3e9ab66f1d721c29837042c435e5c to your computer and use it in GitHub Desktop.
list recent files from folders. Useful for recent downloaded files and newly taken screenshots
#!/bin/bash
# Usage: ./recent.sh <parameters> [FOLDER]
SS_FOLDER="$HOME/Pictures/Screenshots/"
DL_FOLDER="$HOME/Downloads/"
list_amount=1
sorter="tail"
while getopts "h?n:rsd" opt; do
case "$opt" in
r) sorter="head"
;;
n) list_amount=$OPTARG
;;
s) custom_path=$SS_FOLDER
;;
d) custom_path=$DL_FOLDER
;;
\?) echo "Try './recent -h' for more information."
exit 0
;;
h) printf "Usage: ./recent.sh <parameters> [FOLDER]
List recent or first files of a folder.\n
-r Revert sorting and list first files instead
-n List last 'n' files
-s Set path to screenshot folder
-d Set path to your downloads folder\n
" | tr -d '\t'
exit 0
;;
esac
done
shift $((OPTIND-1))
folder_path="$*"
if [ -n "$custom_path" ];then
folder_path="$custom_path"
elif [ -z "$folder_path" ];then
folder_path="./"
elif [ "${folder_path: -1}" != "/" ];then
folder_path="${folder_path}/"
fi
ls -Art "$folder_path" |
$sorter -n "$list_amount" |
xargs -L 1 -I % echo "${folder_path}"%
@KebabLord
Copy link
Author

KebabLord commented Nov 23, 2020

$ recent -h
Usage: ./recent.sh <parameters> [FOLDER]
List recent or first files of a folder.

-r    Revert sorting and list first files instead
-n    List last 'n' files
-s    Set path to screenshot folder
-d    Set path to your downloads folder

$ recent -sn 3
/home/owo/Pictures/Screenshots/2020-11-22_00-23.png
/home/owo/Pictures/Screenshots/2020-11-22_00-26.png
/home/owo/Pictures/Screenshots/2020-11-23_14-22.png

$ recent -rd 
/home/owo/Downloads/i3-4.18.3.tar.gz

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