LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 04-21-2011, 11:51 PM   #1
casualzone
Member
 
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
anomie
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):
Code:
_name=$(echo ${1} | awk -F'.' '{print $1}')
 
Old 04-22-2011, 06:25 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Here is a pure bash solution:
Code:
dir_name=${1%.*}
If your name has several dots in it like
my.file.name.txt

then it will only delete the last part like
my.file.name

If you want the output to be just
my

then you can use
Code:
dir_name=${1%%.*}
 
1 members found this post helpful.
Old 05-24-2011, 10:01 PM   #4
casualzone
Member
 
Registered: Jan 2010
Posts: 189

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


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
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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