[LINUX] find / grep command (beginner's personal memo)

This is a survey of points that I did not understand in the first week of joining the company. Personal notes.

find command

Search for files by name Change the output contents (execute the ls command)

Basic grammar ** find [option] [search path] [search expression] **

If you execute only "find", all files under the current directory will be displayed. "Find / etc" shows all the files in the "/ etc" directory.

Specify the file name you want to search

Use the ** " -name "option ** to specify the file name you want to search. (The directory is also searched.)

To search for files containing "bash" in the file name in ** ~ ** on your home directory: If you put a wildcard ** * ** before and after bash, you can include files such as ●● _bash.js and .bash ●● in the search target. If you do not specify a wildcard, you do not need to add double quotes ** " **.

When using wildcards

find ~ -name "*bash*"

In case of fixed file name without using wildcard

find ~ -name .bash_profile


Search only files, not directories ** -type f option ** Example) When you want to search "profile.txt" in the hoge directory

find /hoge -name profile.txt -type f

If you want to search the directory ** -type d option **

When targeting symbolic links ** -type l ** option

Hide the "Operation not permitted" error message

When I execute find, I get a lot of execution results like this, and the execution results are very difficult to understand. It was the same when I ran it with sudo find ~. find: /Users/username/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted

This is because if you specify "/" as the search location, unauthorized locations will also be searched and a large number of error messages will be displayed, so you can use "** 2> / dev / null ** " It's a good idea to throw it away (redirect to a null device). And that.

** 2> / dev / null ** example of discarding error messages

find /hoge -name profile.txt -type f 2>/dev/null

This will discard the search error message for unauthorized locations and display only pure search results.

Change the output contents (execute the ls command)

It can be executed by specifying "action" in addition to the search expression.

** -print action ** (default) Show file name

** -ls action ** Detailed information about the file is displayed. The information displayed by the "ls -dils" command is quite long because it includes the file size and so on.


 find ~ -name .bash_profile -type f -ls 2>/dev/null

Execution result

12902229406        8 -rw-r--r--1 Username staff 629 Nov 24 17:56 /Users/username/.bash_profile

If the amount of information is about "ls -dF", use the "-exec" action and specify it as "-exec ls -dF {};". .. See the source for details.

grep command

Quote source: https://www.atmarkit.co.jp/ait/articles/1604/07/news018.html https://eng-entrance.com/linux-command-grep

Extract only the necessary parts from the execution result Search by word Search the previous and next lines Display with line numbers

Basic grammar ** grep [optional] search pattern file ** Or ** command | grep [option] search pattern **

Example) When searching for the character "a" in all the files in the "work" directory
grep a work/*
Search and with grep
grep search string filename|grep search string 2

Display with line numbers

If you want to display the search results of the grep command with line numbers, use the ** -n option **. ** grep -n search string text file **

Example) Search the "~ / .ssh / hoge.txt" file for a character string containing the character string "EA", and display the line number if it exists.

grep -n EA ~/.ssh/hoge.txt

Execution result


Recommended Posts

find / grep command (beginner's personal memo)
[Personal memo] Python virtual environment command memo
grep command
command memo
Command memo
Linux command (basic in basic) personal memo
[OpenCV] Personal memo
scp command memo
who command memo
Linux # Command Memo 1
vi (vim) command memo
Basic Python command memo
Find, locate command summary
[Personal memo] Auto-completion of bash
virtualenv Basic command usage memo
Linux tar xz command memo
Supervised learning ~ Beginner's memo ~ (scikit-learn)
Linux command memorandum [for beginners]
[Learning memo] Django command summary