2

I have a large list of .mp4 files in one directory on a NAS(Network Attached Storage).

Some are listed as follows :

XXXXX_3800.mp4

and other like

XXXXX_8000.mp4

XXXXX is the same for both files.

I would like to automate delete the XXXXX_8000.mp4 but ONLY if the file XXXXX_3800.mp4 exists.

How should I proceed?

4
  • Is there a common pattern for XXXXX or every other pair has different pattern?
    – heemayl
    Feb 3, 2015 at 22:18
  • @heemayl Please, don't just improve the formatting, check the spelling and grammar as well. BTW: a NAS is Network Attached Storage not a linux command or output thereof... ;-)
    – Fabby
    Feb 3, 2015 at 22:24
  • 1
    @Fabby: ....as you said so ;)
    – heemayl
    Feb 3, 2015 at 22:29
  • Are the dupes in the same (sub) directory? Feb 9, 2015 at 13:02

2 Answers 2

4
  1. Go to a terminal by pressing Ctrl+Alt+T and type md bin
  2. Go to the dash
  3. type gedit and press Enter
  4. Copy paste the following text into it:
    #!/bin/bash

    #
    # This script deletes video files of 8000 bps if and only if the 3800 bps file exists 
    # as set in http://askubuntu.com/questions/581400/how-to-delete-files-selected-by-rules
    #

    # Copyright (c) Fabby 2015

    # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
    # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. See the GNU General Public License for more details.
    # You DID NOT receive a copy of the GNU General Public License along with this program as the license is bigger than this program.
    # Therefore, see http://www.gnu.org/licenses/ for more details.

    for szFile in $(ls *3800.mp4)
    do
      if [ -f ${szFile:0:${#szFile}-8}'8000.mp4' ] ; then
        echo "deleting ${szFile:0:${#szFile}-8}8000.mp4..."
        rm -f ${szFile:0:${#szFile}-8}'8000.mp4'
      fi
    done
  1. Save the file under ~/bin/del8000
  2. Go back to the terminal and type: chmod +x ~/bin/del8000
  3. In the terminal type: cd ~/Videos
  4. Type del8000

Done!

13
  • I think it should be the other way around, if XXXXX_3800.mp4 exists then delete XXXXX_8000.mp4, anyway same logic.
    – heemayl
    Feb 3, 2015 at 23:16
  • @heemayl: Why didn't you answer then? You were editing the question! Now I feel guilty for answering... Were you making a script as well?
    – Fabby
    Feb 3, 2015 at 23:19
  • 1
    '+1' for your answer.
    – heemayl
    Feb 3, 2015 at 23:31
  • 1
    Sorry but as I said in my previous comment, this scripts DOES NOT delete the *8000.mp4 present in my test directory where I've put both *3800.mp4 and *8000.mp4 files. Feb 8, 2015 at 23:09
  • 1
    And @kasiya just improved it even further! Thanks!
    – Fabby
    Feb 9, 2015 at 8:30
1

In a single (flat) directory

If all files are in one and the same directory, the script below should do the job.

#!/usr/bin/env python3
import os
import sys

dr = sys.argv[1]; ids = ("_3800.mp4", "_8000.mp4")
checklist = [f[:-9] for f in os.listdir(dr) if f[-9:] in (ids)]
for f in [f for f in set(checklist) if checklist.count(f) != 1]:
    os.remove(dr+"/"+f+"_8000.mp4")

How it works:

  • The script lists all files, ending with either _3800.mp4 or _8000.mp4, stripped from their endings
  • If the (stripped-) name occurs twice, both versions exist (since duplicate names are impossible in one and the same directory)
  • Subsequently, the script removes the _8000.mp4 - version from the duplicates

In a layered directory (search recursively)

A slightly different approach. It compares files recursively and (only) deletes the _8000.mp4 - version if the _3800.mp4 - version exists.
I wasn't sure there was a need for a recursive search, but added it anyway.

How it works:

  • First list the files to keep anyway
  • Then remove possible "throw aways", if the version-to-keep exists.
#!/usr/bin/env python3
import os
import sys

files_dir = sys.argv[1]; file_list = []
#---
keep = "_3800.mp4"; rm = "_8000.mp4"
#---
for root, dirs, files in os.walk(files_dir):
    for name in files:
        if name.endswith(keep):
            file_list.append(name)
    for name in files:
        if name.endswith(rm) and name.replace(rm, keep) in file_list:
            os.remove(root+"/"+name)


How to use

To use either one of these scripts:

  1. Copy the script into an empty file, save it as clean_directory.py
  2. Run it, with the directory to clean up as an argument, by the command:

    python3 /path/to/clean_directory.py </path/to/directory/to/clean>
    

You must log in to answer this question.

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