Rename / Truncated Files

  • diego75
  • Born
  • Born
  • diego75
  • Posts: 3

Post 3+ Months Ago

Hello everyone,

I like to write a script to rename files but neither I can not.

My problem:

In a directory is file types:

ABCDEF_blablablablablablabla.extension

How to rename them by keeping only the first 6 characters ABCDEF + lextension? ABCDEF.extensionn

Thank you for your davance lights!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Assuming you're sure that all of your renames will result in unique filenames, there's something like this.

BASH Code: [ Select ]
#!/bin/bash
 
for file in $( ls ./); do
   if [[ -f $file && $file == *.* ]]; then
      mv $file "${file:0:6}.${file#*.}"
   fi
done
  1. #!/bin/bash
  2.  
  3. for file in $( ls ./); do
  4.    if [[ -f $file && $file == *.* ]]; then
  5.       mv $file "${file:0:6}.${file#*.}"
  6.    fi
  7. done


When saved in the directory you want to work on, it will rename anything that is a regular file and has a file extension to the first 6 characters in the filename and the extension.
  • diego75
  • Born
  • Born
  • diego75
  • Posts: 3

Post 3+ Months Ago

Thank you for your quick response joebert...by cons it does not work.

Jai forgot to specify that threw in KSH (AIX)

And after an error occurs jai message:

0403-011 Substitution error for this command

So, back to square one lol
  • diego75
  • Born
  • Born
  • diego75
  • Posts: 3

Post 3+ Months Ago

Here is a solution that works (jai transferred the extension)

Code: [ Select ]
cd $pathtemp;
for test in *.toto ;
    do mv $test $pathout/$(echo "$test" | sed 's/\([^_]*\).*//');    
done
  1. cd $pathtemp;
  2. for test in *.toto ;
  3.     do mv $test $pathout/$(echo "$test" | sed 's/\([^_]*\).*//');    
  4. done


That, I hope that this will serve!

Thank you for your help

:P:P:P gif "alt =": P "title =" Razz "> :P
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Works for me. :)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 71 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.