Yes to all linux

An example of when we need yes command

What is the usage of yes command. I follow the man yes also I try that but I want to know when we need to use this command. You can follow man page to know the usage of yes . But I am looking for a time when we need that? In which situation I need example.

2 Answers 2

«yes» was created to allow automation of tasks that request confirmation. If you have a program that wants you to tell it «y» before it does something, you can now automate that program by piping yes into it.

Here yes is piped for confirmation to delete all txt files in the directory .

Another option print a string repeatedly :

Stop by Ctrl + C

It also can be used to say no , this repeat n after the rm :

The above example says not to remove a file when rm -i prompts to remove the file.

The Wikipedia article has a good explanation.

Basically it spams a «yes» to wherever it is run. This can be used to automate responses to programs that ask for user verification. As an example, the following:

is functionally equivalent to

It pipes the «yes» to rm , so to any question that rm asks (write protection or similar), it will immediately get a «yes» and continue.

Common programs like apt or rm have a «force» function built in, but other programs may not. This is where yes can be useful.

It can also be used as input to create a certain size of file, or to stress test a system.

Источник

Команда rm

Описание команды rm

По умолчанию команда rm не удаляет директории. Чтобы удалить директорию и все ее содержимое, включая вложенные директории, нужно использовать опцию -r (рекурсивное удаление).

Синтаксис

файл(ы) — один или несколько файлов и/или директорий, записанных через пробел. Можно использовать шаблон (например, *.txt ).

Читайте также:  Downloading from github linux

Опции

  • never — никогда не выдавать запросы на подтверждение удаления.
  • once — выводить запрос один раз (аналог опции -I ).
  • always — выводить запрос всегда (аналог опции -i ).

Если значение КОГДА не задано, то используется always

Примечание

Если нужно удалить файл, название которого начинается с символа — , например, файл -myfile , то используется следующая команда:

Примеры использования команды rm

Удалить файл

Чтобы удалить файл с помощью команды rm достаточно указать название файла:

Можно удалить несколько файлов, перечислив их имена через пробел:

Принудительно удалить защищенный файл

Если файл защищен от записи, то по умолчанию будет выдан запрос на подтверждение операции удаления. Чтобы запрос не выводился, и происходило удаление защищенных файлов, используется опция -f

Удалить все файлы в текущей директории

Чтобы удалить все файлы в текущей директории можно использовать шаблонную запись звездочка *

Выдавать запрос перед удалением каждого файла

Чтобы перед удалением каждого файла выводилось подтверждение на удаление данного файла, используется опция -i

Выдать запрос на удаление один раз

При использовании опции -I выводится всего один запрос на подтверждение удаления файлов, причем, только если удаляемых файлов четыре и более. Если файлов 3, или 2, или 1, то запрос не выводится.

Удалить директорию и ее содержимое

Для удаления директорий и их содержимого используется опция -r . Удалим директорию mydir и все файлы и директории внутри нее:

Видео: Удаление корневого раздела командой RM

Источник

Saying ‘yes to all’ using rm -i

Say I wanted to make sure I’m removing the right files first, so I did something like:

just to make sure that I’m okay with the files that I am removing. So this will ask me for each file. After a few files, suppose I realize it’s exactly what I wanted to remove. Instead of CTRL+C ing and just doing rm * , is there a way I can just say Yes to all?

This question comes more so from curiosity rather than functionality.

7 Answers 7

(Unless you find a way to flip the ‘interactive’ bit with a debugger.)

Well, this doesn’t really answer your question. But instead of using rm -i , consider aliasing rm to rm -I :

The man page states:

this is actually useful!

Is there a way I can just say Yes to all?

The answer is yes, using this code:

$ yes «yes» | rm -vRI directory

  • v: show the list of files that have been removed
  • R: remove directories and their contents recursively
  • I: as per the recommendation above.
Читайте также:  Как создать виртуальный компьютер windows 10

Just check first using ls *.bla and then rm -f *.bla maybe?

If you are running in screen (a good idea in general), you can do:

This would cause screen to run the ‘yes’ command with y being the output, and direct said output to the running program (rm -i).

This can be done by replacing the application file descriptors on the fly. You’ll need an intermediate file though.

You can use gdb and a named pipe like this (assuming you are using more terminals, else you have to use screen or something else):

  • create a named pipe with «mkfifo myYesYesPipe»
  • start the interactive copy with rm -i and find its PID
  • open gdb

Then type the following commands in gdb, replacing the PID

This replaces the keyboard with a named pipe for rm.

Now you have to fill the named pipe

  • run yes > /path/to/myYesYesPipe

rm will read the pipe and overwrite everything.

Источник

Linux yes command

On Unix-like operating systems, the yes command is used to output «y«, continually, until it is aborted.

This page covers the GNU/Linux version of yes.

Description

The yes command outputs the same string, STRING, in a constant stream. If STRING is not specified, the word it repeats is «y«.

yes dates back to a time before Unix commands included the «force» (-f) option, which for many commands is the same as answering «yes» to all prompts.

yes can do something very similar: if the output of yes is piped to a command, it will effectively answer «yes» to any confirmation prompts.

Syntax

Options

—help Display a help message, and exit.
—version Display version information, and exit.

Examples

Removes all files with the extension .txt from the current directory. Here, yes outputs a constant stream of «y» characters, and that output is piped to the rm -i command, which prompts for confirmation before deleting each file. The y characters from the yes command will respond «yes» to each prompt automatically.

Here, the yes command outputs «n» in a constant stream to the rm -i command, answering «no» to all the same questions. Therefore, this command deletes no files.

Читайте также:  How long windows update

Источник

Remove file without asking

How can I remove a file without asking the user if he agrees to delete the file? I am writing shell script and use rm function, but it asks «remove regular file?» and I really don’t need this.

14 Answers 14

You might have rm aliased to rm -i so try this:

To see your aliases you can run alias .

The force flag removes all prompts;

May the force be with you — rm -f

the yes program repeatedly replies yes to any prompts. so you can pipe it into the interactive rm program to get the desired effect too.

conversely, if you want to not do something interactive, you can do

and that will repeat ‘n’ on the input stream (effectively answering no to questions)

Within a shell script, you would want to use rm -f but you also have the option of getting rid of the implicit -i option for your environment by entering unalias rm in your shell (or profile).

If you have the required permissions to delete the file and you don’t want to be prompted, do the following (-f = force):

If you don’t have permissions to the file, you will need to use:

Backslash \ bypasses aliases.

Apart of using -f parameter, alternatively you can use find command instead, e.g.

My favourite way to do this is simply use command command in bash, just the same way you use sudo . This will run your command without aliases, just like running it by /bin/rm (probably rm is aliased as rm -i ).

Currently I am working at a system, where the bash shell recieved the definition of the rm command as a function in one of the global configuration files:

Hence, none of the above answers regarding aliases did work. To counter the annoying behaviour I unset the rm function in my .bashrc file

I had a similar problem then the opener. However I did not found any answer that mentioned the possibility that rm is hidden by a shell function. So I added the answer here in the hope it would be of help for somebody facing the same type of problem.

Typing /bin/rm or rm -f all the time is inconvenient, and may have bad consequences (in the case of rm -f ).

Источник

Оцените статью