How to search for files in Linux?

Sometimes you want to apply command on several files or a subset of files. For example, you want to delete all files whose name starts with “TH”. So there are two ways to do it. One, you delete them one by one or you specify some pattern which matches all files starting with “TH”.
In this post, we are going to learn about – How to search for files in Linux? in order to accomplish a specific task on a set of files.

Search Patterns

The shell provides certain characters which help in pattern search.

1. The Asterisk (*)

‘*’ – means the shell will will match any character any number of times

For example,

$ls th*

means list all files and directories whose names start with ‘th’

$rm *.c

means delete all files having extension ‘.c’ or in other words delete all C files

$echo ~/f*c

will list all files and directories in the home directory whose name that start with ‘f’ and end with ‘c’

2. The Question Mark (?)

‘?’ – means any one character (except slash)

For example, a pattern
p?.c
will match anything like

p1.c
pg.c
p-.c
among others

The figure below shows the difference between the use of ls p*.c, ls p?.c . First we list all the files present with ls command.

how to search for files using * and ?
Difference between * and ?

with p*.c the ls command lists all files starting with character ‘p’, then there can be any number of character and the file name ends with ‘.c’. But with ‘p?.c’ the ls command lists those files whose name starts with ‘p’ then there is any one character between ‘p’ and ‘.c’

3. Square Brackets ‘[ ]’

[1av] – will match exactly one character out of ‘1’ or ‘a’ or ‘v’.

For example, the pattern p[6fq].c will match

p6.c
pf.c
pq.c

The example below shows the use

how to use square brackets to search file patterns

Ranges

We can also use range within [ ]. For example, to specify all digits one can write [0-9], or to specify all alphabets [A-Za-z], only uppercase alphabets [A-Z], a selective set [C-M4-5]. For example

$rm [0-9]*

will delete all files starting with a digit

$rmdir dc[2-5$#]d

will delete all directories whose name start with ‘dc’, followed by either 2 or 3 or 4 or 5 or $ or # and then ends with ‘d’

Negation ‘!’

Sometimes you want to negate a pattern. Like in the above example where we searched for p[6fq].c, suppose you want to want to list files whose name starts with ‘p’ but the second character is not ‘6’ or ‘p’ or ‘f’. Then we will use the exclamation ‘!’ sign. For example:

use of negation in shell pattern

The difference can be compared.

4. Braces ‘{ }’

‘{ } – help to specify a group of characters for matching. With [ ] one can match exactly single character within the square braces. But what if you want to match say “preet”, “jeet” or “meet”. For example,

har{preet,jeet,meet}

will match

harpreet
harjeet
harmeet

Other example on How to search for files in Linux?

Q1. How to delete all files having an extension?
Ans: $rm *.*

Q2. List all files that end with a digit
Ans: $ls *[0-9]

Q3. Make a directory for each student for each section of 3rd year. Suppose the sections are K1801, K1802, K1803, K1804 and each section has 10 students with roll number R1, R2, R3, so on upto R10.
Ans: $mkdir -p K180{1,2,3,4,5}/R{1,2,3,4,5,6,7,8,9,10}

PPT on File Globbing

Video on File Globbing

Other Relevant Commands

ls
touch
chmod
useradd
groupadd

Leave a Reply

Your email address will not be published. Required fields are marked *