Validate File Types by Reading the First Few Bytes of a File

preview_player
Показать описание
Quickly check if a file is a jpg, png, etc.. We'll use the od command line tool or Python / Ruby.

Hit the subscribe button to receive more videos like this!

REFERENCE LINKS
---------------------------------------------------

COURSES
---------------------------------------------------
Courses I've created that focus on web dev and deployment topics.

THE TOOLS I USE / GEAR
---------------------------------------------------

FOLLOW ME ELSEWHERE
---------------------------------------------------

TIMESTAMPS
---------------------------------------------------
0:00 -- Intro
0:16 -- File types typically have well known starting bytes
0:51 -- All 3 ways will be constant time O(1)
1:15 -- Using the od (octal dump) built-in command
4:52 -- Constant vs linear time with getting the last bytes
8:57 -- Python version
11:54 -- Ruby version
15:19 -- Using a library to help detect file types
Рекомендации по теме
Комментарии
Автор

file command is useful one, but when you need to see the details and then manually check it, your solution looks nice.

adrianbiro
Автор

This was interesting, thanks!
P.s. I prefer "head -1" over "head -n 1".

yomajo
Автор

xxd image.png | head -1 | cut -d\ -f2
will give us the same numbers.
Peculiar thing is code (the other way around) also works:
cut -b -2 afisha.png | head -1 | xxd
(and it is even colored one) - cut can work with bytes

samoylov
Автор

the magic number if I am not mistaken.

uxjkdkn