0
#!/bin/bash echo Enter a Directory Name: read if [ -d "$1" ]; then find "$1" -type f -size 0 -delete find "$1" *.tmp -type f -delete find "$1" *.swp -type f -delete tar -cvzf mytarfile.tgz "$1" else echo "This is not a directory" fi 

The script runs when I put in the directory myself using ./program.sh Desktop. If I have an empty txt, .tmp and .swp file on my desktop it removes them and makes a tar file. How can I enter a directory at the (read) line for example Documents or MyMusic. It allows me to type in something but then goes right to the else and prints "This is not a directory.

1
  • 1
    The second and third find commands do not do what you seem to want; they delete all files (of any type) in the specified directory, and also the .tmp and .swp files in the current directory (not the specified one). I'm pretty sure you want e.g. find "$1" -name '*.swp' -type f -delete. Commented Nov 18, 2018 at 18:34

2 Answers 2

2

Replace all $1 with $REPLY.


Take a look at read's syntax: help read

Sign up to request clarification or add additional context in comments.

1 Comment

Ok I will. Thank you.
1

You can use a variable to read the Directory name,like:

echo "Enter a Directory Name:" read Dir_Name 

or better:

read -p "Enter Directory Name:" Dir_Name 

and then replace all $1 with ${Dir_Name}

$1 is a special variable in Unix. Read it here: special variables

1 Comment

${Dir_Name} could be safely replaced with $Dir_Name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.