RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Match beginning and end of the filename using meta characters and regex

Article Index
1. Matching a pattern
2. Meta-characters
3. Regular expressions

Question:

What is the name of the command which search for all the files starting with 'A' and end with 'K'?

Answer:

ls | grep ^A.*K$

Long answer:

Rather than searching for a single command we need a combination of commands to do this trick. Before we are able to do such a trick we need to get acquainted with couple bash features and terms:

Pipes

Pipe "|" allows us to redirect an output from one command to another command.

$ command-1 | command-1

Any output produced by command-1 is redirected for a further processing to a command-2. Here is a practical example:

$ ls
file1  file2  file3  file4

ls command returned names of all files and directories ( hidden files are not included ) currently residing in a current working directory. Redirecting an output from ls to wc command we can count number of files and directories located within a current working directory.

ls | wc -l
4

1. Matching a pattern

In addition to a command output redirection we can also search for a pattern within a filename ( or standard input ) using grep command. For example we want to count how many files in our current working directory contain a digit 4 within their filename:

$ ls | grep 4
file4

To pipe this output to yet another command such us wc we can also count number of files:

ls | grep 4 | wc -l
1

At this point we can clearly say that in our current working directory is only one file which contains a digit 4 in its file name.

2. Meta-characters

The real power comes when we start using meta-characters to search a pattern within a given string. The table below list all meta-characters and their meaning:

Meta-character Meaning
. (period) Matches any one character no matter what the character is
? Matches the character immediately before it either zero times or one time
* Matches the character immediately before it any number of times including zero (the character may not be in the string at all)
+ Matches the character immediately before it one or more times (the character must be in the string at least once)
^ Indicates that the characters which follow are at the start of the string only
$ Indicates that the characters which precede it are at the end of the string
\d Matches any decimal digit
\D Matches any character that is not a decimal digit
\s Matches a tab or space character
\S Matches any character that is not a tab or a space
\w Matches any letter, any digit, or the underscore character
\W Matches any character which is not a letter, a digit, or the underscore
\ Escape character allowing the use of any of the metacharacters with their regular keyboard meaning. For instance, \. matches a period (.) in a regular expression. A period (.) matches any one character no matter what the character is.

3. Regular expressions

Based on the knowledge above we can combine all what we have learned into a more complicated linux command using regular expression. Simply put, a regular expression allows us to search for a pattern within a string by use of meta-characters.

At this point we can go back to our original problem, which is to search for all files starting with 'A' and ending with 'K'.Here is a list of meta-characters we need to use in order to accomplish this task: "^", ".", "*" and "$".

Looking at the list of meta-characters and their meaning above we can construct a following command to display all files and directories starting with "a" and ending with "k" ( lowercase ! ) character.

$ cd /usr/bin/
$ ls | grep ^a.*k$
amarok
ark
authconfig-gtk
awk 

Linux questions and answers

Share this linux post:

Submit Match beginning and end of the filename using meta characters and regex in Delicious Submit Match beginning and end of the filename using meta characters and regex in Digg Submit Match beginning and end of the filename using meta characters and regex in FaceBook Submit Match beginning and end of the filename using meta characters and regex in Google Bookmarks Submit Match beginning and end of the filename using meta characters and regex in Stumbleupon Submit Match beginning and end of the filename using meta characters and regex in Technorati Submit Match beginning and end of the filename using meta characters and regex in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download