AT2k Design BBS Message Area
Casually read the BBS message area using an easy to use interface. Messages are categorized exactly like they are on the BBS. You may post new messages or reply to existing messages!

You are not logged in. Login here for full access privileges.

Previous Message | Next Message | Back to Computer Support/Help/Discussion...  <--  <--- Return to Home Page
   Networked Database  Computer Support/Help/Discussion...   [1655 / 1834] RSS
 From   To   Subject   Date/Time 
Message   Zip    Sean Dennis   rename help under Linux   December 20, 2024
 10:47 PM *  

Hello Sean!

On 19 Dec 2024, Sean Dennis said the following...
 SD> ## Rename files
 SD> cd ./pics
 SD> for i in *.*; do
 SD>   new=$(printf "%04d.*" "${a}";) #04 pad to length of 4
 SD>   mv -i -- "$i" "$new"
 SD>   ((a++))
 SD> 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)
  Show ANSI Codes | Hide BBCodes | Show Color Codes | Hide Encoding | Hide HTML Tags | Show Routing
Previous Message | Next Message | Back to Computer Support/Help/Discussion...  <--  <--- Return to Home Page

VADV-PHP
Execution Time: 0.0163 seconds

If you experience any problems with this website or need help, contact the webmaster.
VADV-PHP Copyright © 2002-2025 Steve Winn, Aspect Technologies. All Rights Reserved.
Virtual Advanced Copyright © 1995-1997 Roland De Graaf.
v2.1.250224