Depending on your way to look at files, it is easy to distinguish, by icons, by conventions, by small indicators.
Many of the comments are to some degree true, but nothing is sufficient as an explanation.
- On Windows, you may create text files without extension. There is no mandatory requierement to use extensions.
- "Why are there no extensions for text documents in Linux?" There are. On my system, a lot of them, .txt, .csv, .sh, .scala, .java, .sql, .dat .svg, .xml, .html and many more. Many of them indicate more, than just that it is readable text. None of them is a guarantee.
- You are even allowed to name a directory "foo.txt", and guess what - it's possible on windows too.
- "execute.txt.exe" is of course a trap, but only problematic, since windows introduced hiding of the suffix (only the last one) and deciding by extension, how to execute a file. Unixlike Systems have permisssions, one of them is, to execute a file, and then the system will try to execute it. It's not a guarantee, that it is possible, either.
In the shell, you can get a long output format like:
-rw-rw-r-- 1 stefan stefan 2184 Feb 5 04:27 ShortestPalindrome.java
-rw-rw-r-- 1 stefan stefan 283 Dez 20 03:58 speedtests.lst
drwxr-xr-x 4 stefan stefan 4096 Nov 28 2012 src
-rw-rw-r-- 1 stefan stefan 2609 Jan 16 23:01 Stringkuerzen-2018-01-16_23:01:27.java
with ls -la
. The first character 'd' tells you, it is a directory.
Just using ls
gives you a brief list. Then the color, which is set up by default, indicates which file is what, especially directories. If you don't fiddle around with it, it should work fine.
Linux has the 'file' command, to make an informed guess, what kind of file a file is, and most of the time this is right. But it can be fooled too. Not as easy as the windows extension system, but it can.
However, naming your PNG-files winter.png, your text winter.txt and a shell script winter.sh is still a good idea, because it is user friendly.
A file browser, like Thunar, will indicate which kind of file a file is, by Icons in the icon view, smaller icons in the list view, and smallest icons in the compact view. Often the sort order is placing directories first.
The midnight commander has it's own way of indicating directories in bright color. I guess most file managers do it in the one or other way.
But in contrast to other files, directories can't be mocked (except from symbolic links, which is a different topic). The type directory is deeply fixed in the system - you can't
cd abc.txt
if abc.txt isn't a directory. And yes, it can be, but most of the time it shouldn't.
With much effort, somebody could generate a program, which would catch the error, to step into a file instead of directory, which could be useful, if the file was an archive, for example, but on the shell I haven't seen such a thing.
And a directory can't hide it's nature. But that's the same for windows too, isn't it?
Conclusion: Most of the time, extensions are useful, but they guarantee nothing. If you have files from trustworthy sources, you can rely on them, if not, the file
command is a good help, but no guarantee either.
This is a sample output of
file *
funreturn.scala: ASCII text
hexcodes: ASCII text, with very long lines
inkscapepfad.scala: FORTRAN program, UTF-8 Unicode text
iterfind.sh: Bourne-Again shell script, ASCII text executable
leaderbord.aoc: ISO-8859 text
lib: directory
The guess, that inkscapepfad.scala is a Fortran code, is just wrong, and relying on the extension would here have been the better choice, and that, without intend to fool file
, just by incident.
ls -la
is your friend. If you see startingd
it is directory. In GUI folders and files are visually different as in windows (but files may not have extensions, it's normal). – N0rbert Feb 17 '18 at 21:07file /etc/issue
(sorry if this was in provided link, I didn't see it). Sure its not perfect (old bug on can't print on tuesdays 255161) but its pretty accurate and is less error prone than users adding a filetype by adding .txt to signify text. – guiverc Feb 17 '18 at 22:03file /etc
returns/etc: directory
andfile /etc/issue
returns/etc/issue: ASCII text
Thank you! – Shayan Feb 17 '18 at 22:46