Useful Tip I Just Learned

Ever have a command you run in Linux that ends up asking you the same question over and over again? Isn’t it annoying how you have to type the same thing in over and over again. I’m specifically referring to if you have a folder with a bunch of files that you’re trying to remove. So of course you say:

rm -r <direcotory>

Or maybe:

cd <direcotory>
rm *

Either way you do it, you might end up with an “Are you sure” for ever file. I know it happens sometimes for me when I’m deleting files as root that are owned by another user – which I don’t do very often, but do do every once in a while. Pragmatists will tell you to use the -f (force) parameter

rm -rf <directory>

Or

cd <directory>
rm -f *

But the purists will tell you that that’s bad bad bad. Or maybe you’re a pragmatist and whatever command you’re using doesn’t have a force parameter. Then let me introduce you to “yes”. That’s right, folks. Try it out. Open terminal and type:

yes

Magic! The screen repeated echos y on a new line until you press CTRL+C. Moreover, if you pass it a parameter, it will echo the parameter you pass until you press CTRL+C.

yes n

Print ‘n’ instead of ‘y’. So for our above scenario, let’s say we want to remove a bunch of files and say yes to all the confirmation prompts, but don’t want to use -f and don’t want to type y over and over again. Our solution is simple:

yes | rm -r <directory>

Or

cd <directory>
yes | rm *

Neat trick! I’m sure it has other uses too, and I’m sure a quick perusal of “man yes” will reveal other ways to customize it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s