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 {} \;
Related
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.
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 \;
The situation is
I have a directory A, I have a bunch of files and folders in the foldler.
Such as folder B , foler C , tmp1.txt , Hello.txt , tmp3.txt , okay.txt.
And in folder B,there are also a bunch of files in it.
So I want to move all txt files recrusively to another folder such as /home.
Here is my code.
find . -name "*.txt"| grep -v [\s\S]*tmp[\s\S]* -exec mv {} /home \;
I can only select these files,however it won't execute move operation.
because linux find has path in result.So it annoy me a lot.
To move only regular files, add -type f and exclude the files you don't want with \!:
find . -type f -name '*.txt' \! -name '*tmp*' -exec mv -i -t /home {} +
The -i asks for permission to overwrite a file if it already exists and the + instead of the \; is used to move as many files as possible with one invocation of mv (and thus we need to specify the target directory with the -t option as {} contains more than one file).
I'm looking for a linux command to search a folder directory and it's subfolders for a file by name and replace all of them with a copy of another file. Any idea?
Assuming you have the replacement file in your home directory (~), you can use find to do the replacing. This will find all boom.txt files and replace them with the replace.txt file (keeping the boom.txt name).
find . -name "boom.txt" -exec cp ~/replace.txt {} \;
Adding to the answer by user1683793, the following command can also be an alternative for the copy and replace. This command is verified on Mintty(Windows 10).
find . -name "boom.txt" -print -exec cp ~/replace.txt {} \;
I want to create an index.html file in each folder of my project in linux.
index.html should contain some sample code.
How can I create a file in single command?
find . -type d -exec touch {}/index.html \;
This'll create an index.html in . and all subdirectories.
cd /project_dir && find . -type d -exec touch \{\}/index.htm \;
HTH
Assuming you have a list of your project directories in a file called "projects.txt", you can do this (for bash and zsh)
for i in $(cat projects.txt)
do
touch $i/index.html
done
To create your projects.txt, you can use the find command. You could replace the cat directly with a find invocation but I thought it more clear to separate the two operations.
I know it's an old question but none of the current answers allow to add some sample code, here my solution:
#create a temp file
echo "<?php // Silence is golden" > /tmp/index.php
#for each directory copy the file
find /mydir -type d -exec cp /tmp/index.php {} \;
#Alternative : for each directory copy the file where the file is not already present
find /mydir -type d \! -exec test -e '{}/index.php' \; -exec cp /tmp/index.php {} \;
The following command will create an empty index.html file in the current directory
touch index.html
You will need to do the appropriate looping if necessary.