Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Monday, January 6, 2014

Finding and Replacing Unicode Characters

If you'd like to find a particular multibyte character, you can do the following.
/\%u001c
You can also do a find and replace as you normally would.
%s/\%u001c//g

Tuesday, December 2, 2008

Find Dot Files

Sometimes I forget the name of a particular config file but know it's stored as a dot file in my home directory. The following one-liner will non-recursively display any regular file begining with a dot.

find . -maxdepth 1 -type f -name '.*'

Find does it again. ;-)

Tuesday, June 10, 2008

Quickly Find Subdir

I used to use a long-winded invocation of find with the prune option to non-recursively find all the directories in the current dir:

find . -name . -o -type d -print -prune

From there, I moved to using ls and grep:

ls -l | grep '^d'

I recently learned that the following shell pattern works just as well:

ls -ld */.

Tuesday, May 27, 2008

Running Out of Space?

Running out of space on your HD? I am on a few of my machines. I found the following small Perl script that does a great job helping me locate redundant files. It recurses through a given root directory and looks for files of the same size. When it finds two files of the same size, it does an md5sum on them to determine if they're duplicates. It runs pretty quickly and is easy to tweak to pipe into other scripts.

Find duplicate files.

Thursday, April 17, 2008

Easier Search + Sub

I can't say how many times I've tried to work out a complex substitute command in Vim only to have the pattern fail and not match what I wanted. I've recently come across an easier way to accomplish a substitution on a pattern that's already guaranteed to match. Say you have the following data in your editor.

"this is a line 12345"

Execute a search (instead of a sub):
" match digits
/\d\+

Now execute a sub with no pattern:

%:s//7890/g

The result would be:

"this is a line 7890"

Obviously this is a simple example, but I find this technique handy whenever I'm working out complex patterns.

Friday, March 21, 2008

Perl Mass Substitution

Here's a nice trick I've used many many times over the years. Say that you have a group of files, and you want to replace some text in every file quickly and easily. Perl makes this really easy, and it's ubiquity pretty much guarantees it's around if you're on a *nix platform. For the sake of example, we'll pretend we have a group of html files, and we want to change every occurrence of index.html to index.php. The syntax is as follows:

[edited - thanks Chris!]

perl -pi.bak -e 's/index\.html/index.php/gi' *.html

The command line options provided break down as follows:

-p assumes a while loop over each line of every file and implicitly prints
-i specifies that you want in-place substitution on the file (no redirection of STDOUT required)
-i also takes an option argument of a backup file extension (.bak in this case)
-e tells perl to run the following code on the command line

From there, you're using standard Perl regular expressions. Don't worry if you're not a regexp guru. In the simplest form, you can simply replace one string with another.

The Perl code breaks down as follows:

s(means substitute)/string you want to match/string to replace/
(g means replace multiple instances per line)
(i means case-insensitive matching... aka ignore uppercase and lowercase differences)

Of course if you know regular expressions, you can do all kinds of fancy stuff.

Here's a nice reference.