0
#!/bin/bash
for f in *.{jpg,png,webm,mp4}; do mv "$f" "$(sed 's/[^0-9A-Za-z_.]/_/g' <<< "$f>; done

Currently using this script to turn all of the specified files of that extension and strip their special characters, but I'm wondering if it's possible to then pipe that and filter the useless output (namely the warning that a file was skipped due to already having no special characters).

I know I can use > /dev/null 2>&1 to throw everything away, but is it possible to filter only certain output which matches a pattern/keyword and then remove only those lines from output? Or, inversely, only filter lines which match a pattern/string.

2
  • you didn't get the whole script...
    – user986805
    Apr 1, 2020 at 16:29
  • @bac0n Sorry lol, copied it wrong.
    – Joshua
    Apr 1, 2020 at 16:37

1 Answer 1

0

You could use a more specific shell glob for file selection - and use the same glob inside a parameter expansion (avoiding the call to sed altogether). Ex.

#!/bin/bash

shopt -s nullglob globasciiranges

for f in *[^0-9A-Za-z_.]*.{jpg,png,webm,mp4}; do 
  echo mv -n -- "$f" "${f//[^0-9A-Za-z_.]/_}"
done

(the globasciiranges option so as to avoid surprises with collation order).

You might want to take a look at the Perl-based rename command instead though.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .