LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-21-2005, 09:27 PM   #1
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
dirname basename; how to capture extensions from bash?


anyone know how to capture a file extension in bash?

basename /home/file.txt
file.txt

dirname /home/file.txt
/home/file
 
Old 07-21-2005, 10:33 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
how about:
Code:
basename /usr/src/file.txt | cut -s -d'.' -f2
It will not work correclty if there are more than one '.' in the filename.

So perhaps a regular expression:
Code:
for i in `ls`; do
echo $i{##*[.]} 
done
 
Old 07-22-2005, 07:26 AM   #3
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by crabboy
how about:
Code:
basename /usr/src/file.txt | cut -s -d'.' -f2
It will not work correclty if there are more than one '.' in the filename.

So perhaps a regular expression:
Code:
for i in `ls`; do
echo $i{##*[.]} 
done
brilliant thank you

cp "$(dcop amarok player path)" "$(dirname "$(dcop amarok player path)")"/0"$(dcop amarok player track)"\ -\ "$(dcop amarok player artist)"\ -\ "$(dcop amarok player title)"."$(basename "$(dcop amarok player path)" | cut -s -d'.' -f2)"

## */ Copies song currently playing in Amarok to the same directory it already is with filename based on id3track - id3artist - id3title . original extension /*
Basically mv will rename it instead. cp is for debugging purposes

i don't know how your second suggestion can be used however the first one is more than sufficient.


and what does the
"| "
do in the command basename /usr/src/file.txt | cut -s -d'.' -f2
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - execute commands on a remote machine capture results, etc dkrysak Programming 1 11-18-2005 01:50 PM
bash help, renaming file extensions trey85stang Linux - General 8 07-21-2005 04:51 PM
dirname error when installing Java JRE jrdioko Linux - Software 0 05-20-2005 05:35 PM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM
basename counterpart? glass Linux - General 1 08-24-2002 06:11 AM


All times are GMT -5. The time now is 07:39 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 Google+: linuxquestions
Open Source Consulting | Domain Registration