-3

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.

h0tw1r3
  • 823
  • 6
  • 10
  • The x flag is for eXtraction; you probably want the c flag for Creating an archive. You might want to read up on the -delete option for find. It's in the man page (man find). Actually, since it's only referencing the current directory, a simple rm *.tmp might be sufficient to remove files with a .tmp extension. – Chris Davies Mar 25 '16 at 17:38
  • 2
    tar isn't the only problem, it's all sorts of messed up. – h0tw1r3 Mar 25 '16 at 17:42
  • 1
    yep, "all sorts of messed up", starting with the fact that $1 is used without double-quotes, and then not used at all in the find or tar commands. One find 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 using tar x to extract an archive rather than tar c to create one. The echo command is the only one you've used correctly. – cas Mar 26 '16 at 00:01

2 Answers2

1
  1. If this is homework, you should say so.
  2. Your script only searches the current directory regardless of what directory name you give it.
  3. Scripted rm -f in a find command is a really bad idea unless you really know what you're doing.
  4. \; at the end of the tar command will cause an error.
  5. Based on #4, you seem to be confused about the role of ; in the find command. It's passed as an argument to tell find where the end of the passed in command is.
  6. Your tar command is attempting to extract from an archive, not create one.
  7. Your variables aren't quoted.
  8. /~/ will not be turned by the shell into your home directory name; that only occurs if ~ is at the start of the word.
  9. If you do it right, all three of your find commands can be consolidated into just one command.

I'm sure there's more; that's just off the top of my head. Poke around here on this site and you can learn an awful lot.

We're happy to help, but this is not actually a "homework help" site. You will probably get more help if you demonstrate that you really want to learn, rather than just wanting to get through your homework.

Wildcard
  • 36,499
0

Specifically answering the question about how to tar a directory - because there are several other problems with your script:

tar -cvzf tarfile.tgz "$1"

will create a gzipped tar called tarfile.tar, and put the directory from the script parameter in it.