Terminal Linux Ubuntu

Tìm kiếm theo noi dung chua trong file trên Linux Ubuntu

You can search recursively i.e. read all files under each directory for a string “192.168.1.5”

$ grep -r "192.168.1.5" /etc/

Search for old command in ubuntu Terminal:

  1. set: sudo chown $USER:$USER ~/.bash_history

  2. Then can use Ctrl+r for searching

List all previous commands:

history

fc -l <linenumber> list all commands from line_number until the end

history | grep <string can tim kiem>

Find a file

Finding by Name

The most obvious way of searching for files is by name.

To find a file by name, type:

find -name "query"

This will be case sensitive, meaning a search for "file" is different than a search for "File".

To find a file by name, but ignore the case of the query, type:

find -iname "query"

If you want to find all files that don't adhere to a specific pattern, you can invert the search with "-not" or "!". If you use "!", you must escape the character so that bash does not try to interpret it before find can act:

find -not -name "query_to_avoid"

Or

find \! -name "query_to_avoid"

Finding by Type

You can specify the type of files you want to find with the "-type" parameter. It works like this:

find -type 
type_descriptor
query

Some of the most common descriptors that you can use to specify the type of file are here:

  • Finding by Typef: regular file

  • d: directory

  • l: symbolic link

  • c: character devices

  • b: block devices

For instance, if we wanted to find all of the character devices on our system, we could issue this command:

find / -type c
/dev/parport0
/dev/snd/seq
/dev/snd/timer
/dev/autofs
/dev/cpu/microcode
/dev/vcsa7
/dev/vcs7
/dev/vcsa6
/dev/vcs6
/dev/vcsa5
/dev/vcs5
/dev/vcsa4
. . .

We can search for all files that end in ".conf" like this:

find / -type f -name "*.conf"

Ubuntu bash history with PageUp/PageDown

In /etc/inputrc, uncomment:

# alternate mappings for "page up" and "page down" to search the history

"\e[5~": history-search-backward

"\e[6~": history-search-forward

Restart your shell or use Ctrl+X, Ctrl+R to tell it to re-read.

Last updated