0

my problem is that i have to count the numbers of files whit a common part of the name (Stefano) in a folder (Name) and for every name i want to echo the name with the number of count's. So names of the files in the folder will be like:

Stefano.A1
Stefano.B2
Stefano.H3 

The problem i have is how to echo the files.Because it's giving me the error:

ls Stefano* | wc -l: syntax error in expression (error token is "Stefano* | wc -l")  

here's the script

i=1

while [ $i -le $(ls Stefano* | wc -l) ]; do
      echo "Stefano*${i}"
      (i++)
done        
2
  • i'm trying to make the expression $i -le $(ls Stefano* | wc -l) go
    – steT
    Commented Jan 13, 2015 at 14:38
  • check my answer, it should make it go.
    – buydadip
    Commented Jan 13, 2015 at 15:23

4 Answers 4

1

find prints out the names for you :)

find -name "*Stefano*"
3
  • find is an epic command, I posted an answer using find's regex but this is much cleaner!
    – ShellFish
    Commented Jan 13, 2015 at 14:36
  • 1
    I don't think OP wants to use find command... he said himself that he is trying to make $i -le $(ls Stefano* | wc -l) work for his script
    – buydadip
    Commented Jan 13, 2015 at 15:27
  • You are right. But indeed the code just looked like he wants to print out the names ("The problem i have is how to echo the files."). so he could use find and if needed its -exec builtin.
    – Marc Bredt
    Commented Jan 13, 2015 at 15:29
0

I understand what you want now... you want to make $i -le $(ls Stefano* | wc -l) work for your while loop, so do the following:

i=1
while [ $i -le $(cd $1 && ls Stefano* | wc -l) ]
do
    echo "Stefano*${i}"
    ((i++))
done

I added cd so that you can execute the script from whatever directory you are in, without actually moving you from your current directory.

0
0

In BASH you can do:

i=1
for f in Stefano*; do
   echo "${f}${i}"
   ((i++))
done
0

How about something more simple using a for loop. Something like

for file in Stephano*; do (i++); done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.