I have a bash script for deploying code from a beta environment to a production environment but currently I have to add the list of files to a txt file manaully and sometime I miss some. Basically my deployment script cat/loops copying the files over. (exports/imports db as well but that's not relevant..lol)
Anyway, I'd like to use the find
command to generate a list of files modified in the last 14 days. The problem is I need to strip the path out ./
in order for the deployment script to work.
Here's an example of the find command usage:
find . -type f -mtime -14 > deploy.txt
Here's the line that cats deploy.txt
in my deployment script:
for i in `cat deploy.txt`; do cp -i /home/user/beta/public_html/$i /home/user/public_html/$i; done
Any idea how to accomplish this using bash scripting?
Thanks!
You can use the
-printf
command line option with%f
to print just the filename without any directory informationor you can use sed to just remove the ./
The
./
should be harmless. Most programs will treat/foo/bar
and/foo/./bar
as equivalent. I realize it doesn't look very nice, but based on what you've posted, I see no reason why it would cause your script to fail.If you really want to strip it off,
sed
is probably the cleanest way:If you're on a system with GNU find (e.g. most Linux systems), you can do it in one shot with
find -printf
:The
%P
returns the complete path of each file found, minus the path specified on the command line, up to and including the first slash. This will preserve any subdirectories in your directory structure.Why do you need to strip off the
./
? It is a valid to have in a path. Sois just ok!
But if You would like to strip off the directory name in find You can use the
%P
arg to-printf
.man find(1) says:
An example
Mind the first empty line! If You want to avoid it use
-mindepth 1
"find -printf" solution won't work on FreeBSD because find don't have such an option. In this case AWK can help. It returns a last name ($NF), so it can work on any depth.
PS: taken from D.Tansley "Linux and Unix shell programming" book
Well you have a few options. You could use the
-printf
option in find to only print out the filename, or you could use a tool like sed to simply strip out the./
.A quick solution, if I understand correctly the question, is using
cut
on the output of thefind
command:This will strip the first to characters from each line of the result (in your case
./
). Probably not the best solution, but works in your scenario.sed
is perfect for this sort of thing.I don't know what kind of filenames we're talking about, but anything with spaces or newlines puts most of the solutions here at risk.
My suggestion would be to use shell's parameter expansion to strip of the characters from each filename:
If you really really like pipes, you could use a "safe" delimiter, like a null, with xargs to receive each filename:
Remember that the output of this is NOT null-delimited, so while it may be sufficient for an eyeball check, none of these solutions produce a deploy.txt file that is safe to use for automation, unless you're very confident about your source filenames.