Moving multiple files having spaces in name (Linux

2019-03-01 04:59发布

I have a directory which contains multiple files with spaces in their names. I want to find a pattern in the name and those file will be moved to some other directory. Now the problem is that when the particular pattern is found in a single file name, that file is moving to the destination path but when there are multiple files this method fails. Below is the code that I'm using :

for file in `find . -maxdepth 1 -name "*$pattern*xlsx" -type f`
do
 mv "$file" $destination/
done

2条回答
成全新的幸福
2楼-- · 2019-03-01 05:17

No need to use a loop:

find . -maxdepth 1 -name "*$pattern*xlsx" -type f -exec mv {} $destination +
查看更多
淡お忘
3楼-- · 2019-03-01 05:25

Working fine with following code

find . -maxdepth 1 -name "*$pattern*xlsx" -type f -print0 | xargs -I{} -0 mv {} "$destination/"
查看更多
登录 后发表回答