Remove files from directory based on partial name in Linux

Wanted to know what is the best way to remove any files from directory no matter what their extension is based on a partial name. You have multiple files in directory but wanted to delete specifices file which files name are started from or end from or having some characters.

Cron_foo_something.jpg
Cron_foo_today.jpg
Text_something.png
Logs_foo_site.html
logo.php

The simple way go to directory path:

cd /path/to/directory

run command rm. The following example used for file name having _foo_

rm *_foo_*

or
you can directly run rm commant by passing file path

rm /path/to/directory/*_foo_*

 


Delete the file which name started from Cron_foo_

rm Cron_foo_*

or

rm /path/to/directory/*Cron_foo_*

Delete the file which name started from _something

rm *_something

or

rm /path/to/directory/*_something

 

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Yes0
No0
Harinder Singh

Harinder Singh

My name is Harinder Singh and I specialize in Software industry. I consider myself as a life learner. I love learning new concepts, embracing new ideas and reading and searching for innovation.