Reputation: 3216
I have files of below name format.
test_1_452161_987654321.ARC
test_1_452162_987654321.ARC
test_1_452163_987654321.ARC
.
.
.
test_1_452190_987654321.ARC
i.e i need to delete 30 files in the above case where user will give the below inputs
Start_File = 452161
End_File = 452190
How to delete the above series files? (i am new to bash/shell programming i tried using while loop and remove which dint work so seeking experts help)
My Code
echo "Enter Start File"
read start
echo "Enter End File"
read end
i=$start
j=$end
while [$i -le $j]; do
rm *$i*.ARC
((i++))
done
Upvotes: 0
Views: 287
Reputation: 87074
Just to add to your options, you can use seq
:
echo "Enter Start File"
read start
echo "Enter End File"
read end
for i in $(seq $start $end)
do
rm -f *_${i}_*.ARC
done
To me this seems clean and clear.
Upvotes: 0
Reputation: 9
You may be interested in expanding a sequence additional documentation. The code below will echo the file names you are interested in based on the provided start and end input.
echo -n "Enter Start File => "; read start
echo -n "Enter End File => "; read end
for file in *{$start..$end}*
do
echo $file
done
EDIT: Variable expansion works in zsh but not bash
Upvotes: 0
Reputation: 88583
Replace everything after fourth line by
for ((i=$start; i<=$end; i++)); do
echo rm *_${i}_*.ARC
done
If everything looks fine, remove echo
.
Upvotes: 1
Reputation: 785128
You can use awk + xargs
:
printf "%s\n" test*ARC | awk -v s=452161 -v e=452190 -F_ '$3 >= s && $3 <= e' | xargs rm
awk is splitting file names on _
and then checking 3rd field is within start/end range.
Upvotes: 1