Bash find and move files with '[ ]' in name

主宰稳场 提交于 2019-12-12 11:55:56

问题


Part of a bash script I'm making involves rar splitting files and then moving the split files to another directory once they are done.

So if I have a file like "test file.txt", it would first get rarred to "[test] file.txt.part1.rar", "test file.txt.part2.rar", and then both of the rar's would get moved to another directory.

I have the rar bit working fine, but i'm having trouble on the find & move.

Here's my script:

#!/bin/bash

# [...]    

rar a -m0 -v104857600b "$1.rar" "$1";

find $folder -name "$1.part*" -exec mv {} $someotherfolder \;

However it doesn't seem to be working. I've tested that find one liner from the shell and I'm guessing the problem is because the files have parenthesis in the names -> "[" and "]"

What do you guys think?


回答1:


'[' and ']' are used in shell to describe sets of characters. You have to escape them with '\' to get the correct behavior. If you do not escape them you tell find to look for files with 't' or 'e' or 's' or 't' :)

To do that with the parameter $1, you have to use something like :

param=$(echo $1 | sed 's@\[@\\[@g'| sed 's@\]@\\]@g')

and use '$param' instead of '$1'

my 2 cents



来源:https://stackoverflow.com/questions/6648112/bash-find-and-move-files-with-in-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!