You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

95 lines
1.9 KiB

= An Introduction to Linux !
Apostolos rootApostolos@swarmlab.io
// Metadata:
:description: Intro and Install
:keywords: Linux, apt
:data-uri:
:toc: right
:toc-title: Table of contents
:toclevels: 4
:source-highlighter: highlight
:no-header-footer:
:nofooter:
:last-update-label!:
:icons: font
:sectnums:
{empty} +
== Find and grep
=== find
The find tool, known from UNIX, is very powerful. This command not only allows you to search file names, it can also accept file size, date of last change and other file properties as criteria for a search.
The most common use is for finding file names:
NOTE: find <path> -name <searchstring>
This can be interpreted as "Look in all files and subdirectories contained in a given path, and print the names of the files containing the search string in their name" **(not in their content).**
[source,bash]
----
find /etc -name "*.conf"
----
=== grep
grep is used for filtering input lines and returning certain patterns to the output.
NOTE: grep "string" path/to/file
[source,bash]
----
grep "root" /etc/passwd
----
=== find and grep command together
find /etc -name "*.conf" -exec grep -Hns "conf" {} \;
.Explanation
[source,bash]
----
-H, --with-filename
Print the filename for each match
-n, --line-number
Prefix each line of output with the 1-based line number within its input file
-s, --no-messages
Suppress error messages about nonexistent or unreadable files.
----
NOTE: This can be interpreted as +
- "Look for *.conf files and subdirectories contained in /etc, and **if true** exec **grep -Hns conf** in the given file"
TIP: Very powerful in bash scripts
create a file test.sh
.bash script
[source,bash]
----
#!/bin/bash
STRING=$(find /etc -name "*.conf" -exec grep -Hns "conf" {} \;)
echo $STRING
----
exec it
.bash script
[source,bash]
----
chmod 700 test.sh
./test.sh
----
:hardbreaks:
{empty} +
{empty} +
{empty}
:!hardbreaks: