The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Truncate Log files anonymous1 Shell Programming and Scripting 1 10-21-2007 04:05 PM
*** Truncate certain field *** sannmayaz UNIX for Advanced & Expert Users 2 08-15-2007 07:14 AM
Truncate File contain rinku Shell Programming and Scripting 2 05-30-2007 03:43 AM
How to truncate as filesize? Lestat Shell Programming and Scripting 1 06-07-2005 12:24 PM
Truncate what is It? rocker40 UNIX for Dummies Questions & Answers 2 10-11-2003 04:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-16-2007
Registered User
 

Join Date: Jun 2002
Location: Edinburgh
Posts: 39
How can i truncate filenmes?

I am using FC6 just in case it matters, though i hope it doesn't.

If i have a file or some files that i want to truncate the filename of, so that it is only a certain number of characters in length, how would i do that on the command line?

Also, just to make it more interesting, say i wanted to keep the filename extension... would that be possible also? for example, the input filename would be something like a-ridiculously-long-filename_That_You_Wouldnt-give-to-your-WORST_enemy.TXT and then you run a command to truncate it to (say) 24 characters and you end up with a file called a-ridiculously-long-.TXT which is 24 characters, but the final four characters are the same file extension (including dot) from the original filename.

For extra marks, though it's not something i think i'll actually use at the moment, any way to make that sensitive to different file extensions? For example, files ending in longer extensions or with more then one ot, like *.flac or *.tar.gz

Thanks in advance, all!
Reply With Quote
Forum Sponsor
  #2  
Old 07-16-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
Try:
Code:
#!/bin/ksh
find /path -type -f | \
while read filename
do
	file=${filename%%.*}
	ext=${filename##*.}
	if [[ ${#file} -gt 24 ]] ; then
	     shorter=`expr $file 1 24`
	     mv $filename "$shorter"."$ext
	fi     
done
Reply With Quote
  #3  
Old 07-16-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,857
Use zsh (sudo yum -y install zsh, if not already present):


Code:
% ls
a-ridiculously-long-filename_That_You_Wouldnt-give-to-your-WORST_enemy.TXT
a-ridiculously-long-filename_That_You_Wouldnt-give-to-your-WORST_enemy.TXT.tar.gz
% autoload -U zmv
% zmv -n '(*).*' '${f[1,$((23-${#f#*.}))]}.${f#*.}'
mv -- a-ridiculously-long-filename_That_You_Wouldnt-give-to-your-WORST_enemy.TXT a-ridiculously-long-.TXT
mv -- a-ridiculously-long-filename_That_You_Wouldnt-give-to-your-WORST_enemy.TXT.tar.gz a-ridiculousl.TXT.tar.gz
Remove -n to execute.
Reply With Quote
  #4  
Old 07-19-2007
Registered User
 

Join Date: Jun 2002
Location: Edinburgh
Posts: 39
thanks folks! quite surprised to see a ksh and a csh response and no sh/bash ones! well, this means i don't properly understand the examples, since i have only ever used bash but perhaps this is y opportunity to start getting familiar with csh and ksh.

thanks again...
Reply With Quote
  #5  
Old 07-19-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,857
csh ?

(message too short)
Reply With Quote
  #6  
Old 07-19-2007
Registered User
 

Join Date: Jun 2002
Location: Edinburgh
Posts: 39
sorry, i meant zsh, I'm always getting those pesky letters of the alphabet mixed up!
Reply With Quote
  #7  
Old 07-20-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Calum,
Here is a solution in plain shell:
Code:
#!/bin/sh
mName24=`echo ${mFullName} | cut -c1-24`
mExt=`echo ${mFullName} | sed 's/.*\(\..*\)/\1/'`
m24PlusExt=${mName24}${mExt}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0