Showing posts with label Linux locate. Show all posts
Showing posts with label Linux locate. Show all posts

Thursday, 1 August 2019

Linux Commands – find, locate, whereis, sort, grep

1) find = finds one or more files assuming that you know their approximate filenames

Examples:

find -name mypage.htm
In the above command the system would search for any file named mypage.htm in the current directory and any subdirectory.

find / -name mypage.htm
In the above example the system would search for any file named mypage.htm on the root and all subdirectories from the root.

find -name file*
In the above example the system would search for any file beginning with file in the current directory and any subdirectory.

find -name ‘*’ -size +1000k
In the above example the system would search for any file that is larger then 1000k.

find /usr -name *stat

Find every file under the directory /usr ending in “.stat”.

find / -mtime -2 -print

Search the system for files that were modified within the last two days (good candidates for backing up)

2) locate = find files by name


Searches are conducted against a database of system contents that is updated periodically. To update the database, use the updatedb command.

locate "*.png"

to display all files on the system that have the .png filename extension

locate "*.png" -q

to suppress error messages, such as those that might be returned in the event that the user does not have permission to access designated files or directories

locate -n 15 "*.html"

display only 15 results in a search for files that have an .html extension:

locate "*.html" | less

presents only one screenful of output at a time

locate -i "*.HtmL"

The -i option performs a case-insensitive search. That is, it will return any results that match the arguments regardless of whether individual letters are lower case or upper case.

.

3) whereis = the whereis command is used to locate the binary, the source code and the online manual page for any specified program. Take note it is not for searching your files.

Example:

whereis locate

locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz

whereis -b locate (search only binaries)

locate: /usr/bin/locate

.

4) sort =  sort lines of text files

sort can be used in many ways, below are some simple examples

a) example 1

assume i have 1 text file with name fruits containing below

orange
lemon

and another text file veggy with contents below

papaya
cucumber

sort fruits veggy : it will produce output in alphabetical order as below

cucumber
lemon
orange
papaya

sort fruits veggy > fruitveggy : this command will save into a file fruitveggy

b) example 2

i can create a text file that is sorted in real time, for example, by typing

sort > color

it will put me into data entry mode, i can keep entering data for example blue, green, yellow each followed by <enter> command. once im done, pressing Ctrl-D keep me back at command prompt. now if i check the text file color i can see alll my input is there already sort out

c) example 3

i have a data file data.txt as below:

24 John
45 Sharon
98 Robert

i want to sort it on second field (name by alphabetical order), i can use this command

sort -d -k 2 data.txt , the output will be

24 John
98 Robert
45 Sharon

Explanation: The ‘-d’ switch stands for ‘dictionary sort’ and ensures that sorting

takes place alphabetically as a dictionary would do it. The ‘-k’ switch

stands for ‘key’ and with the ‘2’ tells sort to sort on the second

field in the file, that is the names.

.

5) grep = finds text within a file

grep tutorial *.htm = search all .htm files in the current directory for the text “tutorial”

grep -i love history.txt = print all lines containing love (ignoring uppercase and lowercase) in history.txt

grep -ic “love me” history.txt = To print just the number of lines containing the word “love me”

grep -r tutorial * = -r is to search not only in the current directory but recursively in all the sub directories, the symbol * means to search all files

ls | grep readme = this command is to print all files that has the phrase “readme” in the filename. take note it is not the text inside the document but the name of the document

ls -l | grep rwxrwxrwx = to find all filesystem objects in the current directory whose permissions have been set so that any user can read, write and execute them

Linux locate, find, and grep

inding things on Linux can get incredibly complex, especially if you ask a
regular expression guru for help. By the time they're done helping you, you
feel inferior and too lowly to be worthy of Linux. While being able to find a
specific comma on a 160-gigabyte hard drive is fun, or a particular instance
of the word "the," you can actually do quite a lot with the three basic Linux
finding-things utilities: locate, find, and grep. Locate and find search for
files, and grep searches for text in files.

Locate is the easiest. It maintains its own database of files on your system,
so it's blazing fast. Some distros install with a cron job that rebuilds the
locate database regularly. However you do it, it must be updated
periodically:

# updatedb

Finding files is like so easy:

$ locate filename

Do a case-insensitive search:

$ locate -i filename

It is typical that locate will return a skillion hits. Locate and grep
together are the Dynamic Duo:

$ locate -i tuxracer | grep -i readme
/usr/share/doc/tuxracer-data/README
/usr/share/doc/tuxracer/README
/usr/share/doc/tuxracer/README.Debian
/usr/share/games/tuxracer/music/readme

Now suppose you need to find a file with a particular bit of text in it. grep
is wicked cool for searching dmesg:

$ dmesg | grep -i usb
usb.c: registered new driver usbdevfs
usb.c: registered new driver hub
host/usb-uhci.c: $Revision: 1.275 $ time 20:17:46 Aug 3 2003
host/usb-uhci.c: High bandwidth mode enabled
host/usb-uhci.c: USB UHCI at I/O 0xd800, IRQ 11
host/usb-uhci.c: Detected 2 ports

Blah blah, and there are many more lines like this. Grep prints the whole line
each time it finds your search string. dmesg is a special case, as it is a
command, and not just a text file. For example, to search an ordinary text
file, such as the XFree log, for references to 'glcore', this is the normal
grep syntax:

# grep -iw glcore XFree86.0.log
(II) LoadModule: "GLcore"
(II) Module GLcore: vendor="The XFree86 Project"
(II) Loading sub module "GLcore"
(II) LoadModule: "GLcore"

grep - options - search string - file to search
The -w flag means 'whole word search.' Which is really handy, because
otherwise grep returns every single line that contains your search string.

Here is the kewlest grep trick of all: searching bales of files for a word or
string or regexp. CD to the directory you want to search, then:

$ grep -lir "word" *
Lesson5.html

-l means print only the filenames containing your search term, -r is
recursive, and * means all files. If you omit the -l flag, it prints the
filenames and the lines containing your search term:

$ grep -ir swap *
Lesson5.html:chuck the data, and none of the other options matter. A swap
device is
Lesson5.html:initialised with <code>mkswap /dev/something</code>, and can be
Lesson5.html:manually activated with <code>swapon /dev/something</code>,
although you'll

Find is wicked cool. Do this to search for a file named arrow.jpg:
$ find / -name arrow.jpg

That tells find to start its search from /. Maybe you only know part of the
filename, so give it some wildcards. This searches your home directory for
filenames that start with arrow, and can end with anything:

$ find ~ -name 'arrow*'
/home/carla/downloads/neverball-0.25.11/data/mtrl/arrow-green.jpg
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow.xbm
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow-mask.xbm

Maybe you only want 'arrow' images in .xbm format:
$ find ~ -name 'arrow*.xbm'
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow.xbm
/home/carla/downloads/tuxpaint-0.9.12/src/mouse/arrow-mask.xbm

Or all the .xbm files in the home directory:

$ find ~ -name '*.xbm'

A cool thing with find is tracking down orphaned files, after you've booted a
user off the system. You need to locate all the files that belonged to that
user. Oops, their account is long gone, so you can't search by login name. No
problem, search on their UID:

# find / -uid 1005

If you're searching system directories as a lowly unprivileged user, you'll
generate bales of error messages. These are like, so depressing, so they must
be suppressed, or redirected into the bitbucket, hahaha:

# find / -uid 1005 2>/dev/null