0

edit:
I dont know you are a bot or a man who close this, but I DONT WANT TO FIND FILES WhICH HAVE SPACE IN THEIR NAMES. I WANT TO MOVE THEM, PLEASE STOP CLOSING THIS.

original:
I have a script that finds n number of files into a folder. It works perfectly until there is a file with space in it's name.

mv $(find . -maxdepth 1 -mindepth 1 -type f | sort | grep -m 10 '.*') ./myFolder

As it is understandable it try to reach every part of the name as a separate file.
Then I tried to fix it with replacing spaces:

mv $(find . -maxdepth 1 -mindepth 1 -type f | sort | grep -m 10 '.*' | tr " " "\ ") ./myFolder

Ans it didn't work neither, although when I hardcode one move it works just fine:

mv Screenshot\ from\ 2020-09-05\ 01-07-36.png ./Folder04

So I want to know what is wrong here?
Why tr command does not replace all spaces with \space?
And how can I fix this?

P.S: I read these links and they didn't help:
https://unix.stackexchange.com/questions/392393/bash-moving-files-with-spaces
mv a file that contains spaces from a shell script
moving a file with spaces in name
https://superuser.com/questions/295994/how-do-i-rename-files-with-spaces-using-the-linux-shell/295997

Pouria Moosavi
  • 662
  • 7
  • 22
  • 2
    This is [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29). `for item in $(anything)` shouldn't be used, _ever_. – Charles Duffy Nov 13 '20 at 18:32
  • 2
    Backslashes only matter when they're _shell syntax_, not when they're _data_. When you insert them with `sed`, they're _data_. – Charles Duffy Nov 13 '20 at 18:32
  • 2
    I am sad that the link from @CharlesDuffy does not mention `find -0 ... | xargs -0 ...`, which I think of as the canonical solution for this sort of task. – larsks Nov 13 '20 at 18:33
  • 2
    ...and whereas you may assert that you already read https://unix.stackexchange.com/questions/392393/bash-moving-files-with-spaces, you aren't showing _how_ its solution failed to help you; that solution is precisely on-point. – Charles Duffy Nov 13 '20 at 18:33
  • 1
    @larsks, I'd actually consider `find ... -exec ... {} +` better than the `find ... -print0 | xargs -0` approach. – Charles Duffy Nov 13 '20 at 18:34
  • 2
    @PouriaMoosavi, [Using Find](https://mywiki.wooledge.org/UsingFind) is a great place to start here -- it shows all the fixes we're talking about, including both `find ... -exec ... {} +` and the `find ... -print0 | xargs -0` approaches. – Charles Duffy Nov 13 '20 at 18:35
  • 1
    @PouraMoosavi, ...that said, don't just tell us you read the links, _show_ us the specific code you used _to try to use the answers in those links_, and the bugs you had when trying to use those answers. Saying that answer-X "didn't work" doesn't let us know _why_ it didn't work in enough detail to address the problem that was hit while trying to use it. – Charles Duffy Nov 13 '20 at 18:36
  • 1
    @PouriaMoosavi, re: the objections about the question being closed -- whether you're moving things doesn't matter -- the point is that we tell you how to _run an arbitrary command using filenames you found_, and the linked duplicates cover that; it doesn't change the practice you need to follow if the command you want to run is `mv` vs `cp` or `cat` or anything else. As a concrete example, `find /some/place -type f -name '*.csv' -exec mv -t /destination -- {} +` (if you have the GNU version of `mv`) will move CSV files in `/some/place` to `/destination`. – Charles Duffy Nov 13 '20 at 18:39
  • 1
    @PouriaMoosavi, ...if you actually followed the "Using Find" link and read the page behind it, you'd see sections about actions that run arbitrary commands against the files that were found. `mv`, in this context, is just another possible command; it follows the same rules as any other. – Charles Duffy Nov 13 '20 at 18:43

0 Answers0