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

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

Related

Create ZIP of hundred thousand files based on date newer than one year on Linux

I have a /folder with over a half million files created in the last 10 years. I'm restructuring the process so that in the future there are subfolders based on the year.
For now, I need to backup all files modified within the last year. I tried
zip -r /backup.zip $(find /folder -type f -mtime -365
but get error: Argument list too long.
Is there any alternative to get the files compressed and archived?
Zip has an option to read the filelist from stdin. Below is from the zip man page
-# file lists. If a file list is specified as -# [Not on MacOS],
zip takes the list of input files from standard input instead of
from the command line. For example,
zip -# foo
will store the files listed one per line on stdin in foo.zip.
This should do what you need
find /folder -type f -mtime -365 | zip -# /backup.zip
Note that I've removed the -r option because it isn't doing anything - you are explicitly selecting standard files with the find command (-type f)
You'll have to switch from passing all the files at once to piping the files one at a time to the zip command.
find /folder -type f -mtime -365 | while read FILE;do zip -r /backup.zip $FILE;done
You can also work with the -exec parameter in find, like this:
find /folder -type f -mtime -365 -exec zip -r /backup.zip \;
(or whatever your command is). For every file, the given command is executed with the file passed as a last parameter.
Find the files and then execute the zip command on as many files as possible using + as opposed to ;
find /folder -type f -mtime -365 -exec zip -r /backup.zip '{}' +

Linux bash command -backup=numbered. Put the number BEFORE the file extension

Using a one-line bash command with GitBash on windows, using find and cp, I am backing up a bunch of script files that exist in multiple sub-directories. I am currently backing them up to a single directory. As you can imagine, naming conflicts arise. This is easy enough to avoid with the --backup=numbered option which creates a copy of the file. However, the problem with this is that it puts the number AFTER the file extension, naming the file like this: example.js.~2~. What I want is to preserve the file extension and name the file like this: example2.js rather than putting the number after the file extension. Is there any way to do this?
Another option would be to prepend the directory name (from the directory that it is being copied from) to the file that is being copied instead of adding a number. I would accept either of these as a solution.
Here is what I have so far:
find . -path "*node_modules*" -prune -o -type f \( -name '*.js' -or -name '*.js.map' -or -name '*.ts' -or -name '*.json' \) -printf "%h\n" -exec cp {} --backup=numbered "/c/test/" \;
Any help would be appreciated! Thank you!
what about :
#!/bin/bash
# your find command here
FILES=$(find . -type f .....)
# loop through files and create a new filename with the path within ( slashes replaced by underscores
for FILE in $FILES; do
NEW_FILENAME=$(printf "%s" "$FILE" | sed s/\\//_/g)
cp "$FILE" "/c/test/${NEW_FILENAME}"
done
from your question, I am unsure if a one liner is mandatory...

Recursively delete files named `log` under multiple directories

I have a folder structure like the below:
feat1
feat2
feat3
Now within each folder we have another folder called builds.
Under builds we have numbered folders like 1, 2, etc.
Under each numbered folder we have files of the pattern *.log and then a particular file with the name log.
I need to run a command in the linux server / unix server to recursively delete only the file with the name log not the *.log for all these folders feat1, feat2 and feat3. How to do that?
The command you run depends on how careful you want to be about your search criteria and whether you need to search recursively.
Delete anything named log under feat*/builds/*/:
rm feat*/builds/*/log
Recursively find and delete anything named log anywhere under the current directory:
find . -name log -delete
Recursively find and delete anything named log, only under feat*:
find feat* -name log -delete
Recursively find and delete anything named log, only under feat*/builds/*/:
find feat*/builds/*/ -name log -delete
Recursively find and delete any file named log, only under feat*/builds/*/:
find feat*/builds/*/ -name log -type f -delete
Here is a quick and dirty solution using a for loop that should get the job done.
for f in `find feat* -name 'log'`; do rm "$f"; done;
for f in is saying we're going to do a for loop with f as the variable for each result.
find feat* -name 'log' searches through any directory starting with "feat" with * as a wildcard.
-name says we're looking for a file name of log.
do rm "$f" removes each result that was found.
Sometimes it is helpful to use echo before doing destructive commands this way. See below.
for f in `find feat* -name 'log'`; do echo "$f"; done;

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 {} \;

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

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

Resources