Permission denied when using -exec {} ls with find command

execfindls

When run following command it gives me permission denied message for all the files.

find /data/code/ -name "*.jar" -exec {} ls \;


find: `/data/code/project/shared/build/thirdparty/log4j-1.2.8/commons-logging-1.0.4.jar': Permission denied

But if I do

ls  /data/code/project/shared/build/thirdparty/log4j-1.2.8/commons-logging-1.0.4.jar

It prints gives the proper output without any permission denied message.

/data/code/project/shared/build/thirdparty/log4j-1.2.8/commons-logging-1.0.4.jar

What am I doing wrong?

p.s. : I need to list and remove all the jar files in /data/code

Best Answer

While doing:

find /data/code/ -name "*.jar" -exec {} ls \;

you are trying the execute the file found (e.g. /data/code/project/shared/build/thirdparty/log4j-1.2.8/commons-logging-1.0.4.jar) with ls as an argument to it, leading to the permission denied error.

Just switch the order:

find /data/code/ -name "*.jar" -exec ls {} \;

GNU find has -ls option too, so in GNU find, you can just do:

find /data/code/ -name "*.jar" -ls
Related Question