• rename help under Linux

    From Sean Dennis@618:618/1 to All on Thu Dec 19 22:42:26 2024
    Hello everybody!

    I am trying to find a way under Linux to take files of various names and extentions in a single directory to sequential filenames and not change the extensions.

    After some research on the interwebz, part of the script is functioning:

    === Cut ===
    ## Rename files
    cd ./pics
    for i in *.*; do
    new=$(printf "%04d.*" "${a}") #04 pad to length of 4
    mv -i -- "$i" "$new"
    ((a++))
    done
    === Cut ===

    This works but it assigns "*" as the file extension.

    Could I try using "find" instead?

    I know there are solutions (renameutils, the "File::Rename" Perl code) but they don't work for my needs.

    -- Sean

    ... Adultery is the application of democracy to love.
    --- GoldED+/LNX 1.1.5-b20240209
    * Origin: Outpost BBS * Johnson City, TN (618:618/1)
  • From Tiny@618:618/12 to Sean Dennis on Fri Dec 20 08:02:00 2024
    Hi Sean,
    In a message to All you wrote:

    I am trying to find a way under Linux to take files of various names
    and extentions in a single directory to sequential filenames and not change the extensions.

    If you want to do this in pascal check out the code that Nick and someone
    else (you?) helped me with. I think you could probably modify that in no
    time to do what you want.

    Shawn

    ... A truly wise man never plays leapfrog with a unicorn.


    --- Grumble
    * Origin: Dirty Ole' Town (618:618/12)
  • From Arelor@618:250/24 to Sean Dennis on Fri Dec 20 09:35:43 2024
    Re: rename help under Linux
    By: Sean Dennis to All on Thu Dec 19 2024 10:42 pm

    Hello everybody!

    I am trying to find a way under Linux to take files of various names and extentions in a single directory to sequential filenames and not change the extensions.

    After some research on the interwebz, part of the script is functioning:

    === Cut ===
    ## Rename files
    cd ./pics
    for i in *.*; do
    new=$(printf "%04d.*" "${a}") #04 pad to length of 4
    mv -i -- "$i" "$new"
    ((a++))
    done

    Quick one in ksh, try it for filenames that have no special characters and a single extension (ie no .tar.gz)

    i=0
    for file in *; do
    extension=`echo "$file" | cut -d . -f 2`
    mv "$file" $i.$extension
    eval let i++
    done


    --
    gopher://gopher.richardfalken.com/1/richardfalken
    --- SBBSecho 3.23-Linux
    * Origin: Palantir * palantirbbs.ddns.net * Pensacola, FL * (618:250/24)
  • From Sean Dennis@618:618/1 to Arelor on Fri Dec 20 13:44:44 2024
    Quick one in ksh, try it for filenames that have no special characters and a single extension (ie no .tar.gz)

    I sure will try it tonight. Thanks for your help.

    -- Sean



    --- MBSE BBS v1.1.0 (Linux-x86_64)
    * Origin: Outpost BBS * Johnson City, TN (618:618/1)
  • From Zip@618:500/27 to Sean Dennis on Fri Dec 20 22:47:18 2024
    Hello Sean!

    On 19 Dec 2024, Sean Dennis said the following...
    ## Rename files
    cd ./pics
    for i in *.*; do
    new=$(printf "%04d.*" "${a}") #04 pad to length of 4
    mv -i -- "$i" "$new"
    ((a++))
    done

    How about this one (assuming bash as the shell):

    a=0
    while IFS= read -r -d $'\0' old; do
    new_path="${old%%/*}"
    new_name="${old##*/}"
    new_name="$(printf "%04d" "$a").${new_name##*.}"
    echo mv -i -- "$old" "${new_path}/${new_name}"
    ((a++))
    done < <(find pics/ -xdev -mindepth 1 -maxdepth 1 -type f -print0)

    (Remove the "echo" to activate.)

    It will find files in the pics subdirectory -- only there, not deeper, and not traversing into any other file systems -- and feed their full paths to the while loop, separated by NUL characters. The while loop will extract the path and the filename, and construct the new desired full path, using the padded counter as the first part of the filename.

    The process substitution/redirection feeding the while loop will ensure that no command inside the while loop (e.g. the "mv" command) can interfere with its input (e.g. interpreting a full path on the input as a user response to overwriting a destination file that already exists).

    The IFS stuff and the NUL separation should help for paths/filenames containing (very) special characters.

    Hope this helps, or at least sparks some ideas! =)

    Best regards
    Zip

    --- Mystic BBS v1.12 A49 2024/05/29 (Linux/64)
    * Origin: Star Collision BBS, Uppsala, Sweden (618:500/27)