LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-26-2006, 09:33 AM   #1
daysleeper
LQ Newbie
 
Registered: Aug 2005
Location: Jhb, South Africa
Distribution: SUSE
Posts: 6

Rep: Reputation: 0
Question Truncating the length of files\directors to 31 characters or less


Hi guys,

I've literally got thousands of files\directories that are between 50-60 characters in length and I need to rename them or simply just cut of anything in the filename after the 31st character so the new filenames are no longer than 31 characters. The files are mp3's

Does anyone know if I could create a script in bash or tcsh that would allow me to automate this function?

Basically after specifying a directory (eg. /name) it would need to recursively scan all the subdirectories and files, check their length and if they are longer than 31 characters simply cut off everything after the 31st character. eg. A file like "ThisFileIsJustWayTooLongAndReallyNeedsToBeShortened.mp3" would become "ThisFileIsJustWayTooLongAnd.mp3" Catch is it needs to keep the .mp3 extension. Although all the files are mp3's so I could always trucate to 27 characters and then add .mp3 to the filename.

I'm very new to linux scripting but have some programming knowledge. I would be very gratefull for a push in the right direction of if there is already a program that could do this.

Thanks,
Daysleeper
 
Old 09-26-2006, 01:21 PM   #2
J.W.
LQ Veteran
 
Registered: Mar 2003
Location: Boise, ID
Distribution: Mint
Posts: 6,642

Rep: Reputation: 87
I guess my main question would be, What is your real goal here? Simply lopping off anything past the "Nth" character could easily result in having multiple files having the same name. To re-use your example, suppose you had
Code:
"ThisFileIsJustWayTooLongAndReallyNeedsToBeShortened_Track_1.mp3" 
"ThisFileIsJustWayTooLongAndReallyNeedsToBeShortened_Track_2.mp3" 
"ThisFileIsJustWayTooLongAndReallyNeedsToBeShortened_Track_3.mp3" 
....
Using the truncation approach, they'd all become "ThisFileIsJustWayTooLongAnd.mp3" and that wouldn't work.

Can you provide the background as to why this operation is necessary, or what the update is intended to accomplish?
 
Old 09-26-2006, 04:43 PM   #3
kloss
Member
 
Registered: Nov 2004
Location: France & Germany
Distribution: (Pure) Debian Etch & Sid
Posts: 116

Rep: Reputation: 15
Code:
#!/bin/sh
  find . -regextype posix-extended -regex ".*/[^/]{32,}" | while read file; do
  name=`basename "$file"`
  echo "$file" "${file%$name}${name::27}.mp3"                                            
done
This script is basically what you want. Just s/echo/mv and add a line to take JW's comment in account, since he's right. If it screws up your box and eat your cat, I'm not responsible.

Last edited by kloss; 09-26-2006 at 04:53 PM.
 
Old 09-27-2006, 12:28 AM   #4
daysleeper
LQ Newbie
 
Registered: Aug 2005
Location: Jhb, South Africa
Distribution: SUSE
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks for the reply guys. I realise that I could end up with multiple files with the same name but after looking at the files I'm pretty sure that the first 30 characters are defintely unique. The reason I'm doing this is that I work for a sound studio and we have been supplied with a digital music library. The new Macs (OSX) can handle very long file names but the old Macs (OS9x) have the file length limitation. Many thanks for the script, will give it a try /
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to remove the first three characters in the name of a ton of files... ooagentbender Linux - Newbie 17 10-04-2013 02:02 PM
Trying to figure out the number of characters in a std::string, using length. RHLinuxGUY Programming 4 05-23-2006 09:39 AM
Samba truncating files to 0 bytes KimVette Linux - Networking 6 01-30-2005 06:42 PM
Truncating trailing characters liguorir Programming 0 05-23-2004 04:00 AM
Command line tool to print length of mp3 files? J_Szucs Linux - Software 3 05-04-2004 05:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration