Bash find and transfer files named "[]"

The part of the bash script that I am doing includes splitting rar files and then moving the split files to another directory upon completion.

So, if I have a file like "test file.txt", it will first be torn to "[test] file.txt.part1.rar", "test file.txt.part2.rar", and then both rar will move to another directory.

My RAR bit is working fine, but I have problems finding and moving.

Here is my script:

#!/bin/bash # [...] rar a -m0 -v104857600b "$1.rar" "$1"; find $folder -name "$1.part*" -exec mv {} $someotherfolder \; 

However, it does not seem to work. I tested that I found one liner from the shell, and I assume that the problem is that the files have brackets in the names → "[" and "]"

What do you guys think?

+4
source share
1 answer

'[' and ']' are used in the shell to describe character sets. You must avoid them with "\" in order to get the correct behavior. If you do not escape them, you will say find files with "t" or "e" or "s" or "t" :)

To do this with the $ 1 parameter, you should use something like:

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

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

my 2 cents

+4
source

All Articles