{"id":351,"date":"2010-01-20T16:31:34","date_gmt":"2010-01-20T21:31:34","guid":{"rendered":"http:\/\/nylinuxhelp.com\/blogs\/?p=351"},"modified":"2010-01-20T16:31:34","modified_gmt":"2010-01-20T21:31:34","slug":"the-linux-find-command","status":"publish","type":"post","link":"https:\/\/nylinuxhelp.com\/blogs\/command-line\/the-linux-find-command","title":{"rendered":"The linux find command"},"content":{"rendered":"<h2>Using the find command to find and move files<\/h2>\n<p>Today I&#8217;d like to discuss one of my favorite shell commands: <strong>find<\/strong>.<br \/>\nfind is awesome.\u00a0 It locates files on your unix\/linux system by using options and criteria.<\/p>\n<p>Many of us are familiar with using the Microsoft Windows &#8220;Search Tool&#8221; in XP to Find Files or Folders, files by date, etc.\u00a0 Finding a file this way involves populating text fields, drop-down menus, and (possibly) date-range selectors to find the files you&#8217;re looking for.<\/p>\n<p>In unix and linux, the find command is used like this:<\/p>\n<pre><span style=\"color: #993300;\">find [directory to search] [options] [actions]<\/span><\/pre>\n<p>Here&#8217;s a <strong>find command example<\/strong>.\u00a0 Let&#8217;s say we want to find .<strong>mp3<\/strong> files within your &#8220;Music&#8221; folder (\/home\/yourName\/Music).\u00a0 For this example, let&#8217;s pretend that you have the following files:<em> song1.mp3, song1.wav, song2.mp3, song2.wav, song3.mp3, and song3.wav<\/em>.<\/p>\n<p>To find files in the Music directory, the command is: [don&#8217;t type the dollar sign- it&#8217;s the prompt, start typing at the word &#8220;find&#8221;.]<\/p>\n<pre><span style=\"color: #993300;\">$ find Music\/ -type f [press ENTER]<\/span>\r\nMusic\/song1.mp3\r\nMusic\/song1.wav\r\nMusic\/song2.mp3\r\nMusic\/song2.wav\r\nMusic\/song3.mp3\r\nMusic\/song3.wav<\/pre>\n<p>We used the &#8220;<span style=\"color: #993300;\">-type f<\/span>&#8221; option to find a regular file and not a directory.\u00a0 The output (above) shows that we have media files (mp3 and wav) in the Music folder.\u00a0 Remember we&#8217;re not interested in <em>listing<\/em> files so much as performing a &#8216;find&#8217; on them.<\/p>\n<p>On a Linux system you can use a GUI program to find files, but you can <strong>find them faster using the find command<\/strong>.\u00a0 With its many options, find&#8217;s power is in its flexibility.\u00a0 <em>Flexibility to do what??? Finding files is finding files, right?<\/em> Yes, I suppose so.<\/p>\n<p>But what if you wanted to <strong>perform an action on files that you find?<\/strong> In Windows you&#8217;d have to first find them, navigate to them, and then (possibly) perform actions on them one file at a time.\u00a0 While the steps are few, they can be manual and tedious, thus begging for a shortcut.<\/p>\n<p>Let&#8217;s take our example a step further.\u00a0 Let&#8217;s filter the command to only find &#8216;mp3&#8217; files.\u00a0 We&#8217;ll add the &#8220;<span style=\"color: #993300;\">-name<\/span>&#8221; option to search wildcard patterns for files ending in mp3.<\/p>\n<pre><span style=\"color: #993300;\">$ find Music\/ -type f -name \"*mp3\"<\/span>\r\nMusic\/song1.mp3\r\nMusic\/song2.mp3\r\nMusic\/song3.mp3<\/pre>\n<p>If you wanted to find only the .wav files, then you&#8217;d change *mp3 to *wav.\u00a0 Now let&#8217;s <strong>use find to<\/strong> <strong>move files to another folder<\/strong>.\u00a0 For the sake of neatness, let&#8217;s create 2 sub-folders (also known as sub-directories) to hold mp3 files and a separate sub-folder for wav files.<\/p>\n<p>To create a new directory use <span style=\"color: #993300;\"><strong>mkdir [folderPath]<\/strong><\/span>.\u00a0 If you do not specify a folder path, the shell creates the directory within the current working folder.\u00a0 More on &#8220;current working folder&#8221; later.\u00a0 For now, let&#8217;s assume that you&#8217;re in the &#8220;home\/yourName&#8221; folder.<\/p>\n<p>Creating folders &#8220;Music\/mp3&#8221; and &#8220;Music\/wav&#8221; requires 2 identical commands.\u00a0 However, you can create 2 sub-directories in the &#8220;Music&#8221; folder with the command below.<\/p>\n<pre><span style=\"color: #993300;\">mkdir Music\/{mp3,wav}<\/span><\/pre>\n<p><span style=\"color: #993300;\">B<\/span><span style=\"color: #993300;\">rackets<\/span> tell the shell to expect a sequence of characters.\u00a0<span style=\"color: #993300;\"> The comma separates<\/span> each new directory.\u00a0 This is the same as issuing <span style=\"color: #993300;\">mkdir Music\/mp3<\/span> and <span style=\"color: #993300;\">mkdir Music\/wav<\/span>.<\/p>\n<p>With the sub-folders created, we have a neater storage arrangement for our music files.\u00a0 But we&#8217;re not done yet.\u00a0 Remember that our &#8220;Music&#8221; folder currently stores both mp3 and wav files.\u00a0 Let&#8217;s move each type into their respective sub-directory.\u00a0 To make this happen we <strong>add an action<\/strong> onto our command known as &#8220;<span style=\"color: #993300;\">-exec<\/span>&#8221; (followed by the &#8220;command terminator&#8221; <span style=\"color: #993300;\">\\;<\/span>) and the <span style=\"color: #993300;\"><strong>mv<\/strong><\/span> command to move the files as seen in the example below:<\/p>\n<pre><span style=\"color: #993300;\">mv fileOldLocation fileNewLocation<\/span><\/pre>\n<p>Note: mv also renames files and can overwrite them\u2014<strong>so use caution because<\/strong> <a title=\"Use the mv command with caution. Its actions cannot be undone.\" href=\"http:\/\/www.linuxquestions.org\/questions\/linux-newbie-8\/how-do-undo-mv-or-rm-command-236834\/\" target=\"_blank\">mv actions cannot be undone<\/a>.\u00a0 For safety, use <span style=\"color: #993300;\"><strong>mv -i<\/strong><\/span> which is interactive in the event of a possible naming conflict or unintentional overwrite\u2014it may save you anguish.<\/p>\n<p>We then add brackets <span style=\"color: #993300;\">{}<\/span>, BUT brackets behave differently in this context.\u00a0 When brackets are part of <span style=\"color: #993300;\">-exec<\/span> they represent each found file.\u00a0 Also, when moving a file to another directory, <strong>put the trailing slash \/<\/strong> on the target directory name.<\/p>\n<p>To move the mp3 files from directory level &#8220;Music&#8221; to &#8220;Music\/mp3&#8221; issue this command:<\/p>\n<pre><span style=\"color: #993300;\">$ find Music\/ -type f -name \"*mp3\" -exec mv -i {} Music\/mp3\/ \\;<\/span><\/pre>\n<p>Since there weren&#8217;t any files in Music\/mp3 we didn&#8217;t get any warnings from mv -i<br \/>\nResult: mp3 files moved from \/home\/yourName\/Music to \/home\/yourName\/Music\/mp3\/<\/p>\n<p>To check it, you can use the ls command in the shell to &#8220;list&#8221; files in a given directory<\/p>\n<pre><span style=\"color: #993300;\">$ ls Music\/mp3\/<\/span>\r\nsong1.mp3\u00a0 song2.mp3\u00a0 song3.mp3<\/pre>\n<p>To move the wav files from directory level &#8220;Music&#8221; to &#8220;Music\/wav&#8221; issue this command:<\/p>\n<pre><span style=\"color: #993300;\">$ find Music\/ -type f -name \"*wav\" -exec mv -i {} Music\/wav\/ \\;<\/span><\/pre>\n<pre><span style=\"color: #993300;\">$ ls Music\/wav\/<\/span>\r\n song1.wav\u00a0 song2.wav\u00a0 song3.wav<\/pre>\n<h2>Summary<\/h2>\n<ul>\n<li> <strong>find <\/strong>(and its options) allow you to find files and perform actions on the found items<\/li>\n<li><strong>mkdir<\/strong> allows you to create new directories<\/li>\n<li><strong>ls <\/strong>lists files (and directories, subdirectories) within folder(s)<\/li>\n<li><strong>mv <\/strong>moves or renames files<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Using the find command to find and move files Today I&#8217;d like to discuss one of my favorite shell commands: find. find is awesome.\u00a0 It locates files on your unix\/linux system by using options and criteria. Many of us are familiar with using the Microsoft Windows &#8220;Search Tool&#8221; in XP to Find Files or Folders, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,23],"tags":[26,35,37,36,38],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-command-line","category-use-linux","tag-commands","tag-find","tag-ls","tag-mkdir","tag-mv"],"_links":{"self":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":33,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/posts\/351\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nylinuxhelp.com\/blogs\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}