linux command to find files and replace with another file - linux

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

Related

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.

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

check if a file is in a folder or its subfolder using linux terminal

I want to check if the particular file is in a folder or its sub folder or not using Linux terminal.
Which should I use for this? I use find and grep command but it travels only one folder.
In order to search from your current directory, use
find . -name filename
In order to search from root directory use
find / -name filename
If you don't know the file extension try
find . -name filename.*
Also note that find command only displays the files in the path which you have permission to view. If you don't have permission for a/b/c path then it will just display a message mentioning that path can't be searched
If you want to search for by filename, use find:
find /path -name "filename"
example:
find . -name myfile.txt
If need to find all files containing a specific string, use grep:
grep -r "string" /path
example:
grep -r foobar .
By default, find will traverse all subdirectories, for example:
mkdir level1
mkdir level1/level2
touch level1/level2/file
find . -name "file"
Output:
./level1/level2/file
locate file name
This is the simple command
I also prefer using a combination of tree and grep. Something like
tree | grep filename
Try
find . -name "filename" -type f
-type f restricts to only files in the current directory (replace . with your path).

Command Line to find and delete Similar files in Linux

I want to find and delete all the "similar files as shown below" recursively from the specified directory of my Linux Server by Using COMMAND LINE....
"file.php?p=10&file=load%2Fnew Animations 1%2FAwesome_flowers-(text1).gif&sort=0"
"file.php?p=10&file=load%2Fnew Games 1%2FAwesome_flowers_-(text1).gif&sort=1"
Note: All these files having different file sizes and The mathed text is in all files is "file.php?p***"
This should help you to delete files recursively..
find /path/to/dir -type f -name 'file.php\?p*' -exec rm -i {} \;

How can I create a file in each folder?

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.

Resources