Is there a one line command to make chmod interactive?
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.
This does what you are looking for:
find . -maxdepth 1 -type f -print0 | xargs -L1 -p0 chmod +x
This uses 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.
Explanation
-
find . -maxdepth 1 -type f -print0
This selects the files. This can be customized using any of find's many options. The option
print0
tellsfind
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.
The -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 xargs
.
eg:
ls|xargs -I path -p chmod +x path
Option -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'.