-2

In command line, How can we recursively find out all the zip files in a directory and its sub directories and keep only the latest modified 5 files and delete the remaining.

The files paths would be something like below:

  1. basedirectory/2015/12/18/abc.zip
  2. basedirectory/2015/12/18/def.zip
  3. basedirectory/2015/12/18/ghi.zip
  4. basedirectory/2015/12/18/jkl.zip
  5. basedirectory/2015/12/08/mno.zip
  6. basedirectory/2015/12/08/pqr.zip
  7. basedirectory/2015/12/08/stu.zip
  8. basedirectory/2015/12/07/stu.zip

1 Answer 1

0

I have a way, but it involves several (easy) steps. There are probably more elegant ways of doing this, but here is how I know how. They come from a couple sources, which I list at the end of my answer. You will use the already installed utilites cd, find, ls, rm and head. it will involve a creating and executing two bash scripts.

  1. Open a terminal and change into your base directory with cd ~/basedirectory

This sets up the following commands. It is important that you stay in this directory for the rest of the commands.

  1. Type findpwd-name *.zip > find_zip

This creates a list of all the zip files with the full path relative to the directory you changed in to. Instead of printing them to the screen, it writes them to a find_zip file in the directory you changed into.

  1. type cp find_zip remove_old_zip

This creates a second, duplicate file that you will later use to delete the old files.

  1. Open the find_zip file in your favorite text editor. If you're not used to using any, you can use gedit. If you don't have it, install it with sudo apt-get udpate && sudo apt-get install gedit

  2. Do a search and replace as follows (in gedit): search for \n , and replace it with " \\n"

This places the list of folders within quotes. the first backslash places a "\" at the end of each line, which means continue reading the next line and execute all the code together. The \n preserves the line endings. The last " puts a quote at the beginning of each line. You need the quotes to escape special characters like ' and ( that may be in your file name.

  1. Create 2 new lines at the top of the file and type:

    !/bin/bash

    ls -lt \

The first line turns your file into a bash script. The second line will list all the files you found with the find command and order them by date.

  1. Create a new line at the bottom of your file and type: | head -5. Save and exit the file.

| is a "pipe" that will take the output of the ordered file list that ls creates and feed it into the head command. The head command will list just the 5 most recently modified files and display or print them on your screen.

As a result of steps 5-7, your file should go from looking like this:

basedirectory/2015/12/18/abc.zip
basedirectory/2015/12/18/def.zip
basedirectory/2015/12/18/ghi.zip
basedirectory/2015/12/18/jkl.zip
basedirectory/2015/12/08/mno.zip
basedirectory/2015/12/08/pqr.zip
basedirectory/2015/12/08/stu.zip
basedirectory/2015/12/07/stu.zip

to this:

#!/bin/bash
ls -lt \
basedirectory/2015/12/18/abc.zip \
basedirectory/2015/12/18/def.zip \
basedirectory/2015/12/18/ghi.zip \
basedirectory/2015/12/18/jkl.zip \
basedirectory/2015/12/08/mno.zip \
basedirectory/2015/12/08/pqr.zip \
basedirectory/2015/12/08/stu.zip \
basedirectory/2015/12/07/stu.zip \
| head -5
  1. Type bash find_zip into in the terminal. With your newfound list of the 5 most recent files, open up the remove_old_zip file created in step 3.

You will also be turning this file into a bash script, but it will remove all but the five newest files.

  1. Delete the lines in the remove_old_zip file containing the 5 files you want to keep.

  2. Do a search and replace as follows (in gedit): search for \n , and replace it with " \\n"

This is the same as step 5.

  1. Create 2 new lines at the top of the file and type:

    !/bin/bash

    rm \

This is similar to step 6 except that rm will delete the files still listed.

  1. remove the final \ on the final line of the remove_old_zip file. Save and exit.

  2. Type bash remove_old_zip.

  3. Type rm find_zip remove_old_zip. This remove the two scripts, which are now useless since the files have been deleted.

sources: How can I list (ls) the 5 last modified files in a directory?

http://www.geekinterview.com/talk/758-how-to-continue-to-next-line.html

List files recursively in Linux CLI with path relative to the current directory

Not the answer you're looking for? Browse other questions tagged or ask your own question.