You are not logged in.
.
Last edited by Arciere (2025-04-30 08:35:59)
Offline
Sure it's possible. But you'd need to more clearly articulate your data model. Provide examples of the file names, and date formats, and how you'd like to determine a file is redundant.
See, for example: https://hg.sr.ht/~jasonwryan/shiv/brows … an?rev=tip
Offline
I would like to append the dates to these backups that I'm going to do (thus keeping more than one save) and then automatically delete all previous backups except the last n in chronological order.
Is it possible to do this from the terminal, perhaps through an option of rm? Or maybe through a script? Or maybe via an rsync function?
rsync can do differential backups, eg.:
rsync -a --delete --quiet --inplace --backup --backup-dir=/location/to/backup/incr/$DAY /path/to/backup/ /location/to/backup/full/
Last edited by sabroad (2021-06-04 08:27:56)
--
saint_abroad
Offline
.
Last edited by Arciere (2025-04-30 08:35:47)
Offline
.
Last edited by Arciere (2025-04-30 08:35:39)
Offline
.
Last edited by Arciere (2025-04-30 08:35:32)
Offline
I built the following formatting to attribute to the files for my backups:
filename\($(date +%d-%m-%Y_%H:%M:%S)\)
The result is the following:
filename(31-12-2020_17:46:59)
Could this be fine or should I format them in US format, for example?
You'd better prefix the filenames by a date in a naturally sortable format, like this:
$(date +%Y-%m-%d_%H:%M:%S)-filename
Then you work with an array:
files=(/backup/dir/*)
n_files=${#files[@]}
for ((n = 0; n < n_files - n_to_keep; n++)); do
rm "${files[-1-n]}"
done
where n_to_keep is the number of files you want to keep, to define (not tested: prefix rm with echo before trying this ).
Xfce maintainer: https://gravatar.com/gaelbdx1
Offline
.
Last edited by Arciere (2025-04-30 08:35:23)
Offline
Yes, that's it, that's how we usually do it.
For your questions about arrays, see man bash
The line
files=(/backup/dir/*)
is a way to set up an array.
Xfce maintainer: https://gravatar.com/gaelbdx1
Offline
.
Last edited by Arciere (2025-04-30 08:35:13)
Offline
The language is bash ^^
"most practical" I don't know: this is a (bash-builtin) way to do it when files are named correctly.
Xfce maintainer: https://gravatar.com/gaelbdx1
Offline
If you'd be happy deleting files older than a specific date or number of days old (rather than keeping 'n' files) just use find:
find $path_to_backups -mtime +$days
When you have replaced the path and days variables, understand what the command does, and are satisfied with the resulting list of deletion candidates, just run the command again appending the flag '-delete'.
I'd also suggest if you are going to continue using arch linux that you invest some time in learning about your system. I'd strongly advise working through the "linux tutorial" and "bash scripting" guides here: https://ryanstutorials.net/
Last edited by Trilby (2021-06-04 13:12:14)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
.
Last edited by Arciere (2025-04-30 08:35:04)
Offline
It's brittle to use ls: you don't control the output format, it's not made for that.
If you want to use an external command, use find instead:
find /backupFolder -print0 | sort -z | tail -z -n+11 | xargs -0 rm -rfv
Last edited by lambdarch (2021-06-04 17:36:21)
Xfce maintainer: https://gravatar.com/gaelbdx1
Offline
.
Last edited by Arciere (2025-04-30 08:34:55)
Offline
The filenames can contain spaces, special characters and so on: that's why ls is a bad idea (this is a little mentioned in the link you quote by the way).
In fact there is no need for find here:
printf '%s\0' /backupFolder/* | tail -z -n+11 | xargs -0 rm -rfv
EDIT: I didn't see the -d in your ls: in this case, it's probably better to use find as above, adding -type d, and maybe -maxdepth 1.
To summarize, this would give:
find /backupFolder -maxdepth 1 -type d -print0 | sort -z | tail -z -n+11 | xargs -0 rm -rfv
Last edited by lambdarch (2021-06-04 18:05:18)
Xfce maintainer: https://gravatar.com/gaelbdx1
Offline
.
Last edited by Arciere (2025-04-30 08:34:47)
Offline
.
Last edited by Arciere (2025-04-30 08:34:37)
Offline
Well, it depends on what you really want to do in the end: list directories or not, search depth, etc.
In any case, it is useless to concatenate commands like that: if you use find, filter as much as you can with its options, then sort the result (if needed and only once), select some lines (if needed and only once), and finally act on these lines.
As you describe your need in #18, it seems to me that find is unnecessary:
printf '%s\0' /backupFolder/* | tail -z -n+11 | xargs -0 rm -rfv
should do the job.
You can replace rm -rfv with printf '%s\n' to see what it gives.
On a small example:
$ ls -l ./
total 0
-rw-r--r-- 1 user user 0 6 juin 12:35 20210601-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210602-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210603-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210604-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210605-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210606-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210607-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210608-filemane
-rw-r--r-- 1 user user 0 6 juin 12:35 20210609-filemane
$ printf '%s\0' ./* | tail -z -n+5 | xargs -0 printf '%s\n'
./20210605-filemane
./20210606-filemane
./20210607-filemane
./20210608-filemane
./20210609-filemane
$
Xfce maintainer: https://gravatar.com/gaelbdx1
Offline
.
Last edited by Arciere (2025-04-30 08:34:26)
Offline
But as for the -z option next to both sort and tail I'm not finding anything: what are they for?
How are you not finding anything? You're clearly not looking. It is very clearly covered in the man pages for each of these tools.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
.
Last edited by Arciere (2025-04-30 08:34:16)
Offline
.
Last edited by Arciere (2025-04-30 08:34:09)
Offline
If the input you are `sort'ing or 'tail'ing is delineated by NULL rather than newline, then yes, that's precisely what that flag is for. Otherwise `sort` will just receive one line of input, so there is nothing to sort and it will output exactly what it was given as input completely unchanged.
Of course, you could quite easily test this yourself rather than asking.
You are expected to read man pages, and make an effort to learn on your own.
Do not take on "help vampire" habits.
Last edited by Trilby (2021-06-06 13:59:15)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline