1

When I do

echo $filename

I get

Pew Pew.mp4

However,

echo "${#filename}"

Returns 19

How do I delete all characters after the file extension? It needs to work no matter what the file extension is because the file name in the variable will not always match *.mp4

baruch
  • 13
  • 4
  • How do you know these extra characters are part of the file extension? (And if they are, are you supposed to throw them away?) Anyway, (1) are some of the characters actually UTF8 characters in that name? (see http://stackoverflow.com/a/31009961/2564301), and (2) if not, you'd better check your hard disk. – Jongware Dec 21 '15 at 09:28
  • 1
    Good question. They aren't UTF8 characters in the name. When I create a new file with $filename.conf as the name, it makes a file like this: Pew Pew.mp4??[23;0t.conf. I don't know what those characters are, but I don't think it's my hard disk. These are the file names that youtube-dl --get-filename returns – baruch Dec 21 '15 at 09:48
  • Ouch - looks like some batch rename command ran amok. Perhaps it's possible with `sed` to snip off everything after a first not-ASCII character. – Jongware Dec 21 '15 at 09:51
  • See http://stackoverflow.com/questions/8562354/remove-unicode-characters-from-textfiles-sed-other-bash-shell-methods for ways of removing utf-characters – Fredrik Pihl Dec 21 '15 at 10:33
  • *Pew Pew.mp4??[23;0t* looks like you have an embedded ASCII escape that was copied as part of the filename. What OS are you using, and how did you generate the filename to begin with? – David C. Rankin Dec 22 '15 at 21:49

1 Answers1

0

You should try to find out why you have such strange files before fixing it. Once you know, you can rename files.
When you just want to rename 1 file, just use the command

mv "Pew Pew.mp4"* "Pew Pew.mp4"

Cutting off the complete extension (with filename=${filename%%.*}) won't help you if you want to use the stripped extension (mp4 or jpg or ...). EDIT: I think OP want a work-around so I give another try. When you have a a short list of extensions, you can try

for ext in mpeg mpg jpg avo mov; do
   for filename in *.${ext}*; do
      mv "${filename%%.*}.${ext}"* "${filename%%.*}.${ext}"
   done
done

You can try strings to get the readable string.

    echo "${filename}" | strings | wc  
# Rename file
    mv "${filename}" "$(echo "${filename}"| strings)"

EDIT: strings gives more than 1 line as a result and unwanted spaces. Since Pew Pew has a space inside, I hope that all spaces, underscores and minus-signs are in front of the dot.
The newname can be constructed with something like

tmpname=$(echo "${filename}"| strings  | head -1)
newname=${tmpname% *}

# or another way  
newname=$(echo "${filename}"| sed 's/[[:alnum:]_- ]*\.[[:alnum:]]*\).*/\1/')

# or another (the best?) way (hoping that the first unwanted character is not a space)
newname="${filename%%[^[:alnum:]\.-_]*}"
# resulting in
mv "${filename}" "${filename%%[^[:alnum:]\.-_]*}"
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • Thanks for your help. echo "${filename}" | strings returns the following 2 lines: "Pew Pew.mp4"; "[23;0t". I don't know where this extra text is coming from, but I'm trying to create a file called "Pew Pew.mp4.conf". Every time I run this scripts, $filename will contain the name of a different file I'm downloading and I'm making .conf files for each of them. – baruch Dec 21 '15 at 18:38
  • When you send the output through `head -1` you will have Pew Pew. – Walter A Dec 22 '15 at 07:23
  • I tried that and when I tried to name it "Pew Pew.mp4.conf" I still got "Pew Pew.mp4 .conf". Any idea why that space shows up? – baruch Dec 22 '15 at 12:50
  • I do not have your input so I can not test my solutions in my edited answer. Give it another try! – Walter A Dec 22 '15 at 20:39