I am looking for something like:
ls | ask_yes_no_for_each_file | chmod +x the_files_approved
Or similar syntax.
Also could work on other commands that you want individual confirmation for.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
This does what you are looking for:
find . -maxdepth 1 -type f -print0 | xargs -L1 -p0 chmod +x
find rather than
ls because, generally, parsing
ls output is unreliable. This form, using
find, however, will work with filenames even if they contain newlines or other difficult characters.
find . -maxdepth 1 -type f -print0
This selects the files. This can be customized using any of find's many options. The option
find to print the file names in a null-separated list. This is the only reliable to transmit lists of file names.
xargs -L1 -p0 chmod +x
This takes the null-separated list of file names generated by
find and applies your command to them.
-L1 option tells
xargs to work on only one file name at a time. The
-p option tells
xargs to prompt for approval before continuing. The
-0 option tells
xargs to use the null character as the delimiter between file names.
[I was unaware of the
-p option to
xargs until @kwan pointed it out.]
You can use
ls|xargs -I path -p chmod +x path
-p: Prompt the user about whether to run each command line and read a line from the terminal. Only run the command line if the response starts with 'y' or 'Y'.