Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi all, I'm totally new to unix and the unix scripting. I am to rename all the files within a directory (which contains multiple subdirectories) recursively without invalid characters. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6

    Trying to rename multiple files in multiple directories/subdirectories recursively


    Hi all, I'm totally new to unix and the unix scripting.
    I am to rename all the files within a directory (which contains multiple subdirectories) recursively without invalid characters.

    I tried the coding posted above.

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



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


    but I get an error on both of them stating
    "find: bad option -printf
    find: [-H | -L] path-list predicate-list"

    PLEASE HELP...need to solve this asap..


    Thanks

  2. #2
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    -printf isn't a valid switch/option for print. Check out the find manpage.

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6
    Okay, when I run

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

    it gives me no new results. The changes are not affected in the actual physical folder, however in the unix command prompt I see the changes have been done but not in the actual file title is not.

    any ideas?

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    from the man page
    Code:
           -fprintf file format
                  True; like -printf but write to file like -fprint.   The  output
                  file  is always created, even if the predicate is never matched.
                  See the UNUSUAL FILENAMES  section  for  information  about  how
                  unusual characters in filenames are handled.
    Code:
           -print True; print the full file name on the standard output,  followed
                  by  a  newline.    If  you  are  piping  the output of find into
                  another program and there is the faintest possibility  that  the
                  files  which you are searching for might contain a newline, then
                  you should seriously consider using the -print0  option  instead
                  of  -print.   See  the UNUSUAL FILENAMES section for information
                  about how unusual characters in filenames are handled.

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6

    Renaming multiple files within multiple directories but they are moved to the root

    Hi everyone,

    So I am successfull at using the script below to change all the file names which contain invalid characters (such as #, : etc) but once the changes are made all the files are moved to the root of the directory. I want to rename and replace the current file without moving the files location. With this query I will be updating about 8 millions files within about 100's of folders and I DEFINATELY DON'T WANT ALL THE FILES TO THE ROOT AFTER THE CHANGES.

    Please help. Thanks

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

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    you are taking just the basename of the file, which is just the filename, you need the full path, you can either get the path using dirname command, or take out the basename call

  7. #7
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6
    Hey, I am totally new to lunix/unix scripting.
    Infact this is my first script ever. I have a folder
    in my root directory called 'cggmaximo' which contains
    another folder called 'test' and that contains few folders
    called 't1' 't2' 't3'. In those t1, t2, t3 folders contains files
    which have invalid characters as their file name.
    I am totally confused of how I can add the dirname command within my already script. What my goal is to rename the original file name without the invalid characters and keeping it at the same location. Keeping it at the same location is very critical.

    this is the path to the files: (<root>-<cggmaximo>-<test>-(<t1> and <t2> and <t3>) (those 3 folders contain several files)

    I tried removing the basename but then it gives me no results.
    Please if you can help me.

    Thanks in advance

  8. #8
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Code:
    #!/bin/bash
    
    for file in `find . -type f`
    do
        oldfile=$(basename $file)
        dirname=$(dirname $file)
        newfile=$(echo "$oldfile" | sed 's/[^A-Za-z0-9_.]/_/g')
        if [ ! "$newfile" = "$oldfile" ]
        then
            echo "$file" "$dirname/$newfile"
        fi
    done
    if this is to your satisfaction, replace echo with mv

  9. #9
    Just Joined!
    Join Date
    Aug 2010
    Posts
    6

    still not working

    when I try to run the scrip you suggested it says:

    "Usage: basename string [ suffix ]
    Usage: dirname [ path ]"

    I tried playing with the query to change the quotes around but still coudln't get it to work.

  10. #10
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    works perfectly for me on my machine, try oldfile=`basename $file` and dirname=`dirname $file`

Page 1 of 2 1 2 LastLast

Posting Permissions

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