mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +00:00
Userland: Implement find -name clause
Closes https://github.com/SerenityOS/serenity/issues/4191
This commit is contained in:
parent
1abda05d38
commit
952c0dc2a0
2 changed files with 30 additions and 0 deletions
|
@ -35,6 +35,10 @@ specified commands, a `-print` command is implicitly appended.
|
||||||
* `-size number[c]`: Checks if the file has the given size in 512-byte blocks,
|
* `-size number[c]`: Checks if the file has the given size in 512-byte blocks,
|
||||||
rounded up. If the size is followed by the `c` character, checks if the file
|
rounded up. If the size is followed by the `c` character, checks if the file
|
||||||
has the given size in bytes.
|
has the given size in bytes.
|
||||||
|
* `-name pattern`: Checks if the file name matches the given global-style
|
||||||
|
pattern (case sensitive).
|
||||||
|
* `-iname pattern`: Checks if the file name matches the given global-style
|
||||||
|
pattern (case insensitive).
|
||||||
* `-print`: Outputs the file path, followed by a newline. Always evaluates to
|
* `-print`: Outputs the file path, followed by a newline. Always evaluates to
|
||||||
true.
|
true.
|
||||||
* `-print0`: Outputs the file path, followed by a zero byte. Always evaluates to
|
* `-print0`: Outputs the file path, followed by a zero byte. Always evaluates to
|
||||||
|
@ -62,6 +66,8 @@ $ find -type d
|
||||||
$ find /tmp "(" -type s -o -user anon ")" -exec rm "{}" ";"
|
$ find /tmp "(" -type s -o -user anon ")" -exec rm "{}" ";"
|
||||||
# Concatenate files with weird characters in their names:
|
# Concatenate files with weird characters in their names:
|
||||||
$ find -type f -print0 | xargs -0 cat
|
$ find -type f -print0 | xargs -0 cat
|
||||||
|
# Find files with the word "config" in their name:
|
||||||
|
$ find -name \*config\*
|
||||||
```
|
```
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/NonnullOwnPtr.h>
|
#include <AK/NonnullOwnPtr.h>
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
@ -212,6 +213,25 @@ private:
|
||||||
bool m_is_bytes { false };
|
bool m_is_bytes { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class NameCommand : public Command {
|
||||||
|
public:
|
||||||
|
NameCommand(const char* pattern, CaseSensitivity case_sensitivity)
|
||||||
|
: m_pattern(pattern)
|
||||||
|
, m_case_sensitivity(case_sensitivity)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool evaluate(const char* file_path) const override
|
||||||
|
{
|
||||||
|
LexicalPath path { file_path };
|
||||||
|
return path.basename().matches(m_pattern, m_case_sensitivity);
|
||||||
|
}
|
||||||
|
|
||||||
|
StringView m_pattern;
|
||||||
|
CaseSensitivity m_case_sensitivity { CaseSensitivity::CaseSensitive };
|
||||||
|
};
|
||||||
|
|
||||||
class PrintCommand final : public Command {
|
class PrintCommand final : public Command {
|
||||||
public:
|
public:
|
||||||
PrintCommand(char terminator = '\n')
|
PrintCommand(char terminator = '\n')
|
||||||
|
@ -335,6 +355,10 @@ static OwnPtr<Command> parse_simple_command(char* argv[])
|
||||||
return make<GroupCommand>(argv[++optind]);
|
return make<GroupCommand>(argv[++optind]);
|
||||||
} else if (arg == "-size") {
|
} else if (arg == "-size") {
|
||||||
return make<SizeCommand>(argv[++optind]);
|
return make<SizeCommand>(argv[++optind]);
|
||||||
|
} else if (arg == "-name") {
|
||||||
|
return make<NameCommand>(argv[++optind], CaseSensitivity::CaseSensitive);
|
||||||
|
} else if (arg == "-iname") {
|
||||||
|
return make<NameCommand>(argv[++optind], CaseSensitivity::CaseInsensitive);
|
||||||
} else if (arg == "-print") {
|
} else if (arg == "-print") {
|
||||||
g_have_seen_action_command = true;
|
g_have_seen_action_command = true;
|
||||||
return make<PrintCommand>();
|
return make<PrintCommand>();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue