Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 2 lines of code

1) With the following code:

for i in `ls *.properties`; do cat $i; done

I get the error:

cat: file_name.properties: No such file or directory.

2) On the other hand:

for i in *.properties; do cat $i; done

Works fine!

I thought both were the same. Can someone help me understand the difference between the two? Using bash shell.

share|improve this question
2  
does your filename contain a space? –  Karoly Horvath May 15 '12 at 20:50
    
No it does not. Names are separated by underscores. –  Win Man May 15 '12 at 20:52
    
Please show an actual example. It doesn't have to be your real file names if you don't want to share, but something that demonstrates the problem you're seeing would be helpful. –  Greg Hewgill May 15 '12 at 20:53
2  
The bare glob is correct anyway. You should never do for i in `ls`. It might also be helpful to do if [[ -f $i ]]; then cat "$i"; fi to make sure you're only getting files and not directories. Note also that the variable should be quoted as I have done. –  Dennis Williamson May 15 '12 at 21:04
2  
@khaled_webdev: That brings up lots of things, some incorrect. Can you be more specific what to search for on that page? –  Dennis Williamson May 15 '12 at 21:07

2 Answers 2

up vote 6 down vote accepted

What does the following command print?

cat -v <<< `ls *.properties`

I guess the problem is, that ls is a strange alias, e.g. something like

ls='ls --color'

Edit: I have seen this before. The alias should be: alias ls='ls --color=auto'

share|improve this answer
    
alias | grep ls –  Karoly Horvath May 15 '12 at 20:55
    
Thank you nosid. There was an alias exactly as you mentioned. Appreciate it. –  Win Man May 15 '12 at 21:04
    
Yes. Your "Edit"ed answer worked for me. –  Win Man May 15 '12 at 21:23
    
@KarolyHorvath: Or even shorter: alias ls –  l0b0 May 16 '12 at 14:49

Most probably there is a directory which matches *.properties. Then ls will output the files in this directory without the directory name. Then the cat will not find the given filename.

So please check, whether file_name.properties is in the actual directory or in some subdirectory.

EDIT

To reproduce the problem you can try this:

# cd /tmp
# mkdir foo.properties
# touch foo.properties/file_name.properties
# for i in `ls *.properties`; do cat $i; done
cat: file_name.properties: No such file or directory
share|improve this answer
    
nice idea but in this case he would first see cat: directoryname:: No such file or directory. (note the double colon) –  Karoly Horvath May 15 '12 at 21:03
    
@KarolyHorvath you won't, because ls dir will output the contents of the directory but not the text dir itself. Hence cat wont see this name and cannot produce the output you proposed. –  A.H. May 15 '12 at 21:05
    
@Karoly, neat, I've never noticed that before; cat: rc0.d:: No such file or directory. :) –  sarnold May 15 '12 at 21:05
    
@A.H.: that's because you did the ls with only one argument. –  Karoly Horvath May 15 '12 at 22:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.