LinuxQuestions.org
Help answer threads with 0 replies.
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, 02: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, 02:42 AM   #2
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
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, 02:52 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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 02:53 AM.
 
1 members found this post helpful.
Old 01-10-2013, 03:06 AM   #4
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
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, 03: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, 04:13 AM   #6
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
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 05:24 AM.
 
Old 01-11-2013, 05:34 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 05:44 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 01:53 AM
Rename mutiple image files through terminal fortran General 2 02-08-2012 09:29 AM
Application to eliminate doubles in files and rename changed files with date? S. Chapelin Linux - Software 6 01-16-2011 02:02 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
get files from ftp and rename those files.. amaulana Linux - Newbie 2 11-15-2008 10:14 PM

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

All times are GMT -5. The time now is 08:31 PM.

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
Open Source Consulting | Domain Registration