I have the code below. If I want to loop through PKA1 through PKA24, how do i do that, without manually typing it all out
cd PKA1/1keV/
rm cascade.dump
cd ../../
I'd consider using something like this
rm PKA{1..24}/1keV/cascade.dump
bash.
The first thing is that you don't have to be in a directory to remove the file, so you could just say:
rm PKA1/1keV/cascade.dump
rm PKA2/1keV/cascade.dump
etc. However, a loop can be used:
for i in $(seq 24); do
rm PKA$i/1keV/cascade.dump
done
(the seq command generates the number 1 to 24 automatically)
rm PKA{1..24}/1keV/cascade.dump. or find . -name cascade.dump -type f -delete.
bash also applies here. The find solution is a bit dangerous, as there might be other subdirectories containing cascade.dump that you don't want to delete.
'find' is your answer:
First use it with 'echo' to check out that everything will work correctly. I suppose that you don't care about the same filename in more PKA directories.
find PKA* -name "cascade.dump" -exec rm {} \;
Otherwise you have to fix the first pattern.
PKA99 (if it exists) whereas the OP as specified only PKA1..PKA24. Your -exec rm{} \; kicks off an instance of rm for every file. While there are only 24 it's not a big deal but for a large number it files it could be. You'd be better with -delete if it exists for your find version, which doesn't fork any separate processes.