![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|
#3
|
||||
|
||||
|
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
|
|
#4
|
|||
|
|||
|
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... |
|
#5
|
||||
|
||||
|
csh ?
(message too short) |
|
#6
|
|||
|
|||
|
sorry, i meant zsh, I'm always getting those pesky letters of the alphabet mixed up!
|
|
#7
|
||||
|
||||
|
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}
|
||||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|