LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script for rename files. (https://www.linuxquestions.org/questions/linux-newbie-8/script-for-rename-files-4175589381/)

hack3rcon 09-14-2016 12:52 AM

Script for rename files.
 
Hello.
I have many files with .MP3 and .mkv suffix but after .MP3 and .mkv they have other characters. For example, guitar.mp3?1234yhh, How can I write a script that rename my files to a correct suffix?

Tnx.

grail 09-14-2016 01:01 AM

What have you tried and where are you stuck? Searching both on this site and google would yield a plethora of results to aid any solution you require.

pan64 09-14-2016 02:38 AM

you do not need script, there is a command rename to do that.

Turbocapitalist 09-14-2016 06:41 AM

Yes, "rename" is what you want, unless you really want to write your own script. Just be sure to use it with the -n option while figuring out the right patterns to use. As far as the patterns go, they are normal perl expressions, so anything perl works. You may have already encountered perl patterns in the form of PCRE.

syg00 09-14-2016 07:03 AM

Depends which "rename" is installed.

Turbocapitalist 09-14-2016 07:10 AM

Quote:

Originally Posted by syg00 (Post 5605085)
Depends which "rename" is installed.

Thanks. I've only encountered the perl-based one. I see now that there is another one but notice upon inspection that it seems weak in comparison.

syg00 09-14-2016 07:13 AM

Tell me about it - bloody annoying actually ... :rolleyes:

keefaz 09-14-2016 07:14 AM

Quote:

Originally Posted by Turbocapitalist (Post 5605086)
Thanks. I've only encountered the perl-based one. I see now that there is another one but notice upon inspection that it seems weak in comparison.

Yes the binary version is just a tool to do quick basic rename. It's still useful to do padding zeros filenames, fix file extension etc

schneidz 09-14-2016 07:50 AM

i would hack around the file command to help print out the file type (maybe the -i argument to have it print out the mime-type).

IsaacKuo 09-14-2016 10:17 AM

Another command which you can use for such tasks is "mmv". To strip off everything after .mp3, you'd use:

Code:

mmv -v "*.mp3*" "#1.mp3"
-v flag means "verbose". This lets you see what it's doing.

"*.mp3*" matches any file with ".mp3" in it, with two wildcards. Those wildcards will get mapped to #1 and #2.

"#1.mp3" means the contents of the first wildcard (#1) plus ".mp3". The contents of the second wildcard (#2) are ignored.

Note that this means there is a possibility of a name collision. But mmv has a built in safety check that will warn you if there is a name collision. If it detects a name collision, it will do nothing to the files.

teckk 09-14-2016 02:01 PM

Lots of ways to do that. To the OP, you have 886 posts. Why haven't you studied even a little bash and regex?

Some examples:
Code:

list="
guitar.mp3?1234yhh
guitar.mkv?1234yhh
house.mp312345
house.mkv54321
"

Code:

for i in $list; do
    if grep -q .mp3 <<< $i; then
        echo ${i%.*}.mp3
    elif
        grep -q .mkv <<< $i; then
        echo ${i%.*}.mkv
    fi
done

OR
Code:

for i in $list; do
    if [[ $i == *".mp3"* ]]; then
        echo ${i%.*}.mp3
    elif
        [[ $i == *".mkv"* ]]; then
        echo ${i%.*}.mkv
    fi
done

OR
Code:

for i in $list; do
    case $i in
        *.mp3*) echo ${i%.*}.mp3
        ;;
   
        *.mkv*) echo ${i%.*}.mkv
        ;;
    esac
done


Sefyir 09-14-2016 02:47 PM

Can do this with parallel

Code:

parallel 'echo {1} {1.}.mp3; echo {2} {2.}.mkv' ::: *mp3* ::: *mkv*

keefaz 09-14-2016 02:48 PM

Perl rename (when rename script not installed :/)
Code:

cd /dir/of/files
perl -e 'map {rename $_, s/(\.m..).+/$1/r} grep {/\.mp3.+|\.mkv.+/} <*>'


keefaz 09-14-2016 02:54 PM

Quote:

Originally Posted by Sefyir (Post 5605268)
Can do this with parallel

Code:

parallel 'echo {1} {1.}.mp3; echo {2} {2.}.mkv' ::: *mp3* ::: *mkv*

Nice, I have to install this :)

pan64 09-15-2016 01:50 AM

and one more solution can be:
Code:

for f in *.mp3*; do
    mv $f ${f%%mp3*}mp3
done
# not tested

(and similar to mkv)


All times are GMT -5. The time now is 04:23 AM.