removing some files out of several folders - linux

I have a question about removing some files out of several folders.
To be more specific: There are 5 Folders which are only the same by a few characters. For example: o1_FolderF_xy and zz_FolderF_34. And in each folder with the characters "FolderF" I want to delete all the files which starts with "filename".
The last time I did it by hand.
Will this work? Or do i need a script with a loop?
rm -rf /path/toFolder/*FolderF*/filename*
I'm sorry, I think for most it's a stupid question. But I'm new to all the stuff and I just do not want to go wrong with the delete

Your suggested command will work just fine.

You could use find instead:
find /path -name 'filename*' -exec rm {} \;
Basically it search's files with filename pattern on /path directory and for each file it executes rm.
Or, if you want to just check into those specific directories:
find /path -wholename '*folder*/filename*' -exec rm {} \;

Related

Linux - Can't recursively delete large directories

I have a pretty big find that is supposed to delete any files/dir it finds. I just can't get it to work properly.
If I attach -exec rm -fr {} \;, at some point, I always get the following errors:
find: ‘/path/to/dir/file123.local’: No such file or directory
If I replace it with -delete, I get the following error:
find: cannot delete `/path/to/dir': Directory not empty
I looked for suggestions online but the suggestion is always the other option (replace -exec with -delete and vice-versa)
Does anyone happen to know a way to fix it without redirecting stderr to null?
Thanks ahead!
find doesn't know what your command passed to -exec does. It traverses the directory tree in this order:
find a file
execute a command on that file
if it's a directory, traverse it down
Now if the directory is removed with rm -fr, there is nothing to traverse down any more, so find reports it.
If you supply the -depth option, then the traversal order changes:
find a file
if it's a directory, traverse it down
execute a command on that file
This will eliminate the error message.
-delete implies -depth, so ostensibly it should work. However it is your responsibility to make sure the directories you want to delete are completely cleaned up. If you filter out some files with -time etc, you may end up trying to delete a directory which is not completely clean.
You could try to wrap {} in double quotes, there may have space in directry path.
-exec rm -rf "{}" \;
If I read your question well, you want to remove files, but sometimes it happens that they already have been removed by some other process, and you wonder what you should do.
Why do you think you should do anything? You want the file to be gone, and apparently it is gone, so no worries.
Obviously the corresponding error messages might be annoying, but this you can handle adding 2>/dev/null at the end of your command (redirect the error output to <NULL>).
So you get:
find ... -exec rm -fr {} \; 2>/dev/null
Edit after comment from user1934428:
I might be a good idea to drop the r switch:
find ... -exec rm -f {} \; 2>/dev/null
In that case, you should have no errors anymore:
find ... -exec rm -f {} \;

How can I remove specific directories that all start with a common letter?

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.

How to delete files and directories older than n days in linux

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

Iteratively remove file type

I'm trying to delete the files that visual sourcesafe inserts into various folders. It's this file:
vssver2.scc
Since I have many nested folders, I'd like to do this recursively from the parent folder. What would the linux code be to delete all files with .scc extension? (I'm on a mac).
Thanks.
Look for them and remove:
find . -name "*.scc" -exec rm {} +
To make sure you are going to delete the correct files, you can replace the rm with ls so that it will show these files.
Also, you can replace find . with find /your/path to indicate the exact path from which you want to remove. With find . it will start from the current path.
find . -name ".scc" -print0 | xargs -0 rm -rf

Bash script to recursively step through folders and delete files

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?
Change directory to the root directory you want (or change . to the directory) and execute:
find . -name "._*" -print0 | xargs -0 rm -rf
xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.
find . -name '._*' -exec rm -Rf {} \;
I've had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:
http://github.com/houbysoft/short/blob/master/tidy
find /path -name "._*" -exec rm -fr "{}" +;
Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean.
dot_clean -- Merge ._* files with corresponding native files.
For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.
If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.
Because dot_clean works recursively by default, use:
dot_clean <directory>
If you want to turn off the recursively merge, use -f for flat merge.
dot_clean -f <directory>
find . -name '.*' -delete
A bit shorter and perform better in case of extremely long list of files.

Resources