Find folder by name then copy files - linux

I need to find subdirectories called "child" and copy the files in those folders to a new directory. Can't get it to work.
I can get as far as finding the subdirectories but I can't copy the files from them.
> find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child
The above will find all subdirectories in 03_2016 named "child" but how do I now copy the files inside those directories?
I tried this but the problem is that it seems to want to copy the directories themselves and not just the files:
> find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child | xargs cp '{}' /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST \;
I can't get it to target only the files.

This might be a little cleaner than the above answer:
find $(find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child | xargs) -type f -exec cp {} /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST \;
Two calls to find, one call to xargs. Like the other answer, this also will overwrite duplicate file names.

You're close:
> find $YOUR_PATH/03_2016/ -type d -iname child | xargs -I {} find -type f {} | xargs -I {} cp {} /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST
Note: You might overwrite files that share the same name at the final destination

Related

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.
I tried
cp -R *.log /destination
But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.
find /path/to/logdir -type f -name "*.log" |xargs -I {} cp {} /path/to/destinationdir
Explanation:
find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol
Another version without xargs:
find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \;

Bash: Find files containing a certain string and copy them into a folder

What I want:
In a bash script: Find all files in current directory that contain a certain string "teststring" and cop them into a subfolder "./testfolder"
Found this to find the filenames which im looking for
find . -type f -print0 | xargs -0 grep -l "teststring"
..and this to copy found files to another folder (here selecting by strings in filename):
find . -type f -iname "stringinfilename" -exec cp {} ./testfolder/ \;
Whats the best way to combine both commands to achieve what I described at the top?
Just let find do both:
find . -name subdir -prune -o -type f -exec \
grep -q teststring "{}" \; -exec cp "{}" subdir \;
Note that things like this are much easier if you don't try to add to the directory you're working in. In other words, write to a sibling dir instead of writing to a subdirectory. If you want to wind up with the data in a subdir, mv it when you're done. That way, you don't have to worry about the prune (ie, you don't have to worry about find descending into the subdir and attempting to duplicate the work).

Linux find all files in sub directories and move them

I have a Linux-System where some users put files with ftp in a Directory. In this Directory there are sub-directories which the users can create. Now I need a script that searches for all files in those subdirectories and moves them in a single Directory (for backup). The Problem: The Sub directories shouldn´t be removed.
the directory for the users is /files/media/documents/
and the files have to be moved in the Directory /files/dump/. I don´t care about files in /files/media/documents/, they are already handled by another script.
I already tried this script:
for dir in /files/media/documents/
do
find "$dir/" -iname '*' -print0 | xargs -0 mv -t /files/dump/
done
Instead of iterating, you could just use find. In man-page there is a "-type" option documented, so for moving only files you could do:
find "/files/media/documents/" -type f -print0 | xargs -0 mv -t /files/dump/
You also won't like to find files in /files/media/documents/, but all sub-directories? Simply add "-mindepth":
find "/files/media/documents/" -type f -mindepth 1 -print0 | xargs -0 mv -t /files/dump/
Alternatively you could also use "-exec" to skip a second command (xargs):
find "/files/media/documents/" -type f -mindepth 1 -exec mv {} /files/dump/ \;

How to copy all the files with the same suffix to another directory? - Unix

I have a directory with unknown number of subdirectories and unknown level of sub*directories within them. How do I copy all the file swith the same suffix to a new directory?
E.g. from this directory:
> some-dir
>> foo-subdir
>>> bar-sudsubdir
>>>> file-adx.txt
>> foobar-subdir
>>> file-kiv.txt
Move all the *.txt files to:
> new-dir
>> file-adx.txt
>> file-kiv.txt
One option is to use find:
find some-dir -type f -name "*.txt" -exec cp \{\} new-dir \;
find some-dir -type f -name "*.txt" would find *.txt files in the directory some-dir. The -exec option builds a command line (e.g. cp file new.txt) for every matching file denoted by {}.
Use find with xargs as shown below:
find some-dir -type f -name "*.txt" -print0 | xargs -0 cp --target-directory=new-dir
For a large number of files, this xargs version is more efficient than using find some-dir -type f -name "*.txt" -exec cp {} new-dir \; because xargs will pass multiple files at a time to cp, instead of calling cp once per file. So there will be fewer fork/exec calls with the xargs version.

git find and rename a string in multiple filenames and folder names

So Basically I need to find all files and folders in my github project containing the string 'persons'
find . -type f -print | grep "persons"
find . -type d -print | grep "persons"
The above works for me.
But I also need to rename all the above files and folders with 'members'
Can I do the above with a couple of commands? Instead of manually replacing them one by one
i dont know how to do a git mv oldfilename newfilename rescursively to the above
for dir in `find /DIR -type d -iname '*persons*'` ; do
git mv "${dir}" "${dir/persons/members}"
done
Will do. For the files do it with -type f.
find . -depth -name persons | while read F; do mv $F $(dirname $F)/members; done

Resources