@laks wrote:
Today will have rather short Journey on Day-4.
- Locate or find a file
- Which one is that?
- Search for a text inside a file or files
- Counting the impossible
- How to sorting file contents
Locate or find a file :
I have created a file named "hideme" , but I don't where resides now.How to locate it?
fastest way is to use "locate" command,
[efg@fedori ~]$ locate hideme
/home/efg/dir1/hideme
another way is to use a 'find' command,
[efg@fedori ~]$ find /home/efg -name hideme
/home/efg/dir1/hideme
which scans the directory /home/efg for file 'hideme'.the difference between locate and find is that,locate command maintains database(which was created by updatedb
),it actually check that database for file.
updatedb is command which runs from time to time and update filename and its path on the database which was la
ter used by locate command.But find command doesn't use any database,It simply the scans the directories everytime.
This is very basic usage of find command,but its very powerful command,next section(advanced find) deals with
it.which one is that?
If you have multiple binary installed on different path.I mean,for example,assume you have php5 installed on /
usr/bin and php4 installed on /usr/local/bin.
[efg@fedori ~]$ whereis php
php: /usr/bin/php /usr/local/bin/php
below command uses php to print "Hello world".
[efg@fedori ~]$ php -r 'echo "Hello World\n";'
Hello World
How to find out which get executed when your simply typed "php".to figure out that we can use,
[efg@fedori ~]$ which php
/usr/local/bin/php
thus when you simply use "php" it invokes binary from /usr/local/bin.How which command works?.Remember in our first section, when we talked about PATH environmental,
[efg@fedori ~]$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/efg/bin
which command uses above search path.Though we have php on /usr/bin and /usr/local/bin ,since /usr/local/bin c
omes before /usr/bin,
that binary got executed.Search for a text inside a file or files:
So now we know, how to find a file. But how to find a whether specific text available on a file or not?
For example,secretfile contents is -
[efg@fedori ~]$ cat secretfile
This is some random data for the file
In order to find whether the string "data" available on that file. we can use grep command.
grep command is used to search for given pattern on a file,If the pattern is found,it will print that whole li
ne.
[efg@fedori ~]$ grep 'data' secretfile
This is some random data for the file
To understand more about grep , we need to add more data to secretfile.
[efg@fedori ~]$ cat >> secretfile
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
^D
Lets check the contents again -
[efg@fedori ~]$ cat secretfile
This is some random data for the file
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..Lets try the same grep command , with option -n which display the line number too.
[efg@fedori ~]$ grep -n 'data' secretfile
1:This is some random data for the file
3:Note that we used >> to append data to the file.Suppose we want to print all lines except the line containing the word 'data' - Use v , invert option like-
[efg@fedori ~]$ grep -v 'data' secretfile
This will go as second line.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
grep has lot of option like printing lines that begin with specific text or ending with specific text etc.
check out 'man grep' or 'info grep' for all available option.(huh...Now i can move on ,If someone messes with
grep command,
i can blame him for not reading man or info pages)
Counting the impossible
Here is content again, the task is to count the number of lines it has ,
[efg@fedori ~]$ cat secretfile
This is some random data for the file
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
5? thats Right.pretty easy one. How about counting number of words ? thats slightly difficult. Then how about
counting number of characters ? Thats even harder. But wait there is one easy way to complete this task.
Just two word command named 'wc'.
[efg@fedori ~]$ wc secretfile
5 44 218 secretfile
secretfile has 5 lines , 44 words and 218 characters. Feel free to validity the resultHow to sorting file contents:
I'm not going to say anything about this sort command - you should be able to figure out yourself.
You know original content of secretfile ,this is what i get when used sort .
[efg@fedori ~]$ sort secretfile
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
Note that we used >> to append data to the file.
This is some random data for the fileOkay we are done! short day, isn't it ?
Posts: 1
Participants: 1