Deleting a file inside a directory with excluding a folder inside - linux

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

Related

-exec option of the find command does not work as expected on Ubuntu

I am doing some practice on find command but I don't get the expected result when I attempt to use -execoption of it. The command I wrote just works without -exec option as the following:
$ find ~ \( -type f -not -perm 0600 \) -or \( -type d -name 'D*' \)
/home/baki/.bashrc
/home/baki/.bash_logout
/home/baki/.cache/motd.legal-displayed
/home/baki/.config/wslu/baseexec
/home/baki/.config/wslu/oemcp
/home/baki/.gitconfig
/home/baki/.landscape/sysinfo.log
/home/baki/.motd_shown
/home/baki/.profile
/home/baki/.ssh/known_hosts
/home/baki/.sudo_as_admin_successful
/home/baki/ssh_start
/home/baki/token
However, when I add the -exec option to the end of the command, it doesn't give any output:
find ~ \( -type f -not -perm 0600 \) -or \( -type d -name 'D*' \) -exec ls -l '{}' ';'
I have searched about it but I couldn't find a piece of useful information that can solve my problem.
Is my command wrong or is it about something else?
Thank you for your help.
The default -and operation has higher precedence than -or. Use extra parentheses:
find ~ \( \( -type f -not -perm 0600 \) -or \( -type d -name 'D*' \) \) -exec ls -l '{}' ';'
You can probably omit the inner parentheses in this case.

FInd patternf for multiple pattern of files

I need to search for multiple pattern of files and check their mtime and if it morethan 30 days then delete all the files. I am using the below command but it's deleting only one pattern of file and not all. Kindly let me know where is the mistake in my command.
find /root -type f \( -name "*.tgz" -o -name "*.bz2" \) -mtime +30 -print -exec rm '{}' +
Try escaping parentheses in the command and adding a wildcard character:
find /root -type f \( -name "*.tgz" -o -name "*.bz2" \) -mtime +30 -exec rm {} \+

Find with or operators

I've a bash command who should find and remove files:
/usr/bin/find /srv/www/vest2/produktion/ -type f \( -iname 'vest*.xml' -o -iname 'vest*.xls'-o -iname 'vwk*.xls' \) -ctime +90 -exec rm {} \ ';'
but it doesn't work.
Whats the problem?
Many thanks
Martin
Remove the single quotes around the final semi colon
Before:
/usr/bin/find /srv/www/vest2/produktion/ -type f \( -iname 'vest*.xml' -o -iname 'vest*.xls'-o -iname 'vwk*.xls' \) -ctime +90 -exec rm {} \ ';'
/usr/bin/find: missing argument to `-exec'
Now:
/usr/bin/find /srv/www/vest2/produktion/ -type f \( -iname 'vest*.xml' -o -iname 'vest*.xls'-o -iname 'vwk*.xls' \) -ctime +90 -exec rm {} \;
/usr/bin/find: `/srv/www/vest2/produktion/': No such file or directory
If that directory existed for me, it would have executed.

using command find, find a directory excluding some elements

I have to find a directory "Dir1" excluding "Dir2" and "Dir3", using command find
$ find . -name Dir1 -type d \( -name Dir2 -o -name Dir3 \) \
-prune -o -print
dosen't work
Move -prune to the beginning:
find . \( -name Dir2 -o -name Dir3 \) -prune -o -name Dir1 -type d

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