LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-09-2009, 05:34 PM   #1
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Rep: Reputation: 15
if file *.txt then do x else if *.mp3 file then do xx (bash)


So I want to check if there is a file that has an extension of ether .txt or .mp3 and to exicute code if one is found.


Kinda like this: (but this doesn't work for obvious reasons )
Code:
for rfile in *.txt *.mp3; do
    if *.txt ; then
        echo here is a txt "$rfile"

	#do stuff
		
		
    elif *.mp3 ; then
       	echo  here is mp3 "$rfile"

	#do stuff

    else
        echo other files "$rfile"
    fi
done
Thanks in advanced for any help
 
Old 04-09-2009, 05:48 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could break it up into two loops.
Code:
for rfile in *.txt; do
  echo $rfile is a text file
  # do stuff
done

for rfile in *.mp3; do
  echo $rfile is an mp3 file
  #do stuff
done
Another thing you could do is use the find command.
find . -type f -iname "*.txt" -exec <command> \;
find . -type f -iname "*.mp3" -exec <command> \;

Be sure to read through the bash info manual. In particular the section on "test" & "[[" and the section on variable expansion.
also enter "help test" in the shell.
 
Old 04-12-2009, 07:31 AM   #3
Techno Guy
Member
 
Registered: Dec 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks jschiwal!

Really appreciate your help, I will use the 2 for loops for now.
When I have some more time ill look into the other options for a more permanent solution.
 
Old 04-12-2009, 08:52 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Try this. Use the for loop to find the files you want, then test them with an if or case statement. I prefer case myself usually. {} can be used to match multiple patterns at once.

Code:
for rfile in *.{txt,mp3}; do

   case rfile in

     *.txt )
       echo $rfile is a text file
       # do stuff
     ;;
     *. mp3 )
       echo $rfile is an mp3 file
       #do stuff
     ;;
   esac
done
 
1 members found this post helpful.
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to compare numbers in a txt file leopard86 Programming 6 09-11-2012 12:10 AM
bash command for make difference between two txt file kkpal Linux - Newbie 7 05-12-2008 07:07 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM


All times are GMT -5. The time now is 08:42 AM.

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