I have a folder 'masterfolder' that has subfolders with a numbered naming scheme:
\masterfolder\S01
\masterfolder\S02
\masterfolder\S03
\masterfolder\S04
\masterfolder\S05
Now I want to find and delete all folders below a specific number, for example S03. This means, S03, S04, S05 etc should not get deleted, S01 and S02 should get deleted.
I normally use this command to find and delete a specific folder:
find "/mnt/USBDRIVE/masterfolder" -type d -name "S02" -exec rm -rf '{}' \;
I tried finding a solution myself, but the only method I have found is to delete everything except the number I know I want to keep:
find "/mnt/USBDRIVE/masterfolder" -mindepth 1 -maxdepth 1 -type d -not -name "S03" -exec rm -rf '{}' \;
This will keep S03, but delete all others. I want to keep S03 and any other folder with a higher number than S03.
Any ideas appreciated.
There are many ways to solve this.
Since your numbers are zero padded, the easiest way is to just send a list of the directories to a file sorted alphabetically. Then delete the ones you want ignored (they'll all be together), do a global change to add "rm " to the beginning of each line, and run the file as a script.
This will take you less than 30 seconds. Any programmatic solution will take longer.
Related
find /home/root/public_html/_sess -type f -mtime +3 -name 'sess-*' -execdir rm -- {} \;
I feel like I understand find , but I'm not 100% sure what -type is, I think that is the file type f not sure yet -mtime I feel like -mtime means a time setting of some sort, and +3 means maybe that time setting +3? , I feel like -execdir rm -- just means remove the files in the directory call -name 'sess-*' as well. But again not 100% sure of all the command elements within and wanted to get clarification.
You can do man find to get information on how Linux find works and all the options you can pass to it.
In this case, the command is using the Linux find utility to search for files in the /home/root/public_html/_sess directory with the following options:
-file f - searches for files of filetype f, which is regular files (not directories, links, etc)
-mtime +3 - searches for files modified more than 3 days ago (the + is for more than, -3 would be less than 3 days old)
-name 'sess-* - searches for files whose name matches the regex sess-* (name starts with "sess-")
-execdir <command> {}; - executes <command> on each file that find finds in the directory that the file was found in, in this case <command> is rm to remove the file
So in summary, this job searches for files located in a certain directory, whose names start with a specific string, and which are more than 3 days old, and deletes them.
I have many EC2 instances in a folder that I need to delete. Using -delete doesn't work because the directories are not empty. I tried looking for a way to get -rmdir -f to work with no success. The instance folders are all started with "i-" which led me to add wildcard "i-*" like that to get it to delete all directories starting with those characters. How can I manage to do this? the directories will never be empty either.
Assuming your current dir is the folder in question, how about:
find . -type d -name 'i-*'
If that lists the directories you want to remove, then change it to:
find . -type d -name 'i-*' -exec rm -r {} \;
In the command line interface/shell/born again shell/etc...
rm -r i-*
will remove ANY and ALL contained file(s) or directory(s) with subfiles and sub directories (recursive = -r) where the name begins with "i-" .
To delete the directories matching the pattern graphene-80* directly under /tmp, use
rm -rf /tmp/graphene-80*/
Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted (or symbolic links to directories), and not files etc.
To find the matching directories elsewhere under /tmp and delete them wherever they may be, use
find /tmp -type d -name 'graphene-80*' -prune -exec rm -rf {} +
To additionally see the names of the directories as they are deleted, insert -print before -exec.
The two tests -type d and -name 'graphene-80*' tests for directories with the names that we're looking for. The -prune removes the found directory from the search path (we don't want to look inside these directories as they are being deleted), and the -exec, finally, does the actual removal by means of calling rm.
I'm trying to use find to find and exclude/filter few directories from being copied to another backup directory.
My attempts to do so using find's '-exec' option end up copying every processed file instead of only the matches, so I'm quite confused about what the expected behavior should be and would appreciate help gaining better understanding.
Starting point:
me#computer>ls
AddMonitorsOnEntry MantisCoreFormatting MantisGraph PastePicture XmlImportExport
Make sure find excludes the unwanted 'files' as expected
me#computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport'
.
./AddMonitorsOnEntry
./PastePicture
Now to copy those 2 directories to a backup dir:
me#computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport' -exec cp -dr '{}' ~/backup \;
Now to see if it worked...
me#computer>cd ~/backup
me#computer>ls
AddMonitorsOnEntry backup MantisCoreFormatting MantisGraph PastePicture XmlImportExport
WTH??
I thought '-exec' only operated on the matches, according to this snippet from the man page: " ...The specified command is run once for each matched file..."
I know there are other ways to accomplish this task, but '-exec' seems to work well enough for the poster here https://unix.stackexchange.com/questions/50612/how-to-combine-2-name-conditions-in-find/50633. I'm looking for help understanding how to make use of "-exec" versus using xargs or something else. Thanks.
Now to copy those 2 directories to a backup dir
You don't have 2 matches. Your command shows 3:
.
./AddMonitorsOnEntry
./PastePicture
. is the current directory, so your cp command copies everything.
Instead of find . you can use find * to skip the current directory ., but still process all the (non-hidden) files/dirs within it.
Silly of me..
My initial find expression includes the current directory as a result, so any files in the current dir will be operated on by "-exec".
To fix I added the current dir among the ones excluded.
me#computer>find . -maxdepth 1 -not -regex '.*MantisCoreFormatting\|.*MantisGraph\|.*XmlImportExport\|\.'
./AddMonitorsOnEntry
./PastePicture
I have a directory named repository which has a number of files and sub directories. I want to find the files and directories which have not been modified since last 14 days so that I can delete those files and directories.
I have wrote this script but it is giving the directory name only
#!/bin/sh
M2_REPO=/var/lib/jenkins/.m2/repository
echo $M2_REPO
OLDFILES=/var/lib/jenkins/.m2/repository/deleted_artifacts.txt
AGE=14
find "${M2_REPO}" -name '*' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
find /path/to/files* -mtime +5 -exec rm {} \;
Note that there are spaces between rm, {}, and \;
Explanation
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.
This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.
You can give the find -delete flag to remove the files with it. Just be careful to put it in the end of the command so that the time filter is applied first.
You can first just list the files that the command finds:
find "${M2_REPO}" -depth -mtime +${AGE} -print
The -d flag makes the find do the search depth-first, which is implied by the -deletecommand.
If you like the results, change the print to delete:
find "${M2_REPO}" -mtime +${AGE} -delete
I know this is a very old question but FWIW I solved the problem in two steps, first find and delete files older than N days, then find and delete empty directories. I tried doing both in one step but the delete operation updates the modification time on the file's parent directory, and then the (empty) directory does not match the -mtime criteria any more! Here's the solution with shell variables:
age=14
dir="/tmp/dirty"
find "$dir" -mtime "+$age" -delete && find "$dir" -type d -empty -delete
I have several config folders (ex: .gnome, .mozilla) that I need to delete across a large array of directories. They all start with two alphabetical characters (ex: ag52156,ge51789) and are located in the same place.
I don't write bash so I wouldn't know how to start tackling this in the first place - but what should I look into so that I can write this?
Try this :
find [a-z][a-z]* -type d \( -name .gnome -o -name .mozilla \) -exec rm -r {} \;