Find Files and Directories - new tasks - it's getting more interesting!


The previous tasks didn't really require me to search for very complex knowledge. However, I see that the difficulty level is increasing from task to task. Is it a good thing? Of course. However, it should be noted that each task is well described on internet forums such as Reddit or HTB. Nevertheless, this should be a last resort. After all, one must engage their gray matter and get into battle.

Task one: What is the name of the config file that has been created after 2020-03-03 and is smaller than 28k but larger than 25k?

I think we need to break it all down into smaller parts, and then go through it step by step.

I'm searching for an object of type file.

-type f

I'm specifying the query by narrowing down the sought-after files to the type .conf.

-iname *.conf

(I'm using -iname instead of -name, as -iname doesn't consider letter case. It will show results regardless of whether the name is written in uppercase or lowercase. This essentially expands the search scope.)

It should be a file created after the date 2020-03-03.

-newermt 2020-03-03

And the final condition is that the file size should be greater than 25k but less than 28k.

-size +25k

-size -28k

Now I'll put it all together. Of course, the directory from which I start the search is the root directory (/).

find / -type f -iname *.conf -newermt 2020-03-03 -size +25k -size -28k

It should work - but it's not working, it's showing me a lot of paths I don't have access to. And a few files that answered the question aren't there.

 I'll reluctantly turn to the search engine for help - what was missing in the command?

Alright, time to get rid of errors and redirect them to standard output using 2>/dev/null.

find / -type f -iname *.conf -newermt 2020-03-03 -size +25k -size -28k 2>/dev/null

What is my result now?

 Great, I'm left with only one file :D Success? No - of course, it's not the answer to the question asked.

After seeking help on the forum, I'm adding the following code snippet to the command:

 -exec ls -al {} ; 2>/dev/null

and now

find / -type f -iname *.conf -newermt 2020-03-03 -size +25k -size -28k  -exec ls -al {} \; 2>/dev/null

The answer is displaying only one file.


However, where did the difference in results come from? So, what does this part of the command change?

 This code snippet executes the `ls -al` command for each found file or directory. Here's what each part does:

- `-exec`: This is a find command option that allows executing a specific command for each found file or directory.
- `ls -al`: This is the `ls` command displaying detailed information about files and directories, including hidden ones, in long format.
- `{}`: It's a placeholder symbol that will be replaced with the name of each found file or directory by the find command.
- `\;`: This signifies the end of arguments for the `-exec` command and then terminates the command.

This snippet will display detailed information for each found file or directory, which can help understand why certain files meet or don't meet the search criteria.

 From this, I deduce that this file was a hidden file and therefore was not considered during the system search - however, if I am mistaken, please correct me.


 Task two: How many files exist on the system that have the ".bak" extension?

 I already know a few things. I'm starting to search in the root directory.

find / 

I'm searching for objects of type file

-type f

The name must contain 'bak'.

-iname *.bak

And I'll discard all errors from the screen.


 Altogether, it will give me this syntax.

find / -type f -iname *.bak 2>/dev/null

The search result shows me all the .bak files in my system - but what would I do if there were many more? Here I can use the pipe | and additionally the

 wc -l 

command, which counts the number of displayed lines.

 find / -type f -iname *.bak 2>/dev/null | wc -l

And what does the corrected version of this syntax say?

 And is this answer - "4"- correct? Check it yourself :D

Task three - the last one for today - Submit the full path of the 'xxd' binary.

I'm searching for the path to the executable file using the command

 locate xxd

What's the result?

A few paths to files with 'xxd' in the name were displayed - considering that the path to executable files is typically /usr/bin, I can consider the task completed. However, is it possible to narrow down these searches to one answer - one path?

Yes, it can be done better by replacing the 'locate' command with the 'which' command, which will narrow down the search scope to executable files. So, the last sentence for today is:

which xxd










Popular posts from this blog

Task Scheduling and System Services: A Theoretical Overview

Three tasks related to user management.

Working with Web Services.