Yes to all for recursive commands in linux

Today I ran into a strange problem:

On centos cp command is “-interactive” by default:

cp  -i, –interactive  –   prompt before overwrite

I had to copy a large folder with lots of subfolders onto another folder, overwriting all duplicate content.

Normally you do that with:

$ cp -rf folder1/* folder2/

forcing with -f to overwrite, and making it recursive with -r

The problem!

The problem was that on centos cp is -interactive by default, I had to press y -> enter a thousand times, which is not very pleasant.

The solution?  the yes command! yes, it’s true, there is a yes command, beautiful.

Usage: yes [STRING]...
 or: yes OPTION
Repeatedly output a line with all specified STRING(s), or `y'.
    --help display this help and exit
    --version output version information and exit

All you need to do is prepend command yes “yes” to your script or command. Of course is useful for recursive deleting files or other actions you may need

$ yes “yes” | your_command

$ yes “yes” | script

In my case, the solution was:

$ yes “yes” | cp -rf folder1/* folder2/

 

And it worked like a charm.

 

 

Tags: , , ,

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


%d bloggers like this: