Find non empty directories with condition on their names in linux - linux

There's a directory that has several subdirectories. These subdirectories' names are the date that the subdirectory was created. Now I want to list the subdirectories created in 'June' of 2021 and are not empty, so their names all contain this: 202106*.
How can I do this?
The command I use to list non-empty directories is this:
find . -mindepth 1 -maxdepth 1 -not -empty -type d
But I don't know how to set the name condition.

The name is specified by -name
find . -mindepth 1 -maxdepth 1 -not -empty -type d -name '202106*'
For more information read the find man page

suggesting
find . -type d -path "*202106*" -not -empty

Related

How to exclude multiple subdirectories (same directory name) when using find command to delete files older than 30 days in a batch file?

Given the below linux directory structure, how would I skip each excluded directory, (/assess), to remove files older than 30 days for the rest of the entire directory structure?
Thanks for your assistance...
/mnt/nfsmountpoint/location1/appliance1
/assess
/discover
/bkup
/mnt/nfsmountpoint/location1/appliance2
/assess
/discover
/bkup
etc...
I cobbled together an answer and proofs:: (my asterisks * are not showing sorry)
Run from the /mnt/nfsmountpoint/ directory.
find .// -not -path "/assess/" -type f -mtime +30 -delete
validate::
Does it skip the directory?:
find .// -not -path "/assess/" -type f -mtime +30 -ls|more
Verify no current month (January 2021) files included?:
find .// -not -path "/assess/" -type f -mtime +30 -ls|grep Jan
How much space is made free?:
find .// -not -path "/assess/" -type f -mtime +30 -print0 | du --files0-from=- hc | tail -n1
find /path/to/dir -mtime +30 -type f -not -name "*assess*" -delete
Find files (-type f) in /path/to/dir as well as children directories. Specify only files that have been modified more than 30 days ago (-mtime +30) and do not include files that contain "assess" (-not -name "assess")

Exclude hidden files and folders in linux find

I am trying to exclude hidden files and folders when doing a find in linux.
I have to exclude files or folders that start with a dot (.hidden) but also have to exclude folders that start with an # (like #eaDir).
So far I have the following command which seems to work but maybe there is a more elegant way?
find /path/to/start/search/ -not -path '*#eaDir*' -not -path "*/\.*" -type f -mtime -2
I did see examples using regular expression like so:
find . \( ! -regex '.*/\..*' \) -type f
but not sure how I would also exclude #eaDir directories with the -regexoption?
I believe there can also be hidden files that start with two dots? like "..hidden"? Is this already covered with my command or would I simply add a third option like -not -path "*/\..*" to exclude those as well?
Then I saw some examples of using -prune so that find won't descend in hidden directories, however I am unsure how I would use this correclty in my example. I would be interested in this to speed things up.
Thanks!
Use -not -name '.*'. This will exclude any names that begin with ..
Exclude files and folders starting with a . or an #:
find /path/to/start/search/ -not -path '*/[#.]*' -type f -mtime -2
Exclude files starting with a . and files and folders starting with a . or an #:
find /path/to/start/search/ -not -path '*/.*' -type f -mtime -2 | grep -v '/#.*/.*'

How display only files inside subdirs using the -haltr format?

How do I add the output looks of ls -haltr into the command below?
find 20180913/ -maxdepth 5 -not -type d -and -not -name '.*'
Basically I am trying to list only files inside my subdirs but need to display the file time stamp sorted by date with latest at the bottom.
Thank you
You can try using the exec option of find:
find 20180913/ -maxdepth 5 -not -type d -and -not -name '.*' -exec ls -haltr {} +
Updated my answer based on PesaThe's correction.

find files or directories that are 30 or more days old, recursively, BUT starting the search from specific directory

I have been searching but I cannot find a way to essentially do the following in 1 line at Linux, so as to find files and directories that are more than 30 days old, starting the recursive search from script_dir:
cd $script_dir
find . -type f -or -type d -mtime +30
If I do not do the cd to change to directory that I need to start searching from recursively (and use directly only the find), then, although I specify the script_dir at find the recursive search starts from the directory I am currently and NOT from the script_dir and beneath this directory. I want to do something like the following and even if I am currently at other directory than script_dir, the recursive search to start from script_dir:
find $script_dir -type f -or -type d -mtime +30
Thank you.
In one line, you can do like this :
cd /path/to/directory && find . -type f -or -type d -mtime +30
that do the search from the specified directory

How to stop find command from listing folders with dot(.)

In my home dir , i have folders like .cpan .cpcpan and they are also showing.
How i hide them . i am using
find /home -mindepth 1 -maxdepth 1 -type d -printf "%f\n"
Prune them.
find /home -mindepth 1 -maxdepth 1 -type d \( -name '.*' -prune -o -printf "%f\n" \)

Resources