Truncate file name to 40 characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate file name to 40 characters
# 8  
Old 01-12-2010
CPU & Memory

OK.

After doing some testing, I have found that the following script works, but on files only. Also, the script itself must be in the same directory as the files for it to work. I also changed the extension to "mp3" only as that is the only file type I want to rename.

Code:
#! /bin/bash

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

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


Could someone please help me with doing this same process but recursively from the /home/music directory and also apply the renaming to the directories as well?

Maybe I didn't explain better before, but I have my music organized in folders by artist name and beneath that, by album name.

For example,

/home/music/Beatles, The/Abbey Road/Because.mp3
/home/B-52's, The/Cosmic Thing/B-52's, The - Rock Lobster.mp3

Hence the script should search through all of the directories and rename them to 38 characters and remove any instances of punctuation marks and hyphens.

Thanks again to Sychronizer and rdcwayx for their help so far.

Quote:
Originally Posted by marcozd
Thanks [COLOR=Black][COLOR=#22229C]Scrutinizer.

I tried replacing it with

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 againSmilie
# 9  
Old 01-12-2010
do you see my reply in page 1? It had already included the process to rename directory.

Below code (also included) can be used to fix your another problem: the script itself must be in the same directory as the files for it to work.

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

# 10  
Old 01-12-2010
Power

Hi rdcwayx.

I did see your reply and I am grateful for your help, but it doesn't work.

This is my output from the terminal.

Code:
marco@marco-desktop:~/Music$ ./both.sh
./both.sh: line 2: i: command not found
./both.sh: line 19: cd: /home/marco/Music/ARTIST_NAME/ALBUM: No such file or directory
mv: cannot stat `just_files.sh/just_files.mp3': No such file or directory
mv: cannot stat `just_files.sh/just_files.mp3': No such file or directory
mv: cannot stat `B_52_s_ The _ Rock Lobster99999999999999999999999999999999.mp3/B_52_s_ The _ Rock Lobster99999999999999999999999999999999.mp3': No such file or directory
mv: cannot stat `B_52_s_ The _ Rock Lobster999999999999999999999999999999.mp3/B_52_s_ The _ Rock Lobster999999999999999999999999999999.mp3': No such file or directory
mv: cannot stat `Folder.jpg/Folder.mp3': No such file or directory
mv: accessing `both.sh/both.mp3': Not a directory
mv: accessing `both.sh~/both.mp3': Not a directory
mv: cannot stat `Dname': No such file or directory
mv: cannot stat `Dname': No such file or directory
mv: cannot stat `Dname': No such file or directory
marco@marco-desktop:~/Music$

As I mentioned in my last post, in /home/marco/music/ there are about 1800 folders each corresponding to a particular musical artist. Then below each artist is an album folder. And in each album folder are the .mp3 files that I want to rename. There is also a file called "folder.jpg" in each album folder that doesn't need renaming that is why I replaced the "$ext" with "mp3" in the script.
To give an example:

/home/marco/Music/B-52's, The/Cosmic Thing/B-52's, The - Rock Lobster.mp3
/home/marco/Music/Pink Floyd/Dark Side of the Moon/Pink Floyd - Money.mp3

I guess the problem is that it is not changing to the proper directory?

Also, when I run the script you suggested it does not rename any of the files, while the one in my last post will rename the files.

I am very new and don't understand too well the scripting language but I am trying so bear with me pleaseSmilie.

Quote:
Originally Posted by rdcwayx
do you see my reply in page 1? It had already included the process to rename directory.

Below code (also included) can be used to fix your another problem: the script itself must be in the same directory as the files for it to work.

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

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