Friday, April 26, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  114] [ 0]  / answers: 1 / hits: 61389  / 3 Years ago, sat, july 17, 2021, 1:33:03

Yes, I'm sorting out my music. I've got everything arranged beautifully in the following mantra: /Artist/Album/Track - Artist - Title.ext and if one exists, the cover sits in /Artist/Album/cover.(jpg|png).



I want to scan through all the second-level directories and find the ones that don't have a cover. By second level, I mean I don't care if /Britney Spears/ doesn't have a cover.jpg, but I would care if /Britney Spears/In The Zone/ didn't have one.



Don't worry about the cover-downloading (that's a fun project for me tomorrow) I only care about the glorious bash-fuiness about an inverse-ish find example.


More From » bash

 Answers
3

Case 1: You know the exact file name to look for


Use find with test -e your_file to check if a file exists. For example, you look for directories which have no cover.jpg in them:


find base_dir -mindepth 2 -maxdepth 2 -type d '!' -exec test -e "{}/cover.jpg" ';' -print

It's case sensitive though.


Case 2: You want to be more flexible


You're not sure of the case, and the extension might be jPg, png...


find base_dir -mindepth 2 -maxdepth 2 -type d '!' -exec sh -c 'ls -1 "{}"|egrep -i -q "^cover.(jpg|png)$"' ';' -print

Explanation:



  • You need to spawn a shell sh for each directory since piping isn't possible when using find

  • ls -1 "{}" outputs just the filenames of the directory find is currently traversing

  • egrep (instead of grep) uses extended regular expressions; -i makes the search case insensitive, -q makes it omit any output

  • "^cover.(jpg|png)$" is the search pattern. In this example, it matches e.g. cOver.png, Cover.JPG or cover.png. The . must be escaped otherwise it means that it matches any character. ^ marks the start of the line, $ its end


Other search pattern examples for egrep:


Substitute the egrep -i -q "^cover.(jpg|png)$" part with:



  • egrep -i -q "cover.(jpg|png)$" : Also matches cd_cover.png, album_cover.JPG ...

  • egrep -q "^cover.(jpg|png)$" : Matches cover.png, cover.jpg, but NOT Cover.jpg (case sensitivity is not turned off)

  • egrep -iq "^(cover|front).jpg$" : matches e.g. front.jpg, Cover.JPG but not Cover.PNG


For more info on this, check out Regular Expressions.


[#35123] Saturday, July 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fenddy

Total Points: 361
Total Questions: 103
Total Answers: 113

Location: Turkmenistan
Member since Sun, Aug 2, 2020
4 Years ago
fenddy questions
Tue, Nov 22, 22, 10:11, 1 Year ago
Tue, Sep 27, 22, 09:16, 2 Years ago
Wed, Dec 28, 22, 13:09, 1 Year ago
Fri, Jun 18, 21, 14:04, 3 Years ago
;