LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-14-2016, 01:52 AM   #1
hack3rcon
Member
 
Registered: Jan 2015
Posts: 928

Rep: Reputation: Disabled
Post 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.
 
Old 09-14-2016, 02:01 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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.
 
Old 09-14-2016, 03:38 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,120

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
you do not need script, there is a command rename to do that.
 
Old 09-14-2016, 07:41 AM   #4
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 970
Blog Entries: 3

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
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.
 
Old 09-14-2016, 08:03 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Depends which "rename" is installed.
 
Old 09-14-2016, 08:10 AM   #6
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 970
Blog Entries: 3

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
Quote:
Originally Posted by syg00 View Post
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.
 
Old 09-14-2016, 08:13 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Tell me about it - bloody annoying actually ...
 
Old 09-14-2016, 08:14 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by Turbocapitalist View Post
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
 
Old 09-14-2016, 08:50 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
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).
 
Old 09-14-2016, 11:17 AM   #10
IsaacKuo
Senior Member
 
Registered: Apr 2004
Location: Baton Rouge, Louisiana, USA
Distribution: Debian 8 Jessie
Posts: 1,688
Blog Entries: 5

Rep: Reputation: 163Reputation: 163
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.
 
2 members found this post helpful.
Old 09-14-2016, 03:01 PM   #11
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,825

Rep: Reputation: 221Reputation: 221Reputation: 221
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
 
Old 09-14-2016, 03:47 PM   #12
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 386

Rep: Reputation: 158Reputation: 158
Can do this with parallel

Code:
parallel 'echo {1} {1.}.mp3; echo {2} {2.}.mkv' ::: *mp3* ::: *mkv*
 
1 members found this post helpful.
Old 09-14-2016, 03:48 PM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Perl rename (when rename script not installed :/)
Code:
cd /dir/of/files
perl -e 'map {rename $_, s/(\.m..).+/$1/r} grep {/\.mp3.+|\.mkv.+/} <*>'
 
Old 09-14-2016, 03:54 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by Sefyir View Post
Can do this with parallel

Code:
parallel 'echo {1} {1.}.mp3; echo {2} {2.}.mkv' ::: *mp3* ::: *mkv*
Nice, I have to install this
 
Old 09-15-2016, 02:50 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,120

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
and one more solution can be:
Code:
for f in *.mp3*; do
    mv $f ${f%%mp3*}mp3
done
# not tested
(and similar to mkv)
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script Rename files linux2man Linux - Newbie 3 02-10-2015 07:38 PM
Rename files using script vadslogin Linux - Newbie 6 01-31-2012 06:46 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
Rename files with script sharathkv25 Programming 14 06-25-2007 04:00 AM


All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration