Ask Your Question
0

I need a Bash Script to delete every second file in a directory

asked 2023-04-24 22:08:53 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-24 22:09:40 +0000

qstack gravatar image

updated 2023-04-25 10:52:34 +0000

The following script does the job:

#!/bin/bash
counter=0
for file in *
do
   # Do not delete the script itslef
   if [[ "$(basename -- "$0")" == "$file" ]]; then
      echo "\$file is the filename of the current script."
      continue
   fi
  ((counter++))
  if (( $counter % 2 == 0 ))
  then
    rm "$file"
  fi
done
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

1 follower

Stats

Asked: 2023-04-24 22:08:53 +0000

Seen: 16 times

Last updated: Apr 25 '23