what is linux command to find hidden file
The find command is used for searching for files and directories in the Linux command line.
Detect is i of the nearly powerful and oftentimes used commands. It is as well one of the nearly extensive commands with over fifty options and this makes information technology a scrap disruptive, particularly when it is paired with the exec or xargs command.
It is incommunicable for a sysadmin or software developer to avoid the observe command while working in the command line. Instead of existence afraid of it, you should comprehend its power.
I am going to discuss some of the about mutual examples of the find command that you are likely to employ. But before that, let me show you lot its syntax and how to use information technology.
Find command in Linux
The full general syntax for the detect command is:
detect [directory to search] [options] [expression] Everything in brackets [] are optional. Information technology means that you can run observe control without any options and arguments. It will but dump all the files and directories in the current location. That's not very useful, right?
Let's expect at it in more detail:
-
directory to searchis basically the location from where you want to start your search. Past default, the search is recursive and starts from your current location. -
optionsspecify the blazon of search, be it by proper noun, by type, by modified time etc. At that place are more than l options possible here. -
expressionallows you to specify the search term. If y'all want to find a file by its name, expression is the file name. If you desire to discover files with name matching a blueprint, expression in the pattern.
Let me take a elementary instance:
find . -type f -proper name myfile This command volition run a search in the current directory and its subdirectories to find a file (not directory) named myfile. The option -type f asks information technology to expect for files just. The unmarried dot . means the electric current directory.
Let'southward see some practical examples of the find control.
Discover files and directories past proper name
Y'all can search for files and directories by its name:
observe . -proper noun SEARCH_NAME Since in that location is no file type mentioned, information technology searches for both files and directories with the given proper name.
The below case finds both file and directories named mystuff:
[e-mail protected]:~/Examples$ notice -proper name mystuff ./new/mystuff ./mystuff Observe simply files or but directories
If you only want to look for files, specify file type -f:
find . -blazon f -name SEARCH_NAME The lodge of type and proper noun does not matter. Take the previous case and observe for files only:
[email protected]:~/Examples$ discover -blazon f -name mystuff ./mystuff If you just desire to search for directories, specify type -d:
notice . -type d -name SEARCH_NAME In the previous file, look for directories merely:
[email protected]:~/Examples$ find -type d -name mystuff ./new/mystuff Run a case-insensitive search
By default, the find control is example sensitive. Y'all can run a example-insensitive search with the given name by using -iname instead of -proper noun.
find . -blazon f -iname SEARCH_NAME You can apply it with type d also.
[electronic mail protected]:~/Examples$ find -iname mystuff ./new/mystuff ./MyStuff ./mystuff Screenshot of above three examples:
Search files by their extension (important)
One of the most mutual utilise of the find command is to detect files of a specific type or should I say a specific extension.
For example, let'south say, you lot want to search for all the C++ files in the electric current directories. The C++ files cease with extension .cpp, so you can search it like this:
detect . -type f -proper name "*.cpp" This way, you tell the find control to look for type file and with names that end with .cpp.
[email protected]:~$ find . -type f -name "*.cpp" ./file.cpp ./.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-ane.1.3/src/zlib/contrib/iostream2/zstream_test.cpp ./.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-i.1.3/src/zlib/contrib/iostream/test.cpp ./.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-1.ane.3/src/zlib/contrib/iostream/zfstream.cpp Ever put your search expression in double quotes when using the find control.
Why do I recommend using double quotes or unmarried quotes effectually your search term? Considering if you do not exercise that, the vanquish will expand the wildcard.
If you lot do non wrap your search term in quotes:
detect . -blazon f -proper name *.cpp Your shell will expand *.cpp and supervene upon it with all the files in the electric current directory whose names end with .cpp.
This could piece of work if there is only one file but if there are more than one, your beat will mutter of incorrect syntax.
In the higher up example, there is but 1 cpp file and hence when the command expands to find . -type f -name file.cpp, it works considering information technology file.cpp still works as search term.
Merely there are ii .txt files in the aforementioned directory and hence when the command expands to find . -type f -proper noun another.txt new.txt, it complains because there is more than than one search term now.
This is why you should e'er wrap your search term in double quotes.
Search for multiple files with multiple extensions (or condition)
The in a higher place control searched for files with a given extension. What if you want to look for files with different extensions?
Instead of running the find command multiple times, run it once by using the -o pick that works every bit logical OR status:
find . -type f -name "*.cpp" -o -name "*.txt" Here's an example:
[email protected]:~/Examples$ discover . -type f -proper noun "*.txt" -o -name "*.cpp" ./new.txt ./file.cpp ./new/new.txt ./new/dir2/some other.txt ./new/dir1/new.txt ./another.txt Wait for files in specific directory
So far, all the examples performed search in the current directory because you specified . in the examples.
The dot can be replaced with an absolute or relative path of a directory so that y'all tin can expect for files in the specified directory without leaving your current location.
[electronic mail protected]:~/Examples$ detect ./new -proper noun mystuff ./new/mystuff Search for files in multiple directories
If you think your desired file(southward) could exist located in several locations, you don't have to run find control multiple times. Just specify all the directory paths to search in the notice command:
detect ./location1 /second/location -type f -proper name "pattern" Find empty files and directories
The -empty option enables yous to look for empty files and directories with the discover control.
To detect all the empty files and directories in the current directory, utilise:
find . -empty You can specify the file type to wait only for empty files or directories:
find . -empty -type f You may as well combine information technology with the filename search:
find . -empty -type f -proper name "*.cpp"
Find big files or small (Search based on file size)
Y'all can find large files or small files based on the search performed by the size parameter. This only works with files, not directories.
You use the -size option with +N for size greater than N and -Due north for size smaller than Northward.
Find files of exactly 50 KB in size:
find . -size 50k To search for files bigger than ane GB in the current directory:
find . -size +1G To find smaller than 20 bytes:
find . -size -20c To find files bigger than 100 MB but smaller than 2 GB in size:
find . -size +100M -size -2G Y'all may as well combine the size search with the proper noun search. For example, to search for all files with name catastrophe in .log but size greater than 500 MB in the root directory, you can use:
observe / -size +500M -name "*.log" To recollect:
-
c: bytes -
thousand: kilobytes -
M: Megabytes -
Chiliad: Gigabytes
Detect recently modified files (Search based on modify or cosmos time)
You know the concept of mtime, atime and ctime, right?
- mtime: last modification time of file
- ctime: creation time of the file
- atime: last access fourth dimension of the file
Yous'll often find yourself in situations where you want to notice all the recently modified files. The search past modified fourth dimension helps in such cases.
To observe all the files modified within 3 days (iii*24H), utilize:
discover . -type f -mtime -3 To detect all the files created at least 5 days (5*24H) ago, employ:
find . -type f -ctime +5 I know 24 hours is a huge time frame. What if you want to search for files that were modified just a few minutes ago? For that, you can use mmin, amin and cmin.
To find all the files that were modified in the last 5 minutes, use:
discover . -type f -mmin -5
You tin can specify upper and lower limits along with the search name. The control below will search for all the .java files that take been modified betwixt final twenty to 30 minutes.
find . -type f -mmin +20 -mmin -30 -name "*.java" Find files with specific file permissions
I promise you are familiar with the file permission concept in Linux.
The observe control allows y'all to search for files with specific file permission and access manner.
notice -perm mode For example, to observe all the files access way 777 in the current directory;
detect . -perm 777 To detect all files with access of read and write for all (exact match, information technology won't match if the file has execute permission for all):
find . -perm a=r+w Find files owned past a user
Yous can also search for files based on ownership.
For example, to detect files owned by the user John in the current directory, use:
find . -blazon f -user John You tin can likewise combine it with other options like size, time and proper noun:
notice . -blazon f -user John -name "*.cpp" Don't find recursively, search merely in electric current directory
Past default, the find command searches in all the subdirectories of your current location. If you don't want that, yous can specify the depth of your search to 1. This volition restrict the search to only the current directory and excludes whatever subdirectories.
find . -maxdepth 1 -type f -name "*.txt"
Exclude a directory from search
if you desire to exclude a directory from the search, you can do that by combining path, prune and logical or.
find . -path "./directory_exclude/*" -prune -o -name SEARCH_NAME Exist careful with the * in the path of the directory, -clip afterwards path and -o afterward prune.
Basically, the prune command asks to not utilize the value specified by path. Prune is e'er used with -o to ensure that right side of the terms are evaluated just for directories that were not pruned.
Take activity on the result of notice commands (exec and xargs)
So far, you take learned various means to notice files based on diverse criteria. That's skillful. But you tin can make it better by taking certain actions on the consequence of the find command.
For example, how about finding files matching sure name design and renaming them all at once or finding empty files and deleting them?
Yous know that pipage redirection tin can exist used to combine the output of one command with the input of another command. But this won't piece of work with the output of find command, at least not directly.
You have two options if you desire to have an action on the upshot of find command:
- Use exec
- Use xargs
Using notice and exec
Suppose you want to long list (ls -l) the search files with the detect command. Hither's what you lot use:
discover . -type f -proper name "*.txt" -exec ls -fifty {} + Here's the output:
[email protected]:~/Examples$ detect . -type f -name "*.txt" -exec ls -l {} + -rw-rw-r-- ane abhishek abhishek 39 Oct xiii 19:30 ./another.txt -rw-rw-r-- 1 abhishek abhishek 35 Oct thirteen xv:36 ./new/dir1/new.txt -rw-rw-r-- 1 abhishek abhishek 35 Oct thirteen fifteen:36 ./new/dir2/another.txt -rw-rw-r-- one abhishek abhishek 35 October 13 eighteen:51 ./new/mystuff/new.txt -rwxrwxrwx 1 abhishek abhishek 35 October 13 xv:37 ./new/new.txt -rw-rw-r-- 1 abhishek abhishek 35 October 13 xviii:16 ./new.txt Many people forget to add the {} + at the end of the exec command. Y'all must use it and mind the space between {} and +.
The {} is what references the consequence of the find command. Y'all tin imagine information technology to be like {file 1, file two, file iii}. The + sign is used to terminate the exec control.
There is likewise another convention with exec:
find . -blazon f -name *.txt" -exec ls -l {} \; Hither, ; is used instead of the + sign. The additional \ earlier ; is used to escape the special grapheme ;.
The advantage of {} + is that information technology runs fewer commands as ls -l file1 file2 file3 whereas {} \; volition run ls -l file1, ls -l file2 etc.
But, {} \; has the reward of using {} more than than once in the aforementioned exec statement. For instance, the command below volition rename all the found files with .old extension.
detect . -type f -proper name *.txt" -exec mv {} {}.old \; Using xargs
Many Linux users get used to of the pipe redirection. This exec control with the trailing {} + seems intimidating to them.
This is where xargs helps. Y'all just parse the output of the find command to the xargs command via pipe.
find . -type f -proper noun *.txt" | xargs ls -l
The syntax seems a lot simpler, right? Xargs control is besides very powerful. You may read virtually it here.
How to Use Xargs Control in Linux [Explained With Examples]
xargs is one of the well-nigh powerful commands in Linux. In this tutorial, you'll learn to apply xargs command with some practical and useful examples.
Combining observe and grep
Now that yous know virtually combining find with xargs and exec command, you can use it to combine find and grep.
For whatsoever sysadmin or software developer, notice and grep is i of the about common and yet well-nigh useful combination.
You search for file name patters with notice and and then apply grep to search for the content inside those files.
For example, you want to search for all the .txt files that incorporate the term Alice. You combine notice and grep like this:
observe . -blazon f -proper noun "*.txt" -exec grep -i alice {} + The aforementioned can be achieved with xargs as well:
find . -type f -name "*.txt" | xargs grep -i alice
Of course, this is the simplest of the examples but if y'all are familiar with the grep command, you can use information technology to your liking and need.
There is a lot more with find ...
And it is not possible to list all the find command options and examples. The possibilities are countless simply when you lot get familiar with the find command, you can get-go using it in a variety of situations. That is really up to you how you combine the logic here.
I promise you lot find these examples of find command useful. If you still take questions or suggestions to improve this commodity, please permit me know in the annotate section.
Source: https://linuxhandbook.com/find-command-examples/
Postar um comentário for "what is linux command to find hidden file"