Write a Bash Script that takes a directory name as a command line argument and clean the directory by removing all the files with a zero length, with a .tmp or with a .swp extension that is found in this directory. After, create an archive of this directory, compress the archive and place it in your ~directory?
#!/bin/bash
if [ -d $1 ]
then
find -empty -print -exec rm -f {} \;
find . -name "*.tmp" -print -exec rm -f {} \;
find . -name "*.swp" -print -exec rm -f {} \;
tar -zxvf $filename.tar.gz /~/ \;
else
echo "this file is not a directory"
fi
Im stuck on the archive part, can you guys give me some hints! thanks and have nice day.
x
flag is for eXtraction; you probably want thec
flag for Creating an archive. You might want to read up on the-delete
option forfind
. It's in the man page (man find
). Actually, since it's only referencing the current directory, a simplerm *.tmp
might be sufficient to remove files with a.tmp
extension. – Chris Davies Mar 25 '16 at 17:38tar
isn't the only problem, it's all sorts of messed up. – h0tw1r3 Mar 25 '16 at 17:42$1
is used without double-quotes, and then not used at all in thefind
ortar
commands. Onefind
command doesn't even specify a path, the others use.
(the current directory, i.e. where the script was run).$filename
isn't double-quoted and doesn't seem to be defined anywhere, and WTF is/~/ \;
supposed to mean? You're also usingtar x
to extract an archive rather thantar c
to create one. Theecho
command is the only one you've used correctly. – cas Mar 26 '16 at 00:01