How to remove certain pattern files except another certain pattern files from a list? - linux

I have many file with name chr1_gene_*.raw. I would like to keep some of them. So I use following command.
find . -maxdepth 1 -type f -name "*.raw" -not -name "chr1_gene_448.raw" -not -name "chr1_gene_1914.raw" -not -name "chr1_gene_2456.raw" -not -name "chr1_gene_1554.raw" -not -name "chr1_gene_2024.raw" -not -name "chr1_gene_35.raw" -not -name "chr1_gene_509.raw" -not -name "chr1_gene_1952.raw" -not -name "chr1_gene_575.raw" -not -name "chr1_gene_2249.raw" -not -name "chr1_gene_272.raw" -not -name "chr1_gene_2158.raw" -exec rm -rf {} \;
Sometimes there are too many files I want to keep. I do not want to type "-not -name " too many times. Is there a way to put a list in "-not -name"?

You may achieve this using a script say notnamescript.sh :
#!/bin/bash
while read line
do
echo "-not -name " $line
done<notnamelist
Put all the -not -name names in a file called notnamelist. Remember there
should be no trailing empty lines.
find . -maxdepth 1 -type f -name "*.name" $( ./notnamescript.sh ) -exec rm -rf {} \;

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.

Linux find -type f ignored

I'm creating a cron job, that will find all *.log* files (it will be used to remove them later, when it works).
The find command looks like this:
find /data/dg \( -path /data/dg/kf/data -o -path /data/dg/pg/data \) -prune -o -name "*.log*" -type f
And it should find all files with name ".log" that are not in directories /data/dg/kf/data and /data/dg/pg/data
However the output this command gives contains also the directories.
...
/data/dg/kf/log/controller.log.2019-09-08-22
/data/dg/kf/log/server.log.2019-09-09-07
/data/dg/kf/data
/data/dg/pg/log/postgresql-2019-09-27_000000.log
/data/dg/pg/log/postgresql-2019-09-27_100859.log
/data/dg/pg/log/postgresql-2019-09-27_102411.log
/data/dg/pg/data
/data/dg/sim/log/sim_2019-09-27-11.0.log
/data/dg/sim/log/sim_2019-09-27-12.0.log
...
It seems that -type f doesn't work. What's wrong?
put -type f right after /data/dg
find /data/dg -type f -not -path "/data/dg/kf/data*" -not -path "/data/dg/pg/data*" -name "*.log*"

'Sed' not working on result of 'find' with multiple parameters

I'm trying to do a find and replace function, finding files which match a criteria then find/replace text within them.
Find statement (works find and returns list of files):
find / -type f -name "*.properties" -o -name "*.xml" -not \( -path '/tmp/*' -o -path '/var/tmp/*' \)
Sed find/replace:
sed -i 's/find/replace/g' {} \;
Putting together:
find / -type f -name "*.properties" -o -name "*.xml" -not \( -path '/tmp/*' -o -path '/var/tmp/*' \) -exec sed -i 's/10\.32\.19\.156/10.32.19.165/g' {} \;
However this does not seem to work. Removing some 'find' parameters causes it to work, for example this works:
find / -type f -name "*.properties" -exec sed -i 's/10\.32\.19\.156/10.32.19.165/g' {} \;
How can I get sed to work with the extended 'find' parameters?
Currently these two 'find' statements return exactly the same result in a test folder with only 2 files:
find /var/tmp/ipreplace/ -type f -name "*.properties"
find /var/tmp/ipreplace/ -type f -name "*.properties" -o -name "*.xml" -not \( -path '/tmp/*' -o -path '/var/tmp/*' \)
I guess the use of -path parameter in your find command is wrong.
Try the following:
find / -not \( -path '/tmp' -prune \) -not \( -path '/var/tmp' -prune \) -type f -name "*.properties" -o -name "*.xml" -exec sed -i 's/10\.32\.19\.156/10.32.19.165/g' {} \;
Look at this post for reference

Recursively find and delete (*.xml / *.txt / *.csv) files older than x days in Linux

From what I gather through a quick search on the web, you can recursively remove any files folder than x days like this:
find /path/to/the/files -mtime +7 -exec rm {} \;
How do you amend this to only delete *.xml / *.csv / *.txt files (not case sensitive) recursively and leave other files/folders alone.
This is what I came up with and I am not sure if this is the correct way to go about this:
find /path/to/the/files -type f \( -name "*.xml" -or -name "*.XML" -or -name "*.csv" -or -name "*.CSV" -or -name "*.txt" -or -name "*.TXT" \) -mtime +7 -exec rm {} \;

Linux Shell Script execute without save to temporary variable

If I want to execute a command with variables in it, I always have to store the string first in a variable and then can execute it...
Example:
path_fasta="/home/xxx/yyy/zzz/qqq/"
name_fasta="CTA_Mix_DNA.fna"
path_outp"/some/Path/"
temp='find . -maxdepth 1 -not -name '$name_fasta' -not -name letsgo.sh -delete'
$temp
temp=$path_mothur'mothur #set.dir(output='$path_outp');summary.seqs(fasta='$path_fasta''$name_fasta')'
$temp
How do I do this directly without storing it first in temp? Must be easy, but did not find a solution...
Instead of:
temp='find . -maxdepth 1 -not -name '$name_fasta' -not -name letsgo.sh -delete'
$temp
...just use:
find . -maxdepth 1 -not -name "$name_fasta" -not -name letsgo.sh -delete

Resources