LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 04-09-2008, 04:26 PM   #1
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Rep: Reputation: 30
How can I script to shorten a filename?


I have a file called foo123.txt.gpg. I need it to become foo123.txt.

I know how to append characters to a filename but how can I get rid of a few characters at the end of a filename?

Thx..
 
Old 04-09-2008, 04:29 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
What is it that is determining what you want to remove? The last 4 characters? The last . and everything after it? The first two strings separated by dots are kept and everything else after is removed?

Let us know,

Forrest
 
Old 04-09-2008, 04:34 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
file=foo123.txt.gpg
mv $file ${file%.gpg}
 
Old 04-09-2008, 05:19 PM   #4
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by forrestt View Post
What is it that is determining what you want to remove? The last 4 characters? The last . and everything after it? The first two strings separated by dots are kept and everything else after is removed?

Let us know,

Forrest
There will always be a file thats starts with foo, followed by 3 numbers, then .txt.gpg. (ie: foo123.txt.gpg, foo245.txt.gpg, foo452.txt.gpg).

I need a script to see if foo* exists, if so, remove the .gpg from the end.
 
Old 04-09-2008, 05:20 PM   #5
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by colucix View Post
Code:
file=foo123.txt.gpg
mv $file ${file%.gpg}
What is the purpose of the %? Does that mean get rid of everything after %?
 
Old 04-09-2008, 05:54 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I'm assuming your shell is bash. If so, have a look at the man page for bash and search for "Parameter Expansion":
Code:
${parameter%word}
${parameter%%word}
              The word is expanded to produce a pattern just as in pathname expansion.  If the pat-
              tern  matches  a trailing portion of the expanded value of parameter, then the result
              of the expansion is the expanded value of parameter with the shortest  matching  pat-
              tern  (the ``%'' case) or the longest matching pattern (the ``%%'' case) deleted.  If
              parameter is @ or *, the pattern removal operation  is  applied  to  each  positional
              parameter in turn, and the expansion is the resultant list.  If parameter is an array
              variable subscripted with @ or *, the pattern removal operation is  applied  to  each
              member of the array in turn, and the expansion is the resultant list.
 
Old 04-09-2008, 08:54 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{q="\047"}
{
 old=FILENAME;
 sub(".gpg","",FILENAME)
 cmd="mv "q old q" "q FILENAME q
 system(cmd)
}' foo*.gpg
 
Old 04-10-2008, 09:33 AM   #8
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by gilead View Post
I'm assuming your shell is bash. If so, have a look at the man page for bash and search for "Parameter Expansion":
Code:
${parameter%word}
${parameter%%word}
              The word is expanded to produce a pattern just as in pathname expansion.  If the pat-
              tern  matches  a trailing portion of the expanded value of parameter, then the result
              of the expansion is the expanded value of parameter with the shortest  matching  pat-
              tern  (the ``%'' case) or the longest matching pattern (the ``%%'' case) deleted.  If
              parameter is @ or *, the pattern removal operation  is  applied  to  each  positional
              parameter in turn, and the expansion is the resultant list.  If parameter is an array
              variable subscripted with @ or *, the pattern removal operation is  applied  to  each
              member of the array in turn, and the expansion is the resultant list.
Thanks for everyones responses. We ended up doing this....

Code:
for i in `find /tmp/ -name foo*pgp -print0`; do
newvar=`echo $i | sed s/.pgp//g`
mv $i $newvar
done
Now if someone googles with the same question, they have multiple options.

Thanks again.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script:filename containing space ! Dr_Death_UAE Programming 3 12-16-2006 06:46 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
Help with automated filename script lhoff Programming 2 09-07-2004 10:46 AM
Script to change several filename jan_81 Linux - Newbie 5 04-28-2004 11:10 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM

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

All times are GMT -5. The time now is 11:40 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