1

Running the following script:

for i in $(find dir -name "*.jpg"); do
  ln -s $i
done

incredibly makes symbolic links for 90% of the files and makes of a copy of the remaining 10%. How's that possible?

Edit: what's happening after is relevant:

Those are links to images that I rotate through mogrify e.g.

mogrify -rotate 90 link_to_image

It seems like mogrify on a link silently makes a copy of the image, debatable choice, but that's what it is.

memecs
  • 7,196
  • 7
  • 34
  • 49
  • Do any filenames have spaces/other whitespace? – user000001 Jan 13 '14 at 13:22
  • They don't: Sun2ndCapture-10-S4908-2-00008.jpg -> ../Input/Sun2ndCapture-10-S4908-2/Sun2ndCapture-10-S4908-2-00008.jpg – memecs Jan 13 '14 at 13:23
  • @memecs: That is a symlink actually – Igor Chubin Jan 13 '14 at 13:24
  • @Igor, yes there are 90% of them. – memecs Jan 13 '14 at 13:27
  • How look like the other 10%? – Igor Chubin Jan 13 '14 at 13:30
  • @IgorChubin they are files copy – memecs Jan 13 '14 at 13:42
  • can you please repeat the experiment in a new fresh and clean directory and post the result to pastebin or something like that? – Igor Chubin Jan 13 '14 at 13:44
  • sure, I'll do it, this is never happened to me, I am really curios to know what's wrong – memecs Jan 13 '14 at 13:50
  • I would start with a fresh directory and then by putting an `echo` before the call of `ln`: `echo "ln -s $i"`. Maybe that reveals something strange. If that does not help, I would remove the `echo` but put a `read` after call of `ln` to pause until [enter] is pressed, then I would check the directory for the strange occurrence again after each `ln`. – Alfe Jan 13 '14 at 13:50
  • This question appears to be off-topic because it is about a ghost (the effects of a a simple bug whose true reason was not even mentioned in the original post). – Alfe Jan 13 '14 at 14:24
  • @Alfe if I knew the reason I wouldn't have asked right? – memecs Jan 13 '14 at 14:26
  • Right. I don't blame you (except maybe for the statement that `ln` must be the culprit and not telling us that something else -- mogrify -- was involved), but actually I just mark this question as not helpful for other people because nobody will have that same problem. No hard feelings, but this is a Q&A site which wants to keep mostly those questions other people might have in the future. – Alfe Jan 13 '14 at 14:34
  • @memecs: Just to fix the result: I've updated my answer and have written about your beautiful discovery. The rest of the answer is devoted to the processing of files with spaces in their names. Thank you once again and +1 for the interesting question. – Igor Chubin Jan 13 '14 at 15:10
  • @Alfe No problem, your opinion. It's certainly interesting for many people working in image processing, and they are a lot. – memecs Jan 13 '14 at 15:27

3 Answers3

3

Skip the first paragraph if you want to know more about processing of files with spaces in the names

It was not clear, what is the root of the problem and our assumption was that the problem is in the spaces in the filenames: that files that have them are not processed correctly.

The real problem was mogrify that applied to the created links, processed them and changed with real files.

No about spaces in filenames.

Processing of files with spaces in their names

That is because of spaces in names of the files. You can write something like this:

find dir -name \*.jpg | while IFS= read i
do
  ln -s "$i"
done

(IFS= is used here to avoiding stripping of leading spaces, thanks to @Alfe for the tip).

Or use xargs.

If it is possible that names contain "\n", it's better to use print0:

find dir -name \*.jpg -print0 | xargs -0 -N1 ln -s

Of course, you can use other methods also, for example:

find dir -name '*.jpg' -exec ln -s "{}" \;
ln -s "$(find dir -name '*.jpg')" .
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • How does that explain that there were copies made? The only writing command is that `ln -s …` and that should _never_ create a copy of anything. – Alfe Jan 13 '14 at 13:47
  • Furthermore you should use `while IFS= read i` to avoid stripping of leading spaces and of course you need double quotes at the `ln -s "$i"`. – Alfe Jan 13 '14 at 13:52
  • Found the problem, see my answer – memecs Jan 13 '14 at 14:08
  • @memecs: Perfect! Congrats!! Thank you for an interesting question!! – Igor Chubin Jan 13 '14 at 14:14
1

(Imagemagick) mogrify applied on a link delete the link and makes a copy of the image

memecs
  • 7,196
  • 7
  • 34
  • 49
0

Try with single quotes:

find dir -name '*.jpg' -exec ln -s "{}" \;
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97