• Javascript
  • Python
  • Go

Bash script to delete all but the most recent X files.

Title: Bash Script to Delete All But the Most Recent X Files If you work with a lot of files on your computer, you may find yourself in a si...

Title: Bash Script to Delete All But the Most Recent X Files

If you work with a lot of files on your computer, you may find yourself in a situation where you need to clean up old or unnecessary files to free up space. This can be a time-consuming task, especially if you have a large number of files to sort through. But fear not, there is a solution - a bash script that can do the job for you.

Bash (Bourne Again Shell) is a popular command-line interface used on Linux and Unix systems. It is a powerful tool for automating tasks, and in this case, we will use it to delete all but the most recent X files in a given directory.

First, let's define what we mean by "most recent X files." This refers to the X number of files that were most recently modified or created. For example, if X is set to 5, the script will keep the 5 most recently modified or created files and delete all others.

Now, let's get started with creating the bash script. The first step is to open a terminal window and create a new file using the touch command. We will name our file "delete_old_files.sh" for this example.

Next, we need to add a shebang line at the top of the file to tell the system which interpreter to use. In this case, we will use the bash interpreter, so our shebang line will be #!/bin/bash.

Now it's time to write the actual code for our script. We will be using a combination of the find and rm commands to achieve our goal. The find command allows us to search for files based on various criteria, such as their modification or creation date. The rm command, as the name suggests, is used to delete files.

Our script will have three main parts - finding the most recent X files, storing their names in an array, and deleting all other files.

Let's break down the script line by line:

1. #!/bin/bash

2. # Define the number of recent files to keep

3. X=5

4. # Use find command to get the most recently modified or created files and store their names in an array

5. files=( $(find . -maxdepth 1 -type f -printf '%T@ %p\n' | sort -n | tail -$X | cut -f2- -d" ") )

6. # Loop through the array and delete all files except for the most recent X

7. for file in "${files[@]}"

8. do

9. rm "$file"

10. done

Let's go through each line in detail:

Line 1: Shebang line, as discussed earlier.

Line 2: We are defining the number of recent files to keep using a variable named X. You can change this value to fit your needs.

Line 3: This is a comment to make the code more readable and explain what the X variable is used for.

Line 4: Here, we are using the find command with the following options:

. -maxdepth 1: This limits the search to the current directory and does not go into subdirectories.

-type f: This specifies that we are only interested in regular files and not directories or other file types.

-printf '%T@ %p\n': This prints the modification time of the file in Unix timestamp format, followed by the file's name and a new line character.

| sort -n: This pipes the output of the find command to the sort command, which sorts the files based on their modification time in ascending order.

| tail -$X: This pipes the output of the sort command to the tail command, which selects the last X number of lines.

| cut -f2- -d" ": This pipes the output of the tail command to the cut command, which removes the timestamp from the beginning of each line, leaving us with only the file names.

files=(...): This stores the output of the entire find command in an array named "files."

Line 5: This is a comment to explain what the code on line 4 does.

Line 6: This is the beginning of a for loop that will go through each element in the "files" array.

Line 7: The for loop is followed by a "do" statement.

Line 8: This is the actual command that will be executed in the loop. It uses the rm command to delete each file in the array.

Line 9: This is the end of the do statement.

Line 10: This is the end of the for loop.

Now that our script is complete, we need to make it executable. We can do that by running the following command in the terminal:

chmod +x delete_old_files.sh

To test our script, we can create some dummy files in a directory and run the script. For example, if we create 10 files, the script will delete all but the 5 most recently modified or created files.

In conclusion, the bash script we created allows us to easily delete all but the most recent X files in a given directory. This can save us a lot of time and effort, especially if we have a large number of files to manage. With a little bit of tweaking, this script can also be modified to fit other needs, such as deleting files based on their size or file type. So next time you need to clean up your files, consider using this bash script to make the task a breeze.

Related Articles

Speedy ls Command

The ls command is a fundamental tool for navigating and managing files in a Linux operating system. It is used to list the contents of a dir...