{"componentChunkName":"component---src-templates-blog-js","path":"/blog/unix-commands-III-find","result":{"data":{"contentfulBlogPost":{"title":"Unix Commands - III find","publishedDate":"January 19th, 2021","body":{"json":{"data":{},"content":[{"data":{},"content":[{"data":{},"marks":[],"value":"Hello everyone, today I'll try to explain the basics of find command and the bare minimum that you should know to use find command properly. I hope you'll like it, now let's dive right into tutorial.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"Manual","nodeType":"text"}],"nodeType":"heading-3"},{"data":{},"content":[{"data":{},"marks":[],"value":"As I explained in the former posts the most insightful resource that you can find about a Unix command probably its manual, let's take a look at its content and what we can say from that.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c498Zj8f5wEkiZ1sx5s4jSe","type":"Asset","createdAt":"2021-01-20T16:29:50.190Z","updatedAt":"2021-01-20T16:29:50.190Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"498Zj8f5wEkiZ1sx5s4jSe"},"fields":{"title":{"en-US":"unix-find-man"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/498Zj8f5wEkiZ1sx5s4jSe/62bf78636541296c7c848c8a6f2ae39f/Screen_Shot_2021-01-20_at_17.29.26.png","details":{"size":86818,"image":{"width":1406,"height":470}},"fileName":"Screen Shot 2021-01-20 at 17.29.26.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now here we can see that defined as \" walk a file hierarchy\" and the description says \"The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree.\" Now that basically means with find command we can search in all the subdirectories and files inside of a directory.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"You can always look more from the official manual ","nodeType":"text"},{"data":{"uri":"https://man7.org/linux/man-pages/man1/find.1.html"},"content":[{"data":{},"marks":[],"value":"here","nodeType":"text"}],"nodeType":"hyperlink"},{"data":{},"marks":[],"value":", or you can basically run the man command yourself for the find and see what it offers more.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"We have an example directory with some subdirectories and files for this post now let's take a look at it with tree command. If you are using MacOS it doesn't come built-in yet you can easily install it via \"brew install tree\". I'll not provide a GitHub repo for that because you can create some similar structure within minutes if not seconds yourself.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c2e71dL3PzQ4NxJ6CALTxWC","type":"Asset","createdAt":"2021-01-20T16:49:51.345Z","updatedAt":"2021-01-20T16:49:51.345Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"2e71dL3PzQ4NxJ6CALTxWC"},"fields":{"title":{"en-US":"unix-find-tree"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/2e71dL3PzQ4NxJ6CALTxWC/24936c9219ec84b8e347f7114562d7ba/Screen_Shot_2021-01-20_at_17.49.35.png","details":{"size":109500,"image":{"width":1396,"height":908}},"fileName":"Screen Shot 2021-01-20 at 17.49.35.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"We can see that we have 4 directories and inside each of them we have different types of files to work with. ","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"We can also achieve some similar result via our lovely find command :). Let's take a look.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c7vnfylNCjoPUwUZ9BrMpZy","type":"Asset","createdAt":"2021-01-20T17:02:26.906Z","updatedAt":"2021-01-20T17:02:26.906Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"7vnfylNCjoPUwUZ9BrMpZy"},"fields":{"title":{"en-US":"unix-find-dot"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/7vnfylNCjoPUwUZ9BrMpZy/72399e28281e6711c9f4bbbe8f1351a8/Screen_Shot_2021-01-20_at_18.01.52.png","details":{"size":135288,"image":{"width":1428,"height":862}},"fileName":"Screen Shot 2021-01-20 at 18.01.52.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"As we can see now by calling the find with the directory choice of ours we can display everything inside and print them to standard output.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"Let's take a closer look at the command we ran \"find .\" that dot stands for our current directory in Unix systems and since we didn't provide any other flags find automatically used -print argument and printed everything inside of the current directory, directories itself included with dot on the top. So we can understand that find also has some other arguments and -print is the default one.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"Let's take a look at -name argument for example.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"jr7K6JfwePomzIc8CX9LK","type":"Asset","createdAt":"2021-01-20T17:09:14.470Z","updatedAt":"2021-01-20T17:09:14.470Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"jr7K6JfwePomzIc8CX9LK"},"fields":{"title":{"en-US":"unix-find-name-basic"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/jr7K6JfwePomzIc8CX9LK/63ea77f57615622a588e653b77d14bd0/Screen_Shot_2021-01-20_at_18.08.49.png","details":{"size":52714,"image":{"width":1400,"height":262}},"fileName":"Screen Shot 2021-01-20 at 18.08.49.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now as we can see here above we basically found all the .txt files under our current directory, as you can see we can also use regex with -name argument we can run regexes like we did in this one. But maybe I'm a little bit ahead of myself, let's take a look at the most basic use of -name argument.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c3FHxwENg3LBa5bLbc5sqQG","type":"Asset","createdAt":"2021-01-20T17:14:27.102Z","updatedAt":"2021-01-20T17:14:27.102Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"3FHxwENg3LBa5bLbc5sqQG"},"fields":{"title":{"en-US":"unix-find-basicsss"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/3FHxwENg3LBa5bLbc5sqQG/d5a021c91a25cb322758879ee8162295/Screen_Shot_2021-01-20_at_18.14.09.png","details":{"size":22143,"image":{"width":1388,"height":78}},"fileName":"Screen Shot 2021-01-20 at 18.14.09.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"As we can see above we can just check for the full name of a file and get the full directory.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c25WF3BwDLaqeNCk5vMRQSz","type":"Asset","createdAt":"2021-01-20T17:16:20.851Z","updatedAt":"2021-01-20T17:16:20.851Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"25WF3BwDLaqeNCk5vMRQSz"},"fields":{"title":{"en-US":"unix-find-name-regex"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/25WF3BwDLaqeNCk5vMRQSz/2a6a555e8b8eb8f48d6074835cb96f28/Screen_Shot_2021-01-20_at_18.15.58.png","details":{"size":48165,"image":{"width":1388,"height":234}},"fileName":"Screen Shot 2021-01-20 at 18.15.58.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now let's take a look at this one, we are now just checking if a folder name contains the string \"text\" and as a whole and ignoring all the other characters that came before or after it.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"Another argument we can use is -type I'll quickly walk you through with -type because in my opinion it's quite straightforward and easier to pick up.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c6ncp7tZICkmrpfIEAzYlJk","type":"Asset","createdAt":"2021-01-20T17:20:11.497Z","updatedAt":"2021-01-20T17:20:11.497Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"6ncp7tZICkmrpfIEAzYlJk"},"fields":{"title":{"en-US":"unix-find-type-file"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/6ncp7tZICkmrpfIEAzYlJk/f645dcc4536184b1de3c7cf1a57a4f60/Screen_Shot_2021-01-20_at_18.19.27.png","details":{"size":110588,"image":{"width":1406,"height":616}},"fileName":"Screen Shot 2021-01-20 at 18.19.27.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"As we can see now \"find .\" command with displays only files for us and not the folders. Similarly we can do something like that for folders.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c3qzZQzQD7wJTGRFmB8I1hD","type":"Asset","createdAt":"2021-01-20T17:21:46.614Z","updatedAt":"2021-01-20T17:21:46.614Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"3qzZQzQD7wJTGRFmB8I1hD"},"fields":{"title":{"en-US":"Screen Shot 2021-01-20 at 18.19.50"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/3qzZQzQD7wJTGRFmB8I1hD/0cbfb0939c49eca6232b1b743b92a9cc/Screen_Shot_2021-01-20_at_18.19.50.png","details":{"size":33217,"image":{"width":1390,"height":204}},"fileName":"Screen Shot 2021-01-20 at 18.19.50.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now we only have folders nothing else.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now let's move to a trickier one, -exec argument. With find we can execute other commands on the things we found with find let's have a quick look by combining some other flags with exec.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c5hwSsBMWvQkiSh7y5u7nhe","type":"Asset","createdAt":"2021-01-20T17:24:19.383Z","updatedAt":"2021-01-20T17:24:19.383Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"5hwSsBMWvQkiSh7y5u7nhe"},"fields":{"title":{"en-US":"find-exec-cat-1"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/5hwSsBMWvQkiSh7y5u7nhe/989d81aa4421a8f74575bce8b068032d/Screen_Shot_2021-01-20_at_18.23.58.png","details":{"size":31540,"image":{"width":1404,"height":130}},"fileName":"Screen Shot 2021-01-20 at 18.23.58.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"As we can see we have 2 lines of text here probably from other types of files and directories, why we have only 2 lines of text here, because I got lazy :). ","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"Here what type does is it finds all files with -type f argument and executes cat command for each of them \"{}\" stands for all the things we found, we need to close -exec argument with \";\" and we are using \"\\\" for escape.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"QYF8z25bDfmws4KkxJEtd","type":"Asset","createdAt":"2021-01-20T17:28:28.916Z","updatedAt":"2021-01-20T17:28:28.916Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"QYF8z25bDfmws4KkxJEtd"},"fields":{"title":{"en-US":"unix-find-exec-complex"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/QYF8z25bDfmws4KkxJEtd/a7800c149989f5ddda687251e5042f02/Screen_Shot_2021-01-20_at_18.28.03.png","details":{"size":13063,"image":{"width":713,"height":49}},"fileName":"Screen Shot 2021-01-20 at 18.28.03.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now with adding one more filter we eliminated all the not \".js\" files, speaking of not let's do something different.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c3KShE2ytf4eWD2LkZJe07x","type":"Asset","createdAt":"2021-01-20T17:33:07.590Z","updatedAt":"2021-01-20T17:33:07.590Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"3KShE2ytf4eWD2LkZJe07x"},"fields":{"title":{"en-US":"unix-find-exec-name-not"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/3KShE2ytf4eWD2LkZJe07x/0003cc5d8b9a145c7facaa2e8dafaa19/Screen_Shot_2021-01-20_at_18.32.46.png","details":{"size":27086,"image":{"width":1402,"height":96}},"fileName":"Screen Shot 2021-01-20 at 18.32.46.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"With -not argument instead of looking for \".js\" files we can look for the files that are anything but a \".js\" file as we can see the results are supporting that.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"The last but not least -delete argument, it's a useful argument too and makes find quite powerful, let's see what can we do with that now. ","nodeType":"text"}],"nodeType":"paragraph"},{"data":{"target":{"sys":{"space":{"sys":{"type":"Link","linkType":"Space","id":"qv0hcw26u3ts","contentful_id":"qv0hcw26u3ts"}},"id":"c7tfmqMlt3vm0rZPjKVrHHN","type":"Asset","createdAt":"2021-01-20T17:36:51.417Z","updatedAt":"2021-01-20T17:36:51.417Z","environment":{"sys":{"id":"master","type":"Link","linkType":"Environment","contentful_id":"master"}},"revision":1,"contentful_id":"7tfmqMlt3vm0rZPjKVrHHN"},"fields":{"title":{"en-US":"unix-find-delete"},"file":{"en-US":{"url":"//images.ctfassets.net/qv0hcw26u3ts/7tfmqMlt3vm0rZPjKVrHHN/70c17869e86c346aefe89e645dcb7aa2/Screen_Shot_2021-01-20_at_18.36.31.png","details":{"size":46164,"image":{"width":1400,"height":142}},"fileName":"Screen Shot 2021-01-20 at 18.36.31.png","contentType":"image/png"}}}}},"content":[],"nodeType":"embedded-asset-block"},{"data":{},"content":[{"data":{},"marks":[],"value":"Now I left a \".txt\" file with \"txt\" string inside the file name side and I just want to have \"text\" on that side let's say. What we can do is to check if that's the case by using regexes and then overwriting the -print argument with -delete.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[],"nodeType":"hr"},{"data":{},"content":[{"data":{},"marks":[],"value":"Alright everyone, that was it for today. I hope you enjoyed it, I'm planning to make more Unix posts in the future, until then see you and stay safe.","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"},{"data":{},"content":[{"data":{},"marks":[],"value":"","nodeType":"text"}],"nodeType":"paragraph"}],"nodeType":"document"}}}},"pageContext":{"slug":"unix-commands-III-find"}}}