Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 12 of 12
Hey guys, I want to thank all you of for your feedback and help. With the script below, I was able to sucessfully run and the changes were applied without ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6

    Some progress!!


    Hey guys,

    I want to thank all you of for your feedback and help. With the script below, I was able to sucessfully run and the changes were applied without moving the file location. EXCEPT there was only one problem; all the files with SPACE within their name were not replaced with a "_". That is if I had a file called "test one.txt"
    it is supposed to be replaced to "test_one.txt" however it didn't.

    any help on how I can add the a few more things within this script to replace the space with an underscrole. or if there is another scrip that I can run seperatly which will replace the file name without moving the location/path of the file.

    Thanks again.

    find . -type f | while read file; do
    dirname=$(dirname $file)
    echo "$dirname"
    oldfile=$(basename $file)
    echo "$oldfile"
    newfile=$(echo "$oldfile" | sed 's/[^A-Za-z0-9_.]/_/g')
    echo "$newfile"
    if [ ! "$newfile" = "$oldfile" ]
    then mv "$file" "$dirname/$newfile"
    fi
    done

  2. #12
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    I would add \s inside the sed statement to take care of spaces, but I think most of the problem is with how find pipes to sed when there are spaces in the file name, i'm not sure how to alleviate it

Page 2 of 2 FirstFirst 1 2

Posting Permissions

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