How can I get the extension(s) of a file based on its content?

confetti

I'm planning on downloading a bunch of images from a website that don't come with an extension, so I want to add one based on the file's content or mime-type.

file <filename> does a great job at identifying the filetype, however I need the extension.

--extension
      Print a slash-separated list of valid extensions for the file type found.

This is from file's man page, but it does not seem to work:

$ file --extension test_text_file.txt
test_text_file.txt: ???

$ file --extension test_png_file.png
test_png_file.png: ???

$ file --extension test_gif_file.gif
test_gif_file.gif: ???

It literally prints ??? for every file I pass to it, even those that already have a proper extension. All of these are valid files of their types and get recognized perfectly by file without --extension.

Why does file --extension not work for me and what can I use to get a file's extension?

An idea would be to use file --mime-type and then create a dispatch table array that maps known mime-types to their extensions, but I'd much rather have a simpler and safer solution.

Kamil Maciorowski

Why does file --extension not work for me?

Not only for you. See this question. One of the comments there seems right:

Maybe just a very, very incomplete feature?

I haven't found any standard Unix tool to do the conversion, so your idea may be the easiest solution anyway.

An idea would be to use file --mime-type and then create a dispatch table array that maps known mime-types to their extensions, but I'd much rather have a simpler and safer solution.

Note such a map exists, it's /etc/mime.types. See this another question on Unix & Linux SE. Based on one of the answers I came up with the following function:

function getext() {
   [ "$#" != 1 ] && { echo "Wrong number of arguments. Provide exactly one." >&2; return 254; }
   [ -r "$1" ] || { echo "Not a file, nonexistent or unreadable." >&2; return 1; }
   grep "^$(file -b --mime-type "$1")"$'\t' /etc/mime.types |
      awk -F '\t+' '{print $2}'
}

Usage:

getext test_text_file.txt   # it takes just one argument

Tailor it to your needs, make it a script etc. The main concerns:

  • If succeeded (exit status 0), the output may be non-empty or empty (not even \n).
  • Some mime-types return more than one extension. You can use cut -d ' ' -f 1 to get at most one, it may not be the one you want though.
  • So a custom map file instead of /etc/mime.types may be useful. This command will show you which mime-types exist in the current directory (and subdirectories):

    find . -type f -exec file -b --mime-type {} + | sort | uniq
    
  • grep shouldn't match more than once (at least with /etc/mime.types); ^ (line start) and $'\t' (tab) are there to avoid partial matching. Use grep -m 1 ... (or head -n 1 later) to be sure you'll get at most one line.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I make the ul contain li in its content box?

분류에서Dev

In a node script how can I monitor a file for changes and get the updated content?

분류에서Dev

How to reorder a list in Python based on its content

분류에서Dev

Can I see in a log file all GUI based tasks in its alternative command-line format?

분류에서Dev

How can I get the latest VLC if its not available from the VLC download page and its PPA

분류에서Dev

How can I get the latest VLC if its not available from the VLC download page and its PPA

분류에서Dev

How can I get the name of the file I'm uploading

분류에서Dev

How can I rename a file containing "&" in its name in kubuntu using Dolphin or terminal?

분류에서Dev

How can I get a part of text file by terminal

분류에서Dev

How can I get the contents of a file one line at a time?

분류에서Dev

How can I get "RHEL 7.0 vmlinux" file?

분류에서Dev

How can i get the value of id and its index of a newly created record

분류에서Dev

How can I get a relative div to expand to fit its absolute contents?

분류에서Dev

How can I automate none-PATH plink (Putty Link) on windows? and get its output?

분류에서Dev

How to get a border to size to its content with varying data and footer to adjust

분류에서Dev

Why can not the Bash script file run by shortcut get the content of a file?

분류에서Dev

How can I get the root terminal to add myself into sudoers file so that I can act as root?

분류에서Dev

How can I filter content of a web page?

분류에서Dev

Can I get the same object with its vales and use it in a different method?

분류에서Dev

How can I convert .mp4 files and .srt file to images based on subtitle's timeline?

분류에서Dev

How can I recognize that a certain Linux distribution is based on Debian, looking at file structure?

분류에서Dev

How to cp multiple file extensions?

분류에서Dev

How can I get a RejectedExecutionException

분류에서Dev

How can I get a solution for this?

분류에서Dev

Is there anyway I can get the time of cached file?

분류에서Dev

How do I load content based on first spinner?

분류에서Dev

Can I get content to overlap scroll bar? CSS

분류에서Dev

How can I get $_GET values to a variable

분류에서Dev

Why can't I see gnome extensions?

Related 관련 기사

  1. 1

    How can I make the ul contain li in its content box?

  2. 2

    In a node script how can I monitor a file for changes and get the updated content?

  3. 3

    How to reorder a list in Python based on its content

  4. 4

    Can I see in a log file all GUI based tasks in its alternative command-line format?

  5. 5

    How can I get the latest VLC if its not available from the VLC download page and its PPA

  6. 6

    How can I get the latest VLC if its not available from the VLC download page and its PPA

  7. 7

    How can I get the name of the file I'm uploading

  8. 8

    How can I rename a file containing "&" in its name in kubuntu using Dolphin or terminal?

  9. 9

    How can I get a part of text file by terminal

  10. 10

    How can I get the contents of a file one line at a time?

  11. 11

    How can I get "RHEL 7.0 vmlinux" file?

  12. 12

    How can i get the value of id and its index of a newly created record

  13. 13

    How can I get a relative div to expand to fit its absolute contents?

  14. 14

    How can I automate none-PATH plink (Putty Link) on windows? and get its output?

  15. 15

    How to get a border to size to its content with varying data and footer to adjust

  16. 16

    Why can not the Bash script file run by shortcut get the content of a file?

  17. 17

    How can I get the root terminal to add myself into sudoers file so that I can act as root?

  18. 18

    How can I filter content of a web page?

  19. 19

    Can I get the same object with its vales and use it in a different method?

  20. 20

    How can I convert .mp4 files and .srt file to images based on subtitle's timeline?

  21. 21

    How can I recognize that a certain Linux distribution is based on Debian, looking at file structure?

  22. 22

    How to cp multiple file extensions?

  23. 23

    How can I get a RejectedExecutionException

  24. 24

    How can I get a solution for this?

  25. 25

    Is there anyway I can get the time of cached file?

  26. 26

    How do I load content based on first spinner?

  27. 27

    Can I get content to overlap scroll bar? CSS

  28. 28

    How can I get $_GET values to a variable

  29. 29

    Why can't I see gnome extensions?

뜨겁다태그

보관