8

I am doing a university assignment at the moment which specifies that I list the command to; List all files, showing file types.

I have searched high and low to try and find the correct command to achieve this, but have had no luck!

Would somebody be able to help me out by pointing me in the right direction or maybe telling me what command could do this?

2
  • 2
    read about the ls command
    – Kamaraj
    Oct 13, 2016 at 4:54
  • First, you need to define what you mean by "file type" - to a Windows user, that usually means a 'dot extension' on the filename, whereas in the *nix world it would more likely refer to the actual MIME type of the file (as determined by the file or mimetype commands). Oct 13, 2016 at 10:38

1 Answer 1

12

"List all files, showing file types."

To see all files in the current directory and their file type, try:

file ./*

Example:

$ file ./*
./apt.conf.d:                 directory
./listchanges.conf:           ASCII text
./preferences.d:              directory
./sources.list:               ASCII text
./sources.list.d:             directory
./trusted.gpg:                GPG key public ring

The above shows a human-readable form of the file type. Alternatively, one can print the file's mime-type:

$ file --mime-type ./*
./apt.conf.d:                 inode/directory
./listchanges.conf:           text/plain
./preferences.d:              inode/directory
./sources.list:               text/plain
./sources.list.d:             inode/directory
./trusted.gpg:                application/x-gnupg-keyring

file does a cursory examination of the content of each file to determine its type. The default output, as shown in both examples above, includes the file name and the file type. For more information, see man file.

4
  • 1
    In addition to this answer let's say that you are looking just for HTML files, you would run something like: file * | grep HTML
    – Xedret
    Feb 21, 2020 at 6:01
  • Ahem... In this case, my use was intended exactly as the output of the command. (Someone downloaded jpegs that returned a 404 html with wget or curl without properly adding exceptions)
    – Xedret
    Feb 21, 2020 at 8:38
  • 1
    Depending on your filename structure, file * may output the error file: invalid option -- '-' This can be solved by running file ./* instead (safer too) Dec 31, 2020 at 20:46
  • @LukeRehmann Thank you much. Answer updated with ./.
    – John1024
    Jan 9, 2021 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.