巴什发现并在名称'[]'移动文件 [英] Bash find and move files with '[ ]' in name

查看:77
本文介绍了巴什发现并在名称'[]'移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bash脚本的一部分我做涉及RAR文件分割,然后将分割文件移动到另一个目录,一旦他们这样做。

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.

所以,如果我有一个像考file.txt的文件时,它会首先得到rarred为[测试] file.txt.part1.rar,测试file.txt.part2.rar,然后两个该RAR中的会得到移动到另一个目录。

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.

我有RAR位工作正常,但我有在查找和放大器麻烦;移动。

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

下面是我的脚本:

#!/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 "]"

你们有什么想的?

推荐答案

[和]壳用来描述字符集。你有逃脱他们的'\\',以获得正确的行为。如果你不逃避他们,你告诉发现,以查找't'或'E'或's'或'T'文件:)

'[' 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' :)

要做到这一点与参数$ 1,你必须使用类似:

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

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

和使用'$参数而不是$ 1

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

我的2美分

这篇关于巴什发现并在名称'[]'移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