Score:0

How do I find folders without a certain name?

bf flag

I'm trying to find a lot of folders, all without a certain bracket. They're movie folders, like so:

Zombie Flesh Eaters (1979) [tmdb-7219]
Zombieland (2009) [tmdb-19908]
Zombieland Double Tap (2019) [tmdb-338967]

But I need to find the ones WITHOUT the [tmdb] bracket.

I'm not sure how to do this?

Quick edit: Tried using the reg-ex \[.*?\] to find everything with a bracket in it, but I'm not sure if that works properly (I'm not that good at regex, but using a reg-ex tester confirms it looks for anything inside brackets).

But I need to find the folders WITHOUT the brackets.. And I don't know how to do this

vanadium avatar
cn flag
How are you finding in the first place? In some cases, a `grep -v` may help you out.
SDXCPTDR avatar
bf flag
Using this string `'\[.*?\]'` when using `find`.. Not even sure that works properly. But I need to find the folders without the bracket :)
vanadium avatar
cn flag
We need this information to be in the question. Use "edit" to clarify your questions. Comments may not be read or may disappear anytime.
muru avatar
us flag
Does your "reg-ex tester" use the same regex flavour as `find`? `.*?` looks like Perl, it doesn't make sense to have `?` after `*` in BRE or ERE.
SDXCPTDR avatar
bf flag
Just tried this one: https://www.regextester.com/ With the result: https://i.imgur.com/mKIBLJC.png -- again sorry; I don't know much about regex :/
Score:2
cn flag

find features the -not operator. Tell it where to search, what kind of item to find (e.g. -type d to find only folders) and what should not be in the name, i.e -not -name '*\[*\]'. Thus

find /somewhere -type d -not -name '*\[*\]'
SDXCPTDR avatar
bf flag
Thanks! This worked very well for me. Just added a `-maxdepth 1` in my case :)
Score:1
in flag
Cas

This could be done easily.

ls -Aq1 | grep -Pv '.*\[tmdb-\d+\].*'

Piping after ls is considered a bad thing in general however, I assume all the folders aren't weirdly named because they're just movie folders, so I think piping after ls is fine here.

I haven't tested it but I'm pretty confident that it should work.

Also, I'm guessing that you'll be putting those folders in a loop to add the id to the name. Pure guess but I assume. Then this is handy because the ls output (because of the -..1) will be in a list. So you can easily do this to process the folders one by one:

ls -Aq1 | grep -Pv '.*\[tmdb-\d+\].*' | \
while read -r level
do
   ...
done

or

ls -Aq1 | \
while read -r level
do
   if ! echo "$level" | grep -Pq '.*\[tmdb-\d+\].*'
   then
      ...
   fi
done
SDXCPTDR avatar
bf flag
This also worked very well. Thank you.
Score:0
hr flag

If you don't need to search recursively, then the bash shell's extglob can do negation using !(pattern) ex.

shopt -s extglob

printf '%s\n' !(*\[tmdb-*\]*)/

The extglob option is probably enabled by default in your interactive bash shell. The trailing / is only necessary if you need to exclude plain files from the match - equivalent to the find command's type -d.

Ex. given a directory with contents

$ ls
'Zombie Flesh Eaters (1979) [something else here]'  'Zombieland (2009) [tmdb-19908]'
'Zombie Flesh Eaters (1979) [tmdb-7219]'            'Zombieland Double Tap (2019) [tmdb-338967]'
'Zombieland (2009)'

then

$ printf '%s\n' !(*\[tmdb-*\]*)/
Zombie Flesh Eaters (1979) [something else here]/
Zombieland (2009)/

or

$ ls -d !(*\[tmdb-*\]*)/
'Zombie Flesh Eaters (1979) [something else here]/'  'Zombieland (2009)/'

Unlike find, this won't match hidden directories by default (although you can include them by setting shopt -s dotglob).

If you do need to search recursively, you can add shopt -s globstar and use **/!(*\[tmdb-*\]*)/ although I'd likely choose find in this case.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.