<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title> - Unix</title>
	<link href="https://www.fullstackstanley.com/tags/unix/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="https://www.fullstackstanley.com"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2015-01-30T00:00:00+00:00</updated>
	<id>https://www.fullstackstanley.com/tags/unix/atom.xml</id>
	<entry xml:lang="en">
		<title>Unix commands: Find</title>
		<published>2015-01-30T00:00:00+00:00</published>
		<updated>2015-01-30T00:00:00+00:00</updated>
		<link href="https://www.fullstackstanley.com/articles/unix-commands-find/" type="text/html"/>
		<id>https://www.fullstackstanley.com/articles/unix-commands-find/</id>
		<content type="html">&lt;p&gt;After watching one of Gary Bernhardt’s Destroy All Software screencasts I thought I’d take some of his advice and learn about some basic Unix commands.  I’m going to start with &lt;code&gt;find&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This post will be a small collection of useful commands I’ve found.&lt;&#x2F;p&gt;
&lt;span id=&quot;continue-reading&quot;&gt;&lt;&#x2F;span&gt;&lt;h2 id=&quot;basic-directory-searching&quot;&gt;Basic directory searching&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;find .&lt;&#x2F;code&gt; - Searching the current directory recursively.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find ..&lt;&#x2F;code&gt; - Search from the parent directory.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find $HOME&lt;&#x2F;code&gt; or &lt;code&gt;find ~&lt;&#x2F;code&gt; - Search your user directory&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find $HOME&#x2F;projects&#x2F;*.js&lt;&#x2F;code&gt; - Search for directories and files in your projects directory that end with &lt;code&gt;.js&lt;&#x2F;code&gt; - Not recursive.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find $HOME&#x2F;projects -name *.js&lt;&#x2F;code&gt; Search for .js files in your projects directory - recursive.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;print-types&quot;&gt;Print types&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;find . -print0&lt;&#x2F;code&gt; - Print the full filename followed by a null character instead of a newline. Best used with files that have multiline filenames.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -printf&lt;&#x2F;code&gt; - for formatting output.&lt;&#x2F;p&gt;
&lt;p&gt;Example: Showing filename and access&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -name *.js -printf &amp;quot;%p %M\n”&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can use numbers between %&lt;char&gt; (directives) to align text evenly. &lt;code&gt;%100p&lt;&#x2F;code&gt; will right align the filename for 100 characters. &lt;code&gt;%-100p&lt;&#x2F;code&gt; will left align for the same.&lt;&#x2F;p&gt;
&lt;p&gt;Note that this doesn’t work natively on Macs. Instead, you have two options:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;pipe to xargs like so: &lt;code&gt;find . -name *.js -print0 | xargs -0 stat -f &amp;quot;%p %k KB”&lt;&#x2F;code&gt; (As found from &lt;a href=&quot;http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;752818&#x2F;why-does-macs-find-not-have-the-option-printf&quot;&gt;Stackoverflow&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Install Findutils. If you use Homebrew you can run &lt;code&gt;brew install findutils&lt;&#x2F;code&gt;. Note that to use find this way you need to run &lt;code&gt;gfind&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;fprint&quot;&gt;fprint&lt;&#x2F;h3&gt;
&lt;p&gt;Puts the results into a file. This also requires findutils if you’re on a Mac.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -name *.js -fprint all_files.txt&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;symbolic-links&quot;&gt;Symbolic links&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;find -P .&lt;&#x2F;code&gt; never follow symbolic links&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find -L .&lt;&#x2F;code&gt; follow symbolic links&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find -H .&lt;&#x2F;code&gt; don’t follow except when processing command line arguments&lt;&#x2F;p&gt;
&lt;h2 id=&quot;filter-by-time&quot;&gt;Filter by time&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;find . -mmin n&lt;&#x2F;code&gt; modified n minutes ago&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -mtime n&lt;&#x2F;code&gt; modified n*24 hours ago.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -newer .&#x2F;composer.json&lt;&#x2F;code&gt; find files modified after composer.json&lt;&#x2F;p&gt;
&lt;h2 id=&quot;other-filters&quot;&gt;Other filters&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;find . -size n[cwbkMG]&lt;&#x2F;code&gt; find files that use n units of space. where c = bytes, k = kilobytes, M = megabytes, G = gigabytes&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -maxdepth 1&lt;&#x2F;code&gt; - Only go one directory deep.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -mindepth 1&lt;&#x2F;code&gt; - Ignore the first directory&lt;&#x2F;p&gt;
&lt;h2 id=&quot;actions&quot;&gt;Actions&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;delete&quot;&gt;Delete&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;find . -delete&lt;&#x2F;code&gt; - delete files in directory&lt;&#x2F;p&gt;
&lt;p&gt;Example - deleting all .js files.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -name *.js -delete&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exec&quot;&gt;Exec&lt;&#x2F;h3&gt;
&lt;p&gt;Removing all .bak files:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;find . -name \*.bak -exec rm {} \;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The curly braces will get replaced with each filename that&#x27;s found.&lt;&#x2F;p&gt;
&lt;p&gt;As seen &lt;a href=&quot;http:&#x2F;&#x2F;nixcraft.com&#x2F;showthread.php&#x2F;16297-find-exec-bash-example&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;windows-alternative-to-find&quot;&gt;Windows alternative to find&lt;&#x2F;h2&gt;
&lt;p&gt;For powershell use &lt;code&gt;Find-ChildItem&lt;&#x2F;code&gt;. &lt;a href=&quot;http:&#x2F;&#x2F;superuser.com&#x2F;questions&#x2F;401495&#x2F;equivalent-of-unix-find-command-on-windows&quot;&gt;Read more here&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;useful-resources&quot;&gt;Useful resources&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;unixhelp.ed.ac.uk&#x2F;CGI&#x2F;man-cgi?find&quot;&gt;find documentation&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;printf&quot;&gt;printf documentation&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
</feed>
