What is the name of the command which search for all the files starting with 'A' and end with 'K'?
ls | grep ^A.*K$
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:
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
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.
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:
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