LinuxQuestions.org
Review your favorite Linux distribution.
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-15-2004, 07:18 AM   #1
Cruger
LQ Newbie
 
Registered: Mar 2004
Posts: 14

Rep: Reputation: 0
Unhappy Identifying files in bash script


Hi...
I'm writing a bash script, but I've run into some trouble..

I need the script to identify what type of file it recieves... for example if the file in question is Filename.txt, then I need the script to identify that it is a .txt-file... the same goes for any type (.zip, .tar....)

Does anyone understand what I'm asking I have trouble continuing the script whitout this...

See ya on the flipside!!
 
Old 03-15-2004, 07:42 AM   #2
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
You can check the command file, but from your post it seems that you're not after that kind of info. So, if you're just after the extension of the file, maybe something like this is ok:
Code:
#!/bin/sh

ls | grep '\.' | while read FILE ; do
   EXTENSION=`echo $FILE | rev | tr '\.' '\ ' | cut -f1 -d' ' | rev`
   echo "$FILE has extension $EXTENSION"
done
A bit cumbersome perhaps, but it shows you the idea.

HTH

Last edited by Bebo; 03-15-2004 at 07:44 AM.
 
Old 03-15-2004, 07:54 AM   #3
Cruger
LQ Newbie
 
Registered: Mar 2004
Posts: 14

Original Poster
Rep: Reputation: 0


Najjs Bebo... now I have something to work with

That one tells me what all my files have for extention, now all I have to do is rewrite it so it only looks at the file in question (for example $1), thanks for the tip !!
 
Old 03-15-2004, 08:03 AM   #4
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
Grrreat, glad I could help ya
 
Old 03-15-2004, 09:34 AM   #5
Cruger
LQ Newbie
 
Registered: Mar 2004
Posts: 14

Original Poster
Rep: Reputation: 0
Angry

AAAhhhrrgggggg....

This is hopeless !!! I've tried to put the ls | grep '\.' command in a for and an if-loop to try and identify ONLY the file I'm sending to the script... but I keep messing it up.. it lists all files of that kind.. but I need it to only compare it... in other words to see if it "fits" the file-type I'm looking to compare it to...

If I send in a file: Filename.txt and my script looks at it, it should only be answering true or false... what am I doing wrong??? "Sighhhhh"
 
Old 03-15-2004, 10:16 AM   #6
hiteshmaisheri
Member
 
Registered: Nov 2003
Location: Kerala, India
Distribution: Red Hat, Knoppix, Mandrake, FreeBSD
Posts: 231

Rep: Reputation: 30
can you just post your script here... if you can please do that...
 
Old 03-15-2004, 10:21 AM   #7
Cruger
LQ Newbie
 
Registered: Mar 2004
Posts: 14

Original Poster
Rep: Reputation: 0
Here comes part of the script:

..............................................................................
ls | grep '\.'
if [ $1 = "\.txt" ]
then
echo " This is a .txt file"
else
echo "This is not a .txt file"
fi
...............................................................................
 
Old 03-15-2004, 10:49 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
Code:
for file in * ;do

    case ${file##*.} in

    txt) echo "$file is a TXT file"
          ;;

    c) echo "$file is a C file"
          ;;

    *)
        echo "$file is UNKNOWN"
        ;;

    esac
done
yields:

billym.primadtpuat> tt
test.c is a C file
wozzle is UNKNOWN
wozzle.c is a C file
wozzle.txt is a TXT file
billym.primadtpuat>
 
  


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
bash script to sort files by extension otheralex Programming 7 08-19-2005 02:40 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
bash script to output only differences in two files czarherr Programming 4 11-08-2004 06:25 PM
bash script on manipulating files fiomba Linux - Software 8 10-30-2004 08:31 PM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM


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