Linux - Recursively go through directories? - linux

Let's say I am in the directory /home/videos and want to iterate recursively through all of the directories underneath it. If the directory name contains "images" I want to delete the directory and all of its contents. Also, can this be done for files? Let's say in each directory go through every file and check if its name ends with ".mp3" and delete it?
Thanks

find . -name "*images*" -type d -exec rm -r {} \;
find . -name "*.mp3" -type f -exec rm -rf {} \;
-exec rm -rf {} \; : Delete all files matched by file pattern.
-type f : Only match files and do not include directory names.
-type d : matches only directory names

Here is a nice tutorial on this. http://www.cyberciti.biz/faq/linux-unix-how-to-find-and-remove-files/
So you can do something like
find /home/videos -type d -name "*images*" -exec rm -rf {} \;
and
find /home/videos -type f -name "*.mp3" -exec rm -rf {} \;

Related

log deleted files in linux

I am using the following command to delete the files that are older than 10 days, but I also want to store(log) the list of files that are being deleted using the below command.
find ./path/delete -type f -name '*' -mtime +10 -exec rm {} \;
If you create a file to log to (touch ./path/to/logfile), just add another -exec to your command. Below is a very basic example, but you can add to it:
find ./path/delete -type f -name '*' -mtime +10 -exec rm {} \; -exec echo {} >> ./path/to/logfile \;

How to clean up folders efficiently using shell script

I am using a directory structure with various folders. There are new files created daily in some of them.
I have created some programs to clean up the directories, but I would like to use a shell script to make it more efficient.
Therefore I would like to store an "archiving.properties" file in every folder that needs to be cleaned up. The properties file should contain the following variables
file_pattern="*.xml"
days_to_keep=2
Now my clean up routine should:
find all properties files
delete all files that match the file name pattern (file_pattern) and that are older then the defined number of days (days_to_keep) in the directory where the properties file was found.
So my question is how can I do this in the most efficient way?
find . -type f -name "archiving.properties" -print
find . -type f -name "<file_pattern>" -mtime +<days_to_keep> -delete
currently I was trying the following in a single folder. It prints out the command correctly, but it is not executed.
#!/bin/bash
. archiving.properties
find . -type f -name "*.xml" -mtime +1 -exec rm -rf {} \;
echo " find . -type f -name \"${file_pattern}\" -mtime +${days_to_keep} -exec rm -rf {} \;"
Result is: find . -type f -name "*.xml" -mtime +1 -exec rm -rf {} \;
Thanks for your help in advance.
I got a final result
echo "start deleting files in " $(pwd) " ... "
#filename of the properties
properties="clean_up.properties"
#find all properties files
for prop in $(find . -type f -name $properties);do
#init variables
file_pattern="*._html"
days_to_keep=14
#load the variables from the properties file
. "$prop"
#define the folder of the properties file
folder=${prop%?$properties}
#remove all files matching the parameters in the folder where the properties were found
echo ">>> find $folder -type f -name \"${file_pattern}\" -mtime +${days_to_keep} -exec rm -f {} \;"
find $folder -type f -name "${file_pattern}" -mtime +${days_to_keep} -exec rm -f {} \;
done
echo "... done"

How to delete multiple type of files?

I can delete .zip files using following command.
find . -type f -name '*.log.*.zip' -exec rm \{\} \;
Is it possible to delete .zip and .gz file at the same time ?
find . -type f -name '*.log.*.zip' | '*.log.*.gz' -exec rm \{\} \;
You can try like this using brace expansion:
$ rm -rf log.{zip,gz}

How to delete content of file present in multiple directories in linux

