Indu Pillai
Indu Pillai

Reputation: 377

How do I delete all the MP4 files with a file name not ending with -converted?

I converted/compressed several MP4 files from several folders using VLC. The names of the converted/compressed files end with -converted, for example. 2. bubble sort-converted.mp4.

It's really cumbersome to go into each folder and delete all the original files and leave the converted files.

Using some zsh/bash command I'd like to recursively delete all the original files and leave the converted files. For example I'll delete 3 - sorting/2. bubble sort.mp4 and will leave 3 - sorting/2. bubble sort-converted.mp4.

TLDR; In easy words, delete all the files with .mp4 extension, where filesnames don't end with -converted using some zsh/bash command.

Also If there is some way to rename the converted file to the original name after deleting the original files, that will be a plus.

Thank you!

Upvotes: 0

Views: 1607

Answers (4)

Ole Tange
Ole Tange

Reputation: 33685

This will work even if your file names contain ', " or space:

find . -name '*.mp4' |
  grep -v 'converted' |
  parallel -X rm -f

Upvotes: 0

SergioAraujo
SergioAraujo

Reputation: 11790

The zsh pure solution:

rm -f ^(*.mp4-converted)(.)

^ ................. negates
*-converted ....... pattern
(.) ............... regular files

Using gnu parallel (in case of many files)

parallel --no-notice rm -rf ::: ^(*converted)(.)

Upvotes: 0

Nguyen Cong
Nguyen Cong

Reputation: 189

Give this a try:

find . -name '*.mp4' | grep -v 'converted' | xargs rm -f

Upvotes: 0

kaylum
kaylum

Reputation: 14046

find can be used with a logical expression to match the desired files and delete them.

In your case the following can be used to verify whether it matches the files you want to delete. It finds all files that don't have converted in their names but do end in .mp4.

find . -type f -not \( -name '*converted*' \) -a -name "*.mp4"

Once you are satsified with the file list result then add -delete to do the actual delete.

find . -type f -not \( -name '*converted*' \) -a -name "*.mp4" -delete

Upvotes: 2

Related Questions