1

I have a hard time writing simple script using find command. I want to delete files with given size in some directory. I want to be able to specify names of files (like Efficiency_*) and size of files to delete. I tried following script:

#!/bin/bash
CD=($pwd)
find $CD -name $1 -size $2 -delete

I am running it from the correct directory as follows:

/path/to/directory/script.sh 'Efficiency_*' '-500c'

but it does not work.

What is the correct way to do it?

8
  • 1
    I think the problem is your assignment to CD. Why are you using an array there? Commented Dec 19, 2017 at 22:17
  • Where do you set the variable $pwd? Commented Dec 19, 2017 at 22:17
  • To debug shell scripts, put set -x at the beginning so you see each command as it's being executed. Commented Dec 19, 2017 at 22:17
  • Why do you even need that variable? If you want to start in the current directory, use find . instead of find $CD. Commented Dec 19, 2017 at 22:18
  • 1
    find . -name "$1"... will work just fine :) or if you insist: CD=$(pwd); find "$CD" -name "$1"... Commented Dec 19, 2017 at 22:19

2 Answers 2

5

You don't need the CD variable, just use . to refer to the current directory.

And the other variables need to be quoted. Otherwise, the shell will expand the wildcard instead of passing it to find.

#!/bin/bash
find . -name "$1" -size "$2" -delete

In general, you should always quote variables unless you have a specific reason not to.

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

4 Comments

How to run this script? How to write arguments in the command line?
Does it matter if I run it with single quotes or double qoutes? ./script.sh "Efficiency_" "-500c" or ./script.sh 'Efficiency_' '-500c' ?
@Suzie, the script itself literally can't tell what quotes were used when starting it. The interpreter that the command is typed into is responsible for breaking the arguments into an array of C strings, so by the time the script is started, it gets an argv that (in C syntax) is something like string[][]{"./script.sh", "Efficiency_", "-500c", NULL} -- thing is, it looks exactly like that no matter if it's Efficiency_ or 'Efficiency_' or "Efficiency_" that was originally entered by the user (or, say, a glob pattern like Eff* when a file named Efficiency_ exists).
@Suzie It doesn't matter. Read stackoverflow.com/questions/6697753/…
1

The problem is the value you give to the CD-variable. In Bash scripts you have two different ways to assign the output of a program call to a variable ...

# method 1
CD=`pwd`

# method 2
CD=$(pwd)

3 Comments

...though none of these is particularly good practice in the context at hand. cd=$PWD is much more efficient than running a subshell (thus, fork()ing off a whole separate copy of the shell) just to find the current directory.
And find . is usually better than find "$PWD" unless you specifically need find to output the full path.
@tripleee in this case me too, but it's in general good to know - for more complicated scripts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.