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

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" \)

Related

Find non empty directories with condition on their names in 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

Deleting a file inside a directory with excluding a folder inside

I have a folder structure like below. I am deleting all files inside /data/mydata/ which are older than 2 hrs using the command below, but I want to exclude the metadata folder. How can I do it?
Command:
/usr/bin/find /data/mydata -mindepth 1 -type d -empty -delete -o -cmin +120 -type f -delete
Directory structure:
/data/mydata/
--test1
--f2.txt
--foo
--metadata
--test1.json
--test2
--metadata
--foo(under metadata)
--test2.json
--f2.txt
To exclude a particular path, you can do:
/usr/bin/find /data/mydata -path /data/mydata/test2/metadata -prune \
-o -mindepth 1 \( \( -type d -empty \) -o \( -cmin +120 -type f \) \) -delete
To exclude all instances of directories named metadata, you probably want:
/usr/bin/find /data/mydata -name metadata -prune \
-o -mindepth 1 \( \( -type d -empty \) -o \
\( -cmin +120 -type f \) \) -delete

How to use -regex in the find command on Linux command line

The objective is to find and list anything with "messages" and/or "error.log" etc.. in the beginning then list both "messages.1..99" and "error.log.1..99" using regular expressions.
This command works for however, it would require me to make many -or searches, but to simplify, I would like to have multiple in a set within the search. Like for instance:
# find /var/log -maxdepth 1 -type f -size +1M -name [messages|error.log|secure.log|kern.log...]?[0-9]|[0-9][0-9] ! -iname "*.gz"
not
# find /var/log -maxdepth 1 -type f -size +1M -name "messages?[0-9]" -o -name "messages?[0-9][0-9]"
How might I perform this command with regular expressions?
# find /var/log -maxdepth 1 -type f -size +1M -name "[messages,error.log,kern,secure]?[0-9]" ! -iname "*.gz"
My attempt with regex doesn't print anything in standard out:
# find /var/log -maxdepth 1 -type f -size +1M -regex -name "[messages,error,kern,secure]?[0-9]" ! -iname "*.gz"
Try this:
find /var/log -maxdepth 1 -type f -size +1M -type f -regextype egrep -regex '.*(messages|error|kern|secure)\.[0-9]+.*' -not -name \*gz

To delete only files that has at least one digit in its file name not directories

I know how to delete only files not directories as follows:
find /path/to/directory -maxdepth 1 -type f -exec rm -iv {} \;
I learned the above snippet from Here
If i want to delete files that has at least one digit in its file name.
find /path/to/directory -maxdepth 1 -type f -exec rm -iv *[0-9]* {} \;
Should this work for my case? Any suggestion?
You can use a glob pattern in -name option and then use -delete option:
find /path/to/directory -maxdepth 1 -type f -name '*[0-9]*' -delete
If -delete is not available then:
find /path/to/directory -maxdepth 1 -type f -name '*[0-9]*' -exec rm -iv {} \;

find command search only non hidden directories

In the following command i want to search only only the directories which are non hidden how can i do this using the following command .Iwant to ignore hidden directories while searching the log file
find /home/tom/project/ -name '.log.txt'
ls /home/tom/project/
dir1
dir2
.backup
.snapshot/
.ignore/
Try
find /home/tom/project -type d -name '.*' -prune -o -name .log.txt -print
This will find all files but ignore those that start with a dot so hidden files.
find /home/tom/project/ -type f \( -iname ".log.txt" ! -iname ".*" \)
EDIT:
If the above those not work, this should do the trick. It has a better regex.
find /home/tom/project/ \( ! -regex '.*/\..*' \) -type f -name ".log.txt"
EDIT2:
The following will exclude hidden folders but will search for the hidden files that have the requested pattern:
find /home/tom/project/ \( ! -regex '.*/\..*/..*' \) -type f -name ".log.txt"
EDIT3:
The grep solution :) if this doesn't work i'm lost :)
find /home/tom/project/ \( ! -regex '.*/\..*/..*' \) -exec grep -l ".log.txt" {} \;
EDIT4:
Have you tried the simple solutions?
find /home/tom/project/ -type f -name ".log.txt"
OR
find /home/tom/project/ -type f -name "*" -exec grep -l ".log.txt" {} \;

Resources