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. |
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.
|
you do not need script, there is a command rename to do that.
|
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.
|
Depends which "rename" is installed.
|
Quote:
|
Tell me about it - bloody annoying actually ... :rolleyes:
|
Quote:
|
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).
|
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" "*.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. |
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=" Code:
for i in $list; do Code:
for i in $list; do Code:
for i in $list; do |
Can do this with parallel
Code:
parallel 'echo {1} {1.}.mp3; echo {2} {2.}.mkv' ::: *mp3* ::: *mkv* |
Perl rename (when rename script not installed :/)
Code:
cd /dir/of/files |
Quote:
|
and one more solution can be:
Code:
for f in *.mp3*; do |
Hi ,
You can use the following script. I have tested it. Let me know if it not works for your scenario. ______ #!/bin/bash cd [mp3/mkv files directory] for i in `ls -A` do real=`echo $i | awk -F '.' '{print $1"."substr($2,RSTART-3,3)}'` mv $i $real done ______ Regards, Agnel |
Warning about any script using "mv": make sure to at least use the flag "-i" with mv. By default, mv will merrily overwrite the target if it already exists, with no warning or even any output telling you that it has done so.
As a result, using "mv" in a script without "-i" is a great way to accidentally squash all of your files except one. Or more troubling, any name collision will result in some lost files, without it being obvious that this has happened. By including the "-i" flag, a name collision will result in mv interactively asking you whether or not to overwrite. The result will likely be a mess to clean up...some files renamed but others not yet renamed. But that's easily preferable to silent data loss. |
I agree but on this particular case (downloaded mp3 and mkv with added text on extension), I would think it's a safe bet to assume that names are unique (movie title or audio song title). I mean OP issue looks like more a quick fix than a long term repeatable solution
|
I would not think it's a safe bet. The example given was "guitar.mp3?1234yhh", which may be a completely made up name, but it might also indicate that some of the mp3 files may be very common names (such as names of files from some sort of download?).
Also, there could be subfolders involved, where a slip up could result in files from different folders going to the same destination folder. Depending on how subtracks are named, that could mean a LOT of name collisions. |
yes, I know you're right. But what if the Op prefers a fast quick n' dirty rename and doesn't mind overwriting files than being asked the same question again and again, I mean he didn't bother naming the files in the first place so what is the importance of guitar.mp3?1234yhh, guitar.mp3 etc
Edit: rewriten perl code to add some uniqueness Code:
# guitar.mp3?1234yhh => guitar?1234yhh.mp3 etc |
The original is pretty short as it is. It's mostly comments / POD:
Code:
#!/usr/bin/perl -w |
All times are GMT -5. The time now is 04:10 AM. |