0

Please give me any shell bash script that will help me doing following task:

if last added 4 file size of a directory are same with each other then exit other wise continue

Example :

ls -l $dir_path | awk '{print $5}' | tail -4

if the 4 printed values are same to each other then exit otherwise continue.

Samiul
  • 1

2 Answers2

1
zsh -c 'zmodload zsh/stat
  [[ $(zstat -N +size -- *(.om[1,4])) =~ $'\''(.*)\n\\1\n\\1\n\\1'\'' ]]' && exit

Would exit if the 4 newest non-hidden regular files in the current directory all have the same size.

On a GNU system, you could also do:

find . -maxdepth 1 ! -name '.*' -type f -printf '%T@ %s\n' |
  sort -rn |
  awk 'NR == 1 {v = $0}; v != $0 {exit}; NR == 4 {exit 1}' || exit

POSIXly:

ls -tnq -- "$dir_path" |
  awk '!/^-/ {next}
       n++ == 0 {v = $5}
       v != $5 {exit}
       n == 4 {exit 1}' || exit

If like in your own approach, instead of the 4 newest ones, you want the last 4 (regardless of whether they're regular files or symlinks or sockets...) in the ls output (which is an alphabetically sorted list), you can do (still POSIXly):

ls -rnq -- "$dir_path" |
  awk 'NR == 1 {next}
       NR == 2 {v = $5}
       v != $5 {exit}
       NR > 4 {exit 1}' || exit
0

Use ls -c to sort by ctime, and uniq to see if they're the same.

ls -crntq | tail -4 | awk '{print $5}' | uniq -c | grep -q "^\s*4\s" && exit
JigglyNaga
  • 7,886