How can I find all video files on my system?

31,318

Solution 1

Alternative: search on file:

sudo find . -type f -exec file -N -i -- {} + | grep video

or if you only want the filenames ...

sudo find . -type f -exec file -N -i -- {} + | sed -n 's!: video/[^:]*$!!p'

-N, --no-pad: Don't pad filenames

-i, --mime: Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say 'text/plain; charset=us-ascii' rather than 'ASCII text'. In order for this option to work, file changes the way it handles files recognized by the command itself (such as many of the text file types, directories etc), and makes use of an alternative 'magic' file. (See the FILES section, below).


The FILES section points to:

Files

/usr/share/misc/magic.mgc Default compiled list of magic.

/usr/share/misc/magic Directory containing default magic files.


file is slowwwwwwwwwwwwwwwww though (it will open all the files find finds) but has the advantage you do not need to add all those extentions.


Using locate:

locate *.mkv *.webm *.flv *.vob *.ogg *.ogv *.drc *gifv *.mng *.avi$ *.mov *.qt *.wmv *.yuv *.rm *.rmvb *.asf *.amv *.mp4$ *.m4v *.mp *.m?v *.svi *.3gp *.flv *.f4v

Solution 2

I imagine this could be done as a 1-liner but it seemed a bit cumbersome so I created a script for ease of launching and editing and called it findvids.sh This is what worked for me.

Note: I may not have covered ALL the video file types, but I'm sure I have most of them. One notable exception is .mkv as that is the target format for the project and I don''t need to find the files that have already been processed. It should be very simple to add additional formats (extensions) to the script to suit your needs by examining the pattern and adjusting accordingly while maintaining the quotes at the beginning and end of the expression. Note that files you don't have permission to read will not be found.

#!/bin/bash
#This script is intended to find virtually all video file formats.
find /. -type f | grep -E "\.webm$|\.flv$|\.vob$|\.ogg$|\.ogv$|\.drc$|\.gifv$|\.mng$|\.avi$|\.mov$|\.qt$|\.wmv$|\.yuv$|\.rm$|\.rmvb$|/.asf$|\.amv$|\.mp4$|\.m4v$|\.mp*$|\.m?v$|\.svi$|\.3gp$|\.flv$|\.f4v$"

Edit based on comment: The $ at the end of the extension signifies that the search term must be found at end of line. if we wanted to match the beginning of the line instead we'd use ^ before the term we intend to match. You can find these anchors explained in more detail here.

I did a speed comparison to using locate and the results were:

time locate *.mkv *.webm *.flv *.vob *.ogg *.ogv *.drc *gifv *.mng *.avi *.mov *.qt *.wmv *.yuv *.rm *.rmvb *.asf *.amv *.mp4$ *.m4v *.mp *.m?v *.svi *.3gp *.flv *.f4v

real    0m8.887s
user    0m5.814s
sys 0m0.052s

vs.

time find /. -type f | grep -E "\.webm$|\.flv$|\.vob$|\.ogg$|\.ogv$|\.drc$|\.gifv$|\.mng$|\.avi$|\.mov$|\.qt$|\.wmv$|\.yuv$|\.rm$|\.rmvb$|/.asf$|\.amv$|\.mp4$|\.m4v$|\.mp4$|\.m?v$|\.svi$|\.3gp$|\.flv$|\.f4v$"

real    0m2.795s
user    0m0.657s
sys 0m1.115s

Unexpectedly find is faster. I'll be using this approach.

Edit: further testing indicates that locate was faster on a different machine. I think my initial speed test results were bunk due to caching.

Sources:

man find

man grep

https://stackoverflow.com/questions/7190565/unix-find-multiple-file-types

Solution 3

In the find command in Elder Geek's answer, case sensitivity caught me out. My devices seem to have used capitals quite often when taking videos etc. Just need to add -i option to grep to fix this:

find /. -type f | grep -iE "\.webm$|\.flv$|\.vob$|\.ogg$|\.ogv$|\.drc$|\.gifv$|\.mng$|\.avi$|\.mov$|\.qt$|\.wmv$|\.yuv$|\.rm$|\.rmvb$|/.asf$|\.amv$|\.mp4$|\.m4v$|\.mp*$|\.m?v$|\.svi$|\.3gp$|\.flv$|\.f4v$"
Share:
31,318

Related videos on Youtube

Elder Geek
Author by

Elder Geek

Updated on September 18, 2022

