How I found symbolic links for gitignore with find and cut
I was cleaning up a directory of old files for addition to git, and I came across a bunch of symlinks to a completely different codebase. In this particular case it was a wordpress blog that had a series of its files linked into the webroot of the site, that existed in an entirely different directory. I didn't want these files in the git repo.
Finding all the symlink files is easy enough.
cd /path/to/root
The find command gives us an easy way to get all symlink files:
find . -type l
But that's just a start -- we have to get these into the .gitignore file.
Finding all the symlink files is easy enough.
cd /path/to/root
The find command gives us an easy way to get all symlink files:
find . -type l
But that's just a start -- we have to get these into the .gitignore file.
./wp-blog-header.php
./wp-login.php
These are perfect for a cut and paste addition to the .gitignore or even just appending them to it, if not for the leading period. How could we remove that?
Well as it happens, we can pipe the output to cut. What if we cut the first character? Specifying cut -c n, will cut n characters.
find . -type l | cut -c 1
.
.
But we want the filenames without the first character.
If we use -c n- we'll get characters from n on.
find . -type l | cut -c 1-
./wp-blog-header.php
./wp-login.php
What the heck? Same output! Let's double check the cut man page.
N- from N'th byte, character or field, to end of line
While the trailing dash looks cryptic, it's really part of a range, as in the option N-M where N is the first character and M is the last character position. Leaving off the 'M" is just telling cut to give us "from N- to the last character in the line".
So the problem we have is that we want the 2nd character in the line, and then all the rest.
Our fixed cut:
find . -type l | cut -c 2-
/wp-blog-header.php
/wp-login.php
Everything looks good, so I appended to the .gitignore
find . -type l | cut -c 2- >> .gitignore
Comments
Display comments as Linear | Threaded