Truncate file name to 40 characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate file name to 40 characters
# 1  
Old 01-08-2010
Question Truncate file name to 40 characters

Hello all.

I would like to make a script (or two shell scripts) that will do the following.

I need the maximum file name and directory name to be 38 characters long.
As well, if shortening the file name ends up making all of the files in that directory have the same name, then I would like the script to recognize this problem and then append numbers to the end of the file name. For example, the files shown below would have the same name if shortened to 38 characters.:

Quote:
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - The Best is Yet to Come.mp3
becomes
Quote:
Sinatra, Frank & Tony Bennet & Sammie.mp3
and
Quote:
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - New York, New York.mp3
becomes
Quote:
Sinatra, Frank & Tony Bennet & Sammie.mp3
so they have the same name, hence I would like to add a suffix of two digit numbers, starting at 01. So the originals would become
Quote:
Sinatra, Frank & Tony Bennet & Sammie01.mp3
Quote:
Sinatra, Frank & Tony Bennet & Sammie02.mp3
As well, I need to replace any occurrence of " [<>=?:;"*+,|]" in both the directory and file name with an underscore "_".

So, the script would modify the following directory

Quote:
/home/music/The Best of the Rat Pack + more/

to
[QUOTE]/home/music/The Best of the Rat Pack _ more/

AND it would change the following file names located in that directory

Quote:
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - The Best is Yet to Come.mp3
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - New York, New York.mp3
to [QUOTE]Sinatra_ Frank & Tony Bennet & Sammie01
Sinatra_ Frank & Tony Bennet & Sammie02[/QUOTE]

I would need this to work recursively on all of the directories below /home/music/.

I found this "ruby" script using google, but it does not help with appending numbers to file names that are the same once truncated. Perhaps someone knows how to modify it so it does what I want it do doSmilie. I hope this helps:

Code:
    #!/usr/bin/ruby

# usage xbox_prep.rb DIRECTORY_NAME

DirName = ARGV[0]
file_name = ""          # name of the file to be checked
old_full_path = ""      # name and path of the file to be checked
new_full_path = ""      # new name and path of the file (modified if necessary)
basename = ""           # basename of the file
extension = ""          # extension of the file to be checked


if DirName == nil
        puts "usage:"
        puts ""
        puts "xbox_prep.rb DIRECTORY_NAME"
        puts ""
else

