Delete all hidden files in folder and subfolders - bash

I need to delete all hidden files in the current folder and its sub folders. Is there any way to do it with a single line command without creating a script?

Use
find "$some_directory" -type f -name '.*' -delete
If you want to remove hidden directories as well, you'll need to take a little more care to avoid . and .., as mentioned by Ronald.
find "$some_directory" -name '.*' ! -name '.' ! -name '..' -delete
With either command, you should run without the -delete primary first, to verify that the list of files/directories that find returns includes only files you really want to delete.
For completeness, I should point out that -delete is a GNU extension to find; the POSIX-compliant command would be
find "$some_directory" -type f -name '.*' -exec rm '{}' \;
i.e., replace -delete with -exec ... \;, with ... replaced with the command line you would use to remove a file, but with the actual file name replaced by '{}'.

For my Netgear Stora, I wanted to remove all the hidden .webview .thumbnails .AppleDouble etc files and folders.
This works from the /home/yourusername/ folder:
find -type f -name '.*' ! -name '.' ! -name '..' -exec rm -fv '{}' \;
and then
find -type d -name '.*' ! -name '.' ! -name '..' -exec rm -frdv '{}' \;

Related

How to extract files with extension from directories?

I am currently extracting pictures from a large multi-levels directory using the following bash command:
find . -name \*.jpg -exec cp {} /newdir_path_.. \;
However, all pictures are stored under 3 versions:
xxx-LD.jpg
xxx-SD.jpg
xxx.jpg
I just want to extract the xxx.jpg pictures, not the LD and SD...
how should my command be modified to perform such extraction?
You can add more tests:
find . -name '*.jpg' -not -name '*-[LS]D.jpg' -exec cp {} /newdir_path_.. \;
-not is a GNU extension; you can use ! -name instead. In some shells, ! has to be escaped: \! -name.
This may also work for you considering your filenames only contain digits before .jpg:
find . -iregex '.*/[0-9]*\.jpg$' -exec cp {} /newdir_path_.. \;

How to delete a specific folder in specific folders

Using
find . -name "*_develop-*"
I am able to find all folders in the current directory that contain _develop- in the folder name, e.g.
myfolder_develop-abcd
myfolder_develop-efgh
Now inside these found folders I'd like to delete the folder "temp".
How do I pipe the required command to look into the resulting folders, find the required folder and then delete it?
You can use -exec option to make a rm -rf command on match folder.
Command should be like that :
find . -name "*_develop-" -exec rm -rf {}/tmp/ \;
{} represent a match folder, so {}/tmp/ represent tmp folder inside a match folder
You could use -regex flag
find . -regex '.*_develop-.*/temp' -type d -delete
or if it is the folder directly below
find . -regex '.*_develop-[^/]*/temp' -type d -delete
Use find with xargs
find . -wholename "*_develop-*/temp" -print0 | xargs -0 rm -rf
Using print0 and xargs -0 replace spaces by a NULL character. It is useful if there is spaces in some directory names, so xargs would fail without it.
You could use find . -wholename "*_develop-*/temp" -delete but that will fail if temp directory is not empty.

Bash script for removing specific file from certain subdirectories

On a unix server, I'm trying to figure out how to remove a file, say "example.xls", from any subdirectories that start with v0 ("v0*").
I have tried something like:
find . -name "v0*" -type d -exec find . -name "example.xls" -type f
-exec rm {} \;
But i get errors. I have a solution but it works too well, i.e. it will delete the file in any subdirectory, regardless of it's name:
find . -type f -name "example.xls" -exec rm -f {} \;
Any ideas?
You will probably have to do it in two steps -- i.e. first find the directories, and then the files -- you can use xargs to make it in a single line, like
find . -name "v0*" -type d | \
xargs -l -I[] \
find [] -name "example.xls" -type f -exec rm {} \;
what it does, is first generating a list of viable directory name, and let xargs call the second find with the names locating the file name within that directory
Try:
find -path '*/v0*/example.xls' -delete
This matches only files named example.xls which, somewhere in its path, has a parent directory name that starts with v0.
Note that since find offers -delete as an action, it is not necessary to invoke the external executable rm.
Example
Consider this directory structure:
$ find .
.
./a
./a/example.xls
./a/v0
./a/v0/b
./a/v0/b/example.xls
./a/v0/example.xls
We can identify files example.xls who have one of their parent directories named v0*:
$ find -path '*/v0*/example.xls'
./a/v0/b/example.xls
./a/v0/example.xls
To delete those files:
find -path '*/v0*/example.xls' -delete
Alternative: find only those files directly under directory v0*
find -regex '.*/v0[^/]*/example.xls'
Using the above directory structure, this approach returns one file:
$ find -regex '.*/v0[^/]*/example.xls'
./a/v0/example.xls
To delete such files:
find -regex '.*/v0[^/]*/example.xls' -delete
Compatibility
Although my tests were performed with GNU find, both -regex and -path are required by POSIX and also supported by OSX.

Deleting files older than 10 days in wildcard directory loop

I would like to delete old files from multiple directories but there is a wild card for one of the path attributes. So I'm trying to loop through each of those directories without specifying each one. I think I'm almost there but I'm not sure how to cd into the specific directory to delete the relevant files.
#! /bin/bash
DELETE_SEARCH_DIR=/apps/super/userprojects/elasticsearch/v131/node*/elasticsearch-1.3.1/logs/
for entry in `ls $DELETE_SEARCH_DIR`; do
find $path -name "*super*" -type f -mtime +10 -print -delete
#find . -type f -name $entry -exec rm -f {} \;
done
Any ideas on how to get into the specific directory and apply the delete?
find can search in multiple directories. You can do it like this:
DELETE_SEARCH_DIR=/apps/super/userprojects/elasticsearch/v131/node*/elasticsearch-1.3.1/logs
find $DELETE_SEARCH_DIR -type f -name '*super*' -mtime +10 -print -delete

How to use find to copy and remove directories?

I am trying to find the files which are older than some days and i want to copy the files to the specific folder and to remove the files from the source directory in a single command.
find . -type d -mtime +600 -exec cp -rf '{}' destination folder \; -exec rm -rf '{}' \;
Error:
find: ./one: No such file or directory
find: ./two: No such file or directory
find: ./three: No such file or directory
How can I resolve the error?
Try something like:
find . -type d -mtime -600 -exec cp -rf '{}' destination_folder \; -delete
Alternatively, you could even use
find . -type d -mtime -600 -print0 -exec cp -rf '{}' destination_folder \; | xargs -0 rm -rf
I'm not quite sure why you're doing cp and rm, rather than the much faster mv?
But, you can largely ignore that error, find pre-processes part of the tree, if you want to force it to ignore the removed subdirectories add -prune (you probably should quote destination dir)
find . -type d -mtime +600 -exec cp -rf '{}' 'destination folder' \; -exec rm -rf '{}' \; -prune
.
-prune True; if the file is a directory, do not descend into it. If -depth is given, false; no effect.
Because -delete implies -depth, you cannot usefully use -prune and -delete together.

Resources