I want to replace all special characters (ie _?:/)(\\-\\n\\t)
in all pdf files' name by a space.
I wasn't able to do that so I just tried to remove them using this command.
For some reasons this command works sometimes for a test case, but not for others. It gives error mv: target is not a directory, and also input and target files' name are the same (if no special char to remove).
find . -name '*.pdf' -exec sh -c 'if="{}"; of=$(echo $if|tr -d '_:-' ); mv $if $of ;' \;
I expect to change filename:
a:bc-de_(fg).pdf
To be
a bc de fg .pdf
"mv: target is not a directory" is because of the spaces in the file name, and can be fixed by putting file name within double quotes.
Try this
find . -name '*.pdf' -exec sh -c 'if="{}"; of=$(echo $if|tr "()_:-" " " ); mv "$if" "$of" ;' \;
Alternatively you could use sed:
find . -name '*.pdf' sh -c 'mv "$0" "$(sed "s/[-_:()]/ /g" <<< $0)"' {} \;
sample files:
$ ls
'a:bc-de_(fg).pdf' 'f:sc-de_(fg).pdf' somefile
output files:
$ ls
'a bc de fg .pdf' 'f sc de fg .pdf' somefile
Just more efficient
find ~+ -regextype awk -iregex '.*/[^/]*[-_:()\t][^/]*\.pdf' -exec sh -c 'mv "{}" "`echo "{}"|sed -E "s/[-_:\(\)\t]+/ /g"`"' \;
also does rename such a:bc-_:d to 'a bc d'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.