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 communityThis 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
tells 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.
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.]
find
command POSIX compatible by using -exec printf '%s\0' {} +
. Don't think there's any way to make the whole thing POSIX compatible though.
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
So it works with the -maxdepth 1
before the -type f
, as: find . -maxdepth 1 -type f -print0 | xargs -L1 -p0 chmod +x
.
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'.
touch "a very long file name
and press enter. See the line continuation and simply type the closing quote and press enter again. tadaa, newline in filename. Out of laziness to retype a long filename.