Recusively delete all files that do not contain a word in their title - linux

Hi I wanted to recursively delete all files in a directory with various folders that do not contain the word "weight" in their file name. How can I do this?

Type this command first:
find '/path/to/directory' -type f \! -iname '*weight*'
If you are OK with deleting all the files suggested by the command, then you can actually delete them with:
find '/path/to/directory' -type f \! -iname '*weight*' -delete

Related

Find a file and export the full path to a list

I'm looking for a way to search certain files named "XYHello.pdf" or "BDHello.pdf" so basically "*Hello.pdf" in a directory with subfolder and export the found files including path to the file in a text file.
So that at the end I have a list with all found files including the paths in a list. I spontaneously thought about Linux Command find.
find . -type f -iname "*Hello.pdf"
But the problem is i need the full path to the file in a list.
find $PWD -type f -iname "*Hello.pdf"
or
find . -type f -iname "*Hello.pdf" -exec realpath {} \;

Delete old files using ls and find command

My folder structer looks like below:
folder1
---tmp
---sub1
folder2
---tmp
---sub2
folder3
---tmp
---sub3
folder4
---tmp
---sub4
I want to delete files which older than 30 days in all tmp folder.
list all tmp folder:
ls -d */tmp
delete all files which older than 30 days
find . -mtime +30 -type f -delete
Could i combine these 2 steps into one command line?
What you can do is replace the . in your find with the actual directories you want to search in.
find */tmp -mtime +30 -type f -delete
If tmp can be several levels deeper then you might be interested in
find . -regex '.*/tmp/[^/]+' -mtime +30 -type f -delete
or similar to the first option, but by using the double-star globular expression (enabled with shopt -s globstar)
find **/tmp -mtime +30 -type f -delete
* Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
source: man bash
Note: you have to be careful though. Imagine that you have a directory folder1/tmp/foo/ then the above commands (with exception of the regex version) will select also files in folder1/tmp/foo and this might not be wanted. You might be interested in the extra option -maxdepth 1

How to "find all xml files in multiple directories (textfile) and delete"

I have a text file containing the full path to about 1000 directories. Each on a separate line. I would like to delete all .xml files contained in these folders. I can obtain this one at a time using:
find /mnt/local/hhd1/output/data/2010-aug-ph1/ -name "*.xml" -type f -delete
but obviously will take a long time to do by hand. How do I get find to read each line and perform the delete from a text file?
Use a loop to read the directory names from your file and issue a find ... -delete on each of them:
#!/bin/bash
while read -r dir; do
find "$dir" -name "*.xml" -type f -delete
done < file

Alternative way to find all files in a folder and subfolders

Is there any alternative for below command to find files in a folder and subfolders as well.
find . | egrep '\./[^/]+/[^/]+/[^/]+'
Note: I don't want directories I want to get only files
Additionally, you could specify list of files extensions as your search options:
find . -type f -name "*.js" -o -name "*.ros" -o -name "*.php"
Above example, would only display file names with *.ros, *.php, and *.js as file extensions under specific folder and subfolders.
Why don't you just use find?
I am not sure from your question if you want to limit the depth. If so:
find . -type f -depth 2 -print
If you just want to find files
find . -type f -print
If you just want to find directories
find . -type d -print
You can also use -ls if -print does not float your boat.
find . -type f -ls

Find command ignore directory

I am trying to find all files ending in .jar, but it is picking up folders that ends in .jar which is something I do not want.
My command so far
find . -name ".*jar"
I need it so it ignores directories ending in .jar
Additional request.
I now need to ignore a specific folder when looking, is this possible?
Thanks.
just add -type f:
find . -name "*.jar" -type f
Others have already shown, how to pick up files named ending in .jar. As an answer to your comment "How to ignore a specific folder". You use -prune for that
find . -name 'specific/folder' -type d -prune -o -type f -name '*.jar'
that's what you want
find . -type f -name "*.jar"
see man page:
-type Type
Evaluates to the value True if the Type variable specifies one of the following values:
b
Block special file
c
Character special file
d
Directory
f
Plain file
l
Symbolic link
p
FIFO (a named pipe)
s
Socket

Resources