LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-10-2013, 03:09 AM   #1
kentfoo
LQ Newbie
 
Registered: Jan 2013
Posts: 6

Rep: Reputation: Disabled
Question rename files in terminal


when I use win7, I can rename files like this:
C:\>rename *.bak *.txt
how to do this in linux?
------------------------
kent$mv *.bak *.txt
it does not work.
 
Old 01-10-2013, 03:42 AM   #2
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 879

Rep: Reputation: 113Reputation: 113
I'm sure there is more than 1 way to do it but I do it like this

Quote:
$ mv thisFile.txt thatfile.txt
for documentation on mv
Quote:
$ man mv
the $ represents a user shell.
 
Old 01-10-2013, 03:52 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,317

Rep: Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008
You want to change the extension of a group of files? It isn't that simple as it is in WinDos:

Code:
for i in *.bak
    do mv -- "$i" "${i%.bak}.txt"
done

Last edited by NevemTeve; 01-10-2013 at 03:53 AM.
 
1 members found this post helpful.
Old 01-10-2013, 04:06 AM   #4
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 223

Rep: Reputation: 62
To rename all *.bak files to *.txt, execute the following command:

rename .bak .txt *.bak
 
2 members found this post helpful.
Old 01-10-2013, 04:21 AM   #5
kentfoo
LQ Newbie
 
Registered: Jan 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jmc1987 View Post
I'm sure there is more than 1 way to do it but I do it like this



for documentation on mv


the $ represents a user shell.
not only one file...so I have to mv mv mv mv....

Quote:
Originally Posted by NevemTeve View Post
You want to change the extension of a group of files? It isn't that simple as it is in WinDos:

Code:
for i in *.bak
    do mv -- "$i" "${i%.bak}.txt"
done
it works! thx!


Quote:
Originally Posted by rosehosting.com View Post
To rename all *.bak files to *.txt, execute the following command:

rename .bak .txt *.bak
perfect! very cool.
 
Old 01-10-2013, 05:13 AM   #6
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 879

Rep: Reputation: 113Reputation: 113
Quote:
Originally Posted by kentfoo View Post
not only one file...so I have to mv mv mv mv....
Sorry I misread your question. Bash script would be easiest but for fun and games I wrote a poopy perl script for practice.

Code:
#!/usr/bin/perl
use strict;
use warnings;

print "Directory Path: ";
my $dir_path=<>;
print "Old extention: ";
my $oldext=<>;
print "New Extenstion: ";
my $newext=<>;
chomp ($dir_path, $oldext, $newext);
my $dir;
my @files;
opendir $dir,$dir_path || die "could not open $dir_path $!\n";
while (readdir $dir){
    if ($_ =~ m/$oldext$/ and ! -d "$dir_path/$_"){
	push @files, $_;
    }
}
closedir $dir;

for (@files){
    my $newname = (split(/\./,$_))[0].$newext;
    unless (rename "$dir_path/$_", "$dir_path/$newname"){
	warn "$_ File could not be renamed \"$!\"\n";
    }
}
Needs some tweaking though.

Last edited by jmc1987; 01-12-2013 at 06:24 AM.
 
Old 01-11-2013, 06:34 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Please try doing a forum search before you post common questions like this. There must be hundreds of previous threads discussing how to bulk-rename files.
 
Old 01-11-2013, 06:44 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by rosehosting.com View Post
To rename all *.bak files to *.txt, execute the following command:

rename .bak .txt *.bak
Maybe it's worth to mention that there are two versions of the rename command. The one mentioned here is usually available on RPM based systems and come from the util-linux-ng package. The syntax is
Code:
rename from to [files]
where from and two are the matching and replacement strings respectively. The other one is usually available on DEB based systems and it is a perl script that uses regular expressions and sed-like commands to perform its task:
Code:
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
In this case the correct command line would be:
Code:
rename 's/\.bak$/.txt/' *.bak
 
  


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
best way to rename files graphicsmanx1 Programming 27 12-13-2012 02:53 AM
Rename mutiple image files through terminal fortran General 2 02-08-2012 10:29 AM
Application to eliminate doubles in files and rename changed files with date? S. Chapelin Linux - Software 6 01-16-2011 03:02 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
get files from ftp and rename those files.. amaulana Linux - Newbie 2 11-15-2008 11:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:44 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