LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to change extensions for many files in other directory path (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-change-extensions-for-many-files-in-other-directory-path-644913/)

FahadOnline 05-26-2008 02:21 PM

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.

bigrigdriver 05-26-2008 04:42 PM

Perhaps something like this:
Code:

for x in /path/to/files/*.ext1; do mv "$x" "${x%.ext1}.ext2"; done

FahadOnline 05-26-2008 11:36 PM

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

chrism01 05-27-2008 12:22 AM

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

alMubarmij 07-16-2008 02:40 PM

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

kenoshi 07-16-2008 04:08 PM

Quote:

Originally Posted by Mubarak's (Post 3216767)
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.

alMubarmij 07-17-2008 02:40 AM

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

Quote:

Originally Posted by kenoshi (Post 3216879)
Quote:

Originally Posted by Mubarak's ...

My name here is "alMubarmij" .. Why you changed it to "Mubarak" ?!


All times are GMT -5. The time now is 12:15 PM.