Combine two find commands in one - linux

I currently have the following line:
find . -type f ! -name "*.xml" -delete && find . -type d -empty -delete
But as they are 2 find commands, I suppose I can merge them into one. But I don't want to use -exec.
I wan't to use something native in bash, that comes preinstalled with any linux distro.

You can group primaries with the -o ("or", as opposed to the implict "and" that find applies to its primaries) primary. The parentheses are escaped to avoid shell syntax errors and ensure they are passed as arguments to find.
find . \( \( -type f ! -name "*.xml" \) -o \( -type d -empty \) \) -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 {} \+

Using "find … -delete" to remove image files only from special subdirectories

I need to clean up some subdirectories inside my music collection from unwanted images. I’m using Ubuntu 14.04 and/or Linux Mint 17.2 and a bash shell script using the find command.
The directory structure (minimal example) is as follows:
Tagged
Artist #1
Artist #1 - An Album
Artist #1 - A Track.flac
cover.jpg
something.png
Artist #1 - [compilations]
Artist #1 - A Track.flac
cover.jpg
something.png
Artist #2
Artist #2 - Another Album
Artist #2 - A Track.mp3
cover.jpg
Only in the subfolders ending with "[compilations]", I want to delete all kind of jpeg and/or png images (because the tagging software erroneously puts them there). Any images that happen to be in normal "album" folders I wish too keep.
With my directory structure, folders with "[compilations]" in the name can only happen just below the "Artist" folders, repectively; so a maximum of two levels deep.
I came up with the following find command:
$ cd Tagged
$ find . -maxdepth 2 -type d -name '*\[compilations\]' -exec find {} -type f -iname '*.jp*g' -or -iname '*.png' -delete \;
This seems to do something and takes a while, but the files "./Artist #1/Artist #1 - [compilations]/cover.jpg" and "./Artist #1/Artist #1 - [compilations]/something.png" are still there (and all other image files).
Being quite new to Linux, I assume I make a dumb mistake using find's -delete option, because the following command (without -delete) shows the files correctly:
$ find . -maxdepth 2 -type d -name '*\[compilations\]' -exec find {} -type f -iname '*.jp*g' -or -iname '*.png' \;
./Artist #1/Artist #1 - [compilations]/cover.jpg
./Artist #1/Artist #1 - [compilations]/something.png
So here are my questions:
Why does the -delete not work?
Is this command really safe regarding "extravaganza" like whitespace, glob characters and foreign characters in the paths and filenames?
How would I have to rewrite the above command, still using bash and find?
Could the command be optimized (re speed, safety, nested finds)?
In the actual collection, the command must traverse 16899 folders, almost all of them contain whitespace and foreign characters (like Czech, Russian, Japanese, Greek, German …), so it must be robust.
Thanks in advance for any insights and some enlightenment!
Your -delete predicate only applies to the
-iname '*.png'
predicate, because you missed groupings: when you give find the following:
-type f -iname '*.jp*g' -or -iname '*.png' -delete
because of the precedence of the boolean operators, find understands:
\( -type f -iname '*.jp*g' \) -or \( -iname '*.png' -delete \)
To fix this, use:
-type f \( -iname '*.jp*g' -or -iname '*.png' \) -delete
I'd suggest that to experiment you replace -delete with -print: you'll see what the -delete applies to!
Now, regarding your nested find: because of the structure of your directory tree (your files are only in depth 3), you should be able to do with only one instance of find:
find -maxdepth 3 -path '*/*\[compilations\]/*' \( -iname '*.jp*g' -o -iname '*.png' \) -type f -print
(I put -print instead of -delete so that you can check the command before executing it with -delete).
After some experimentation, I think my error was in not putting the OR'ed part in parentheses—it seems find used the -delete only on the right part of the last OR, i.e., tried to delete '*.png'. Alas, almost all of my cover images were '*.jpg' so I thought it wouldn't work at all!
So I think the corrected command should be:
$ find . -depth -maxdepth 2 -type d -name '*\[compilations\]' -exec find {} -type f \( -iname '*.jp*g' -or -iname '*.png' \) -delete \;
It seems to work correctly on my test case above.
Nevertheless, some confirmation would be nice. An maybe some answers to my other questions, just for information and learning. Thank you!

linux find command is not taking proper argument

find . -maxdepth 1 ! -path . -type f ! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE}
I am trying to execute above command inside the script where ${FILE_PATTERN} and ${DAYS_AFTER_ARCHIVE} are the variable provided while executing the script. The variable value for ${FILE_PATTERN} would be ! -name "*warnings*".
I am looking for executing above command as like below command in script
find . -maxdepth 1 ! -path . -type d ! -name "*warnings*" -mtime +7
I am providing file pattern argument as "! -name "warnings""
but receiving following error message
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
suggest on above.
First of all
-name "*.gz" ${FILE_PATTERN}
has too many option values (this is what usually causes the message shown)
If you use bash or similar, escape the exclamation marks
find . -maxdepth 1 \! -path . -type f \! -name "*.gz" ${FILE_PATTERN} -mtime +${DAYS_AFTER_ARCHIVE}
I am providing file pattern argument as "! -name "warnings"" but receiving following error message
You can't combine flags and their values like that. Also, you can't nest " like that. So, it could be like
! -name "$FILE_PATTERN"
If you're using BASH you can make use of BASH arrays:
# find options
FILE_PATTERN=(! -name "warnings*")
# build the find command
cmd=(find . -maxdepth 1 ! -path . -type f \( ! -name "*.gz" "${FILE_PATTERN[#}}" \) -mtime +${DAYS_AFTER_ARCHIVE})
# execute the command
"${cmd[#]}"
If not using BASH then you will have to use eval with caution:
FILE_PATTERN='! -name "warnings*"'
eval find . -maxdepth 1 ! -path . -type f ! -name "*.gz" "${FILE_PATTERN}" -mtime +${DAYS_AFTER_ARCHIVE}

How to recursively delete multiple files with different extensions?

I am trying to write a command to remove recursively several files with different extensions (*.extension1, *.extension2 etc) from the current directory and all its related sub-directories.
So far I got this command from another post but I couldn't workout how to adapt it to more than one file in the same command line:
find . -name "*.extension1" -type f -delete
Is it as simple as below?
find . -name "*.extension1";"*.extension2" -type f -delete
Just as a side note, these are all output files that I do not need, but not all are necessarily always output so some of the extensions might not be present. This is just as a general clean-up command.
find . \( -name "*.extension1" -o -name "*.extension2" \) -type f -delete
find Documents ( -name ".py" -o -name ".html" ) -exec file {} \;
OR
find . -regextype posix-egrep -regex ".*\.(extension1|extension2)$" -type f -delete
Just add more options. A regex solution can also apply but this one's better and safer.
find . \( -name '*.ext1' -or -name '*.ext2' \) -type f -delete
Edit: You probably need -or as well. Before deleting, test it first without the -delete option. (2) Added a pair of () as suggested by gniourf_gniourf.
Maybe regexp will help you
find . -regextype posix-awk -regex "(.*.ext1|.*.ext2)" -type f -delete
Another solution using rm:
rm -rf ./**/*.{txt,nfo,jpg,jpeg,png,exe,url}
If you want to delete other files too for e.g. those starting with sample. just add that too:
rm -rf ./**/*.{txt,nfo,jpg,jpeg,png,exe,url} ./**/*/sample.*
This simple command will delete all the files with extension1 and extension2 recursively in that directory.
rm find . -name *.extension1 -o -name *.extentions2

Resources