Files
Makefile-tutorial/shell/find.txt
2025-09-13 09:50:19 +05:30

61 lines
1.6 KiB
Plaintext

Linux Command: find
Blueprint:
find <path> <option> <file to search for>
1. find <path> -name <file>
To search <file> in a given path by (-name).
'.' : current directory.
2. find <path> . -name <file/directory> -type f|d
To search either file or directory only.
f: To search only for file.
d: To search only for directory.
3. find <path> -type f|d -name <file/directory> -exec rm {} +
To remove the file being searched.
To execute the rm command using '-exec'.
-exec: It means to execute command against every item in the result.
{}: It is the placeholder for every item itself.
+: It is the termination of each iteration of command that will be executed.
\; : It also do the same thing as '+' symbol.
4. find <path> -type -f -exec chmod 600 {} +
Problem:
While changing the permission of file in a directory (files can be nested)
we can run:
chmod -R 600 <path>
But, it will also change the permission of all the nested directory along
with the parent directory passed as an argument. This result in unexpected
and errorious behaviour.
To avoid such issue we can use above find command.
Solution:
To find all the files and change the its permission.
If no name is passed then it will find all the file in that path.
5. sudo find <path> -type f -name <file|directory> -exec truncate -s 0 {} +
To find the file in the given path and execute command truncate and reduce
the file size to 0.
truncate -s 0
-s: set size of file
For ex:
-s 0: set size of file to 0.