{"id":487,"date":"2009-02-17T11:34:52","date_gmt":"2009-02-17T16:34:52","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=487"},"modified":"2013-07-24T12:43:34","modified_gmt":"2013-07-24T17:43:34","slug":"shell-script","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=487","title":{"rendered":"Unix Shell Script"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#IO\">IO<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Standard_IOs\">Standard IOs<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#IO_Redirects\">IO Redirects<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Loop_Structures\">Loop Structures<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#For_Loop\">For Loop<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#While_Loop\">While Loop<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Condition\">Condition<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#If_Condition\">If Condition<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Logical_and\">Logical <strong>and<\/strong><\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Logical_or\">Logical <strong>or<\/strong><\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Test_File_Operators\">Test File Operators<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Test_Integer_Operators\">Test Integer Operators<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#String_Comparison\">String Comparison<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Boolean_Operators\">Boolean Operators<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Variables\">Variables<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Naming\">Naming<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Declare\">Declare<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Assign\">Assign<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Access\">Access<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#De-referencing\">De-referencing<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Arithmetic_on_Shell_Variables\">Arithmetic on Shell Variables<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Built-in_Variables\">Built-in Variables<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Built-in_Shell_Parameters\">Built-in Shell Parameters<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Special_characters\">Special characters<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Backslash\">Backslash<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Quotes\">Quotes<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Single_Quotes\">Single Quotes<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Double_Quotes\">Double Quotes<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Backquotes\">Backquotes<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Function\">Function<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#read_command\">read command<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#basename_and_dirname\">basename and dirname<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#trap\">trap<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#cut\">cut<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#tr\">tr<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Inverse_Text\">Inverse Text<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Regular_expression\">Regular expression<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#GNU_find\">GNU find<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Syntax\">Syntax<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Examples\">Examples<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"IO\"><h2>IO<\/h2><\/span>\n<span id=\"Standard_IOs\"><h3>Standard IOs<\/h3><\/span>\n<p>0\tstdin<br \/>\n1\tstdout<br \/>\n2\tstderr<\/p>\n<span id=\"IO_Redirects\"><h3>IO Redirects<\/h3><\/span>\n<pre lang=\"bash\">\r\n# Redirect stdout to a file:\r\n1>filename\r\necho \"test\" 1>stdout.txt\r\n\r\n# Redirect stdout and append to a file:\r\n1>>filename\r\n\r\n# Redirect stderr to a file:\r\n2>filename\r\n\r\n# Redirect stderr and append to a file:\r\n2>>filename\r\n\r\n# Redirect both stdout and stderr to a file:\r\n&>filename\r\n\r\n# Send stderr to stdout\r\n2>&1\t\r\n\r\n# Redirect file descriptor i to j\r\ni>&j\r\n\r\n# Redirect i to j\r\n>&j = 1>&j\t\r\n\r\n# Close stdin\r\n<&#038;-\r\n\r\n# Close stdout\r\n>&-\r\n\r\n# Read stdin from file\r\n0<filename\r\n<filename\tgrep \"test\" < test.txt\r\n<\/pre>\n<span id=\"Loop_Structures\"><h2>Loop Structures<\/h2><\/span>\n<span id=\"For_Loop\"><h3>For Loop<\/h3><\/span>\n<pre lang=\"bash\">\r\nfor i in 'ls | grep jar'\r\ndo\r\n\techo \"$i\"\r\ndone\r\n<\/pre>\n<span id=\"While_Loop\"><h3>While Loop<\/h3><\/span>\n<pre lang=\"bash\">\r\ni=1\r\nwhile [ \"$i\" -le 5 ]\r\ndo\r\n\techo \"$i\"\r\n\ti='expr $i + 1'\r\ndone\r\n<\/pre>\n<span id=\"Condition\"><h2>Condition<\/h2><\/span>\n<span id=\"If_Condition\"><h3>If Condition<\/h3><\/span>\n<pre lang=\"bash\">\r\nif [ ]\r\nthen\r\n  # do something\r\nelif [ ]\r\nthen\r\n  # do something\r\nelse\r\n  # do something\r\nfi\r\n<\/pre>\n<span id=\"Logical_and\"><h3>Logical <strong>and<\/strong><\/h3><\/span>\n<p>* If cmd1 succeeds go to cmd2<br \/>\ncmd1 && cmd2<\/p>\n<span id=\"Logical_or\"><h3>Logical <strong>or<\/strong><\/h3><\/span>\n<p>* If cmd1 fails then go to cmd2<br \/>\ncmd1 || cmd2<\/p>\n<span id=\"Test_File_Operators\"><h3>Test File Operators<\/h3><\/span>\n<p>-b FILE\t\t# FILE exists and is block special<br \/>\n-c FILE\t\t# FILE exists and is character special<br \/>\n-d FILE\t\t# FILE exists and is a directory<br \/>\n-e FILE\t\t# FILE exists<br \/>\n-f FILE\t\t# FILE exists and is a regular file<br \/>\n-g FILE\t\t# FILE exists and is set-group-ID<br \/>\n-G FILE\t\t# FILE exists and is owned by the effective group ID<br \/>\n-h FILE\t\t# FILE exists and is a symbolic link (same as -L)<br \/>\n-k FILE\t\t# FILE exists and has its sticky bit set<br \/>\n-L FILE\t\t# FILE exists and is a symbolic link (same as -h)<br \/>\n-O FILE\t\t# FILE exists and is owned by the effective user ID<br \/>\n-p FILE\t\t# FILE exists and is a named pipe<br \/>\n-r FILE\t\t# FILE exists and read permission is granted<br \/>\n-s FILE\t\t# FILE exists and has a size greater than zero<br \/>\n-S FILE\t\t# FILE exists and is a socket<br \/>\n-t FD  \t\t# File descriptor FD is opened on a terminal<br \/>\n-u FILE\t\t# FILE exists and its set-user-ID bit is set<br \/>\n-w FILE\t\t# FILE exists and write permission is granted<br \/>\n-x FILE\t\t# FILE exists and execute (or search) permission is granted<\/p>\n<span id=\"Test_Integer_Operators\"><h3>Test Integer Operators<\/h3><\/span>\n<pre lang=\"bash\">\r\n-eq\r\n-ne\r\n-gt\r\n-ge\r\n-lt\r\n-le\r\n<\/pre>\n<span id=\"String_Comparison\"><h3>String Comparison<\/h3><\/span>\n<pre lang=\"bash\">\r\n[ s1 = s2 ]  # s1 equals to s2\r\n[ s1 == s2 ] # synonym for =\r\n[ s1 != s2 ] # s1 not equal to s2\r\n[ s ]        # is not null\r\n[ -n s ]     # s is not null\r\n[ -z s ]     # s is null\r\n<\/pre>\n<span id=\"Boolean_Operators\"><h3>Boolean Operators<\/h3><\/span>\n<p>!\t\tnot<br \/>\n-a\t\tand<br \/>\n-o\t\tor<\/p>\n<span id=\"Variables\"><h2>Variables<\/h2><\/span>\n<span id=\"Naming\"><h3>Naming<\/h3><\/span>\n<p>* Begins with an alphabetic or underscore character<br \/>\n* Followed by zero or more alphanumeric or underscore characters<br \/>\n* No spaces<\/p>\n<span id=\"Declare\"><h3>Declare<\/h3><\/span>\n<p>* No data type concept<br \/>\n* No need to declare<\/p>\n<span id=\"Assign\"><h3>Assign<\/h3><\/span>\n<p>* var=val    # Notice <strong>no spaces<\/strong> before or after equal sign<\/p>\n<span id=\"Access\"><h3>Access<\/h3><\/span>\n<p>* $var<br \/>\n* ${var}<\/p>\n<span id=\"De-referencing\"><h3>De-referencing<\/h3><\/span>\n<p>* Use <em>eval derefVar=\\$$var<\/em> to do de-referencing.<br \/>\n* After de-referencing, <em>derefVar<\/em> will contain value referenced by <em>$var<\/em>.<\/p>\n<pre lang=\"bash\">\r\n#!\/usr\/bin\/sh\r\nvar1=VariableOne\r\necho \"\\$var1=$var1\"\r\n\r\nvar2=var1\r\necho\r\necho \"Before dereferencing...\"\r\necho \"\\$var2=$var2\"\r\n\r\necho\r\necho \"After dereferencing...\"\r\neval derefVar2=\\$$var2\r\necho \"\\$derefVar2=$derefVar2\"\r\n\r\n# Result:\r\n$var1=VariableOne\r\n\r\nBefore dereferencing...\r\n$var2=var1\r\n\r\nAfter dereferencing...\r\n$derefVar2=VariableOne\r\n<\/pre>\n<span id=\"Arithmetic_on_Shell_Variables\"><h3>Arithmetic on Shell Variables<\/h3><\/span>\n<p>* Operators: <strong>+ - * \/ %<\/strong><br \/>\n* <strong>Need spaces<\/strong> around input numbers.<br \/>\n* Need to escape <strong>*<\/strong> character.<br \/>\n* Examples:<\/p>\n<pre>\r\nexpr 1 + 2\r\n3\r\n\r\n# If no spaces around input numbers:\r\nexpr 1+2\r\n1+2\r\n\r\n# Need to escape *\r\n$ expr 2 \\* 2\r\n4\r\n\r\n# Otherwise\r\n$ expr 2 * 2\r\nexpr: syntax error\r\n\r\n# Assign result\r\n$ i=1\r\n$ i=`expr $i + 1`    # Notice the back quotes\r\n$ echo $i\r\n2\r\n<\/pre>\n<span id=\"Built-in_Variables\"><h3>Built-in Variables<\/h3><\/span>\n<p>$0: name of calling prog<br \/>\n$1 $2...: each individual argument<br \/>\n$*: all arguments<br \/>\n$@: a list of separate arguments<br \/>\n$$: pid of the process being exceed\t# Good for temp file name<br \/>\n$?: exit status<br \/>\n$#: count of args<\/p>\n<span id=\"Built-in_Shell_Parameters\"><h3>Built-in Shell Parameters<\/h3><\/span>\n<p>IFS=\":\" Internal field separater<br \/>\nHOME<br \/>\nMAIL<br \/>\nMAILCHECK<br \/>\nPS1<br \/>\nPS2<br \/>\nSHELL<\/p>\n<span id=\"Special_characters\"><h3>Special characters<\/h3><\/span>\n<pre>\\010  Backspace\r\n\\011  Tab\r\n\\012  New line character\r\n\\013  Line feed\r\n\\014  Form feed\r\n\\015  CR character\r\n\\033  Escape\r\n<\/pre>\n<span id=\"Backslash\"><h3>Backslash<\/h3><\/span>\n<p>* As an escape character:<\/p>\n<pre>\r\n$ echo \\\\\r\n\\\r\n$ echo \\$x\r\n$x\r\n<\/pre>\n<p>* For continuing lines when used as last character on the line:<\/p>\n<pre>\r\n$ lines=one'     # Single quotes tell shell to ignore newline\r\n> 'two\r\n$ echo $lines\r\none two\r\n\r\n$ lines=one\\\r\n> two\r\n$ echo $lines\r\nonetwo\r\n<\/pre>\n<span id=\"Quotes\"><h2>Quotes<\/h2><\/span>\n<span id=\"Single_Quotes\"><h3>Single Quotes<\/h3><\/span>\n<p>* Ignore all quoted characters, for example:<\/p>\n<pre>\r\n$ echo '<> | \/ \\ ` $'\r\n<> | \/ \\ ` $\r\n<\/pre>\n<span id=\"Double_Quotes\"><h3>Double Quotes<\/h3><\/span>\n<p>* ignore most enclosed characters <strong>except<\/strong> $, \\ (backslash), ` (back quotes).<br \/>\n* Examples:<\/p>\n<pre>\r\n$ name=jim\r\n\r\n$ echo \"name: ${name}\"\r\nname: jim\r\n\r\n$ echo \"name: \\${name}\"\r\nname: ${name}\r\n\r\n$ echo `date`\r\nThu May 28 12:34:58 EDT 2009\r\n<\/pre>\n<span id=\"Backquotes\"><h3>Backquotes<\/h3><\/span>\n<p>* Execute command:<\/p>\n<pre>$ echo `date`\r\nThu May 28 12:34:58 EDT 2009\r\n<\/pre>\n<p>* Result reformatted into one line<\/p>\n<span id=\"Function\"><h2>Function<\/h2><\/span>\n<p>* Example<\/p>\n<pre lang=\"bash\">\r\n#!\/usr\/bin\/bash\r\n\r\nfxn1 () {\r\n  if [ $# -ne 1 ]; then\r\n    echo \"Please provide one argument.\"\r\n    exit 1\r\n  else\r\n    echo \"Got argument via \\$1: $1\"\r\n  fi\r\n\r\n  if [ $1 = \"ok\" ]; then\r\n    echo \"Got ok\"\r\n    return 0\r\n  else\r\n    echo \"Not ok\"\r\n    return 1\r\n  fi\r\n}\r\n\r\necho\r\necho \"Calling fxn1 with argument...\"\r\nfxn1 ok\r\necho \"\\$?: $?\"\r\necho\r\necho \"Calling fxn1 with argument...\"\r\nfxn1 notok\r\necho \"\\$?: $?\"\r\necho\r\necho \"Calling fxn1 without argument...\"\r\nfxn1\r\n<\/pre>\n<p>* Result<\/p>\n<pre lang=\"bash\">\r\n\r\nCalling fxn1 with argument...\r\nGot argument via $1: ok\r\nGot ok\r\n$?: 0\r\n\r\nCalling fxn1 with argument...\r\nGot argument via $1: notok\r\nNot ok\r\n$?: 1\r\n\r\nCalling fxn1 without argument...\r\nPlease provide one argument.\r\n<\/pre>\n<span id=\"read_command\"><h3>read command<\/h3><\/span>\n<pre lang=\"bash\">\r\n#!\/bin\/sh\r\necho \"delete...?\"\r\nread ok\r\nif [ \"$ok\" = \"y\" ]; then\r\n\techo \"file name?\"\r\n\tread file_to_del\r\n\techo \"deleting...\"\r\n\trm $file_to_del\r\n\techo \"done.\"\r\nelse\r\n\techo \"none deleted\"\r\n\texit 1\r\nfi\r\n<\/pre>\n<span id=\"basename_and_dirname\"><h3>basename and dirname<\/h3><\/span>\n<p>* <em>basename \/this\/is\/a\/dir\/file<\/em> gives <em>file<\/em><br \/>\n* <em>dirname \/this\/is\/a\/dir\/file<\/em> gives <em>\/this\/is\/a\/dir<\/em><\/p>\n<span id=\"trap\"><h3>trap<\/h3><\/span>\n<p>trap \"<command>\" 15<br \/>\n0\tExit from shell<br \/>\n1\tHangup<br \/>\n2\tInterrupt<br \/>\n15\tSoftware termination signal<\/p>\n<span id=\"cut\"><h3>cut<\/h3><\/span>\n<p>* used for column cutting.<\/p>\n<pre lang=\"bash\">\r\ncut -cchars file\r\nwhere chars specifies what characters to extract \r\nfrom each line of file.\r\n\r\ncommand                    extracts\r\nwho | cut -c5              character 5\r\nwho | cut -c1,13,50        characters 1, 13 and 50\r\nwho | cut -c20-50          characters 20 throught 50\r\nwho | cut -c5-             characters 5 to the end of line \r\n\r\ncut \r\n<\/pre>\n<span id=\"tr\"><h3>tr<\/h3><\/span>\n<p>* Translate or replace characters<br \/>\n* Example, replace underscore with space character:<\/p>\n<pre lang=\"bash\">\r\nstr=\"str1_str2\"; echo $str|tr '_' ' '\r\nstr1 str2\r\n<\/pre>\n<span id=\"Inverse_Text\"><h3>Inverse Text<\/h3><\/span>\n<pre lang=\"bash\">\r\n${smso}text${rmso}\r\n<\/pre>\n<span id=\"Regular_expression\"><h2>Regular expression<\/h2><\/span>\n<pre>\r\nNotation    Meaning                  Example        Matches\r\n.           any character            a..            a followed by any two characters\r\n\r\n^           beginning of line        ^hello         hello only if it appears at the beginning of the line\r\n\r\n$           end of line              friend$        friend only if it appears at the end of the line\r\n\r\n*           zero or more             x*             zero or more x's\r\n            occurrence of            xx*            one or more x's\r\n            previous reg exp         .*             zero or more characters\r\n\r\n[chars]     any char in chars        [tT]           lower or upper case t\r\n                                     [a-z]          lower case letter\r\n                                     [a-zA-Z]       lower or upper case letter\r\n\r\n[^chars]    any char                 [^0-9]         any non-numeric char \r\n            NOT in chars             [^a-zA-Z]      any non-alphabetic char\r\n\r\n\\{min,max\\} at least min and         x\\{1,5\\}       at least 1, at most 5 x's\r\n            at most max occurrences  [0-9]\\{3,9\\}   3 to 9 consecutive digits\r\n            of previous reg exps     [0-9]\\{3\\}     exactly 3 digits\r\n                                     [0-9]\\{3,\\}    at least 3 digits\r\n\r\n\\(...\\)     store char matched       ^\\(.\\)         first char in line and stores it in register 1\r\n            between parentheses      ^\\(.\\)\\1       first two characters in line if they're the same\r\n            in next register (1-9).                 \r\n            retrieve register with\r\n            \\n. e.g. \\1 retrieves\r\n            values stored in first\r\n            register\r\n<\/pre>\n<span id=\"GNU_find\"><h2>GNU find<\/h2><\/span>\n<span id=\"Syntax\"><h3>Syntax<\/h3><\/span>\n<p>find \\<br \/>\n  [-P] \\ # Never follow symbolic link. This is default.<br \/>\n  [-L] \\ # Follow symbolic link.<br \/>\n  [-H] \\ # Do not follow symbolic link except when processing command line arguments.<br \/>\n  [path...] \\<\/p>\n<p>  -daystart \\ # Measure times from the beginning of the day instead of default 24 hrs ago.<br \/>\n  -depth \\ # Process directory contents first<br \/>\n  -d \\ # same as -depth<br \/>\n  -help \\<br \/>\n  -maxdepth \\ # Descent at most levels. -maxdepth 0 mean current dir<br \/>\n  -mindepth \\ # Start find only at designated levels. -mindepth 1 excludes command line dir.<br \/>\n  -mount \\ # Do not descent directories in other file systems<br \/>\n  -noleaf \\ # Do not assume Unix filesystem to accommodate CDROM etc<br \/>\n  -regextype type \\ # Default to emac<br \/>\n  -version \\<br \/>\n  -warn, -nowarn \\ # Turn warning on and off<br \/>\n  -xdev \\ # Same as -mount<\/p>\n<p>  # +n: greater than, -n: less than, n: exactly n<br \/>\n  -amin n \\ # File last accessed n minutes ago<br \/>\n  -anewer file \\<br \/>\n  -atime n \\ # Access n*24 hours ago<br \/>\n  -cmin n \\ # Last changed n minutes ago<br \/>\n  -cnewer file \\<br \/>\n  -ctime n \\<br \/>\n  -empty \\ # File is empty<br \/>\n  -fstype type \\<br \/>\n  -gid n \\ # File numeric group id is n<br \/>\n  -group gname \\ # File belongs to gname group name<br \/>\n  -ilname \\ # Like -lname but case insensitive<br \/>\n  -iname \\ # Like -name but case insensitive<br \/>\n  -iregex \\ # Like -regex but case insensitive<br \/>\n  -links n \\ # File has n links<br \/>\n  -lname pattern \\ # Symbolic links matching pattern<br \/>\n  -mmin n \\ # Last modified n minutes ago<br \/>\n  -mtime n \\ # Last modified n*24 minutes ago<br \/>\n  -name pattern \\<br \/>\n  -newer file \\ # Last modified newer than file<br \/>\n  -nouser \\<br \/>\n  -nogroup \\<br \/>\n  -path pattern \\<br \/>\n  -perm mode \\<br \/>\n  -perm -mode \\<br \/>\n  -regex pattern \\<br \/>\n  -samefile name \\<br \/>\n  -size n[cwbkMG] \\<br \/>\n  -type [bcdpflsD] \\ # Type of byte, char, dir, pipe, file, link,socket,door (Solaris)<br \/>\n  -uid n \\ # Owned by uid of n<br \/>\n  -used n \\<br \/>\n  -user uname \\ # Owned by uname<br \/>\n  -wholename pattern \\ # Matches shell pattern of pattern<br \/>\n  -xtype<\/p>\n<span id=\"Examples\"><h3>Examples<\/h3><\/span>\n<pre lang=\"bash\">\r\nfind .\/ FTP.LOG -exec rm {} \\;\r\n\r\nfind \/ -size +1000000000c;\r\n(look for files larger than 1gb)\r\n\r\n# Find jpg files\r\nfind . -type f -name \"*.jpg\" -print\r\n\r\n# Find files larger than 10KB and list their details\r\nfind . -type f -size +10000 -exec ls -al {} \\;\r\n\r\n# Find files accessed two days ago and list their details\r\nfind . -atime +2 -type f -exec ls -al {} \\;\r\n\r\n# Find files modified 90 days ago and list their details\r\nfind . -mtime +90 -type f -exec ls -al {} \\;\r\n\r\n# Find files modified about 10 minutes ago and list their details\r\n  # Find current date time\r\n  >date\r\n  Tuesday, July 13, 2010 02:22:59 PM GMT\r\n\r\n  # Create an empty anchor file with time stamp set to 10 min ago\r\n  >touch -t 201007131412 \/tmp\/mytemptime.tmp \r\n\r\n  >ls -l \/tmp\/mytemptime.tmp\r\n  -rw-r--r--   1 oracle   dba  0 Jul 13 14:12 \/tmp\/mytemptime.tmp\r\n\r\n  # Find all files newer than the anchor file\r\n  >find . -type f -newer \/tmp\/mytemptime.tmp -exec ls -l {} \\;\r\n  -rw-r-----   1 oracle   dba  97013 Jul 13 14:25 .\/Apache\/Apache\/logs\/access_log.1279022400\r\n\r\n# Find \"admin\" string in all properties files\r\nfind . -type f -name \"*.properties\" -exec grep -i \"admin\" {} \\; \r\n\r\n# Find \"admin\" string in all properties files, also list file name\r\nfind . -type f -name \"*.properties\" -print -exec grep -i \"admin\" {} \\; \r\n\r\n# Find all *non* zip files\r\nfind . -type f \\! -name \"*.zip\" -print\r\n\r\n# Find *non* zip and *non* txt files\r\nfind . -type f \\! -name \"*.zip\" \\! -name \"*.txt\" -print\r\n\r\n# Find files owned by jboss\r\nfind -user jboss\r\n\r\n# Find files accessed two days ago from today's day start time i.e. 0:00am\r\nfind . -atime +2 -daystart\r\n\r\n# Find properties files in the next two levels of subdirectories.\r\nfind . -name \"*.properties\" -depth -print -maxdepth 2\r\n\r\n# Find empty files\r\nfind . -empty -type f\r\n\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"?p=65\">More Bash Examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IO Standard IOs 0 stdin 1 stdout 2 stderr IO Redirects # Redirect stdout to a file: 1>filename echo &#8220;test&#8221; 1>stdout.txt # Redirect stdout and append to a file: 1>>filename # Redirect stderr to a file: 2>filename # Redirect stderr &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=487\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[65,33],"tags":[],"class_list":["post-487","post","type-post","status-publish","format-standard","hentry","category-shell","category-unix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-7R","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=487"}],"version-history":[{"count":61,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":7889,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/487\/revisions\/7889"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}