Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
2  
Is there a reason you can't just use rsync for this? It sounds as if you may be trying to reinvent the wheel and make it a triangle. –  ThatGraemeGuy Jan 27 '12 at 21:05
    
valid point however before deployment the deploy.txt is reviewed and anything that needs fixing gets removed –  Mikey1980 Jan 27 '12 at 21:08
    
Maybe run rsync in --dry-run mode and output that to a file for review? –  Zoredache Jan 27 '12 at 21:14

7 Answers 7

up vote 5 down vote accepted

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
share|improve this answer
    
I believe you have to escape the /.. –  EEAA Jan 27 '12 at 21:11
2  
Not when you use | as the delimiter. –  Iain Jan 27 '12 at 21:13
    
Using %f will strip off all directories, not just ./. This will break his deployment script if any of the files returned by find are located in subdirectories. –  James Sneeringer Jan 27 '12 at 21:13
1  
@Iain You do need to escape the . though. And maybe best to use a start-of-line anchor: s|^\./|| –  James O'Gorman Jan 27 '12 at 21:25
1  
Use %P instead of %f. This just cut off the path given in the command line. –  TrueY May 29 '13 at 10:05

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 ./.

# simply print out the filename, will break if you have sub-directories.
find . -mtime -14 -printf '%f\n'

# strip a leading ./
find .  -mtime -14 | sed -e 's/^\.\///'
share|improve this answer
    
Can you give me an example of it's usage? Sorry, linux is not really my forté –  Mikey1980 Jan 27 '12 at 21:11
2  
Look again, there are examples. –  Zoredache Jan 27 '12 at 21:13
    
Agreed - just try it, you're not going to hurt anything running a combination of find and sed. –  EEAA Jan 27 '12 at 21:15

sed is perfect for this sort of thing.

$ find . -type f -mtime -14 find . | sed 's/^\.\///' > deploy.txt
share|improve this answer

A quick solution, if I understand correctly the question, is using cut on the output of the find command:

$> find . -type f -mtime -14 | cut -b 3- > deploy.txt

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.

share|improve this answer

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.

share|improve this answer

Why do you need to strip off the ./? It is a valid to have in a path. So

cp -i dir1/./somefile dir2/./somefile

is 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:

         %P     File's name with the name of the  command  line  argument
                 under which it was found removed.

An example

$ find other -maxdepth 1
other
other/CVS
other/bin
other/lib
other/doc
other/gdbinit
$ find other -maxdepth 1 -printf "%P\n"

CVS
bin
lib
doc
gdbinit

Mind the first empty line! If You want to avoid it use -mindepth 1

$ find other -mindepth 1 -maxdepth 1 -printf "%P\n"
CVS
bin
lib
doc
gdbinit
share|improve this answer

"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.

find /usr/local/etc/rc.d -type f | awk -F/ '{print $NF}'

PS: taken from D.Tansley "Linux and Unix shell programming" book

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.