To create a directory structure and files, you can use the following commands:
$ mkdir magic
$ mkdir magic/dir{1..100}
$ touch magic/dir{1..100}/file{1..100}.txtAlternatively, you can combine the commands into one:
$ mkdir magic; mkdir magic/dir{1..100}; touch magic/dir{1..100}/file{1..100}.txtTo create a file named test.txt in a randomly chosen directory:
$ touch magic/dir$(shuf -i 1-100 -n 1)/test.txt-
To find the
test.txtfile:$ find magic -type f -name 'test.txt' -
To move
test.txtto the Desktop:$ find magic -type f -name 'test.txt' -exec mv {} ~/Desktop \;
-
To remove all
.txtfiles in themagicdirectory:$ find magic -type f -name '*.txt' -exec rm {} \;
- A) True
- B) False
Answer: B
Q5: To find Files and Directories inside /dev Folder and Limit its Search to only 2 Levels of Depth?
- A)
find -start /dev -depth 2 - B)
find /dev -depth 2 - C)
find /dev -maxdepth 2
Answer: C
- A)
find /dev -maxdepth 2 -type f - B)
find /dev -maxdepth 2 -type d - C)
find /dev -type d -maxdepth 2
Answer: B
- A)
find / '*.txt' - B)
find / -type d -name '*.txt' - C)
find / -type f -name '*.txt'
Answer: C
| Feature | find | locate |
|---|---|---|
| Search Method | Searches directly in the file system | Searches in a database |
| Options | Numerous options for customization | Very few options |
| Search Criteria | Name, type, size, depth, age, user, group, permissions, etc. | Name and permissions |
| Depth of Search | Can be reduced | Cannot be reduced |
| Accuracy | Accurate as it searches directly in the file system | May not be accurate due to database updates |
| Deleted Files | Doesn't include deleted files | May include deleted files |
| Using Search Results | -exec option for further actions |
No direct way to use results |
| Speed | Slower | Faster |