10 Example of find command in UNIX and Linux
source link: https://javarevisited.blogspot.com/2018/08/10-example-of-find-command-in-unix-linux.html#axzz8Y54xDxKb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
10 Example of find command in UNIX and Linux
Like any other command strength to find lies in its various options, which is worth learning, but, to be frank, hard to remember. If you can be even able to remember all the options mentioned in this article, you will be taking much more advantage of the find command than average developers, QA, support people, and Linux users.
By the way, I have been sharing my experience on Unix and Linux command, and their different options, usage, and example, and this article are in continuation of my earlier post like how to convert an IP address to the hostname in Linux. If you are new here, you may find these tips useful for your day-to-day development and support work.
10 find command examples in Linux and UNIX
Here I am listing down some of the ways I use to find in Unix or Linux box regularly; I hope this would help someone who is new in UNIX find command or any developer who has started working on the UNIX environment. This list is by no means complete and just some of my favorites, if you have something to share, please share via comments.
1. How to run the last executed find command in UNIX
!findwill repeat the last find command executed. It saves a lot of time if you re searching for something, and you need to run the same command again and again.
find . -name "*.java" --last find command executed
./OnlineStockTrading.java
./StockTrading.java
2. How to find files that have been modified less than one day, minute, or hour in Linux
Along with –mtime, there are two more options related to time, find -atime, which denotes the last accessed time of the file, and find –ctime denotes the last changed time.
The + sign is used to search for greater than, - sign is used to search for less than and without a sign is used for exact. For example, find –mtime -1 will search all files which have been modified.
javin@testenv1 ~/java : find . -mtime -1 (find all the files modified less than 1 day)
.
./StockTrading.java
javin@testenv1 ~/java : find . -mtime +1 (find all the files modified more than 1 day)
./.vimrc
./OnlineStockTrading.java
./StockTrading.java~
3. How to find all the files and directories which hold the 777 permission in UNIX
find –perm option is used to find files based upon permissions. You can use find –perm 444 to get all files that allow read permission to the owner, group, and others.
If you are not sure how those 777 and 444 numbers come up, see my post on file and directory permission in Unix and some chmod examples to change permissions in Unix.
./.vimrc
./OnlineStockTrading.java
Btw, if you are not familiar with file permissions, then you should first check outLearn Linux in 5 Days and Level Up Your Career, another excellent course for anyone who wants to work in Linux.
4. Case insensitive search using the find in UNIX
This option of the find is beneficial while looking for errors and exceptions in the log file.
UNIX find and xargs command Example
5. How to delete temporary files using the find command in UNIX
In order to delete files, you can use either –delete option of find command or use xargs in combination. It's better to create a housekeeping script for such tasks which can perform cleanup on a periodic basis.See another example below, also worth considering use of -print0 to avoid problems with whitespace in the path when piping to xargs (use with the xargs -0 option) as suggested by Ebon Elaza.
6. How to find all text file that contains word Exception
find . –name "*.java" –print | xargs grep “MemoryCache”, this will search all java files starting from the current directory for the word "MemoryCache". We can also leave -print option in all cases because it the default for UNIX finds command as pointed out by Ben in the comments.7. Finding files only in the current directory not searching on subdirectories
While using the find command, I realized that sometimes I only need to find files and directories that are new, only in the current directory, so I modified the find command as follows.You can use the find –type option to specify the search for the only file, link, or directory, and -maxdepth option specifies how deep the find command has to search.
Example 8 – How to find files based on the size in Unix and Linux
The following find example shows how you can use find –size options to find files based upon a certain size. This willfind all files in the current directory and sub-directory, greater than some size using the find command in UNIX:Also, to find files using a range of file sizes, the minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than."
Suppose if you want to find all the files within a range, you can use find command as in the below example of find:
Example 9 – How to find files some days older and above a specific size
We can combine –mtime and –size to find files that are some days old and greater than some size in Unix. A prevalent scenario where you want to delete some large old files to free some space in your machine.This example of the find command will find which are more than 10 days old and size greater than 50K.
10. Find and AWK
You can use "awk" in a combination of find to print a formatted output, e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:Tip:
$* : $* is one of the special bash parameters which is used to expand positional parameters from position one. if you give double quotes and expansion is done within double quotes, it only expands to a single word, and the corresponding value of each parameter will be separated by the first letter of the IFS environment variable defined in bash.
How to use find command on filenames with space in UNIX
Since we mostly pipe the output of the find command to xargs -print could cause a problem if the file name itself contains a new line or any form of white space. To resolve this issue instead of -print use -print0.
The difference between find -print and find -print0 is print0 display file name on the stdout followed by a "NUL" character, and then you can use xargs -0 commands to process file names with a null character.
let's see UNIX find command example with a file name having space in them:
./cash equity trading ./equity~
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
xargs: WARNING: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option?
ls: cannot access ./cash: No such file or directory
ls: cannot access equity: No such file or directory
ls: cannot access trading: No such file or directory
-rw-r--r-- 1 stock_trading cash_domain trading 0 Jul 21 09:54 ./cash equity trading
-r--r--r-- 1 stock_trading cash_domain trading 0 Jul 15 11:42 ./equity~
In short, always use find -print0 along with xargs -0 if you see the slightest possibility of file names containing space in UNIX or Linux.
Essential points about find commands in UNIX and Linux
P. S. - If you want to learn Linux from scratch and looking for some free resources, then you can also check out this list of free Linux courses for Programmers and Developers. It contains some of the free online courses from Udemy, Pluralsight, and Coursera to learn Linux online.
Recommend
-
39
Error 1020 Ray ID: 5ae361e98d180538 • 2020-07-05 19:09:48 UTC Access denied What happened? This website is using a security service to protect itself from online attacks...
-
6
15 Practical Grep Command Examples In Linux / UNIX by SathiyaMoorthy on March 26, 2009
-
8
ls – Unix users and sysadmins cannot live without this two letter command....
-
10
Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc., In this article, let us review 10 practical unix
-
0
Unix Philosophy with an Example Mar 2, 2019 The Unix philosophy is a set of software development concepts and norms which have shaped th...
-
3
Unix Quiz (Here's another resurrected post from April 30, 2012. Answers in a followup.) People objected that there was no Exit item on the main menu for the mpx program that put windows on the Blit; see
-
6
Unix Quiz answers (Here's the next resurrected post, this time from Jun 8, April 30, 2012.) A while back I posted the questions from the 1984 Unix/mpx Exit quiz:
-
2
Introduction When I tried to get the timestamp of the Facebook post, I found that the timestamp...
-
4
This blog post documents my understanding of how the conventions for Unix command line syntax have evolved over time. It’s not properly sourced, and may well be quite wrong. I’ve not been using Unix until 1989, so I wasn’t there for the early year...
-
10
Bash-Oneliner and GameShell Teach Unix Command Line
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK