Journey of a noob

Learn From Noobs

‘find -ok’ and ‘find -okdir’ by example

Posted at # Unix/Linux

‘find -ok’ and ‘find -okdir’ by example

Introduction

In this article, we will demonstrate the commands find -ok and find -okdir. These commands do the same as find -exec and find -execdir, but they will ask you to confirm before they execute the commands on each file matched. They can be useful if you want to be extra safe.

We have already discussed the difference between find -exec and find -execdir in this article. We are not going to explain their difference again here. If you are interested, feel free to check that article out.

Let’s check out how they differ from each other.

  1. Create the following structure.
noob@learnfromnoobs:~$ tree find-test/
find-test/
├── dir1
│   └── myfile1
└── dir2
    └── myfile2

2 directories, 2 files

You can use these commands to create the structure above.

$ mkdir find-test
$ mkdir find-test/dir1
$ mkdir find-test/dir2
$ touch find-test/dir1/myfile1
$ touch find-test/dir2/myfile2

‘find -ok’ vs ‘find -exec’

  1. Run find -exec under find-test.
noob@learnfromnoobs:~/find-test$ find . -name 'myfile*' -exec ls -l \;
total 8
drwxrwxr-x 2 noob noob 4096 Mar  7 03:31 dir1
drwxrwxr-x 2 noob noob 4096 Mar  7 03:31 dir2
total 8
drwxrwxr-x 2 noob noob 4096 Mar  7 03:31 dir1
drwxrwxr-x 2 noob noob 4096 Mar  7 03:31 dir2
  1. Run find -ok under find-test.
noob@learnfromnoobs:~/find-test$ find . -name 'myfile*' -ok ls -l \;
< ls ... ./dir1/myfile1 > ? yes
total 8
drwxrwxr-x 2 noob noob 4096 Mar  7 03:31 dir1
drwxrwxr-x 2 noob noob 4096 Mar  7 03:31 dir2
< ls ... ./dir2/myfile2 > ? no

As we can see from the result, it shows us the command we are going to execute (ls in this case) and the files matched. The command will be executed only if we confirm that we want to run the command.

‘find -okdir’ vs ‘find -execdir’

  1. Run find -execdir under find-test.
noob@learnfromnoobs:~/find-test$ find . -name 'myfile*' -execdir ls -l \;
total 0
-rw-rw-r-- 1 noob noob 0 Jan 27 09:55 myfile1
total 0
-rw-rw-r-- 1 noob noob 0 Jan 27 09:55 myfile2
  1. Run find -okdir under find-test.
noob@learnfromnoobs:~/find-test$ find . -name 'myfile*' -okdir ls -l \;
< ls ... ./dir1/myfile1 > ? yes
total 0
-rw-rw-r-- 1 noob noob 0 Mar  7 03:31 myfile1
< ls ... ./dir2/myfile2 > ? no

It also prompts the user to confirm whether we want to run the command like find -ok, but it does the same like find -execdir once it is confirmed, i.e. executes the commands in the directories containing the matched target.

Conclusion

In summary, find -ok and find -okdir do the same as find -exec and find -execdir, but they will prompt you for confirmation before executing the commands on each file matched. You might find the use of find -ok and find -okdir unnecessary in the above examples. However, imagine you are using rm to remove the files matched. This can ensure that you are doing what you really want to do.

I hope you enjoyed this article and learned something new.

Keep learning and have fun!