Directory A having two sub directories B and C. Both B and C having same text file like "abc.txt". From directory A itself how to delete content abc.txt in both directories
If there could be more than two subdirectories in A, but you
want to restrict you to B and C, you can use
rm A/{B,C}/abc.txt
to delete both files.
To empty their content, use
: > A/{B,C}/abc.txt
Delete the actual files:
find A/ -name "abc.txt" -delete
Delete the "content" of the files:
find A/ -name "abc.txt" -exec truncate -s 0 {} \;
The more general way is to use find:
find . -type f -name "file" -exec rm -f {} \;
The explanation of the command is:
-name "file" : file name.
-exec rm -f {} \; : delete the files that match.
-type f : specify the type of file, directory are excluded.
Use Kleene star :
rm A/*/abc.txt
find A/ -name "abc.txt" -type f -exec rm -rf{}\;

How to remove folders with a certain name

In Linux, how do I remove folders with a certain name which are nested deep in a folder hierarchy?
The following paths are under a folder and I would like to remove all folders named a.
1/2/3/a
1/2/3/b
10/20/30/a
10/20/30/b
100/200/300/a
100/200/300/b
What Linux command should I use from the parent folder?
If the target directory is empty, use find, filter with only directories, filter by name, execute rmdir:
find . -type d -name a -exec rmdir {} \;
If you want to recursively delete its contents, replace -exec rmdir {} \; with -delete or -prune -exec rm -rf {} \;. Other answers include details about these versions, credit them too.
Use find for name "a" and execute rm to remove those named according to your wishes, as follows:
find . -name a -exec rm -rf {} \;
Test it first using ls to list:
find . -name a -exec ls {} \;
To ensure this only removes directories and not plain files, use the "-type d" arg (as suggested in the comments):
find . -name a -type d -exec rm -rf {} \;
The "{}" is a substitution for each file "a" found - the exec command is executed against each by substitution.
This also works - it will remove all the folders called "a" and their contents:
rm -rf `find . -type d -name a`
I ended up here looking to delete my node_modules folders before doing a backup of my work in progress using rsync. A key requirements is that the node_modules folder can be nested, so you need the -prune option.
First I ran this to visually verify the folders to be deleted:
find . -type d -name node_modules -prune
Then I ran this to delete them all:
find . -type d -name node_modules -prune -exec rm -rf {} \;
Thanks to pistache
To delete all directories with the name foo, run:
find -type d -name foo -a -prune -exec rm -rf {} \;
The other answers are missing an important thing: the -prune option. Without -prune, GNU find will delete the directory with the matching name and then try to recurse into it to find more directories that match. The -prune option tells it to not recurse into a directory that matched the conditions.
This command works for me. It does its work recursively
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
. - current folder
"node_modules" - folder name
find ./ -name "FOLDERNAME" | xargs rm -Rf
Should do the trick. WARNING, if you accidentally pump a . or / into xargs rm -Rf your entire computer will be deleted without an option to get it back, requiring an OS reinstall.
Combining multiple answers, here's a command that works on both Linux and MacOS
rm -rf $(find . -type d -name __pycache__)
I had more than 100 files like
log-12
log-123
log-34
....
above answers did not work for me
but the following command helped me.
find . -name "log-*" -exec rm -rf {} \;
i gave -type as . so it deletes both files and folders which starts with log-
and rm -rf deletes folders recursively even it has files.
if you want folders alone
find -type d -name "log-*" -exec rm -rf {} \;
files alone
find -type f -name "log-*" -exec rm -rf {} \;
Another one:
"-exec rm -rf {} \;" can be replaced by "-delete"
find -type d -name __pycache__ -delete # GNU find
find . -type d -name __pycache__ -delete # POSIX find (e.g. Mac OS X)
Earlier comments didn't work for me since I was looking for an expression within the folder name in some folder within the structure
The following works for a folder in a structure like:
b/d/ab/cd/file or c/d/e/f/a/f/file
To check before using rm-rf
find . -name *a* -type d -exec realpath {} \;
Removing folders including content recursively
find . -name *a* -type d -exec rm -rf {} \;
find path/to/the/folders -maxdepth 1 -name "my_*" -type d -delete

Resources