Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've tried every combination of backslash and asterisk and curly brackets, nothing seems to work. I'm using the terminal in OS X and I would really appreciate it if someone could steer me straight.

share|improve this question
    
What do you mean? Are you changing the " into ', and/or removing the .flac? Are you trying to make this a bulk command, or just this one file? –  evan.bovie May 22 '12 at 1:02
    
My humble thanks to STATUS__ACCESS. This started as Windows not liking the " on a Mac OS X networked drive, probably due to some changes in Lion. I figured (wrongly) that this would be an opportunity to learn more about BASH scripting with a quick rename. –  zoltanzylox May 22 '12 at 3:52

2 Answers 2

up vote 0 down vote accepted

You could use $() in order to task tr to do that conversion, then strip the file extension in a second step. It would look something similar to:

# Say FNAME is the variable that stores your original file name
# Convert the double quotes to single quotes:
SQFNAME=$(echo $FNAME|tr '""' "''")
# or, as an alternative in pure Bash (mind the shell character escaping):
SQFNAME=echo ${FNAME//\"/\'}
# then simply strip the extension
NEWFNAME=${SQFNAME%.flac}
# or, if you wanted to remove the extension delimited by the _last_ dot
NEWFNAME=${SQFNAME%.*}
# or, if you wanted to remove the extension delimited by the _first_ dot
NEWFNAME=${SQFNAME%%.*}

The "new" converted file name will be in the NEWFNAME variable now.


Proof of concept:

#!/usr/bin/env bash
FNAME="\"A\" Train.flac"
echo "Unchanged name: $FNAME"
SQFNAME=${FNAME//\"/\'}
echo "Converted quotes: $SQFNAME"
NEWFNAME=${SQFNAME%.flac}
echo "New file name: $NEWFNAME"

Execution yields:

$ bash test.sh
Unchanged name: "A" Train.flac
Converted quotes: 'A' Train.flac
New file name: 'A' Train
share|improve this answer

I would use the prename Perl script that comes with most Perl installations (linked to rename in e.g. Debian based distributions, but on e.g. Fedora another rename is present, so research the case first if you are not sure, or just use prename as below):

prename 's/"/'\''/g' \"A\"\ Train.flac

The right tool for the right job.

To get a literal ', I first stop the current quoting environment, then escape a ', then start the quoting environment again.


I guess you don't really want to strip the file name extension and the question is just ambiguously worded, but if you do:

prename 's/"/'\''/g; s/\..*$//' \"A\"\ Train.flac

or

prename 's/"/'\''/g; s/\.flac$//' \"A\"\ Train.flac

Use the -n switch to just view the changes without applying them if you want to test the commands beforehand.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.