Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

April 22, 2015

Unix find tip: Excluding files

Sometimes you want to find files on a Unix file system but exclude files either by specific name or by pattern.  The best way I found to do this is by using a combination of find and grep.  Let's take a look.

$ find . -type f | grep -P '^(.(?!Thumbs\.db))*$'

This example uses the find command in the current directory (.) and limits the search to just files (-type f) instead of both files and directories. The result is then piped to grep. The regular expression (-P '^(.(?!Thumbs\.db))*$')  filters out all results from the find command that end with Thumbs.db.

So this is a quick and easy way to exclud files by a specific name.  Next let's take a look at how to exclude files by pattern.

find . -type f | grep -P '^(.(?!\.ffs_db))*$'

This example uses a little more complicated regular expression (-P '^(.(?!\.ffs_db))*$') to filter all results from the find command that end with .ffs_db. This example is essentially exclude by file name extension.

Now what if you want to exclude multiple file names or file types?  Just pipe together more grep statements.

find . -type f | grep -P '^(.(?!Thumbs\.db))*$' | grep -P '^(.(?!\.ffs_db))*$' | grep -P '^(.(?!\.ffs_gui))*$'

In this final example, I am excluding files named Thumbs.db, files that end with .ffs_db and files that end with .ffs_gui.

References
Retrieved April 13, 2015 from http://fineonly.com/solutions/regex-exclude-a-string

Enjoy!

November 17, 2014

DOS equivalent of Unix/Linux alias

The Unix/Linux alias command is great. But now you are stuck in a DOS environment. What do you do?  Use doskey!

The doskey command can be used in a DOS environment to simulate the kinds of things which alias does.  Here is an example from my alias.bat file:

doskey cdhome=cd c:\Users\michael

doskey tw=cd C:\Users\michael\workspace\ferris-tweial\ferris-tweial\ferris-tweial-app\target\unziped\ferris-tweial-app-1.0.0.0-SNAPSHOT

doskey twr=c:\Applications\java\jdk1.7.0_11\bin\java.exe -jar ferris-tweial-app-1.0.0.0-SNAPSHOT.jar 

As you can see, doskey is very similar to alias.  When on a DOS prompt, all you need to do is type tw or twr and that command will be executed. Of course make your own doskey values.

All of my doskey commands are in an alias.bat file.  This means anytime you open a new DOS prompt you need to execute alias.bat to load all the doskey command. Unacceptable! Instead, create a shortcut for either your desktop or toolbar and you edit the shortcut to execute the alias.bat file for you.  The shortcut will look something like this.

cmd.exe /K "c:\Users\Michael\alias.bat"

That's it.

Enjoy!