2

There are 3 or 4 files in same directory as below;

AAA.360p.mp4
AAA.450p.mp4
AAA.720p.mp4
AAA.1080p.mp4

Filenames of those files are almost the same except expression of frame resolution.

(AAA is example of file name. To present that those filenames are same except frame resolution.)

And There are several dots between AAA.

For example, filename is like this;

Interesting.Comedy.E10.200406.450p.mp4
Interesting.Comedy.E10.200406.720p.mp4
Interesting.Comedy.E10.200406.1080p.mp4

Sizes of each file are different

(file size : 360p < 450p < 720p < 1080p) → It is always true.

I'd like to keep only one file, the one that is largest size and delete all other files.

Location of directory is /volume1/video/

It will be run the command only on the synology. (to use task scheduler in the control panel)

If you explain to me, please include path of directory in my case as above.

(because I can't apply the code what you recommend to me for lack of my understanding. I apologize)

7
  • Please edit your question and show us the exact names of your files. Are the *** really part of the file name? Do all names start with AAAor do you have many different prefixes? And, if you do, will there always be exactly 2 . characters in the name, or can you have something like foo.bar.360p.mp4? Will you be running the command on the synology itself or from a regular computer that has acccess to the synology drive? What tools can we use?
    – terdon
    Apr 7, 2020 at 16:09
  • 1. *** It means bold and Italic.
    – FBMan
    Apr 7, 2020 at 16:10
  • Sorry that was my bad, The *** came about when I "fixed" the formatting and generally tidied the question to make it readable. Apr 7, 2020 at 16:13
  • 2. AAA is example of file name. To present that those filenames are same except frame resolution.
    – FBMan
    Apr 7, 2020 at 16:13
  • 2
    I would not advice blindly running code from the internets. Everybody makes mistakes, or you could unintentionaly omit some details that are crucial. This could lead to undesired or even catastrophic results. If you can, make the effort to understand the code that you are running to avoid this kind of scenario. Apr 7, 2020 at 16:19

2 Answers 2

0

The below code will be useful for you, however you can create a copy of files and try on that directory first.

#!/bin/bash

var=`find /volume1/video/ -printf '%s %p\n'| sort -nr | head -n 1 | awk '{ print $2 }'`
largestfilename=`echo $var | rev | cut -d"/" -f1 | rev`

for i in `ls -S /volume1/video/`
do
        if [ ! $i == $largestfilename ]
        then
                rm -f /volume1/video/$i
        fi
done
1
  • Thank you very much. When I run the first line of command, I met this message. "-ash: rev: command not found"
    – FBMan
    Apr 9, 2020 at 12:25
0
LARGEST=$(ls -S /volume1/video/* | head -1)
ls /volume1/video/* | grep -v "$LARGEST" | xargs -d '\n' rm

The first line runs a command to list all files in /volume1/video/ sorted by size and pipes that list into head to identify the first one. The output of that command is then stored in a variable named $LARGEST.

The second line lists all files in /volume1/video/ again, greps out the name of the largest one, and removes the rest.

Edit: Added -d flag to xargs to account for potential whitespace in filenames. See this answer for more info.

1
  • Thank you very much. but I have another question. What should I do if there is a space in the file name? It works well when there is no space in the file name.
    – FBMan
    Apr 9, 2020 at 12:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .