Mac terminal how can i search in all folders and subfolders for image - macos

How can i search for example all .png files on an external disk and copy them to another directory?
Have tried to use the cp command. Have try it but don't work for me
?
Monterey 2.2.1
cp /Volumes/Data *.png /Volumes/Data/pictures_png

cp command won't work if you need to recursively copy from the sub directories. You need to use find.
Syntax:
find $SOURCE -type f -name '*.type' -exec cp '{}' $DESTINATION ';'
In your case,
find /Volumes/Data -type f -name '*.png' -exec cp '{}' /Volumes/Data/pictures_png ';'
Here is how it works:
-type f means copy only files not directories.
-name is to provide the filename to find. Here *.png for pattern matching
-exec executes the following line for each result the above find returns.
{} will be replaced with the results from find
; terminates -exec command

Related

Copy all empty files into a folder with command line

I am trying to copy all empty files from the home directory into a folder that is on the desktop, using this:
find ~ -empty -exec cp {} /desktop/emptyfolder \;
However, I can't make it work.
Are there any other possible solutions to achieve this? Or maybe to write a bash script that could do this?
Add -type f to the find command to force it to search for files and not directories and so:
find ~ -empty -type f -exec cp {} /desktop/emptyfolder \;

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.

How to print the deleted file names along with path in shell script

I am deleting the files in all the directories and subdirectories using the command below:
find . -type f -name "*.txt" -exec rm -f {} \;
But I want to know which are the files deleted along with their paths. How can I do this?
Simply add a -print argument to your find.
$ find . -type f -name "*.txt" -print -exec rm -f {} \;
As noted by #JonathanRoss below, you can achieve an equivalent result with the -v option to rm.
It's not the scope of your question, but more generally it gets more interesting if you want to delete directories recursively. Then:
a simple -exec rm -r argument keeps it silent
a -print -exec rm -r argument reports the toplevel directories you're operating on
a -exec rm -rv argument reports all you're removing

Delete all hidden files in folder and subfolders

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

Resources