Contents[Hide]

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-characterMeaning
. (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



Free Linux eBooks

Go to top