Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I've got to learn how to change the extensions for many files in the working directory using the following in command line Code: for x in *.ext1; do mv ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    8

    Exclamation 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 in command line
    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 wods, I don't want to navigate to each directory using "cd" to run that command.

    Hope someone here have the answer, I've been googling very much for this but couldn't find it.

    Thanks you in advance.

  2. #2
    Just Joined!
    Join Date
    Dec 2007
    Posts
    12
    The easiest way would be to take an optional command line argument, leading you to something like this:

    Code:
    if [ -n "$1" ]; then
    
    cd "$1"
    fi for x in *.ext1; do
    mv "$x" "${x%.ext1}.ext2"
    done

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    8

    Thumbs up

    Thank you very much for your reply

    unfortunately, I'm not an expert in these commands, just I need to know where I have to put the path of the directory.

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    if you just wanna know where to specify the directory path it will be as follows
    Code:
    for x in /path/to/directory/*.ext1; do mv "$x" "/path/to/directory/${x%.ext1}.ext2"; done
    /path/to/directory is the path to the folder you wanna run the command from.
    Linux and me it's a love story

  5. #5
    Just Joined!
    Join Date
    May 2008
    Posts
    8
    this has worked for me
    Code:
    for x in path/to/files/*.ext1; do mv "$x" "${x%.ext1}.ext2";
    done

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...