Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Remove directories and files NOT in list  (Read 4749 times)

0 Members and 1 Guest are viewing this topic.

pk7

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Linux variant
    Remove directories and files NOT in list
    « on: April 16, 2014, 04:10:28 PM »
    We have numerous directories that each contain 2 files, one of which is a hidden file.  We want to remove all those directories and their contents that contain ONLY files that have a modification date older than 180 days.  So, for example, if we have the following:

    Dir1   Jan 1 2000
         File1A   Jan 1 2000
         File1B   Jan 1 2000
    Dir2   Jan 1 2000
         File2A   Jan 1 2014
         File2B   Jan 1 2014
    Dir3   Jan 1 2000
         File3A   Jan 1 2014
         File3B   Jan 1 2000

    I need a command that will remove only Dir1 and all of its contents including the hidden file.  Dir2 and Dir 3 would remain untouched because each contain at least one file that is newer than 180 days ago.

    I've played around with listing those directories that contain a newer file but I couldn't find an "inverse" command that would then remove all the "other" directories.

    Thanks in advance for your help!

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Remove directories and files NOT in list
    « Reply #1 on: April 17, 2014, 04:21:59 PM »
    OK. First, some basic questions.
    Why?
     -Is it to save space on your hard drive?
     - Is it to reduce visual clutter?
     - Does it affect the logic of a process?
    Also, you have enough drive space to allow temporary storage?
    Are you sure a bash command is the best method for you?
     - Some users would use visual interface, but  you can do it in the terminal mode swith a few commands
    We assume you have a backup plan already in place.
    As for me, I am  lazy and do not like to make bad mistakes. So I would make the computer do the work. So this is what I do in case I want to zap just old files.

    Make a sibling directory named 'temp'. (A sibling is a directory alongside the  working you use.)
    Copy all new files and directories to the 'teemp' directory.
    (In Windows you would use  Xcopy. In Linux you might use  'cp' or another utility.

    Next, if all goes well, delete all in the working directory.
    Last step is move contents of the temp dir working dir.
    This can be don e one step at a time and you can view the progress.

    Others here can find a more elegant solution.   ;)