In the Linux ecosystem, file management is key to maintaining a well-organized and responsive system. One general task is finding files according to their size and then archiving or compressing them to optimize storage space. In this post, I will explain the steps to find files according to size and seamlessly archive them through shell script and commands as well as enhancing overall system performance.

Recognizing the Importance of Archiving Files Based on Size

Storage Optimization :

Over time, files stack up, making it difficult to manage the system’s important storage space. Archiving these less-used, larger files is like giving your system a little cleaning, making sure it stays organized and runs smoothly.

Faster Performance:

If you have fewer really big files in your main folders, your computer can work faster. This is especially helpful when you’re running out of space on your hard drive or when you don’t need to use those big files every day. It’s like making your computer run more efficiently!

Here is a bash script for finding all files whose size is higher than the given size in the parameter and archiving it. Below line of code you can put into your script file. Following the find command in the script will search files, not directories that are larger than one character.

Let me explain the shell script below. The first line of the shell script runs the find command in the current path because we use ./, you can use your searching path instead of ./, where you have to search the file. size option we are using for searching for more than one character. and files variable stores all searching output. techtrname stores all files, which fetch from the find command. after that tar command archives the fetch file and creates the file.

More options for finding the command you can use. Click the Here to Read More option in the Find command.

files=$(find ./ -type f -size +1c)
#fix names in case there are spaces. Insert \ to escape spaces
techtrname="${files// /\\ }"
tar -czf techtransit.tar.gz $techtrname

Finding Large Files: To find files larger than a specified size, use:

find /path/to/search -type f -size +100M

The above example command locates files larger than 100 megabytes.

Finding Small Files: To find files smaller than a specified size, use:

 find /path/to/search -type f -size -10M

This example locates files smaller than 10 megabytes

Archiving Files: The tar the command is commonly used for creating archives in Linux.

 tar -czvf archivename.tar.gz /path-to-large-file 

Adjust the archive name and path accordingly.

As you explore the Linux file system, think about adding these commands to your regular tasks. It helps in keeping your system organized and responsive.

By Sachin G

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009.Linux rocks! Sachin G Follow me on LinkedIn and x formerly twitter