LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-15-2016, 09:26 AM   #16
nisagnel
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2

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
 
Old 09-15-2016, 09:48 AM   #17
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
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.
 
3 members found this post helpful.
Old 09-15-2016, 09:58 AM   #18
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
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
 
Old 09-15-2016, 10:52 AM   #19
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
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.
 
Old 09-15-2016, 12:09 PM   #20
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
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
cd /dir/of/downloaded/files
perl -e 'map {rename $_, s/(\.m..)(.+)/$2$1/r} grep {/\.mp3.+|\.mkv.+/} <*>'

Last edited by keefaz; 09-15-2016 at 12:21 PM.
 
Old 09-15-2016, 03:15 PM   #21
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 970
Blog Entries: 3

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
The original is pretty short as it is. It's mostly comments / POD:

Code:
#!/usr/bin/perl -w
#
#  This script was developed by Robin Barker (Robin.Barker@npl.co.uk),
#  from Larry Wall's original script eg/rename from the perl source.
#
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.
#
# Larry(?)'s RCS header:
#  RCSfile: rename,v   Revision: 4.1   Date: 92/08/07 17:20:30 
#
# $RCSfile: rename,v $$Revision: 1.5 $$Date: 1998/12/18 16:16:31 $
#
# $Log: rename,v $
# Revision 1.5  1998/12/18 16:16:31  rmb1
# moved to perl/source
# changed man documentation to POD
#
# Revision 1.4  1997/02/27  17:19:26  rmb1
# corrected usage string
#
# Revision 1.3  1997/02/27  16:39:07  rmb1
# added -v
#
# Revision 1.2  1997/02/27  16:15:40  rmb1
# *** empty log message ***
#
# Revision 1.1  1997/02/27  15:48:51  rmb1
# Initial revision
#

use strict;

use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($verbose, $no_act, $force, $op);

die "Usage: rename [-v] [-n] [-f] perlexpr [filenames]\n"
    unless GetOptions(
        'v|verbose' => \$verbose,
        'n|no-act'  => \$no_act,
        'f|force'   => \$force,
    ) and $op = shift;

$verbose++ if $no_act;

if (!@ARGV) {
    print "reading filenames from STDIN\n" if $verbose;
    @ARGV = <STDIN>;
    chop(@ARGV);
}

for (@ARGV) {
    my $was = $_;
    eval $op;
    die $@ if $@;
    next if $was eq $_; # ignore quietly
    if (-e $_ and !$force)
    {
        warn  "$was not renamed: $_ already exists\n";
    }
    elsif ($no_act or rename $was, $_)
    {
        print "$was renamed as $_\n" if $verbose;
    }
    else
    {
        warn  "Can't rename $was $_: $!\n";
    }
}

__END__

=head1 NAME

rename - renames multiple files

=head1 SYNOPSIS

B<rename> S<[ B<-v> ]> S<[ B<-n> ]> S<[ B<-f> ]> I<perlexpr> S<[ I<files> ]>

=head1 DESCRIPTION

C<rename>
renames the filenames supplied according to the rule specified as the
first argument.
The I<perlexpr> 
argument is a Perl expression which is expected to modify the C<$_>
string in Perl for at least some of the filenames specified.
If a given filename is not modified by the expression, it will not be
renamed.
If no filenames are given on the command line, filenames will be read
via standard input.

For example, to rename all files matching C<*.bak> to strip the extension,
you might say

        rename 's/\.bak$//' *.bak

To translate uppercase names to lower, you'd use

        rename 'y/A-Z/a-z/' *

=head1 OPTIONS

=over 8

=item B<-v>, B<--verbose>

Verbose: print names of files successfully renamed.

=item B<-n>, B<--no-act>

No Action: show what files would have been renamed.

=item B<-f>, B<--force>

Force: overwrite existing files.

=back

=head1 ENVIRONMENT

No environment variables are used.

=head1 AUTHOR

Larry Wall

=head1 SEE ALSO

mv(1), perl(1)

=head1 DIAGNOSTICS

If you give an invalid Perl expression you'll get a syntax error.

=head1 BUGS

The original C<rename> did not check for the existence of target filenames,
so had to be used with care.  I hope I've fixed that (Robin Barker).

=cut
Fortunately it is included in many distros and easily added to the others.
 
  


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:34 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