I’ve a SFTP service using ubuntu server now it have many files and take too many space like:
-rwxrwxrwx 1 systemd-network systemd-journal 7190 Jul 1 2020 'document_1.xlsx'
-rw-r--r-- 1 systemd-network systemd-journal 1606 Jul 1 2021 'document_1.csv'
-rw-r--r-- 1 systemd-network systemd-journal 7191 Jul 1 2021 'document_2.xlsx'
-rw-r--r-- 1 systemd-network systemd-journal 1606 Jul 1 03:10 'document_2.csv'
-rw-r--r-- 1 systemd-network systemd-journal 7191 Jul 1 03:10 'document_3.xlsx'
-rwxrwxrwx 1 systemd-network systemd-journal 1606 Aug 1 2020 'document_3.csv'
-rwxrwxrwx 1 systemd-network systemd-journal 7190 Aug 1 2020 'document_4.xlsx'
-rw-r--r-- 1 systemd-network systemd-journal 1606 Aug 1 2021 'document_4.csv'
Now I want optimal space of server but I can’t delete those.
If better can we compress by filetype and/or month/year and/or modification/creation and/or delete.
Example:
document_2021.gzip
document_2021_csv.gzip
2
Answers
You gave multiple options for a solution, so here’s a simple one for the first option – To compress files by filetype.
Of course, please test any commands on sample files before running on your important ones. Also, always have a backup before doing anything like this. But you already knew that.
You can compress each file by type using the following command from the directory you want to work on:
Depending on how your shell is set up, if you need to end the command with ; or simply ;
The result will compress your files in place, like document_1.xlsx.gz, document_2.xlsx.gz, etc.
Yes, you can string multiple -exec commands after the find by separating with semicolons.
In this example, I first find all files with .csv extension. Then, get their size with -du (disk usage), followed by a long listing.
You mentioned possibly using rm like this, and I would highly discourage using rm in a string of -exec commands.
Only because you wouldn’t want the rm to happen if the previous command failed. So that would be risky.
I rarely do multiple -exec commands. For stringing many commands together, I’d more likely do that in Python. But if doing just a few commands, I’d just run a separate command for each one I wanted. That allows a chance to inspect the result of one command before running the next.
What would be the specific commands you are wanting to run together?