Skip to content

Instantly share code, notes, and snippets.

@v2keener
Last active February 2, 2024 16:42
Show Gist options
  • Save v2keener/537250058bb7d46468175a9021b3f97f to your computer and use it in GitHub Desktop.
Save v2keener/537250058bb7d46468175a9021b3f97f to your computer and use it in GitHub Desktop.
list files with size as kilobytes
#!/usr/bin/bash
# "input field separator" (IFS) to newline (0x0a)
IFS=$'\n'
# for i in
# tail result of `ls -lt` except the first line
for i in $(ls -lt | tail -n $(echo "$(ls -lt | wc -l) - 1" | bc) )
do
size=$(echo "$(echo $i | awk '{print $5}') / 1024" | bc)
file=$(echo $i | awk '{print $9}')
echo "$size KB $file"
done
# IFS back to bash default of '[space][tab][newline]'
# For zsh this would be $' \t\n\0', as bash with '[null]' at the end
IFS=$' \t\n' # for bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment