0

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?

Suzie
  • 103
  • 1
  • 4
  • 1
    I think the problem is your assignment to `CD`. Why are you using an array there? – Barmar Dec 19 '17 at 22:17
  • Where do you set the variable `$pwd`? – Barmar Dec 19 '17 at 22:17
  • To debug shell scripts, put `set -x` at the beginning so you see each command as it's being executed. – Barmar Dec 19 '17 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`. – Barmar Dec 19 '17 at 22:18
  • 1
    `find . -name "$1"...` will work just fine :) or if you insist: `CD=$(pwd); find "$CD" -name "$1"...` – PesaThe Dec 19 '17 at 22:19
  • Because my script is in different directory than the one I am running it from. However, it does not change anything, still does not work. – Suzie Dec 19 '17 at 22:20
  • @PesaThe How do I run it? How to write arguments in the command line? – Suzie Dec 19 '17 at 22:21
  • @Suzie The location of the script does not matter; the script will inherit the working directory of whatever process executes the script, and `pwd` and `.` will both refer to that same directory. (In fact, it's difficult for a script to find out its own location; see [BashFAQ #28](http://mywiki.wooledge.org/BashFAQ/028).) – Gordon Davisson Dec 19 '17 at 22:42

2 Answers2

4

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • How to run this script? How to write arguments in the command line? – Suzie Dec 19 '17 at 22:27
  • Does it matter if I run it with single quotes or double qoutes? ./script.sh "Efficiency_*" "-500c" or ./script.sh 'Efficiency_*' '-500c' ? – Suzie Dec 19 '17 at 22:31
  • 2
    @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). – Charles Duffy Dec 19 '17 at 22:34
  • @Suzie It doesn't matter. Read https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Barmar Dec 19 '17 at 22:36
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)
OkieOth
  • 3,604
  • 1
  • 19
  • 29
  • 1
    ...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. – Charles Duffy Dec 19 '17 at 22:37
  • And `find .` is usually better than `find "$PWD"` unless you specifically need `find` to output the full path. – tripleee Dec 20 '17 at 07:56
  • @tripleee in this case me too, but it's in general good to know - for more complicated scripts – OkieOth Dec 20 '17 at 08:02