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

9
  • 2
    This is BashPitfalls #1. for item in $(anything) shouldn't be used, ever. Commented Nov 13, 2020 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. Commented Nov 13, 2020 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. Commented Nov 13, 2020 at 18:33
  • 2
    ...and whereas you may assert that you already read unix.stackexchange.com/questions/392393/…, you aren't showing how its solution failed to help you; that solution is precisely on-point. Commented Nov 13, 2020 at 18:33
  • 2
    @PouriaMoosavi, Using Find 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. Commented Nov 13, 2020 at 18:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.