This is the fifth post in the Linux Series and this will be all about searching and locating files and some other concepts and commands that are useful to a programmer. First for somemore shortcuts and tricks to make using command line easier.
Autocompleting with tab
When typig some command and using a file in it or changing a directory etc you have to type the name of the file or name of the directory and sometimes the name is too long to hard to write so you can press tab and it will autocomplete the name for you only if the file exists in the current working directory.
For example you want to change the directory to tmp from the /
directory, you type t and press tab and it will autocomplete the name depending on how many dirs or files are there in the cwd that have the name that starts with the letter t. If the first suggestion is not tmp them pressing tab again will show the next suggestion and you can press it again and again to reach the suggestion you want, which is tmp dir.
Now to commands.
Search
There are many ways to look for a file in the filesystem using the terminal but here I will write about only two of them, find
and locate
.
Searching with find
The find command is the most powerful searching command that can look for files and folders with their name as well with their size, owner, date of modification, and group etc. It works like following:
root@User:/$ find / -type f -name cd
First type the command name find
them as the first argument to it type the directory to lookup in which in this case is the root or uppermost /
, then type -type
flag and after that specify the type and after that -name
flag with the name of the file to lookup for.
Example: I create a file named file.txt
in the /tmp directory and a new directory named fold with filename file.txt in it. I ran the find command and it printed the path to the files.
root@User:/$ find /tmp/ -type f -name file.txt
/tmp/file.txt
/tmp/fold/file.txt
Searching with locate
locate
command works simillarly as the find
command. It is also used to locate files in the filesystem. It is easier to work with than the find
command. To use it just type the command keyword and type the name of the file to look for.
root@User:/$ locate file.txt
How it works is it uses the name provided as the argument and look in the filesystem for the files that contain that word. But this command is not perfect because it may not work for files that are created some minutes or hours ago.
and now to grep command.
Filtering input with grep
grep
is one of the most useful commands to work with in Linux. grep stand for Global Regular Expression Printer or in somple words from Wikipedia "globally search for a regular expression and print matching lines". It was originally developed by Ken Thompson (co-creater of UNIX OS).
grep searches for provided keywords in the files or sets of strings and prints out the result. For example: consider a file containing titles of the all the moveis that released in 2020 which are not alot of movies and you provide a keyword "the" to search for and it will print all the movie titles that include the word "the" in it.
searching for a string within a file
Create a file named domains
in the /tmp directory and type grep with .com
as the first argument and filename domains
as the second argument to grep and press enter.
To the terminal and doing some work.
root@User:/tmp$ ls
domains
root@User:/tmp$ cat domains
wikipedia.com
google.com
wikimedia.org
github.com
zainsci.github.io/blog
another-site.com
fake-site.com.something
blah blah
another.blah
newfile.txt
some.com.au.aus.australia
root@User:/tmp$ grep .com domains
wikipedia.com
google.com
github.com
another-site.com
fake-site.com.something
some.com.au.aus.australia
root@User:/tmp$ grep .io domains
zainsci.github.io/blog
grep
can also be used directly with input as the output from another command. The following command is for demonstration only. cat
should not be used to provide input for the grep
as grep can use the file by itself as the input. Using just for an example.
root@User:/tmp$ cat domains | grep .io
zainsci.github.io/blog
The |
character is called a pipe and pipes the output of one command as the input of other command. In the upper command the output of cat command is piped as the input of grep command. grep
can do many more than this and needs more reading to better understand it as it can be very useful when working with Linux Command Line.