Dir.foreach(DirName){
        |file_name|
        if file_name != "." and file_name !=".."
         old_full_path = DirName + "/" + file_name
         basename = File.basename(old_full_path, ".*")
         extension = File.extname(old_full_path)


         # Modify the basename if neccessary
         basename.slice!(38,128)                        # shorten basename to 38 chars
         basename.gsub!(/[<>=?:;"*+,|]/,'_')            # Remove invalid characters

         # rename the file
         new_full_path = DirName + "/" + basename + extension
         File.rename(old_full_path, new_full_path)
         puts basename + extension

        end
}

end

Many thanks in advance for the help with this.
# 2  
Old 01-09-2010
Try this bash/ksh script that renames the files in the current directory.

Code:
rename_seq ()
{
  seq=1
  oldname="$1"
  newname="$2"
  ext="$3"
  while [ -e "$newname$(printf "%02d" $seq).$ext" ]; do
    (( seq ++ ))
  done
  mv "$oldname.$ext" "$newname$(printf "%02d" $seq).$ext"
}

ls | while read name; do
  ext="${name##*.}"
  name="${name%.*}"
  newname="${name//[<>=?:;\"*+,|]/_}"
  newname="${newname:0:38}"
  if [ -e "${newname}.$ext" ]; then
    rename_seq "$newname" "$newname" "$ext"
  fi
  if [ -e "${newname}"??".$ext" ]; then
    rename_seq "$name" "$newname" "$ext"
  else
    mv "$name.$ext" "$newname.$ext"
  fi
done

# 3  
Old 01-09-2010
Bug

Hi and thanks for helping me out.

DO I need to add

#!/bin/bash

at the beginning of this script to work and also, does it work recuresively?

My situtation is such that I have directory structure

/home/music/ARTIST_NAME/ALBUM/

and there are about 1800 folders (artists) in the music folder.

Again, my gratitude for the help
# 4  
Old 01-09-2010
Updates base on Scrutinizer's script.

Code:
#! /bin/bash
i
# rename function

rename_seq ()
{
  seq=1
  oldname="$1"
  newname="$2"
  ext="$3"
  subpath="$4"
  while [ -e "$newname$(printf "%02d" $seq).$ext" ]; do
    (( seq ++ ))
  done
  mv "$subpath/$oldname.$ext" "$subpath/$newname$(printf "%02d" $seq).$ext"
}

BASE=/home/music/ARTIST_NAME/ALBUM
cd $BASE

# Rename filename first

find . -type f -print | while read name; do
  name=${name##*/}
  subpath=${name%/*}
  ext="${name##*.}"
  name="${name%.*}"
  newname="${name//[<>=?:;\"*+,|]/_}"
  newname="${newname:0:38}"
  if [ -e "${newname}.$ext" ]; then
    rename_seq "$newname" "$newname" "$ext" "$subpath"
  fi
  if [ -e "${newname}"??".$ext" ]; then
    rename_seq "$name" "$newname" "$ext" "$subpath"
  else
    mv "$subpath/$name.$ext" "$subpath/$newname.$ext"
  fi
done

# Rename directories' name

find . type d -print |while read Dname; do
  newDname="${Dname//[<>=?:;\"*+,|]/_}"
  mv "Dname" "newDname"
done

# 5  
Old 01-09-2010
Bug

Is there a way I can trouble shoot this without actually making anything happen?

And also, can I use ">& logfile.txt" to pipe the output from the script to a logfile that I can see what is happening?

Thanks again for everyone's help so far.

I am testing with just one directory in the /home/music/ directory.

It is as follows "/home/music/B-52's, The/Cosmic Thing/B-52's, The - Rock Lobster.mp3"

I tried running it and this is what I got:

mv: cannot stat `B-52\'s, The.B-52\'s, The': No such file or directory

Am I doing something wrong?

Last edited by marcozd; 01-09-2010 at 04:58 PM..
# 6  
Old 01-09-2010
Perhaps it is the quotes in the file names. You could try:
Code:
newname="${name//[[:punct:]]/_}"

Instead of the collection of characters..
# 7  
Old 01-09-2010
Thanks [COLOR=Black][COLOR=#22229C]Scrutinizer.

I tried replacing it with
Quote:
newname="${name//[[:punct:]]/_}"
but I get the following:

mv: cannot stat `B-52\'s, The.B-52\'s, The': No such file or directory
mv: cannot stat `xboxmusicrenamer.sh': No such file or directory

thanks again:)

Last edited by marcozd; 01-09-2010 at 07:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to truncate a string to x number characters?

Hello: I have a large file which contains lines like the following: 1/t123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00% /t is a tab, spaces are as indicated the string "&Xx:1:1234:12345:123456$ABCDEFG..." has a slightly variable number of numbers and letters, but it always starts... (9 Replies)
Discussion started by: Tectona
9 Replies

2. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

3. UNIX for Dummies Questions & Answers

recover the truncate file

hi All, how to recover the truncate file in unix. Thanks!:wall: (2 Replies)
Discussion started by: krbala1985
2 Replies

4. Shell Programming and Scripting

how to recover the truncate file in unix

how to recover the file in unix. Thanks in advance.:wall: (1 Reply)
Discussion started by: krbala1985
1 Replies

5. UNIX for Dummies Questions & Answers

How to truncate thousands of file names

Folder of e-mails in maildir format had been corrupted. Typical file name is 1246281161.6777.m21JH:2,S . The " :2,S prevents " copying to another device. How can I simply remove the last four characters? (2 Replies)
Discussion started by: steve900
2 Replies

6. Shell Programming and Scripting

Truncate extra contents from file

Hi all, i have a file and i want that after 6th slash "/" in each line of the file the contents gets truncated. Can anyone tell me how to do that !! thanks in advance One more thing how can i change the size of output buffer of console, as i had very long output and its not... (2 Replies)
Discussion started by: glamo_2312
2 Replies

7. Shell Programming and Scripting

Truncate the content within alt attribute to first 250 characters.

I have a xml file which contains image tag as follows: <image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450" alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain" type="photograph" orient="portrait"/></image> ... (5 Replies)
Discussion started by: parshant_bvcoe
5 Replies

8. UNIX for Dummies Questions & Answers

Truncate last <n> characters from a file

I am trying to concatenate 2 files, but before concatenation, I would like to strip off the final character from the first file. The final character is a form feed (ascii 012 / hex 0C) and there will be an unknown number of these characters in the file. It is only the very last one which I want... (1 Reply)
Discussion started by: Gwailo88
1 Replies

9. Shell Programming and Scripting

Truncate File contain

I have one file which first line is blank and second line has some data. $cat filename output: 30-MAY-07 I want to store 30-MAY-07 value in one variable. for that I wrote var="`head -2 filename`" It will give that result but I want to truncate the first line which is blank. plz help. (2 Replies)
Discussion started by: rinku
2 Replies

10. Shell Programming and Scripting

how to truncate a large (8 GB) file

Hello, I need to truncate a large file without deleting and touching it again. i tried the below commands but no use because of the huge file size cat <<! > errors and echo > errors Could someone please help. Thanks, Sateesh (1 Reply)
Discussion started by: kotasateesh
1 Replies
Login or Register to Ask a Question