Joel Broström
Joel Broström

Reputation: 4040

Enter user text in .bashrc

Im trying to make a simple command to remove all whatever.whatever~ files, that is all hidden junk files, but I the Terminal keeps asking me for yes or no and I don't know how to hard code yes into the script.

This is what I have going on so far:

alias cleandir='
for i in ./*~
do
 rm "$i" 
 yes

done'

This results in the terminal asking me to enter yes or no for each object to be deleted, and then write an infinite amount of 'y'.

for example:

joel test$ cleanup
rm: remove regular file ‘./~main~.cpp~’?

I write yes or no, then:

y
y
y
y
etc...

How do I make my "yes" to actually be prompted in the terminal as if it was the user writing it?

First time writing. Thanks in advance!

Upvotes: 2

Views: 467

Answers (2)

fedorqui
fedorqui

Reputation: 289495

Why don't you just use find for this?

alias cleandir="find /some/path -type f -name '*~' -delete"

This what I use to remove all the .pyc files from Python and works very well (obviously, with -name '*.pyc' instead).

Upvotes: 4

Brian Agnew
Brian Agnew

Reputation: 272217

To answer your question about feeding input into your 'rm' process (more correctly, any process), you can either pipe data from a process, or redirect from a file e.g.

$ yes | rm

pipes the stdout of 'yes' into the stdin of 'rm'. That's useful for dynamic output from a process.

Or you can redirect from a file e.g.

$ rm < 'myYesFile.txt'

which takes the contents of myYesFile.txt and redirects that to the stdin of 'rm'. That's useful for a static set of response/input data. Note that you don't need a separate file, and you could use a heredoc e.g.

$ rm <<EOF
y
y
y
EOF

and declare your inputs explicitly in the shell.

Upvotes: 1

Related Questions