Submind YouTube summaries
Thumbnail for The find command: Multiple tests and logical operators

The find command: Multiple tests and logical operators

Watch on YouTube

Video summary

Bu videoda `find` komutunun sadece düz dosyaları listelemek ve dizinleri dışarıda bırakmak için nasıl kullanılabileceği detaylı bir şekilde anlatılmaktadır. Komutun temel yapısı, başlangıç dizini, dosyaların hangi kriterlere uyduğunu belirleyen ifadeler ve bu dosyalara uygulanacak eylemlerden oluşur; eğer eylem belirtilmezse varsayılan olarak dosya isimlerinin ekrana yazdırılması gerçekleşir. Önceki videolarda tek bir test kullanıldığı görülse de, `find` komutunun birden fazla testi ardışık olarak uygulayabileceği vurgulanmaktadır. Bir dosya bulunduğunda önce tipinin düz dosya olup olmadığı kontrol edilir ve bu kriter sağlanmazsa dosya atlanır; eğer düz dosya ise ikinci test olan tarih karşılaştırması yapılır ve her iki test de geçtiğinde dosya listelenir. Birden fazla testi birleştirmek için mantıksal operatörler kullanılarak karmaşık seçimler yapılabilir. `and` operatörü ile birden fazla kriterin aynı anda sağlanması gerektiği, örneğin bir dosyanın hem düz dosya olması hem de belirli bir tarihten sonra oluşturulmuş olması gerektiği durumlarında gösterilirken; `or` operatörü ise kriterlerden herhangi birinin sağlanması durumunda dosyanın seçilmesini sağlar. Bu mantıksal operatörlerin öncelik sırası, aritmetik işlemlerdeki çarpma ve toplama önceliğine benzer şekilde `and`'nin `or`'dan daha yüksek önceliğe sahip olmasıdır. Bu nedenle, `or` ifadelerinin doğru şekilde değerlendirilmesi için parantez kullanımı kritik önem taşır; örneğin bir dosyanın hem düz dosya olması hem de ya JPEG formatında olması ya da 10 megabyte'dan büyük olması gerektiği gibi karmaşık senaryolarda parantezler olmadan beklenen sonuç alınmayabilir. Videoda ayrıca `not` operatörünün nasıl kullanıldığı ve eski dosyaları bulmak için tarih karşılaştırmasının tersine çevrilerek uygulanabileceği gösterilmektedir. Bazı Linux sürümlerinde doğrudan "eski" testini yapan bir seçenek bulunmasa da, bir dosyanın belirli bir tarihten sonra oluşturulmadığını (`not newer`) belirterek aynı sonucu elde etmek mümkündür. Bu teknik, özellikle disk alanını temizlemek veya yedekleme yapmak gibi pratik senaryolarda çok işe yarar; örneğin 2024'ten eski olan veya 5 megabyte'dan büyük dosyaların bulunup tek bir sıkıştırılmış arşiv dosyasına (`tar.gz`) yüklenmesi işlemi adım adım izlenmektedir. Sonuç olarak, `find` komutu başlangıç noktası, birden fazla testi içeren ifadeler ve eylemlerle donatılarak, mantıksal operatörler ve parantezlerle desteklenerek tam olarak istenen dosyaların seçilmesini sağlayan güçlü bir araçtır.
Read the full video transcript
In a previous video, we saw that this command would list both files and directories in the results. In this video, we'll see how to get only the regular files and omit the directories and we'll learn much more. But first, let's formally introduce the parts of the find command. We have a starting directory followed by an expression that tells which files pass the test and an action to take with those files. If we omit the action, the default is to take the dash print action which shows the file names. And here's an example with an explicit action that sends the file names to ls-l. Up until now, all of our expressions have consisted of exactly one test. But there's no law that says we can have only one test. In fact, we can have as many as we want. Here we have two tests in our expression. Each time find encounters a new file, it first tests to see if it is a regular file. If it isn't, find ignores the file. If it is a regular file, find goes to the next test. If the file isn't newer than May 1st, 2025, find ignores it. If it's newer, then the file has passed both tests and its name can be printed, our default action. Let's see this at the command line. First, with only the test for newer, we're going to find in our downloads folder everything that is newer than our reference file. And again, we have some directories there. In the downloads folder, we first want to test if we have a type that's a regular file and also if it's newer than our reference file. And voila, we have the files only without the directories. Again, to pass the tests, the file must be a regular file and it must be newer than May 1st, 2025. I can make this explicit by adding the logical operator and and I get the same results as before. There's also a logical operator called or and this says that a file passes the test if it passes either the first test or the second test. In this command, if the file is a regular file or it's newer than May 1st, 2025 or both will print it. And you'll notice we get quite a few more files. This time we can even make very complicated expressions. For example, we want to find files where a file is a regular file and it's either a JPEG file or it's larger than 10 megabytes. Here it is in the shell. In the downloads folder, we want to find regular files. And also, and now I'm going to need a parenthesis to group the or expression. And since parenthesis is a special character in this shell, I'm going to have to escape it with a backslash. The name ends in JPG or the size is greater than 10 megabytes. And then I have my closing parenthesis which also needs to be escaped. And there is that complicated expression. And let's see what the results are. Turns out that only two files match. Now the question is why did I need those parentheses? Because just as there is an order of operations for arithmetic operators where multiplication comes before addition. There's an order of operations for logical operators where the and operator takes priority over or. Most people are familiar with the order for arithmetic operations but logical operations not so much. Instead of having to guess which operations have priority, use parentheses to guarantee that you get what you want. You'll notice one operator that we haven't covered yet, the not operator, and its purpose is to reverse the result of a test. The not operator comes in handy when we want to find a file that is older than a certain date. There is no dash older test in some versions of Linux. In fact, on this one, but we can get the desired effect by saying not newer. So instead of older, I'm going to put in my grouping parenthesis and say not newer than ref made 2025 and close off my parenthesis. Again, I put the parentheses here to make sure that everything happens in the order that I want. And let's pipe that result to xgs ls-l so we can see all the information about them. And we see that they're all regular files and they are also dated before May 2025. So that means they're older files. Now let's put this to use. I want to clear out some disk space. So, I want to find all the files in the downloads folder that are regular files and are older than January 1st, 2024 or are larger than 5 megabytes. And I want to put all those files into a tar gzip file that I can put on a backup drive. I'll create a reference file with a date of 2024101 January 1st 2024 in my home folder and I'll call it ref year 2024. Now my find command in the downloads folder. I want to find regular files and files that are not newer than 2024 or have a size that is more than five megabytes. Before I put these in a tar file, I want to see what files I have. And those happen to be the files that I'm going to be backing up. Looks good. I'll bring back the last command with the up arrow. And now I will pipe that to XRS. I'll use the tar command to create a gzipped file called backup.tgz. And again, all the files that were listed in find will go into that backup file. They will be appended after the xs. And let's look at the file to see that it exists. We're going to do T, which means show the contents verbose for the file backup.tgz. And there are my files ready to go to a backup drive. And then I can remove those files later on to save my disk space. To summarize, a find commands consists of a starting point, one or more expressions, and one or more actions. An expression is made up of one or more tests. By default, a file is selected only when it passes all the tests. In this example, test one and test two. And you can combine tests with the logical operators to select exactly the files you want.