LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-05-2024, 06:43 AM   #1
darek
LQ Newbie
 
Registered: Jan 2024
Posts: 2

Rep: Reputation: 0
Problem with script to make copies of directories/files


Hello,
I have a script that makes copies of specific folders. The script is executed once a day creating a folder with the current date (dd.mm.yyyy). The next day the situation is similar, but of course a folder with the next date is created. On the third day, the oldest directory is deleted and the next one is created.
Everything works well during the month. When there is a change of month, the script often ignores the older directories and does not delete them. Is it possible to correct this, where is the error? Here is the script:

#!/bin/bash
katalog1="/home/a/"
katalog2="/home/b/"
katalog3="/home/c/"
katalog4="/home/d/"
katalog5="/home/e/"
katalog6="/home/f/"
katalog7="/home/g/"
katalog8="/home/h/"

sciezka="/home/elicki"
current_date=$(date +"%d.%m.%Y")
backup_dir="/home/alamakota/$current_date"
data_minus_jeden=$(date -d "1 days ago" +"%d.%m.%Y")

for katalog in "$sciezka"/*; do
if [ -d "$katalog" ]; then
nazwa_katalogu=$(basename "$katalog")

if [[ $nazwa_katalogu < $data_minus_jeden ]]; then
rm -r "$katalog"
fi
fi
done
sleep 420
mkdir -p "$backup_dir"
cp $katalog1 $katalog2 $katalog3 $katalog5 $katalog6 $katalog7 $katalog8 -r "$backup_dir"/
chmod -R 755 $sciezka

thank you
 
Old 01-05-2024, 07:03 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Can you please edit the above to wrap the script in [code] [/code] tags? That would preserve the indentation and make it easier to interpret.

The comparison is comparing the dd.mm.yyyy and not a more parsable yyyy.mm.dd or the epoch.
 
Old 01-05-2024, 07:20 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,760

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
To elaborate your if statement is using string comparison and not numbers which is why it fails at a new month. By changing the order of year in the file name string comparison will now be correct. The other method would be to convert the file name to its epoch value which is the number of seconds and then compare the numbers.
 
Old 01-05-2024, 08:40 AM   #4
darek
LQ Newbie
 
Registered: Jan 2024
Posts: 2

Original Poster
Rep: Reputation: 0
thank you for the explanation
 
Old 01-06-2024, 07:33 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,811

Rep: Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210
You can do the alphabetic comparison if your filenames are in the big-medium-small order: "%Y-%m-%d"
GNU date has even a shortcut for it: date "+%F"

You create backup directories in /home/alamakota/
but you delete old ones in /home/elicki/ ??

Perhaps you can use an array:

Code:
Akatalog=(
  "/home/a/"
  "/home/b/"
  "/home/c/"
#  "/home/d/"
  "/home/e/"
  "/home/f/"
  "/home/g/"
  "/home/h/"
)
...
cp -r "${Akatalog[@]}" "$backup_dir"/

Last edited by MadeInGermany; 01-06-2024 at 07:37 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Docker copies the files into the destination but deletes existing files sparkling CentOS 5 09-05-2018 02:43 PM
Need to create a shell script that copies all files owned by a user BrainOfSweden Linux - Newbie 9 09-28-2011 07:22 AM
trying to make a Perl script that automatically copies files from a CD/DVD upon load dave247 Programming 5 08-09-2010 11:36 AM
Script that copies files/folders doesn't work Archosz Linux - Server 1 12-04-2009 09:43 PM
make duplicate copies of files Paul Griesbaum Linux - Newbie 7 06-08-2009 10:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration