1

I want to write a script that allows the user to enter a specific file path to use this variable with the find command

I tried this but it seems that my syntax is not right

enter file path : $PATH
find $PATH -size +1M  -exec rm {}\;

and what if I want the user also to enter the size should I use find $userpath -size $size -exec rm {}\; or something else

  • Two key points: don't use `PATH` variable because it is a key variable of your environment (ex. use `DIR`), and don't use `-exec rm {}` because it may lead you to blindly delete useful files (ex. use `-ok rm {}`). – dan May 18 '20 at 16:01
  • and what if I want the user also to enter the size should I use find $userpath -size $size -exec rm {}\; or something else – karam albousy May 18 '20 at 16:29

3 Answers3

1

You'll need to use read.

echo Enter file path:
read userpath
find $userpath -size +1M  -exec rm {}\;

Also, for safety you might want to use rm -i.

Panki
  • 6,221
  • 2
  • 24
  • 33
  • and what if I want the user also to enter the size should I use find $userpath -size $size -exec rm {}\; or something else – karam albousy May 18 '20 at 16:16
0

Hmm, I seem to have misread your question. So if you want to read input you can use the Bash builtin read (see output of help read).

You can use read with a custom prompt -p and its output automatically goes into a (shell!) variable named REPLY, unless you give a different name.

read -p "enter file path : "
find "$REPLY" -size +1M  -exec rm {} +

NB: I also replaced the \; by a + which should result in less invocations by find, but may be less portable.


The variable PATH has a special meaning and comes prepopulated. You don't say which shell this is, but the ground rules are the same in all of those I am aware of. The PATH environment variable is used to locate folders which contain executable files (binaries, scripts ...). These are then executable without giving their full or relative path.

Please check out this piece of documentation to learn more. Quote:

$PATH Path to binaries, usually /usr/bin/, /usr/X11R6/bin/, /usr/local/bin, etc.

When given a command, the shell automatically does a hash table search on the directories listed in the path for the executable. The path is stored in the environmental variable, $PATH, a list of directories, separated by colons. Normally, the system stores the $PATH definition in /etc/profile and/or ~/.bashrc (see Appendix H).

bash$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin

PATH=${PATH}:/opt/bin appends the /opt/bin directory to the current path. In a script, it may be expedient to temporarily add a directory to the path in this way. When the script exits, this restores the original $PATH (a child process, such as a script, may not change the environment of the parent process, the shell).

Note The current "working directory", ./, is usually omitted from the $PATH as a security measure.

Use echo "$PATH" to see the current contents of this variable in your running shell.

0xC0000022L
  • 16,189
  • 24
  • 102
  • 168
0

You should use something like this:

read -i "Enter path" PATH
find $PATH -size +1M -exec rm {}\;

But, PATH variable would override your binary search path. Also rm command could be deleted quiet early, unable to delete everything.

I suggest following code:

read -i "Enter path" DIR
/bin/find $DIR -size +1M -delete -print

This command will potentially destroy your working environment/OS so use at your own risk!