Remove path from find command output
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 information
find . -type f -mtime -14 -printf '%f\n' > deploy.txt
or you can use sed to just remove the ./
find . -type f -mtime -14 | sed 's|^./||' >deploy.txt
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:
find . -type d -mtime 14 | sed -e 's,^\./,,' > deploy.txt
If you're on a system with GNU find (e.g. most Linux systems), you can do it in one shot with find -printf
:
find . -type d -mtime 14 -printf "%P\n" > deploy.txt
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.