LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 03-28-2005, 01:17 PM   #1
hq4ever
Member
 
Registered: May 2004
Location: Israel
Distribution: Debian
Posts: 98

Rep: Reputation: 15
How to parse full path of file name in bash ?


Help please.

Assuming I have this file:

Code:
/tmp/dir001/somefile.foo.gz
and I need to rename it to it's md5sum, if it didn't had an extersion I would have done it like this:
Code:
for i in *; mv "$i" `md5sum "i$" | cut -d ' ' -f 1`; done
But I need to keep the last extersion, i.e. I need it to be names [md5sum-goes-here].foo.gz

How can this be done ?

Thank you for helping,
Maxim.
 
Old 03-28-2005, 02:25 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Here's an example that shows nothing more than how to determine filename without it's extension, and the extension. Assuming the script is called "filename_split.sh", and we're to pass in a single filename:
Code:
#!/bin/bash

if [ -z "$1" ]; then
  echo "$0 filename_to_tweak"
  exit
fi

#  lines of note
#
pathfilename=${1%.*}
fileext=${1##*.}
#
#

echo "Full: $1"
echo "Pre : $pathfilename"
echo "Post: $fileext"
And run:
Code:
~/bash> ./filename_split.sh gcrt1.o
Full: gcrt1.o
Pre : gcrt1
Post: o
Here's a link for some other interesting bash matching/splitting
 
Old 03-28-2005, 03:31 PM   #3
hq4ever
Member
 
Registered: May 2004
Location: Israel
Distribution: Debian
Posts: 98

Original Poster
Rep: Reputation: 15
Aha "Parameter Expansion".

Exactly what I've been looking for.
Thank you very much.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
full path of a file ttilt Linux - General 1 11-08-2005 06:02 PM
get full path of uploaded file in php spoody_goon Programming 2 05-10-2005 07:32 PM
bash script help to parse out text slack guy Linux - Newbie 3 12-30-2004 08:42 AM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
Newbie bash path parse problem ericcarlson Linux - Newbie 4 08-05-2004 10:43 AM


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

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration