Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 04-21-2011, 11:51 PM   #1
Registered: Jan 2010
Posts: 189

Rep: Reputation: 15
bash script: to truncate filename

try to write a simple bash script

If the input $1 is a "myfilename.txt",
How could I store the "myfilename" as my another variable instead of "myfilename.txt". I want the script to make a new directory name as myfilename
Old 04-22-2011, 12:01 AM   #2
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I believe there is bash(1)-fu to solve this sort of thing, but I'm pretty fond of g/awk(1):
_name=$(echo ${1} | awk -F'.' '{print $1}')
Old 04-22-2011, 06:25 AM   #3
Senior Member
Registered: Jan 2010
Posts: 1,605

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Here is a pure bash solution:
If your name has several dots in it like

then it will only delete the last part like

If you want the output to be just

then you can use
1 members found this post helpful.
Old 05-24-2011, 10:01 PM   #4
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
How about for the csh case?


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
I question on a bash script and filename spaces... trist007 Programming 13 01-17-2010 06:13 AM
bash script - get filename created by script using variables aolong Linux - General 8 11-19-2009 12:40 PM
changing filename using Bash Script tmbigrigg Programming 10 11-10-2007 07:02 AM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration