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

0 comments:

Post a Comment