Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Testing if a file has a particular extension

    I am hoping for some ideas on testing if a file has an extension passed as a command line argument. The challenge involves testing multiple extensions in a script. For example, if the file is a m3u, sfv, or xml file then cat it and perform some command. I don’t think the following works for me; for FILE in *. m3u *. sfv *. xml.

    I chose FIND for added portability and additional options. I have gotten this far.

     find . -depth \(-name ’*.sfv’ -o -name ‘*.m3u’\) -type f -print
    The Bash script should accept command line arguments with several extensions. --ext *.m3u,*.xml,*.sfv.

    The script performs recursive searches and finds files with these extensions. Hard coding the extension would be the easy part. After finding the said files, it iterates (while loop) through an external file with regular expression patterns. Then it performs replacements inside the file and output a new file. This Perl line helps me achieve this goal;

    perl -pi -e "s/$REGEX/$REPLACE/g" $FILE
    This portion script complements the main purpose of renaming files. So the script rename a bunch of files (right now it consist of mp3 files) then renames the files in the playlist (*.m3u) and sfv (*.sfv) files. I may also need to grep for lines with mp3 line, because the script should perform the replacements on the mp3 pointers since these were the files renamed.

  2. #2
    Here's an approach that worked for me.

    find ./ -regex ".*\(m3u\|sfv\|xml\)$" -type f -print

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts