Find the answer to your Linux question:
Results 1 to 8 of 8
Hi Friends, I have many pdf files which contain "%" sign also in the name. I want to rename that all files by replacing "%" to "-" Its hierarchy of ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    22

    Renaming Multiple Files

    Hi Friends,
    I have many pdf files which contain "%" sign also in the name. I want to rename that all files by replacing "%" to "-" Its hierarchy of many files and folders. Is there any solution to do this at one time? OR any script for this?

    Thanks & Regards
    Parag Nehete

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,101
    Code:
    find <PDF_DIRECTORY> -type f -print0 | while read -r -d '' SOURCE ; do DIRECTORY=$(dirname ${SOURCE}); FILENAME=$(basename ${SOURCE}|sed s/%/-/g);echo "OLD : ${SOURCE}   --  NEW : ${DIRECTORY}/${FILENAME}"; done
    Replace <PDF_DIRECTORY> with the actual path

    If the output looks good, then replace the echo with a mv, and of course get rid of the text part
    Last edited by Irithori; 06-21-2011 at 09:31 AM.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    22

    Works

    Thank you very much sir......

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,101
    yw.
    I just noticed, I missed to filter the files more closely via an additional argument to find: -iname "*.pdf"
    But seems to be ok already
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Apr 2011
    Posts
    22
    Quote Originally Posted by Irithori View Post
    yw.
    I just noticed, I missed to filter the files more closely via an additional argument to find: -iname "*.pdf"
    But seems to be ok already

    Sorry sir by mistake i post this reply. Actually i want to give it for another post. I try the above script, also by adding -iname "*.pdf" but it just echoing the names. Nothing chang in the file names.

  6. #6
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,101
    Yep.
    First test, then go live.
    Need to remind my management of that principle every day

    The oneliner was written to be non-destructive, so it just outputs what the names would look like.

    That´s why I have written:
    If the output looks good, then replace the echo with a mv, and of course get rid of the text part
    You must always face the curtain with a bow.

  7. #7
    Just Joined!
    Join Date
    Apr 2011
    Posts
    22
    Yes sir it works......but now i have another problem. Some files contain ' sign also. How can i rename that also by replacing ' to -

  8. #8
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,101
    With almost the same line.
    Modify the sed part accordingly, and protect the ' from being interpreted by the shell by encapsulating the expression in "
    You can also extend it to match multiple chars to be replaced with - by using the OR operator and a group.
    You must always face the curtain with a bow.

Posting Permissions

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