12

I had downloaded a video a few months back. I'm not very well remembering the name by which it is saved. Is there any command or any method that will output only video files so that I can search for my video there? From man pages, I couldn't find any option of find doing that work.

The video file I downloaded may have any extension (like webm etc) and also it may be possible that I had that time changed the name to anything like 'abcde' which I don't remember now. So the search can't be based on file name!

(Just mentioning one similarity: In perl there are commands to check whether a file is text file or binary , etc. Similarly there may be a way to check if its video file or multimedia file)

3 Answers 3

15

The basic idea is to use the file utility to determine the type of each file, and filter on video files.

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

This prints the names of all files in /some/directory and its subdirectories recursively whose MIME type is a video type.

The file command needs to open every file, which can be slow. To speed things up:

  • Restrict the command to the directory trees where it's likely to be, such as /tmp, /var/tmp and your home directory.
  • Restrict the search to files whose size is in the right ballpark, say at least 10MB.
  • Restrict the search to files whose modification time is in the right ballpark. Note that downloading the file may have set its modification time to the time of download or preserved the time, depending on what program you used to download (and with what settings). You may also filter on the inode change time (ctime), which is the time the file was last modified or moved around (created, renamed, etc.) in any way.

Here's an example which constrains the modification time to be at least 60 days ago and the ctime to be no more than 100 days ago.

find /tmp /var/tmp ~ -type f -size +10M \
     -mtime +60 -ctime -100 \
     -exec file -N -i -- {} + |
sed -n 's!: video/[^:]*$!!p'
4
  • Your 1st command fetches many other (other than video) files. For example one line of the output is :/home/ravbholua/Downloads/Music_Command_line/[SOLVED] a code question regarding music file extensions_files/avatar774785_6.gif: image/jpeg; charset=binary Another line of output is : /home/ravbholua/Free Computer Networking Books Download | Ebooks Online Textbooks.html: text/html; charset=iso-8859-1 I need only video files that will run in vlc player, etc
    – Ravi
    Dec 25, 2013 at 10:48
  • @Ravi I'd accidentally left out the option to sed to exclude non-matching lines (and also the option to file to not break lines in the wrong place), see my edit. Dec 25, 2013 at 16:28
  • Yes Gilles, that's what I wanted. That has worked superbly! After running your command, I have come to know that my system has many videos (some complete and some incomplete) which I wasn't aware of. Thank you.
    – Ravi
    Dec 26, 2013 at 7:42
  • 1
    This is the ideal solution if your video files don't have predictable extensions. Resorting to file is the only option.
    – migdsb
    Nov 17, 2014 at 11:03
12

Searching by file name

As Chris mentioned in his answer you can use find to do this but I find it much faster to search through the locate database.

Assuming your distro provides this facility, most of the big ones do, Ubuntu, Fedora, CentOS, etc.

Example

$ locate --basename .mp4 .mkv .wmv .flv .webm .mov .avi | head -5
/home/saml/Desktop/sample_mpeg4.mp4
/home/saml/Downloads/Karrolls_Christmas/Karroll's Christmas (2004) part 1.mp4
/home/saml/Downloads/Karrolls_Christmas/Karroll's Christmas (2004) part 10.mp4
/home/saml/Downloads/Karrolls_Christmas/Karroll's Christmas (2004) part 2.mp4
/home/saml/Downloads/Karrolls_Christmas/Karroll's Christmas (2004) part 3.mp4

Searching by file type

To find the files by type you can use the command file to get a list of info about a particular file's type.

Here's a rough list of these file types from my system, Fedora 19.

  • .mp4: ISO Media, MPEG v4 system, version 1
  • .mkv: EBML file, creator matroska
  • .wmv: Microsoft ASF
  • .flv: Macromedia Flash Video
  • .webm: WebM
  • .mov: ISO Media, Apple QuickTime movie
  • .avi: AVI

You can use this command to find all the files in your /home/<user> directory.

$ find /home/<user> -type f -exec file {} + | \
    grep -E "MPEG v4|EBML|\
      Microsoft ASF|Macromedia Flash Video|WebM|Apple QuickTime movie|AVI"

Alternatively you can use file and search by mime-types that are categorized as "video".

 -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”.

Adapting what we've done above to something like this:

$ find /home/<user> -type f -exec file -i {} + | grep video

You can use sed to get just the filenames:

$ find /home/<user> -type f -exec file -i {} + |
    sed -n '/video/s/:[^:]\+$//p'
3
  • +1, this also, of course, not only assumes that locate is installed, but that the database is built.
    – Chris Down
    Dec 24, 2013 at 2:19
  • Yes also the database usually get's built nightly on most distros, so files recently added won't be in the DB until the nightly job has run and added them.
    – slm
    Dec 24, 2013 at 2:20
  • 1
    You forgot one of the most common, avi. Dec 25, 2013 at 0:45
7

Since you now specify that it can't be based on extension, you can also peruse file (warning: this could take a long time):

find ~ -type f -exec file -i {} + | grep video 

If you could have searched by filename, assuming that it has a common video extension and it is somewhere in your home directory:

find ~ -type f -name '*.mkv' -o -name '*.mp4' -o -name '*.wmv' -o -name '*.flv' -o -name '*.webm' -o -name '*.mov'

You can also use a regex:

find ~ -type f -regex '.*\.\(mkv\|mp4\|wmv\|flv\|webm\|mov\)'

If you have a good idea about when you downloaded it (and assuming the mtime was set to that time), you could also try narrowing it down with GNU find's -mtime option.

4
  • 1
    You could also show all files larger in size than some threshold (assuming video files are very large)... Dec 24, 2013 at 2:13
  • Chris Down, that way of using file name won't work. I have edited my Q. please have a look
    – Ravi
    Dec 24, 2013 at 2:14
  • @Ravi See my updated answer, which uses mimetypes.
    – Chris Down
    Dec 24, 2013 at 2:23
  • Your 1st command is fetching many other (other than video) files even. For ex., one line from the output is: /home/ravbholua/Downloads/Music_Command_line/Any s_w to cut_add audio files, convert video to audio?_files/post_old.gif: image/gif; charset=binary Other line from the output is: /home/ravbholua/Downloads/Music_Command_line/command line - how to start a video from the terminal? - Ask Ubuntu.html: text/html; charset=utf-8 It isn't solving my purpose.
    – Ravi
    Dec 25, 2013 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .