![]() ![]() So its easy to mention that in the name criteria and list all hidden files. Hidden files on linux begin with a period. dir2 -type f -name "abc*"Ĭheck, that it listed files from 2 separate directories. Again, the command is very simple $ find. So lets say you want to search inside 2 separate directories. Sometimes we want to find only files or only directories with a given name. The above command search for files ending in either the php extension or the txt extension. However if we need to perform an OR based matching then the find command has the "o" switch. When using multiple name criterias, the find command would combine them with AND operator, which means that only those files which satisfy all criterias will be matched. This is an example of how powerful search expressions can be build with the find command. ![]() The above find command looks for files that begin with abc in their names and do not have a php extension. It is possible to use multiple criterias when specifying name and inverting. The find command also supports the exclamation mark inplace of not. So in the above example we found all files that do not have the extension of php, either non-php files. This is helpful when we know which files to exclude from the search. It is also possible to search for files that do no match a given name or pattern. Just like maxdepth there is an option called mindepth which does what the name suggests, that is, it will go atleast N level deep before searching for the files. This is very useful when we want to do a limited search only in the current directory or max 1 level deep sub directories and not the entire directory tree which would take more time. The second example uses maxdepth of 1, which means it will not go lower than 1 level deep, either only in the current directory. For example we don't want to go more than 2 or 3 levels down in the sub directories. However the depth of directory travesal can be specified. The find command by default travels down the entire directory tree recursively, which is time and resource consuming. Not doing so will seem to work sometimes and give strange results at other times. test/cool.php Its always better to wrap the search term (name parameter) in double or single quotes. To ignore the case, just use the "iname" option instead of the "name" option. It is often useful to ignore the case when searching for file names. However, if the directory is actually a symlink to some other location then you MUST specify the trailing slash for it to work properly (find. When specifying the directory ("./test" in this example), its fine to omit the trailing slash. Of course you can press Ctrl + c anytime to stop the command. Trying to search the "/" directory which is the root, would search the entire file system including mounted devices and network storage devices. So this is a very powerful way to find all files of a given extension. ![]() Note that all sub directories are searched recursively. The following command searches for files by their name. The following command will look for files in the test directory in the current directory. The command is same as the following $ find. This command lists out all the files in the current directory as well as the subdirectories in the current directory. List all files in current and sub directories The basic syntax of the find command looks like this $ find location comparison-criteria search-term 1. The find command is an essential one to learn, if you want to get super productive with the command line on linux. The find command is available on most linux distros by default so you do not have to install any package. In this post we shall learn to use the find command along with various options that it supports. It can be used to find files based on various search criterias like permissions, user ownership, modification date/time, size etc. The Linux find command is a very useful and handy command to search for files from the command line.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |