Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
 file * |  grep 'ASCII text' | chmod -x 
chmod: missing operand
Try `chmod --help' for more information.

The above command gives me error.Basically I am trying to find all the files whose type is ASCII and change their permissions to -x.What mistake is there in above syntax?

share|improve this question
add comment

4 Answers 4

up vote 3 down vote accepted

One: grep 'ASCII text' returns not only the file name, but also the type of the file itself; you need to process the output to return only the file name

Two: chmod does not accept arguments from STDIN, which is what you're trying to do with the pipe |. You'll have to either use xargs or wrap the above in a for loop

That said, here are two solutions for you:

Solution #1: With Pipes

file * | awk '/ASCII text/ {gsub(/:/,"",$1); print $1}' | xargs chmod -x

Solution #2: With for-loop

for fn in $(file * | awk '/ASCII text/ {gsub(/:/,"",$1); print $1}'); do chmod -x "$fn"; done

Pick yer poison :-)

share|improve this answer
add comment

This should work regardless of whether filenames contain spaces or colons:

find -maxdepth 1 -type f -exec sh -c 'file -b "{}" | grep -sq ASCII' \; -print0 | xargs -0 chmod -x

You can remove the -maxdepth 1 to make it recursive.

There may be false positives if filenames themselves contain the string "ASCII".

Edit:

I incorporated pepoluan's suggestion of using the -b option for file so the filename isn't output for the test by grep. This should eliminate false positives.

share|improve this answer
    
Then the grep should match with [[:space:]]ASCII ... for more details: stackoverflow.com/questions/448997/egrep-search-for-whitespace –  pepoluan Mar 8 '11 at 11:27
    
@pepoluan: If you're talking about the part where I mention potential false positives, then it is correct that the more specific the regex is the less chance there is, but a filename could still match [[:space:]]ASCII. –  Dennis Williamson Mar 8 '11 at 11:31
    
@Dennis: Ah, you're right. In that case, I suggest using awk -F ":" '/ASCII/ && $1!~/ASCII/ {print $1} –  pepoluan Mar 8 '11 at 11:40
    
@pepoluan: But what if a file with "ASCII" in its name is an ASCII file? You don't want to falsely reject a file, either. Or if there's a file named "ASCII:ASCII" ... ad infinitum. You can reduce the chances, but you can't eliminate them. –  Dennis Williamson Mar 8 '11 at 11:44
    
Heh, possible. Maybe better to have for fn in *; do [[ $(file -b "$fn") =~ ASCII ]] && chmod -x "$fn"; done. What do you think? –  pepoluan Mar 8 '11 at 11:49
show 2 more comments
for f in `file * | grep "ASCII text" | awk "{print \\$1}" | awk -F ":" "{print \\$1}"`; do chmod -x "$f"; done
share|improve this answer
add comment

And another one liner - be aware of the need to strip the : ASCII Text and quote the names too

file * | grep 'ASCII text' | sed  's|\(^.*\):.*|\"\1\"|'| xargs chmod -x
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.