A A A
logo2




Searching files and directories



If you would like to find file you can use command find. This command look like that:
  1. word find
  2. pathnames to place, where the file will be searching
  3. criterion
  4. action, what we will do with files


find katalog kryterium akcja


If you would like to find files which name you know. Write find / -name “name of a file”.

owi@laptop:~$ find ~ -name "a*" -print

That command will search In home directory (symbol ”~”) files begin at letter a (-name “a*”) and it will print theirs pathnames.



We can also find files which are empty. We must use command find / -empty.

owi@laptop:~$ find . -empty -print

This command will serach In working direktory (symbol “.”) empty files (-empty) and it will print their pathnames.


If you would like to find files which don’t have a user (his account is removed) use a command find / -nouser .

owi@laptop:~$ find . -nouser -print

This command will search in working directory (symbol “.”) files wich don’t have a user (-nouser) and it will print their pathnames.


If you would like to find files which size is bigger then 200 kilobytes use command find . –size +200k.

owi@laptop:~$ find ~ -size +200k -print

This command will search in home directory files bigger then 200k (-size +200k) and it will print their pathnames.


This command will search files but only this which begin at letter a and are bigger then 200k.


I have show only action –print . If we would like to do something else with files (f.e. copy) we should use –exec then command \;

owi@laptop:~$ find . -name "a*" -exec cp {} directoryname \;

This command will serach files in working directory but only this which begin at letter a and will copy them ({}-list files to copy) to directory which is called namedirectory.



Print lines matching a pattern



If you would like to find In file lines matching a pattern use command grep. Next to grep write pattern and then name of a file.

owi@laptop:~$ grep root /etc/passwd

In file /etc/passwd will be found the lines where is Word Root. This commond will printf them. If you would like to find only the lines where first word is root at the begin of the pattern write symbol “^”.

owi@laptop:~$ grep ^root /etc/passwd

If you would like to know the number of lines where is word root use command grep –c.

owi@laptop:~$ grep -c ^root /etc/passwd

If you want to see only lines which don’t have a searching pattern use flag –v.

owi@laptop:~$ grep -v ^root /etc/passwd




Autor: Aleksandra Przybyło
aleksandraprzybylo@gmail.com