LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2006, 01:14 PM   #1
mr_scary
Member
 
Registered: Aug 2006
Posts: 39

Rep: Reputation: 15
shell script - truncating filenames


In a bourne shell script, what is the easiest way to truncate filenames.

Example:

20060826-1726-26646-27397.cdr-20060109.gz

becomes

20060826-1726-26646-27397.cdr

--
Peter
 
Old 09-01-2006, 02:20 PM   #2
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 33
Here's a sample script that'll do the trick:

Code:
#!/bin/sh
for i in *.cdr*
do
mv $i "${i%%.cdr*}.cdr"
done

exit 0

explanation:
The for i in *.cdr* will assign to i all the filenames in the current directory that have a .cdr in between their name, one by one. The statement
Code:
${i%%.cdr*}
scans the filename from the end, removes the longest part of i that matches .cdr* and returns the rest. Finally we add the .cdr extension to the truncated filename.
 
Old 09-01-2006, 02:22 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
$>longfile=20060826-1726-26646-27397.cdr-20060109.gz
$>shortfile=`echo $longfile| sed 's/-/ /4' | awk '{print $1}'`
$>echo $shortfile
20060826-1726-26646-27397.cdr

Last edited by jim mcnamara; 09-01-2006 at 02:23 PM.
 
Old 09-04-2006, 02:50 PM   #4
mr_scary
Member
 
Registered: Aug 2006
Posts: 39

Original Poster
Rep: Reputation: 15
That's beautiful. Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script for converting filenames to proper case? Yalla-One Programming 9 02-03-2010 02:37 AM
Shell script: Delete filenames containing a substring? Drack Linux - General 4 02-12-2006 03:11 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Shell scripting to find length of filenames ridertech Linux - Newbie 2 08-25-2004 12:07 PM
bash shell script appends '?' to filenames sceptreofjudah Linux - Newbie 3 03-18-2004 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 AM.

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