0

In my folder, filenames are like this:

A00001
A00002
.
.
.
A08525

I want to add this file name as an argument:

max = 8525
for i in `seq 2 $max`
do
    python script.py -r filename 
done

I tried like this:

max = 8525
for i in `seq 2 $max`
do
    python script.py -r "A0000$i" #But this work for 1-9 like wise for A000 it will work for 100-999
done

I can not do

for file in A*; 
do 
    echo "$file"; done
done

Because the folder has the same file with three different extensions, like A0000.txt, A0000.pdf, A0000.png, and I want to input only A0000 as an argument.

How can I write the for loop so that I can read all filenames?

5
  • How about for file in A*; do echo "$file"; done? Commented Feb 26, 2019 at 14:10
  • @MichaWiedenmann: Thanks, this I though but actually folder has same file with three different extensions. like A0000.txt A0000.pdf A0000.png and I want to input only A0000 without extension Commented Feb 26, 2019 at 14:14
  • max=8525; spaces are not allowed. Commented Feb 26, 2019 at 14:40
  • Possible duplicate of stackoverflow.com/questions/54844645/… Commented Feb 26, 2019 at 15:56
  • Your examples have five digits but then a lot of the rest of the discussion shows four. Which is it? Commented Feb 26, 2019 at 16:03

3 Answers 3

3

You can use printf to pad a number with leading zeros.

for ((i=1; i<=8525; ++i)); do
    printf -v filename "A%05i" "$i"
    python script.py -r "filename"
done

Of course, if the base filenames already exist, you can do

for f in A[0-9][0-9][0-9][0-9][0-9].png; do
    filename=${f%.png}
    python script.py -r "$filename"
done

which neatly avoids hardcoding the upper limit.

Sign up to request clarification or add additional context in comments.

1 Comment

If you want only four digits after A, obviously use "A%04i" for the format string instead.
1

Most(?) versions of seq will support outputting 0-padded values.

max=8525
for i in $(seq -w 2 $max); do
  python script.py -r "A$i"
done

Alternately, you can simply loop over the existing files and strip the extension.

for f in A*.txt; do
  f=${f%.txt}
  python script.py -r "$f"
done    

2 Comments

I want to use it without file extention, I directly used f but that also comes with file extension. Any clue?
f=${f%.txt} should remove the extension.
1
for file in $(find /path/to/your/files -type f -name "A?????");do python script.py -r "$file"; done

5 Comments

please check my update question. Thanks for your answer
I tried for file in $(find training2017/ -type f -name "A?????"); do #python script.py -r echo "$file"; done but it did not print anything
@Jhon how many digits are after "A" in the filenames ? , if it's 4, just change the file name pattern to "A????"
Why did you put training2017/ in the find command? Nothing else here mentions a subdirectory like that.
@tripleee: All files are in training2017 directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.