How to remove certain files? [SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
kgully

How to remove certain files? [SOLVED]

Post by kgully »

Hey all,

I need help removing a bunch of old files from a directory, and don't have very good regular-expression foo so I thought I would see if anyone had a quick solution?

I have a directory with files that follow two different patterns:
new pattern: transmission-743.50-285.0-6.0-7.5
old pattern: transmission-743.50-285.0-6.5

As you can see, the old pattern, which I want to remove, has one less dash and one less number. There are thousands of each file type, so going into nautilus is not really a very good option for me. Any help would be much appreciated!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: How to remove certain files?

Post by xenopeek »

For the current directory, this command gives you all the files that match pattern with 3 dashes (transmission-1-2-3) and not with 4 dashes (transmission-1-2-3-4):

Code: Select all

ls | grep "^transmission-[^-]*-[^-]*-[^-]*$"
The following command will delete all the matching files:

Code: Select all

 ls | grep "^transmission-[^-]*-[^-]*-[^-]*$" | xargs rm
Image
kgully

Re: How to remove certain files?

Post by kgully »

Thanks Vincent!
Locked

Return to “Scripts & Bash”