{"id":2679,"date":"2018-11-06T14:54:18","date_gmt":"2018-11-06T14:54:18","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=2679"},"modified":"2018-11-07T14:06:54","modified_gmt":"2018-11-07T14:06:54","slug":"commandline-quick-tips-how-to-locate-a-file","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/06\/commandline-quick-tips-how-to-locate-a-file\/","title":{"rendered":"Commandline quick tips: How to locate a file"},"content":{"rendered":"<p>We all have files on our computers \u2014 documents, photos, source code, you name it. So many of them. Definitely more than I can remember. And if not challenging, it might be time consuming to find the right one you\u2019re looking for. In this post, we\u2019ll have a look at how to make sense of your files on the command line, and especially how to quickly find the ones you\u2019re looking for.<\/p>\n<p>Good news is there are few quite useful utilities in the Linux commandline designed specifically to look for files on your computer. We\u2019ll have a look at three of those: ls, tree, and find.<\/p>\n<h2>ls<\/h2>\n<p>If you know where your files are, and you just need to list them or see information about them, ls is here for you.<\/p>\n<p>Just running ls lists all visible files and directories in the current directory:<\/p>\n<p>$ ls<br \/>\nDocuments Music Pictures Videos notes.txt<\/p>\n<p>Adding the -l option shows basic information about the files. And together with the -h option you\u2019ll see file sizes in a human-readable format:<\/p>\n<p>$ ls -lh<br \/>\ntotal 60K<br \/>\ndrwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Documents<br \/>\ndrwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Music<br \/>\ndrwxr-xr-x 2 adam adam 4.0K Nov 2 13:13 Pictures<br \/>\ndrwxr-xr-x 2 adam adam 4.0K Nov 2 13:07 Videos<br \/>\n-rw-r&#8211;r&#8211; 1 adam adam 43K Nov 2 13:12 notes.txt<\/p>\n<p>Is can also search a specific place:<\/p>\n<p>$ ls Pictures\/<br \/>\ntrees.png wallpaper.png<\/p>\n<p>Or a specific file \u2014 even with just a part of the name:<\/p>\n<p>$ ls *.txt<br \/>\nnotes.txt<\/p>\n<p>Something missing? Looking for a hidden file? No problem, use the -a option:<\/p>\n<p>$ ls -a<br \/>\n. .bash_logout .bashrc Documents Pictures notes.txt<br \/>\n.. .bash_profile .vimrc Music Videos<\/p>\n<p>There are many other useful options for ls, and you can combine them together to achieve what you need. Learn about them by running:<\/p>\n<p>$ man ls<\/p>\n<h2>tree<\/h2>\n<p>If you want to see, well, a tree structure of your files, tree is a good choice. It\u2019s probably not installed by default which you can do yourself using the package manager DNF:<\/p>\n<p>$ sudo dnf install tree<\/p>\n<p>Running tree without any options or parameters shows the whole tree starting at the current directory. Just a warning, this output might be huge, because it will include all files and directories:<\/p>\n<p>$ tree<br \/>\n.<br \/>\n|&#8211; Documents<br \/>\n| |&#8211; notes.txt<br \/>\n| |&#8211; secret<br \/>\n| | `&#8211; christmas-presents.txt<br \/>\n| `&#8211; work<br \/>\n| |&#8211; project-abc<br \/>\n| | |&#8211; README.md<br \/>\n| | |&#8211; do-things.sh<br \/>\n| | `&#8211; project-notes.txt<br \/>\n| `&#8211; status-reports.txt<br \/>\n|&#8211; Music<br \/>\n|&#8211; Pictures<br \/>\n| |&#8211; trees.png<br \/>\n| `&#8211; wallpaper.png<br \/>\n|&#8211; Videos<br \/>\n`&#8211; notes.txt<\/p>\n<p>If that\u2019s too much, I can limit the number of levels it goes using the -L option followed by a number specifying the number of levels I want to see:<\/p>\n<p>$ tree -L 2<br \/>\n.<br \/>\n|&#8211; Documents<br \/>\n| |&#8211; notes.txt<br \/>\n| |&#8211; secret<br \/>\n| `&#8211; work<br \/>\n|&#8211; Music<br \/>\n|&#8211; Pictures<br \/>\n| |&#8211; trees.png<br \/>\n| `&#8211; wallpaper.png<br \/>\n|&#8211; Videos<br \/>\n`&#8211; notes.txt<\/p>\n<p>You can also display a tree of a specific path:<\/p>\n<p>$ tree Documents\/work\/<br \/>\nDocuments\/work\/<br \/>\n|&#8211; project-abc<br \/>\n| |&#8211; README.md<br \/>\n| |&#8211; do-things.sh<br \/>\n| `&#8211; project-notes.txt<br \/>\n`&#8211; status-reports.txt<\/p>\n<p>To browse and search a huge tree, you can use it together with less:<\/p>\n<p>$ tree | less<\/p>\n<p>Again, there are other options you can use with three, and you can combine them together for even more power. The manual page has them all:<\/p>\n<p>$ man tree<\/p>\n<h2>find<\/h2>\n<p>And what about files that live somewhere in the unknown? Let\u2019s find them!<\/p>\n<p>In case you don\u2019t have find on your system, you can install it using DNF:<\/p>\n<p>$ sudo dnf install findutils<\/p>\n<p>Running find without any options or parameters recursively lists all files and directories in the current directory.<\/p>\n<p>$ find<br \/>\n.<br \/>\n.\/Documents<br \/>\n.\/Documents\/secret<br \/>\n.\/Documents\/secret\/christmas-presents.txt<br \/>\n.\/Documents\/notes.txt<br \/>\n.\/Documents\/work<br \/>\n.\/Documents\/work\/status-reports.txt<br \/>\n.\/Documents\/work\/project-abc<br \/>\n.\/Documents\/work\/project-abc\/README.md<br \/>\n.\/Documents\/work\/project-abc\/do-things.sh<br \/>\n.\/Documents\/work\/project-abc\/project-notes.txt<br \/>\n.\/.bash_logout<br \/>\n.\/.bashrc<br \/>\n.\/Videos<br \/>\n.\/.bash_profile<br \/>\n.\/.vimrc<br \/>\n.\/Pictures<br \/>\n.\/Pictures\/trees.png<br \/>\n.\/Pictures\/wallpaper.png<br \/>\n.\/notes.txt<br \/>\n.\/Music<\/p>\n<p>But the true power of find is that you can search by name:<\/p>\n<p>$ find -name do-things.sh<br \/>\n.\/Documents\/work\/project-abc\/do-things.sh<\/p>\n<p>Or just a part of a name \u2014 like the file extension. Let\u2019s find all .txt files:<\/p>\n<p>$ find -name &#8220;*.txt&#8221;<br \/>\n.\/Documents\/secret\/christmas-presents.txt<br \/>\n.\/Documents\/notes.txt<br \/>\n.\/Documents\/work\/status-reports.txt<br \/>\n.\/Documents\/work\/project-abc\/project-notes.txt<br \/>\n.\/notes.txt<\/p>\n<p>You can also look for files by size. That might be especially useful if you\u2019re running out of space. Let\u2019s list all files larger than 1 MB:<\/p>\n<p>$ find -size +1M<br \/>\n.\/Pictures\/trees.png<br \/>\n.\/Pictures\/wallpaper.png<\/p>\n<p>Searching a specific directory is also possible. Let\u2019s say I want to find a file in my Documents directory, and I know it has the word \u201cproject\u201d in its name:<\/p>\n<p>$ find Documents -name &#8220;*project*&#8221;<br \/>\nDocuments\/work\/project-abc<br \/>\nDocuments\/work\/project-abc\/project-notes.txt<\/p>\n<p>Ah! That also showed the directory. One thing I can do is to limit the search query to files only:<\/p>\n<p>$ find Documents -name &#8220;*project*&#8221; -type f<br \/>\nDocuments\/work\/project-abc\/project-notes.txt<\/p>\n<p>And again, find have many more options you can use, the man page might definitely help you:<\/p>\n<p>$ man find<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=262535\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all have files on our computers \u2014 documents, photos, source code, you name it. So many of them. Definitely more than I can remember. And if not challenging, it might be time consuming to find the right one you\u2019re looking for. In this post, we\u2019ll have a look at how to make sense of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/06\/commandline-quick-tips-how-to-locate-a-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Commandline quick tips: How to locate a file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2679","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=2679"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2679\/revisions"}],"predecessor-version":[{"id":2808,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2679\/revisions\/2808"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}