I have a project in Linux. I want to create a file named index.html in all folders.
So I have used the following command:
find . -type d -exec touch {}/index.html \;
It's working! Now I'm trying to copy the existing file from a given location and it to be automatically replaced into all the folders of my project.
This should actually work exactly in the same way:
find . -type d -exec cp $sourcedir/index.html {}/index.html \;
If I understand your question correctly, what you want is to copy a given file in all the directories.
You can use a similar find command :
find . -type d -exec cp -f /tmp/index.html {} \;
where /tmp/index.html is path to the original file (replace it with your own path).
Also, you don't need to create the files if your final objective is to replace them with the original file.
tar -cvzf index.tar.gz `find . -type f -iname 'index.html'` && scp index.tar.gz USER#SERVER:/your/projec/root/on/SERVER && ssh USER#SERVER "tar -xvzf index.tar.gz"
Or if you're in the proper directory localhost, and rsync is available:
rsync -r --exclude='**' --include='**/index.html' . USER#SERVER:/your/projec/root/on/SERVER
HTH
Related
This line works in the terminal, but not in a bash script:
cd /home/me/Downloads/Data/$currentYear/$currentMonth/$currentDay/
find . -name '*.wav' -exec cp {} $tempfolder \;
I'm trying to copy all the WAVE files from all the sub-directories to a temporary folder
So, I solved it. Turns out, that the
cd /home/me/Downloads/Data/$currentYear/$currentMonth/$currentDay/
was not actually changing the directory for the
find .
to work. The script was trying to find the files in its own directory. Once i wrote find "$absolutePath" -name '*.wav' -exec cp {} "$tempfolder" \; it worked
I'm trying to execute this command to copy the latest file that exist in the courant directory to another one .
find . -mtime -1 -exec cp -r {} /media/96DB-120D/bck \;
but after copying the recent files , I find the other content of the folder that does not respond to the condition -mtime -1 .
If any one had an idea about how to fix it to just copy the result of find command and thanks.
The find command probably includes the directory and then cp copies all the files in the directory. Add -type f to only have find report actual files.
Try the -p option of cp command which will preserve the timestamp of the copied file:
find . -mtime -1 -exec cp -pr {} /media/96DB-120D/bck \;
I think this is the best solution :
find . -mtime -1 -type f -exec cp --parents {} /media/960DB-120D/db \;
How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.
I have a folder in ~/Downloads with lots of
files and folders scattered. This consists of various files of different
extensions. I need to copy only the
.pdf files within various directories to ~/pdfs
Use find:
find ~/Downloads -type f -name "*.pdf" -exec cp {} ~/pdfs \;
if ~/pdfs exists in your system use the following command
cd ~/Downloads ; cp -r *.pdf ~/pdfs
if ~/pdfs does not exists in your system use the following command
cd ~/Downloads ; mkdir ~/pdfs ; cp -r *.pdf ~/pdfs
In order to deal with potential file names with spaces, etc., I would recommend this approach:
find ~/Downloads/. -type f -name "*.pdf" -print0 | xargs -0 -I_ cp _ ~/pdfs/.
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.