0
testingfilesizecheck.sh: line 6: /media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/*

I am using the command for f in "/media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/*"

If I am very specific about it and say "/media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/ABRA.bin" then it will work.

I am new to linux (Ubuntu) and bash, and I am struggling using google-fu to diagnose this. Every example site I can find suggests what I am doing with a wildcard should work, but they keep their examples in the same directory as the terminal / bash script is actively working in.

Before making this post, I did craft a workaround using ls which felt very roundabout because I make up my list of files in the folder and kind of go "By hand" instead of using the for loop as a succinct command. So I am looking for an answer academically why the wildcard doesn't expand in this case.

Edit: I tried the suggestion of putting the asterisk outside the double quote. But it didn't work either. Here is what I have AS OF RIGHT NOW POST-RESPONSES:

This is the copy-paste of my terminal after entering one line "bash testingfilesizecheck.sh" and hitting enter:

doctor@doctor-X570-AORUS-ELITE:~/Documents$ bash testingfilesizecheck.sh
/media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/*
testingfilesizecheck.sh: line 24: /media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/*: No such file or directory

The line 24 comes from commenting out all the other attempts I've made in the testinfilesizecheck.sh script. Here is that script minus the commented out lines.

#!/bin/bash

for f in "/media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/"*
    do 
    echo "$f"
    actualsize=$(wc -c < "$f")
done
    echo $actualsize

Again, there is a file at /media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/ABRA.bin

But the wildcard is not expanding to accept ABRA.bin. In both variations it is just a literal asterisk. I thought, yeah, asterisk as a variable makes sense that it can't be in a quoted string based on my rudimentary understanding of other programming languages. But of course it doesn't work for me.

2
  • Edit the question and add the output that you are actually getting from the command and what you want to happen. The asterisk does expand with what you have but gives each file and subdirectory as one space separated line whereas placing the asterisk outside of the double quotes give each as a separate line. Commented Dec 26, 2022 at 16:17
  • What is the directory different to? Commented Dec 26, 2022 at 19:21

2 Answers 2

2

The double quotes prevent the wildcard * ("glob") from being expanded to match files. Instead it's treated as a literal asterisk character.

Use this instead, where the wildcard is unquoted:

for f in '/media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category'/*

The important part here is that items containing spaces or other special characters are quoted, and the wildcard is not. You could equally use any of these variants (paths shortened for readability):

'/media/…/Binary House/Final Structure Directory/Category'/*
'/media/…/Binary House/Final Structure Directory/Category/'*
/media/…/'Binary House/Final Structure Directory'/Category/*
/media/…/Binary' 'House/Final' 'Structure' 'Directory/Category/*

Note that unless you have set nullglob (shopt -s nullglob) a situation where there are no matches will result in your having a single iteration through your for loop with a path that does include a literal asterisk character. If you can't set nullglob then guard against the "false" expansion with something like this as the first line of the loop:

[ -e "$f" ] || continue 
3
  • The first half was the answer. (I will have to study what nullglobing means..) Again I had to be ready to pick up details. Not only is the asterisk outside of the closing quotation mark, but the / is too. This differs from @Arkadiusz Drabczyk's answer which kept the / inside the ". See the OP edit where I tried .../"* and it failed. But once I reread this answer by roaima closer and tried the ..."/*, it works!
    – Tim50001
    Commented Dec 27, 2022 at 15:46
  • 1
    @Tim50001 it makes no difference whether literal characters are inside or outside the quotation marks. Both "/.../"* and "..."/* are acceptable and equivalent Commented Dec 27, 2022 at 16:18
  • It didn't work if I put the final / inside the quote. Maybe Ubuntu is weird or it's the nature of the active directory being in a different partition from where the directory I wanted to work with was, but trust me, that flipping where that / is mattered. When I am back from holiday I can rerun the script, changing only the two characters around, and show that the output is different.
    – Tim50001
    Commented Dec 31, 2022 at 21:36
1

Put * after a closing quote:

for f in "/media/doctor/123A123E1A123456/Users/Tim/Downloads/PokemonRed/Binary House/Final Structure Directory/Category/"*; do echo f == "$f"; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .