3

I have an issue I don't understand. It is simple and it should work, but it doesn't. =EDITED exactly what I can see from terminal= I have a list of filenames:

[molni@archlinux picasa_album]$ cat LIST
IMG_9282.JPG
IMG_9287.JPG
IMG_9300.JPG
IMG_9324.JPG
IMG_9329.JPG
IMG_9463.JPG
IMG_9412.JPG
IMG_9562.JPG
IMG_9511.JPG
IMG_9607.JPG

and want to search for every file in list it's path via find command:

[molni@archlinux picasa_album]$ for i in `cat LIST`; do find /mnt/c/e-m10/  -name "$i";  done
[molni@archlinux picasa_album]$ 

no results, when I exchange it for echo $i (to check if variable $i is OK, it works)

[molni@archlinux picasa_album]$ for i in `cat LIST`;do echo "$i" ;  done
IMG_9282.JPG
IMG_9287.JPG
IMG_9300.JPG
IMG_9324.JPG
IMG_9329.JPG
IMG_9463.JPG
IMG_9412.JPG
IMG_9562.JPG
IMG_9511.JPG
IMG_9607.JPG
[molni@archlinux picasa_album]$ 

when I do it manually, set variable (without loop) it works:

[molni@archlinux picasa_album]$ i=IMG_9607.JPG
[molni@archlinux picasa_album]$ find /mnt/c/e-m10/ -name "$i"        
/mnt/c/e-m10/IMG_9607.JPG
[molni@archlinux picasa_album]$ 

What am I doing wrong?

  • 1
    Should work. can you report output of file LIST – Sebastian Sep 19 '14 at 6:53
  • $ bash --version GNU bash, version 4.3.24(1)-release (x86_64-unknown-linux-gnu) Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. – molni Sep 19 '14 at 6:58
  • You seem not have copied the actual output, based on the variation in spaces before for and the $ in your result of find. Please update the question with copy and paste the complete sequence as appears in your terminal. – Anthon Sep 19 '14 at 6:58
  • It would also be more of a check if you checked with for i in cat LIST;do echo "$i" ABC; done as cat LIST would give the output you indicate as well. You could also have done for i in cat LIST; do echo find /mnt/c/e-m10/ -name "$i"; done and try one of the commands with copy and paste. – Anthon Sep 19 '14 at 7:02
  • yes output of file LIST is so long, it is truncated, so I just copy pasted them – molni Sep 19 '14 at 7:02
6

Do a cat -v LIST to see if there are any special characters that you don't see with a simple echo. I suspect DOS line endings, i.e. extraneous carriage returns before the newline.

EDIT: to convert the LIST file:

dos2unix < LIST > LIST.new && mv LIST.new LIST

Or if you don't have dos2unix, but do have vim: vim LIST, then :set notx, then :wq

  • [molni@archlinux picasa_album]$ cat -v LIST IMG_9282.JPG^M IMG_9287.JPG^M IMG_9300.JPG^M IMG_9324.JPG^M IMG_9329.JPG^M IMG_9463.JPG^M IMG_9412.JPG^M IMG_9562.JPG^M IMG_9511.JPG^M IMG_9607.JPG^M [molni@archlinux picasa_album]$ – molni Sep 19 '14 at 9:53
  • As I suspected (see the ^M attached to each name). – wurtel Sep 19 '14 at 10:04
  • @Hello Wurtel: this hint helped me a LOT, I set out those ^M endings via vim editor and it works now! :) But I have question - how it comes, that when I use only linux I have such endings in the LIST file? I use xfce4-terminal – molni Sep 19 '14 at 10:17
  • You don't show / tell how you created the LIST file so I can't answer your question. – wurtel Sep 19 '14 at 10:27
  • This answer helped me. In my case I had piped output from a grep that had colorized output. The filenames appear to have terminal color reset characters on the end: local-lambda-bucket.yml^[[m^[[K – vastlysuperiorman May 24 '18 at 22:41
0

The following works:

$ mkdir /tmp/test
$ cd /tmp/test
$ cat > LIST << EOD
> IMG_9324.JPG
> IMG_9329.JPG
> IMG_9463.JPG
> IMG_9412.JPG
> IMG_9562.JPG
> IMG_9511.JPG
> IMG_9607.JPG
> EOD
$ mkdir e
$ touch e/IMG_9607.JPG
$ touch e/IMG_9412.JPG
$ find .
.
./e
./e/IMG_9412.JPG
./e/IMG_9607.JPG
./LIST
$ for i in `cat LIST`; do find e/  -name "$i";  done
e/IMG_9412.JPG
e/IMG_9607.JPG
$ 

Therefore start from the above, then replace LIST with the real and then e/. If you are not reaching an internal Bash limit for the for loop (which should throw an error) you must be doing something else wrong which is not in your example output.

0

Cloud you provide a sample file listing in the folder /mnt/c/e-m10/ ?

In the meantime, could you try the below command ?

for i in `cat LIST`; do echo $i; find /mnt/c/e-m10/  -iname $i -print;  done

This will print any info as needed also, it ignores case sensitivity as well.

  • it works only for "echo" part [molni@archlinux picasa_album]$ for i in cat LIST; do echo $i; find /mnt/c/e-m10/ -iname $i -print; done IMG_9282.JPG IMG_9287.JPG IMG_9300.JPG IMG_9324.JPG IMG_9329.JPG IMG_9463.JPG IMG_9412.JPG IMG_9562.JPG IMG_9511.JPG IMG_9607.JPG [molni@archlinux picasa_album]$ – molni Sep 19 '14 at 9:56
  • listing in the related folder : [molni@archlinux picasa_album]$ ls -la /mnt/c/e-m10/ total 1784 drwxrwxrwx 1 molni users 0 Sep 19 09:53 . drwxrwxrwx 1 molni users 24576 Sep 3 21:52 .. drwxrwxrwx 1 molni users 262144 Aug 17 23:58 100OLYMP drwxrwxrwx 1 molni users 393216 Sep 12 13:41 101OLYMP drwxrwxrwx 1 molni users 393216 Aug 29 23:19 102OLYMP drwxrwxrwx 1 molni users 393216 Aug 12 19:16 103OLYMP drwxrwxrwx 1 molni users 344064 Sep 12 13:39 104OLYMP drwxrwxrwx 1 molni users 16384 Aug 31 23:08 upravene [molni@archlinux picasa_album]$ – molni Sep 19 '14 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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