Comments

  • Elder Geek
    Elder Geek over 1 year

    I tried using the Unity Lens File search for *.* and filtering by Last Modified=All, Type=Videos, and Size=All, but found nothing, although I know there are some .3gp files in my Pictures folder. I know that I have numerous video files on another drive mounted on the file system as well.

    I compiled a list of likely extensions and I tried numerous iterations of the find command, utilizing these with:

    find ./ -name "*.ext" -o (etc, etc, ad nauseum)
    

    with no luck whatsoever.

    What is the solution?

    • Jo-Erlend Schinstad
      Jo-Erlend Schinstad over 7 years
      I haven't tried it, but there was an article on OMG! Ubuntu about the FSearch app. You may want to check it out. omgubuntu.co.uk/2016/10/fsearch-fast-file-search-tool-linux
    • Elder Geek
      Elder Geek over 7 years
      @Jo-ErlendSchinstad Thank you for the suggestion but as I'm getting my results via the CLI in under 3 seconds, I'm satisfied with this approach. I will look at it though. :-)
    • Jo-Erlend Schinstad
      Jo-Erlend Schinstad over 7 years
      But unless you understand the command, you're going to forget it. And if you did understand the commands, then you wouldn't have asked the question. Let's improve Ubuntu by making things easier for those who just want to get the job done. You can do that by adding apps or by pointing people to apps that are already there.
    • Elder Geek
      Elder Geek over 7 years
      @Jo-ErlendSchinstad I often begin lacking understanding which I obtain via research and analysis. I believe this approach is vital to development. The discovery process led me to a solution that I've chosen to share here as I've created a script and placed it in ~/bin I'm unlikely to forget and know where to look if I do. Since it's in my answer as well, anyone with the motivation can parse and understand it as well.
    • Jo-Erlend Schinstad
      Jo-Erlend Schinstad over 7 years
      You're right. We don't actually need desktops. We can do nearly everything using command lines. But if we want to grow, we must realize that most people don't want to learn that kind of thing. I know developers who can't even describe the differences between the stack and the heap.
    • Elder Geek
      Elder Geek over 7 years
      That's true. Many people don't want to learn much of anything, but I believe there is more to be gained by encouraging learning than accepting that it won't occur. I could give you a fish or I could teach you to fish. The former would feed you for a day, the latter for life. Perhaps those developers you name would benefit from this: quora.com/What-is-the-difference-between-the-stack-and-the-h‌​eap
  • Rinzwind
    Rinzwind over 7 years
    touch hello.mp4 does not create a video file >:-D and you might want to change it to locate to speed it up.
  • Elder Geek
    Elder Geek over 7 years
    @Rinzwind Valid point regarding touch However since that hasn't been done on this system, it's not really relevant in this case. As far as locate is concerned I'd then have to run updatedb prior to in order to have current data would I not?
  • Elder Geek
    Elder Geek over 7 years
    Which "FILES section, below"
  • Rinzwind
    Rinzwind over 7 years
    Yes, updatedb is done every day so if you could assume you don't need to last few hours (the speed it returns the results is a magnitude higher than with find :D )
  • Rinzwind
    Rinzwind over 7 years
    added! it was hidden in the man page :D
  • Elder Geek
    Elder Geek over 7 years
    @Rinzwind I'd love to see a working example of this using locate.
  • Rinzwind
    Rinzwind over 7 years
    added a locate.
  • Elder Geek
    Elder Geek over 7 years
    That's odd, I would have expected locate to be faster, but with your locate command I get time:real 0m8.985s and with my find command I get time:real 0m2.595s
  • Rinzwind
    Rinzwind over 7 years
    Oh... locate does all files. Your find is from where you are does it not?
  • Elder Geek
    Elder Geek over 7 years
    I did add the -e switch which didn't have much impact but returning results for files that are already gone didn't seem helpful. granted sys time is much lower for locate but real time to results is all I really care about.
  • Elder Geek
    Elder Geek over 7 years
    Yes, locate does all files. I'm running find from / so I don't think that's making a difference. more likely it's because it's skipping entire directories that my user account doesn't have access to (which is fine as their shouldn't be any video files there anyway. Also your locate command is finding .mkv files which I'm skipping as they are already processed.
  • Rinzwind
    Rinzwind over 7 years
    locate probably also does a run for every file type (it lists them in order of extension)
  • Elder Geek
    Elder Geek over 7 years
    The downside of the first find command is grep matching the character string "video" in filenames that aren't videos, such as text, etc.. The second one doesn't suffer from this shortcoming. They are both quite a bit slower than the approach I took undoubtedly due to the number of processes invoked. All that being said, it's a thourough and viable approach.
  • Hashim Aziz
    Hashim Aziz about 5 years
    What's the $ doing in *.mp4$? I'm not familiar with that syntax, is it functioning as a wildcard?
  • Rinzwind
    Rinzwind about 5 years
    $ at the end means "ending with .mp4. If filters out anything that has .mp4 in the name but does not end with .mp4.
  • rofrol
    rofrol over 4 years
    Unbreakable.Extras3.Nights.First.Fight.Sequence-Grym.sub: video/mpeg; charset=binary Why sub is treated as video?
  • Rinzwind
    Rinzwind over 4 years
    Extensions can be anything. It is the 1st byte of the contents that determine what a file is.
  • sgflt
    sgflt over 4 years
    Please do not use sudo where it is not necessary. Find command can work without higher privileges.
  • Rinzwind
    Rinzwind over 4 years
    @sgflt it IS needed here. The exec will error out as it is not allowed to scan the file.
  • sgflt
    sgflt over 4 years
    In this case the problem is in access rights on specific filesystem. I actually have used command succefully without sudo so I think the privilege escalation should not be part of the solution because it just complicates command without explanation. (And in most general cases sudo is really not necessary.)
  • Raleigh L.
    Raleigh L. over 2 years
    This has a false-positive in the case where you have a file and any part of its filename (including parent directories, etc.) have the string 'video' in it.