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 05-26-2008, 03:21 PM   #1
FahadOnline
LQ Newbie
 
Registered: May 2008
Posts: 3

Rep: Reputation: 0
how to change extensions for many files in other directory path


Hi, I've got to learn how to change the extensions for many files in the working directory using the following command
Code:
for x in *.ext1; do mv "$x" "${x%.ext1}.ext2"; done
but I want to know how to do the same job with specifying a path to another directory that contains the files I want to modify, in other words, I don't want to navigate to each directory using "cd" to run that command.

Hope someone here have an answer, I've been googling a lot about this but couldn't find it.

Thanks you in advance.

Last edited by FahadOnline; 05-27-2008 at 12:32 AM.
 
Old 05-26-2008, 05:42 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,898

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Perhaps something like this:
Code:
for x in /path/to/files/*.ext1; do mv "$x" "${x%.ext1}.ext2"; done
 
Old 05-27-2008, 12:36 AM   #3
FahadOnline
LQ Newbie
 
Registered: May 2008
Posts: 3

Original Poster
Rep: Reputation: 0
that worked like charm, but not with the first "/", I did it like this:

Code:
for x in path/to/files/*.ext1; do mv "$x" "${x%.ext1}.ext2"; done
Thank you very much for your help
 
Old 05-27-2008, 01:22 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
bigrigdriver's example uses an absolute path ie tells the cmd where to start in terms of the entire disk partition ie starting from the root dir.
Yours is a relative path ie it'll only work if that path exists in your current dir.
HTH
 
Old 07-16-2008, 03:40 PM   #5
alMubarmij
Member
 
Registered: Dec 2005
Posts: 140
Blog Entries: 1

Rep: Reputation: 16
Oh My God .. you just need like the following command to do this simple operator:
Code:
mv *.txt *.doc
And you can do it in the same way in M$ Windowz:
Code:
move *.txt *.doc
Or:
Code:
ren *.txt *.doc

Last edited by alMubarmij; 07-16-2008 at 03:46 PM.
 
Old 07-16-2008, 05:08 PM   #6
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Quote:
Originally Posted by Mubarak's View Post
Oh My God .. you just need like the following command to do this simple operator:
Code:
mv *.txt *.doc
mv *.txt *.doc will expand to:

mv a.txt b.txt *.doc

*.doc will be interpreted as a directory and fail.

Another ways to do this (my personal favorite):

ls /path/to/dir/*.ext1 | sed -e "s/^\(.*\).ext1/\1.ext1 \1.ext2/g" | xargs -n 2 mv -f

But alas, someone decided that was too much to bear, so in comes:

rename .ext1 .ext2 /path/to/dir/*.ext1

Hehe, hope this helps.
 
Old 07-17-2008, 03:40 AM   #7
alMubarmij
Member
 
Registered: Dec 2005
Posts: 140
Blog Entries: 1

Rep: Reputation: 16
Thank you,
You can also use rename instead of mv by the same method.

Quote:
Originally Posted by kenoshi View Post
Quote:
Originally Posted by Mubarak's ...
My name here is "alMubarmij" .. Why you changed it to "Mubarak" ?!
 
  


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
Change a string in all files in a directory? Randux Programming 2 05-17-2006 12:47 PM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 02:05 PM
Mozilla Directory Path Change: go_sooner Linux - Software 4 06-11-2005 02:21 AM
Change path of log files linuxboy69 Linux - Software 4 12-23-2003 02:17 PM
How to change the path of the current directory ? ndha Linux - General 7 12-03-2003 12:05 PM

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

All times are GMT -5. The time now is 09:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration