I have a bash script I'm trying to write I have 2 base directories:
- ./tmp/serve/
- ./src/
I want to go through all the directories in ./tmp and copy the *.html files into the same folder path in ./src
i.e if I have a html file in ./tmp/serve/app/components/help/ help.html --> copy to ./src/app/components/help/ And recursively do this for all subdirectories in ./tmp/
NOTE: the folder structures should exist so just need to copy them only. If it doesn't then hopefully it could create the folder for me (not what I want) but with GIT I can track these folders to manually handle those loose html files.
I got as far as
echo $(find . -name "*.html")\n
But not sure how to actually extract the file path with pwd and do what I need to, maybe it's not a one liner and needs to be done with some vars.
CodePudding user response:
something like
for i in `echo $(find /tmp/ -name "*.html")\n
do
cp -r $i /src/app/components/help/
done
going so far to create the directories would take some more time for me.
I'll try to do it on my own and see if I come up with something
but for argument sake if you do run pwd
and get a response the pseudo code for that:
- pwd
- get response
- if that directory does not exist in src create that directory
- copy all the original directories contents into the new folder at /src/$newfolder (possibly running two for loops, one to check the directory tree, and then one to go through each original directory, copying all the html files)
CodePudding user response:
You process substitution to loop the output from your find command and create the destination directory(ies) and then copy the file(s):
#!/bin/bash
# accept first parameters to script as src_dir and dest values or
# simply use default values if no parameter(s) passed
src_dir=${1:-/tmp/serve}
dest=${2-src}
while read -r orig_path ; do
# To replace the first occurrence of a pattern with a given string,
# use ${parameter/pattern/string}
dest_path="${orig_path/tmp\/serve/${dest}}"
# Use dirname to remove the filename from the destination path
# and create the destination directory.
dest_dir=$(dirname "${dest_path}")
mkdir -p "${dest_dir}"
cp "${orig_path}" "${dest_path}"
done < <(find "${src_dir}" -name '*.html')
CodePudding user response:
This script copy .html files from src directory to des directory (create the subdirectory if they do not exist)
Find the files, then remove the src directory name and copy them into the destination directory.
#!/bin/bash
for i in `echo $(find src/ -name "*.html")`
do
file=$(echo $i | sed 's/src\///g')
cp -r --parents $i des
done
CodePudding user response:
Not sure if you must use bash
constructs or not, but here is a GNU tar
solution (if you use GNU tar), which IMHO is the best way to handle this situation because all the metadata for the files (permissions, etc.) are preserved:
find ./tmp/serve -name '*.html' -type f -print0 | tar --null -T - -c | tar -x -v -C ./src --strip-components=3
This finds all the .html
files (-type f
) in the ./tmp/serve
directory and prints them nul-terminated (-print0
), then sends these filenames via stdin
to tar
as nul-terminated literals (--null
) for inclusion (-T -
), creating (-c
) an archive which is then sent to another tar
instance which extracts (-x
) the archive printing its contents along the way (optional: -v
), changing directory to the destination (-C ./src
) before commencing and stripping (--strip-components=3
) the ./tmp/serve/
prefix from the files. (You could also cd ./tmp/serve
beforehand, using find .
instead, and change -C
to ../../src
.)