0

I m trying to write a script that cleans up the specified directory by archiving and compressing (.tar.gz) all files older than the specified time.

And also, The archive must be accompanied by a script that allows you to roll back the changes made (i.e. unpack the archive and put the files back into directories).

#!/bin/bash

if [[ -n $1 && -n $2 ]]

then

dir=$1

daysold=$2

name=$(date '+%Y-%m-%d')'.tar'

cd $1

find -type f -mtime $daysold ! -name '*.tar.gz' -a  -exec tar -uvf $name --remove-files {} \;

echo «Arcive was created successfully!»

gzip $name

echo «Compression complited successfully»

echo -e "#!/bin/bash\ntar -xvf $name.gz">$name+'_unzip.sh'

ug +x $name+'_unzip.sh'

echo "Script for unzip archive was created"

else

echo "Please, use the following pattern: archive_script.sh [~/path/to/directory] [N - files older than N days]"

fi

this is what I've got, but it doesn't work

./archievesolution.sh [/Users/glebpavlychev/Documents/ViberDownloads ] [10]
./archievesolution.sh: line 7: cd:  [/Users/glebpavlychev/Documents/ViberDownloads: No such file or directory
find: illegal option -- t
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
«Archive was created successfully!»
gzip: can't stat: 2020-09-24.tar (2020-09-24.tar): No such file or directory
«Compression complited successfully»
./archievesolution.sh: line 13: ug: command not found
Script for unzip archive was created
(base) Glebs-MacBook-Pro:ViberDownloads glebpavlychev$
Valf
  • 1
  • 2
  • 1
    What do you mean, by "it doesn't work" ? What is the output of your current code ? Does it sens you an error message ? – MarleneHE Sep 24 '20 at 11:22
  • 1
    Please give a specific problem rather than “It doesn’t work”. (In other words, make an effort for the people you’re hoping will help you) – donkopotamus Sep 24 '20 at 11:22
  • ./archievesolution.sh: line 9: cd: [/Users/glebpavlychev/Documents/ViberDownloads: No such file or directory find: illegal option -- t usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression] find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression] «Arcive was created successfully!» gzip: can't stat: 2020-09-24.tar (2020-09-24.tar): No such file or directory «Compression complited successfully» ./archievesolution.sh: line 15: ug: command not found Script for unzip archive was created – Valf Sep 24 '20 at 11:31
  • I agree that by saying it doesn't work I don't give enough info. Pardon me. But to be honest, since I am just starting to learn coding, I don't understand much of it as of today. – Valf Sep 24 '20 at 11:35
  • 1
    Please reformat your post and put code with 4 leading spaces to format it in a code block. Please check your script with http://shellcheck.net Please do not post code in comments, they are real bad for it and can't format correctly. Edit your post with that edit button and post additional info in the question. – KamilCuk Sep 24 '20 at 11:36
  • What's with the `[`s you're putting in your command when you run your script? The error messages are _correct_ that no such directory exists, because `[/Users` doesn't exist (though `/Users` does). – Charles Duffy Sep 24 '20 at 11:50
  • Beyond that, there are a bunch of problems here we have other, asked-and-answered questions for, many of which http://shellcheck.net/ will identify for you automatically. Ideally, each question should be about _just one_ problem with your code, with only the shortest code that lets that individual problem be addressed. – Charles Duffy Sep 24 '20 at 11:52
  • Using `set -x` to trace your script's execution will show you how the shell is _actually interpreting_ each command, so you can compare the command that's actually being run to the command you _wanted_ to run; that comparison will do a lot of good to help you figure out why each individual error happens -- and when what `set -x` shows you _is_ being run differs from what you _expect_ to be run, each such specific difference is something you can either look for an existing question about, or ask a new one if not already in our knowledge base. – Charles Duffy Sep 24 '20 at 11:54
  • (and wrt. the `ug +x` line... I'm not sure what the `ug` command you're running is; perhaps you meant `chmod +x`?) – Charles Duffy Sep 24 '20 at 11:56

0 Answers0