'find' and 'locate' in linux to look up files

study notes:

find

locate


1.find

点击打开链接


find by name,

To find a file by name, type:

find -name ""

To find a file by name, but ignore the case of the query, type:

find -iname ""

find -not -name "query_to_avoid"
find \! -name "query_to_avoid"
 


find by type:

find -type type_descriptor query

Some of the most common descriptors that you can use to specify the type of file are here:

  • f: regular file

  • d: directory

  • l: symbolic link

  • c: character devices

  • b: block devices

find / -type c

We can search for all files that end in ".conf" like this:

find / -type f -name "*.conf"


Filtering by Time and Size

Size

You can filter by size with the use of the "-size" parameter.

We add a suffix on the end of our value that specifies how we are counting. These are some popular options:

  • c: bytes

  • k: Kilobytes

  • M: Megabytes

  • G: Gigabytes

  • b: 512-byte blocks

To find all files that are exactly 50 bytes, type:

find / -size 50c

To find all files less than 50 bytes, we can use this form instead:

find / -size -50c

To Find all files more than 700 Megabytes, we can use this command:

find / -size +700M

Time

Linux stores time data about access times, modification times, and change times.

  • Access Time: Last time a file was read or written to.

  • Modification Time: Last time the contents of the file were modified.

  • Change Time: Last time the file's inode meta-data was changed.

We can use these with the "-atime", "-mtime", and "-ctime" parameters. These can use the plus and minus symbols to specify greater than or less than, like we did with size.

The value of this parameter specifies how many days ago you'd like to search.

To find files that have a modification time of a day ago, type:

find / -mtime 1

If we want files that were accessed in less than a day ago, we can type:

find / -atime -1

To get files that last had their meta information changed more than 3 days ago, type:

find / -ctime +3

There are also some companion parameters we can use to specify minutes instead of days:

find / -mmin -1

This will give the files that have been modified type the system in the last minute.

Find can also do comparisons against a reference file and return those that are newer:

find / -newer myfile

Finding by Owner and Permissions

You can also search for files by the file owner or group owner.

You do this by using the "-user" and "-group" parameters respectively. Find a file that is owned by the "syslog" user by entering:

find / -user syslog

Similarly, we can specify files owned by the "shadow" group by typing:

find / -group shadow

We can also search for files with specific permissions.

If we want to match an exact set of permissions, we use this form:

find / -perm 644

This will match files with exactly the permissions specified.

If we want to specify anything with at least those permissions, you can use this form:

find / -perm -644

This will match any files that have additional permissions. A file with permissions of "744" would be matched in this instance.


Filtering by Depth

For this section, we will create a directory structure in a temporary directory. It will contain three levels of directories, with ten directories at the first level. Each directory (including the temp directory) will contain ten files and ten subdirectories.

Make this structure by issuing the following commands:

cd
mkdir -p ~/test/level1dir{1..10}/level2dir{1..10}/level3dir{1..10}
touch ~/test/{file{1..10},level1dir{1..10}/{file{1..10},level2dir{1..10}/{file{1..10},level3dir{1..10}/file{1..10}}}}
cd ~/test

Feel free to check out the directory structures with ls and cd to get a handle on how things are organized. When you are finished, return to the test directory:

cd ~/test

We will work on how to return specific files from this structure. Let's try an example with just a regular name search first, for comparison:

find -name file1
./level1dir7/level2dir8/level3dir9/file1
./level1dir7/level2dir8/level3dir3/file1
./level1dir7/level2dir8/level3dir4/file1
./level1dir7/level2dir8/level3dir1/file1
./level1dir7/level2dir8/level3dir8/file1
./level1dir7/level2dir8/level3dir7/file1
./level1dir7/level2dir8/level3dir2/file1
./level1dir7/level2dir8/level3dir6/file1
./level1dir7/level2dir8/level3dir5/file1
./level1dir7/level2dir8/file1
. . .

There are a lot of results. If we pipe the output into a counter, we can see that there are 1111 total results:

find -name file1 | wc -l
1111

This is probably too many results to be useful to you in most circumstances. Let's try to narrow it down.

You can specify the maximum depth of the search under the top-level search directory:

find -maxdepth num -name query

To find "file1" only in the "level1" directories and above, you can specify a max depth of 2 (1 for the top-level directory, and 1 for the level1 directories):

find -maxdepth 2 -name file1
./level1dir7/file1
./level1dir1/file1
./level1dir3/file1
./level1dir8/file1
./level1dir6/file1
./file1
./level1dir2/file1
./level1dir9/file1
./level1dir4/file1
./level1dir5/file1
./level1dir10/file1

That is a much more manageable list.

ou can also specify a minimum directory if you know that all of the files exist past a certain point under the current directory:
find -mindepth num -name query

We can use this to find only the files at the end of the directory branches:

find -mindepth 4 -name file

You can combine the min and max depth parameters to focus in on a narrow range:

find -mindepth 2 -maxdepth 3 -name file
./level1dir7/level2dir8/file1
./level1dir7/level2dir5/file1
./level1dir7/level2dir7/file1
./level1dir7/level2dir2/file1
./level1dir7/level2dir10/file1
./level1dir7/level2dir6/file1
./level1dir7/level2dir3/file1
./level1dir7/level2dir4/file1
./level1dir7/file1
. . .

Executing and Combining Find Commands

You can execute an arbitrary helper command on everything that find matches by using the "-exec" parameter. This is called like this:

find find_parameters -exec command_and_params {} \;

The "{}" is used as a placeholder for the files that find matches. The "\;" is used so that find knows where the command ends.

For instance, we could find the files in the previous section that had "644" permissions and modify them to have "664" permissions:

cd ~/test
find . -type f -perm 644 -exec chmod 664 {} \;

We could then change the directory permissions like this:

find . -type d -perm 755 -exec chmod 700 {} \;

If you want to chain different results together, you can use the "-and" or "-or" commands. The "-and" is assumed if omitted.

find . -name file1 -or -name file9 


Locate

Find Files Using Locate

An alternative to using find is the locate command. This command is often quicker and can search the entire file system with ease.

You can install the command with apt-get:

sudo apt-get update
sudo apt-get install mlocate

The reason locate is faster than find is because it relies on a database of the files on the filesystem.

The database is usually updated once a day with a cron script, but you can update it manually by typing:

sudo updatedb

Run this command now. Remember, the database must always be up-to-date if you want to find recently acquired or created files.


To find files with locate, simply use this syntax:

locate query

You can filter the output in some ways.

For instance, to only return files containing the query itself, instead of returning every file that has the query in the directories leading to it, you can use the "-b" for only searching the "basename":

locate -b query

To have locate only return results that still exist in the filesystem (that were not remove between the last "updatedb" call and the current "locate" call), use the "-e" flag:

locate -e query

To see statistics about the information that locate has cataloged, use the "-S" option:

locate -S
Database /var/lib/mlocate/mlocate.db:
    3,315 directories
    37,228 files
    1,504,439 bytes in file names
    594,851 bytes used to store database

Conclusion

Both find and locate are good ways to find files on your system. It is up to you to decide which of these tools is appropriate in each situation.

Find and locate are powerful commands that can be strengthened by combining them with other utilities through pipelines. Experiment with filtering by using commands likewc,sort, and grep.

By Justin Ellingwood.


深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值