Results 1 to 5 of 5

Thread: strip hyphens from all files in dir

  1. #1
    Join Date
    Sep 2007
    Location
    Santa Cruz, CA
    Beans
    92
    Distro
    Ubuntu 12.10 Quantal Quetzal

    strip hyphens from all files in dir

    I have a folder with about 5700 files. The files are all named numerically but there are multiple dashes in each name

    ie 978-1-234-5678-9.jpg

    I want to completely remove the hyphen from all the file names.

    I tried this but it doesn't appear to do anything.
    find media/images/ -type f -exec sed -i 's/-//g' {} \;


    Any ideas?

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: strip hyphens from all files in dir

    'sed' is for editing the contents of the found files - you can either use the 'mv' command within a 'find', or just use the 'rename' command e.g.

    Code:
    rename -n -v 's/-//g' *.jpg
    978-1-234-5678-9.jpg renamed as 978123456789.jpg
    (the '-n' means dry run - remove it once you are certain it's matching filenames correctly)

  3. #3
    Join Date
    Sep 2011
    Location
    Behind you!
    Beans
    1,690
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: strip hyphens from all files in dir

    Not 100% sure if this would work but try this:

    Code:
    for file in *.jpg
    do
      echo "${file}" "`echo ${file} | sed 's/-//g'`"
    done
    If that looks correct, change "echo" to "mv" to perform the rename.

    LHammonds

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: strip hyphens from all files in dir

    ^^^ if you want to use mv in a loop instead of a one-shot rename, you could use a builtin bash substitution directly instead of sed though:-

    Code:
    for file in *.jpg
    do 
      echo mv "${file}" "${file//-/}"
    done

  5. #5
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,699

    Re: strip hyphens from all files in dir

    Quote Originally Posted by ZenMasta View Post
    I have a folder with about 5700 files. The files are all named numerically but there are multiple dashes in each name

    ie 978-1-234-5678-9.jpg

    I want to completely remove the hyphen from all the file names.

    I tried this but it doesn't appear to do anything.
    find media/images/ -type f -exec sed -i 's/-//g' {} \;


    Any ideas?
    Aargh!!! Noooooo!!!
    That will corrupt all your images! It will remove the any '-' characters from the file contents. I hope you have a backup of all these images.

Bookmarks

Posting Permissions

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