LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 06-16-2008, 06:53 AM   #1
shivanrathore
Member
 
Registered: Apr 2008
Posts: 33

Rep: Reputation: 15
command to know the file extension type


can some body tell me what is the command to know the extension of the file?
for eg:
if i have a file called lol.txt , will it be command to know this file is a .txt file?
 
Old 06-16-2008, 07:01 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I am missing something here.....Can you not just do a listing (ls) and look at the file names?
Please give more information about what you are trying to do.
 
Old 06-16-2008, 07:04 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If what you want to know is what kind of data is stored in the file, then you probably want to use the "file" command.
 
Old 06-16-2008, 07:08 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
To get the file extension of all files in a directory, you might do something like:
Code:
for FILE in *.*; do 
   echo ${FILE#*.}
done
 
Old 06-16-2008, 09:45 AM   #5
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 42
linux doesn't really use file extensions like windows does.. It's more of a place holder. the file lol and lol.txt are essentially the same file and linux doesn't really know the difference. You can do a vi on them both and get the same results. It just makes things easier for people coming over from windows to see a file with a .txt to know that is a text file.. OR if you are transferring data from a linux PC to a windows PC and you want to be able to read that file in notepad you'll need the .txt extension so you can open it automatically by clicking on it rather than, right click, open with, notepad.

maybe i'm over thinking what you are asking.. If you just want to see that lol.txt has a txt extension you just do
ls
or
ls -l
 
Old 06-16-2008, 10:29 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
If you want to identify a file based on the contents, rather than the extension, then you want to use the "file" command, as someone stated above.

Code:
$ echo test > test.txt
$ file test.txt
test.txt: ASCII text
$ mv test.txt test.jpg
$ file test.jpg
test.jpg: ASCII text
 
Old 06-16-2008, 10:39 AM   #7
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
In the Unix world, it treats all files as either ASCII or Binary, it's really either or if given in a nutshell. I won't go into devices or the such but yeah, extensions are really just to let you know what type of file it is.
 
Old 06-16-2008, 10:57 AM   #8
lifeforce4
Member
 
Registered: May 2003
Location: 2:16:840
Distribution: Slackware, CentOS, RedHat, Xubuntu
Posts: 169
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by pixellany View Post
I am missing something here.....Can you not just do a listing (ls) and look at the file names?
Please give more information about what you are trying to do.
I back up pixellany 'ls' shows all files by their name. If their name is lol.txt or lol.txt.txt.txt or lol-txt-txt it does not matter its just the file name. The txt in the name just helps us know its an ASCII instead of a binary file.

Quote:
Originally Posted by trickykid View Post
In the Unix world, it treats all files as either ASCII or Binary, it's really either or if given in a nutshell. I won't go into devices or the such but yeah, extensions are really just to let you know what type of file it is.
I never thought about that but your right. Humm learn something everyday. ;-)
 
Old 06-16-2008, 11:22 AM   #9
watcher69b
Member
 
Registered: Nov 2007
Location: /home/watcher69b
Distribution: RH, Fedora & CentOS
Posts: 539

Rep: Reputation: 39
I think the problem is that he is coming from a DOS evnt. Where it matters what extension the file uses and Windows sometimes hides them.

The short answer is that
In Linux you do not need extensions. They are nice but not needed for most things
 
Old 06-16-2008, 12:59 PM   #10
farslayer
Guru
 
Registered: Oct 2005
Location: Willoughby, Ohio
Distribution: linuxdebian
Posts: 7,231
Blog Entries: 5

Rep: Reputation: 189Reputation: 189
it looked like he was asking how to verify the type of the file regardless of the extension..
His example even states he has a file called lol.txt and wanted to know how to verify it is indeed a text file

in which case the file command would be the proper one to use, as stated earlier.
 
Old 06-16-2008, 01:35 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,973
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by farslayer View Post
it looked like he was asking how to verify the type of the file regardless of the extension..
His example even states he has a file called lol.txt and wanted to know how to verify it is indeed a text file

in which case the file command would be the proper one to use, as stated earlier.
Actually he's explicitly asking how to verify that a .txt file
is a .txt file (not "a file containing text"), and asks in the
title to know the extension type, which brings us back to
visual examination and ls ;}



Cheers,
Tink

Last edited by Tinkster; 06-16-2008 at 01:36 PM.
 
Old 06-16-2008, 01:39 PM   #12
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
The command stat will also do. Along with ls like mentioned. I think it's good though that myself and other members explained how files are treated in *nix, just an added detail to benefit the OP.
 
  


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
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 04:19 PM
Get file icon from extension, file type, or mime-type Guitarist88 Programming 3 04-21-2008 10:58 AM
type sudo command then i cant type in a password turoblaztr Linux - General 5 01-14-2007 09:58 AM
How to get the file type without using "file" command. kumarraja Programming 2 03-03-2006 06:41 AM
file type from command line?? wr3ck3d Linux - General 4 03-12-2003 01:48 PM


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