Serjudo
Serjudo

Reputation: 13

Script for deleting files starting with texts listed in a file

I have a strings.txt file that contains a list of strings like this:

0100101
0100102
0100103
0100104
...

I need to create a script that deletes all the files in a directory, starting with each of the strings contained in the previous file.

If there are files with these names in the directory, they should be deleted:

0100101.jpg
0100101-01.jpg
0100101A1.jpg

If there are files with these names in the directory, they should not be deleted:

40100101.jpg
570100102.jpg
340100104-02.jpg

Can you help me?

Upvotes: 0

Views: 46

Answers (1)

BlackPearl
BlackPearl

Reputation: 1662

You can use a combination of cat and xargs to achieve this

cat strings.txt | xargs -t -I{} sh -c 'rm {}*' 

More Info on xargs: http://man7.org/linux/man-pages/man1/xargs.1.html

Upvotes: 1

Related Questions