...
/Getting More Information About Filesystem Objects
Getting More Information About Filesystem Objects
Learn how to get information about filesystem objects.
We'll cover the following...
We might be able to tell what type of file we’re working with by looking at its file name. Typically, a three or four-letter extension gives it away. For example, foo.pdf is a PDF file, logo.png is a graphics file, and foo.txt is a plain text file. But sometimes there’s no extension, and sometimes the files are misnamed.
The file command
The file command can help us figure out what type of file we’re working with. Let’s try it out by pointing it at the greetings.txt file we created in the root directory:
$ file greetings.txt
Run the complete code on the terminal below for practice.
echo > "Hello" > greetings.txt
file greetings.txt
The output tells us what’s in the file by looking at its content. Let’s create a file called test.html in our home directory with touch and try to identify it:
$ touch ~/test.html
$ file ~/test.html
Run the complete code on the terminal below for practice.
touch test.html
file test.html
This time, file tells us it’s an empty file, despite having the .html extension. Now, let’s throw some HTML into the file:
$ echo '<h2>this is a test</h2>' > test.html
$ file test.html
Use the terminal below to practice these commands.
Now it says it’s an ASCII text file. We might have expected it to report something like an HTML file, but that’s not quite how it works. Sure, there’s HTML ...