0

I am a beginner with Bash scripting. I have recently been working on a script to harden Ubuntu computers for a school project. Everything is going well, except that the students are not allowed to use any media files(.mp3, .mov, etc), and I am not sure how to do that. What command(s) would I use? grep? I have been researching this for a couple hours now with no luck. It doesn't have to be long. All I need is an example and I can copy it for different extensions. I need it to be able to do the following:

  • Find media files
  • Send file paths to a text file
  • notify a number of media files found

For example, I am looking for it to output something like this:

Searching for media files... 5 media files found! Details can be found in 'media.txt'

Any help is greatly appreciated!

1
  • Try this: find /home -type f -regextype posix-extended -iregex '.*(mp3|wma|mpg|mov)$'. That ought to get you started. Combine with wc -l to see the number of files.
    – muru
    Nov 18, 2014 at 19:36

2 Answers 2

1

To find files of a certain type, I wouldn't search for the file extension. I'd rather scan files for their MIME type using a combination of find and file. This output can then be greped for the desired mimetypesm e.g. audio/* and video/*.

I've created a small script to do so:

#!/usr/bin/env bash
#Don't know whether you want to do so, but this deletes a existing media.txt
rm media.txt
#Find files of desired MIME types and store them in $list
list=$(find | file -if - | grep -P "(audio/.*?;)|(video/.*?;)")
n=0
#Iterate over each line in the list (one file per line)
while read -r line; do
    #Append the filename to media.txt
    echo "$line" | cut -f1 >> media.txt
    #Increase file counter
    n=$(($n+1))
done <<< "$list"
#Output the result
echo "$n media files found! Saved to media.txt"

Note that I use full RegExp with grep. To add some MIME types, build them the same way as I did with the two already included: (type/enc;) and add them with a RegExp OR (|).

PS: As is, the script operates in the current directory. If you want to make it more flexible, add a $1 between find and the following pipe. Now run the script and give the path to search as argument.

2
  • I just ran this code, and it said that there were 1093 files. It turned out that they were not all files... any ideas? Nov 18, 2014 at 20:21
  • Did you search in the correct directory? What files aren't found?
    – s3lph
    Nov 18, 2014 at 21:04
1

Definitely a more verbose option (python) that looks for mimetype(s) using the file command. It is an edited/rewritten version of this one, made fit for your purpose.

What it does

When files are found
it returns a message (in the terminal):

checking for filetypes: image, video, audio...
4 media files found. See for details:  /home/jacob/Bureaublad/found.txt

while a text file is written to the directory/name you defined in the head section of the script

or, if nothing is found:

checking for filetypes: image, video, audio...
no files found

How to use

Copy the script below into an empty file, edit the three lines in the head section of the script:

source_dir = "/path/to/directory"
filetypes = ("image", "video", "audio") # you might want to leave this line as it is, used by the file command
report = "/path/to/report.txt"

save it as find_media.py, and run it in a terminal window by the command:

python3 /path/to/find_media.py

The script:

#!/usr/bin/env python3

import os
import subprocess
# ---
source_dir = "/path/to/directory"
filetypes = ("image", "video", "audio")
report = "/path/to/report.txt"
# ---
print("checking for filetypes: "+(", ").join(filetypes)+"."*3)
found = []

def look_for():
    for root, dirs, files in os.walk(source_dir):
        for name in files:
            file = root+"/"+name
            ftype = subprocess.check_output(
                ['file', '--mime-type', '-b', file]
                ).decode('utf-8').strip()
            if ftype.split("/")[0] in filetypes:
                found.append(file)
    found_files = len(found)
    if found_files != 0:
        print(str(found_files), "media files found. See for details: ", report)
        with open(report, "wt") as out:
            for item in found:
                out.write(item+"\n")
    else:
        print("no files found")

look_for()

You must log in to answer this question.

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