Ubuntu Tutorial

locate command : it find files by file name. it is faster than find command. locate command may result files or directories that no longer exist also it may not result newly created, moved, copied, renamed files or directories. because instead of searching files in the file system, it searches files in database which may not be updated. in another words output of locate command may be incorrect.

syntax : locate option pattern

in following example we search file that has js extension.

image
locate command

find command : find command is used to find files or directory in file system. it is slower than locate command but unlike locate command it gives correct output to user. because it searches data in file system. find can be used in a variety of conditions like you can find files by permissions, users, groups, file type, date, size, and other possible criteria. by default it searches file in current directory.

general syntax : find path option pattern

in following example we search file having extension js.

image
find command

following table shows option for find command.

Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.

Option Description
-P never follow symbolic links (this is the default), except in the case of the '-xtype' predicate.
-L always follow symbolic links, except in the case of the '-xtype' predicate.
-H follow symbolic links specified in the list of files to search, or which are otherwise specified on the command line
-amin n file was last accessed n minutes ago
-atime n file was last accessed n*24 hours ago
-cmin n file's status was last changed n minutes ago
-ctime n file's status was last changed n*24 hours ago
-empty file is empty and is either a regular file or a directory
-executable matches files which are executable and directories which are searchable (in a file name resolution sense) by the current user
-iname pattern Like -name, but the match is case insensitive
-ipath pattern like -path. but the match is case insensitive
-iregex pattern like -regex, but the match is case insensitive
-mmin n file's data was last modified n minutes ago
-mtime n file's data was last modified n*24 hours ago
-name pattern search file by name that matches pattern
-path pattern file name matches shell pattern pattern. The meta characters do not treat `/' or `.' specially; so, for example, find . -path "./sr*sc"
-perm mode file's permission bits are exactly mode (octal or symbolic). Since an exact match is required, if you want to use this form for symbolic modes, you may have to specify a rather complex mode string. For example `-perm g=w' will only match files which have mode 0020 (that is, ones for which group write per‐ mission is the only permission set).
-perm -mode all of the permission bits mode are set for the file. Symbolic modes are accepted in this form, and this is usually the way in which you would want to use them. You must specify `u', `g' or `o' if you use a symbolic mode. See the EXAMPLES section for some illustrative examples.
-perm /mode any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify `u', `g' or `o' if you use a symbolic mode. See the EXAMPLES section for some illustrative examples. If no permission bits in mode are set, this test matches any file (the idea here is to be consistent with the behavior of -perm -000).
-perm +mode This is no longer supported (and has been deprecated since 2005). Use -perm /mode instead.
-readable matches files which are readable by the current user
-regex pattern file name matches regular expression pattern
-size n[cwbkMG] File uses n units of space, rounding up. 'c' for bytes, 'w' for two-byte words, 'b' for 512-byte blocks,...
-type c find file type of c and c can be following d for directory, f for regular file, s for socket

following are some examples for find command.

  • find . -name "*.js" : searches for files having js extension in it's name in current directory(.)
  • find . -iname "*.js" : searches for files having case insensitive js extension in it's name in current directory(.)
  • find .. -iname "*.js" : searches for files having case insensitive js extension in it's name in parent(..) directory.
  • find /home/ubuntu/Desktop -type d -name "s*" : find all directories whose name start with s character on /home/ubuntu/Desktop location.
  • find . -type f -name "*b*.txt" : find all files whose name start with or contain character b and end with word .txt
  • find . -amin -60 : find all files which are accessed in last 1 hour
  • find . -cmin -60 : find all files which are changed in last 1 hour
  • find . -mmin -60 : find all files which are modified in last 1 hour
  • find . -mtime 2 : find all files which are modified 2 days back.
  • find . -atime 2 : find all files which are accessed 2 days back
  • find . -type f -empty : find all empty files.
  • find . -type d -empty : find all empty directories.
  • find . -size 4k : find filed or directories having size approximately 4kb.
  • find . -size +4k : find filed or directories having size more than 4kb.
  • find . -size -4k : find filed or directories having size less than 4kb.