Find text string in files and subdirectories

Search and find for text and strings in files and subdirectories with result in variable

Find text string in files

Usually when searching for strings in files, we use Windows explorer or Windows search. In Linux Gnome uses nautilus or nemo, on macOS we use the finder.

Command-Line commands help with automated processing by scripts and batch processes. This post shows how to search and find for text strings in files in Windows Command Prompt and in the Linux shell.

findstr text strings in the command prompt

The Windows Command Prompt (cmd) a good use provide the findstr command with Windows+R run cmd as the following example shows.

findstr /s /i "dolor" *.*
doc.txt:Lorem ipsum dolor sit amet

If you want to assign the result of a variable for further processing in scripts. This can be made possible in a for loop, the following lines are stored and executed in a batch file.

@echo off
for /f "delims=" %%A in ('dir ^| findstr /s /i "dolor" *.*') do set "var=%%A"
echo %var%

With echo the entire line is output with the searched word dolor, if you want to narrow the output only to the characters of a word, this can be achieved with variable parameters.

doc.txt:Lorem ipsum dolor sit amet 
1234567890123456789012345678901234

From the sign 20 our word is dolor, it is 5 characters long.
The CLI input findstr dolor now brings dolor to the output found in the doc.txt file.

@echo off 
for /f "delims=" %%A in ('dir ^| findstr /s /i %1 *.*') do set "var=%%A" 
echo %var:~20,5%

The variable var is assigned the output of findstr.

Find text strings in files using Linux shell

$ grep -r "dolor" *
Dokumente/doc.txt:Lorem ipsum dolor sit amet

In the Linux bash Console does grep and find are used.

$ find . -type f -print0 | xargs -0 grep "dolor"
./Dokumente/doc.txt:Lorem ipsum dolor sit amet

The text search with grep is as follows, the result is assigned to the variable var, and output with echo.

$ var=`grep -r "dolor" *`
$ echo $var
Dokumente/doc.txt:Lorem ipsum dolor sit amet

When searching with grep, you do not want to output the entire line, but only the third word.

$ var=`grep -r "dolor" * | awk '{ print $3 }'`
$ echo $var
dolor

There are many other possibilities especially in the Linux bash, the command find is very extensive and offers with xargs also regular expressions and other commands for handing over, with find –help you get all possible applications. The only point here is to show an introduction to the application and procedure.

Remarks

This post shows how to find for text string in files in the command line. Typically, when searching for strings in files, we use Windows Explorer or Windows Search. On Linux we use Gnome Nautilus or Nemo, and on macOS we use the Finder.

Command line commands help with automated processing through scripts and batch processes. This shows how to search for text strings in files in the Windows command prompt and Linux shell and assign the results to variables.

extend sudo password timeout

How to extend sudo password timeout

System administrators also authenticate as normal users and use sudo when administrative tasks are performed. The first time sudo run they asks for the password, after which sudo is active for a limited time.

The default password timeout is 15 minutes. Therefore, if sudo is run again within 15 minutes (900 seconds), the prompt to re-enter the password will not occur.

Extend sudoers timestamp timeout

The timestamp_timeout defines the number of minutes that elapse before sudo should ask for the password again. Edit the /etc/sudoers file to change the timestamp_timeout.

I recommend using command visudo to edit the /etc/sudoers file. Add the value timestamp_timeout on the line after “Defaults”, in the /etc/sudoers file.

$ sudo su

A grep shows the searched lines.

$ grep Defaults /etc/sudoers

here a sudoers at Linux Mint.

The default setting is “Defaults env_reset”.

$ sudo visudo 

Extend sudo password timeout

Edit sudoers so that the Defaults line looks like this:

Defaults    env_reset,timestamp_timeout=-0

timestamp_timeout=-1 (minus one) causes the sudo password to never expire.

  For Debian/Ubuntu distributions, the -0 option did not work. A higher value e.g. 60 is accepted.

timestamp_timeout=0 (zero) causes the sudo password to expire every 0 (zero) seconds. This means that each time sudo is called, the password is asked.

 Conclusion

This post shows how to administrators also authenticate as normal users and use sudo when administrative tasks are performed. The first time sudo run they asks for the password, after which sudo is active for a limited time. This article shows how the time for the active session can be extended.

Exit mobile version