How to find files that are N level deep in a log file - linux

I'm trying to find files in a log file that are 2 levels deep from the root (eg. /xxx/xxx/xxx.xxx). I tried "find" and maxdepth, mindepth but that was only useful for files in directories. I need to find strings in a file instead and discard any results that are more than 2 levels.

How do I find all files containing specific text on Linux? seems to do what you want. It's unclear to me if you have matches you want to discard if they are deeper that two levels.

Related

pycharm jetbrains to search for text NOT in test*.py filename pattern

In Pycharm, when search for files that contain a given text e.g. hitting Ctrl-Shift-F, we have the File mask box as in below snapshot to filter file name pattern.
I want a NOT filter here e.g. search for files not starting with test*.py. How can we archive this?
Note
Although this question is old, I'll leave the answer here just in case someone else reaches the question.
Solution
The way to exclude results in File mask is by adding a ! before the mask, for example: !*test*.py
However, this might generate an unwanted situation, because it can bring results from configuration files, or temporary files, or any file we don't want. For this, the solution is to have multiple masks at once, and this can be achieved by separating masks with , (comma).
Example
If we want all files containing the word def in .py files, excluding files containing the word tests for any type of file, and models ending in .py, we would use the File mask: !*test*, !*models*.py, *.py
Hope this helps!

Search different paths for files without asterisk

I was wondering.. if I was to look for 5 different files with a similar path.. the five paths to the file of interest being:
/Users/folder1/exp
/Users/folder2/exp
/Users/folder3/exp
/Users/folder4/exp
/Users/folder5/exp
But I don't want to search for it using an asterisk like so:
find /Users/*/exp
How can I go about this?

How to search for files faster in Sublime Text 3

Right now I do ⌘t then scroll through autocomplete, or start typing the name (but half the time it doesn't find it).
Sublime doesn't find a file in many cases. For example, I typically have all my files called index.<ext> nested inside some folder. So I might have:
my/long/directory/structure/index.js
my/long/directory/structure2/index.js
my/long/directory/structure3/index.js
my/long/directory/structure.../index.js
my/long/directory/structuren/index.js
my/long/directory/index.js
my/long/directory2/index.js
my/long/directory.../index.js
my/long/directoryn/index.js
my/long/index.js
my/index.js
...
But in sublime you have to search for an exact path. I can't search this:
my directory index
And get results for directory, directory2, directory..., directoryn, I just get empty results because there is not my/directory. I can't remember the full folder path most of the time, so it takes a lot of effort to do so and I end up just navigating in the sidebar to find the file which takes some time.
Wondering if there is a better/faster way of doing this. Basically searching for a file by snippets/keywords of the complete path. So m dir would return my/long/directory, etc.
The first thing to note is that you do not have to search for an exact path; anywhere that Sublime provides you a list of items to select from and a text entry, fuzzy matching is in play. In your example searching just for idx will narrow down the list to all items that have those characters in that order, even if they're not adjacent to each other.
The entries show you visually how they're matching up, and there's a fairly sophisticated system behind the scenes that decides which characters make the best matches (relative to some hidden scoring algorithm):
In addition to this you can use multiple space separated terms to filter down the list. Each term is applied to the list of items resulting from the prior term, so they don't need to be provided in the same order as they appear in the file names.
This helps with searches where you know generally the name of the file, and from there can further drill down on segments of the path or other terms that will help narrow things down:
Something to note here is that as seen in these images, the folder structure is my/long/directory/structure, but the names of the files as seen in the panel don't include the my/ at the start.
In cases where your project contains only one top level folder, that folder isn't presented in the names of the files. Presumably this is because it's common to every file and thus not going to be a useful filter. As such trying to use my in the search field will return no matches unless one of the files has an m and a y somewhere in their filenames.
This isn't the case if there are multiple top level folders; in that case Sublime will include the root folder in the names of the files presented because now it's required to be able to distinguish between files in the different folders:
In addition to this, note that for any given filter text you enter in a panel, Sublime remembers the full text of the item that you selected while that filter text was being used, and uses that in it's scoring to prioritize the matches the next time you search in the same panel. The next time you search with the same term, Sublime will automatically pre-select the item that you picked last time under the theory that you probably want it again.
The search terms and their matches are saved in the session file and in your project's sublime-workspace files, so as you move from window to window and project to project you're essentially training Sublime how to find the files that you want.
My advice would be to try and flip your thinking a little bit. In my opinion the power of the fuzzy matching algorithm works best when you try to find files in a more organic way than trying to replicate the path entirely.
Instead, I would throw a few characters from the name of the file that I'm trying to find first, and then add another term that filters on some part of the path that will disambiguate things more; a term of idx s1 in this example immediately finds the two index.js files that are contained in structure1 folders, for example.
In a more real world example the names of the folders might contain the names of the components that they're a part of or something else that is providing a logical structure to the code, so you might do idx con to pull the index.js from the controller folder or idx mod to find the one in the model folder, and so on.
Regarding a better/faster way to do this I don't think there is one, at least in the general case. Sublime inherently knows every file that's in your project as a part of indexing all of the files to power other features such as Goto Symbol and it uses file watchers to detect changes to the structure of the open folders.
Anything else, including a third party plugin or package, would need to first do a redundant file scan to accumulate the list of files and would also have to replicate the file watching that Sublime is already doing in order to know when things change.

How To Recursivley Diff Files With Different Names

I have two directories that I want to compare the contents of. The structure of all the sub-directories underneath the top level directories that I want to compare is the same, but the file names are slightly different. E.g. I want to know if /d1/foo/bar/somefile-v2.txt is different to /d2/foo/bar/somefile-v3.txt.
I've found references that say it should be possible to do this using diff, but I can't find any documentation that tells me how to specify how diff maps from one file name to the other.
What you're referring to is diff's recursive option which in turn will compare all the files in a directory against another.
By reading your comments one can assume that:
File names are different
Using diff alone will not suffice.
You can just write a shell script to take care of this, but to answer your question: No, this can't be done with diff alone.
Note: you can use wildcards for the whole 1 character difference thing but it won't work recursively the way you want it to work.

How to identify line endings on a large number of files

Given a medium-size tree of files (a few hundred), is there some utility that can scan the whole tree (recursively) and display the name of each file and whether the file currently contains CRLF, LF, or mixed line terminators?
A GUI that can both display the current status and also selectively change specific files is preferred, but not essential.
Also prefer a solution for Windows, but I have access to both Bash for Windows and a Linux box that has access to the same file tree, so I can use something Linux-y if necessary.
Related Question: https://unix.stackexchange.com/questions/118959/how-to-find-files-that-contain-newline-in-filename
You can use linux' find to look recursivly for filenames containing newline characters:
find . -name $'*[\n\r]*'
From there you can proceed to do what you need to do.

Resources