find a file and copy it to another directory - linux

I have a directory called main. This directory is located in the root I mean the path to this directory is like this: /HOME/main
Inside this directory there is a folder called f1 and inside it another folder called subf1, so the path is like this: /HOME/main/f1/subf1
I want to check if there is a file in subf1 with a name containing a special string and copy this file to the main directory. I use this:
find . -maxdepth 1 -name "*string*" -exec cp {} ../main \;
It's incomplete and I don't know how to check if condition here, but even when I execute this command in /HOME/main/f1/subf1 path, it doesn't work. what's wrong?

You may use it like this:
find /HOME/main/f1/subf1 -iname "*string*" -exec cp {} /HOME/main/ \;

Related

In BASH, how do you reference a directory name in a copy statement of files you recursively "find"

I want to recursively find all files that end with *DETAIL.pdf
Create a new directory (in another drive) for each file with the same name as the original directory
Copy the file into the new directory
I have this as my current attempt:
find . -name \*DETAIL.pdf -type f -not -path "./test2" -exec cp -R {} ./test2 \;
I am struggling to create new directories for all these files by referencing the original directory name of each file.
The example mentions using cp but the question/problem itself does not, so I would suggest just using find and tar. Also, though the question is a little ambiguous as noted in the comments above, the example seems to suggest that the desired output directory is a child of the same directory being searched. Given that:
find . -path "./test2" -prune -o -type f -name '*DETAIL.pdf' -print0 | \
tar c --null --files-from=- | \
tar xC test2
This uses find for file selection, generating a (null-separated) file list, which tar then uses to copy the files, and the second tar will create the relative directories as needed and write the copied files.

Linux move files from dir to dir by name mask

I am trying to move all files with names starts with SML from directory to another.
Tried with
find /var/.../Images/ -name SML\* mv /var/.../Images/Small but doesnt work
try find /var/.../Images/ -name SML\* -exec mv {} /var/.../Images/Small/ \;
I guess you want something like this:
dir=/path/to/your/Images
mkdir -p "$dir/Small"
find "$dir" -name "SML*" -not -wholename "$dir/Small/*" -exec mv {} "$dir/Small/" \;
Since the directory you move the files to is a subdirectory of the one you seach in, you need to exclude the files already moved there. So I added -not -wholename "$dir/Small/*"
To execute a command for each found file, you need -exec .... The alternative would be to pipe your find results to a while read loop.
When using -exec, the found name can be referenced by {}.
See man find for a lot more information.

Extract .7z's recursively by name - P7zip

I have multiple directories, with .7z's inside each, but also other directories inside the same start directory which I do not want to extract. The ones I do want to extract all have a common name, for example: each directory has the word "extract" in it. How could I recursively extract each .7z to a chosen folder that contains the word "extract" in it?
The command you should use is:
find . -name "*extract*.7z" -print -exec 7z x {} \;
I would have run the command withtout -exec before, just to make sure these are the files you would actually like to extract:
find . -name "*extract*.7z" -print

Recursive find and copy to other directory

I need to find all files in a directory and it's subdirectories, but I need to keep directory structure. For example there is a file
/media/subdir1/subdir2/file.jpg
and I want to copy it to
/new-media/subdir1/subdir2/file.jpg
and the same to all files inside /media/ directory. And by the way, directories inside /new-media/ must be created if not exist.
if I use
find /media/ -name '*.jpg' -exec cp /new-media/ ????? {} \;
how can I get all subdirectories inside /media/?
The above will get you everything in /media, but to get exactly what you want you probably want to use something like:
Method 1: Copy only what you need recursively, per your requirements:
mkdir ../media2; find . -name "*.jpg" -exec cp -r --parents {} ../media2 \;
I ran this from inside the directory you want to search recursively. It does a couple things:
1 - create the new destination directory
mkdir ../media2
2 - then finds all files ending with ".jpg" recursively.
find . -name "*.jpg"
3 - uses -exec to pass the copy command to each file returned to find as a match, and subs that in as the first argument (which with the syntax of cp, is going to be your source file):
-exec cp -r --parents {} ../media2 \;
the "--parents" flag retains existing directory structure and recursively creates subsequent parent directories. Super useful right?
Method 2: there might be a better way to do this with xargs, but the above ended up being the most simple method imho. That said, if you want to think outside the box, you could simply copy the entire directory over recursively, then remove anything NOT ending with ".jpg" with something like:
cp -r media media2; find ./media '!'-name "*.jpg" -type f | xargs rm
I think this is what you want:
cp -r /media /new-media
-R, -r, --recursive
copy directories recursively
Hope this helps.

Copy and replace file recursively

I try to find a way to copy and replace files recursively.
Example:
Folder /home/test/
1/test.jpg
1/sth_other.png
2/test.jpg
2/sth_other.jpg
3/test.jpg
4/test.jpg
You can see that in folder /home/test I have more and more folders (1,2,3,4) which file name 'test.jpg'.
I have a file /home/test.jpg
Question:
How to replace file 'test.jpg' in 1/2/3/4(folders) with file /home/test.jpg ?
With find, you could do:
find /where -name test.jpg -type f -exec cp -i /home/test.jpg {} \;

Resources