linux基本命令示例_Linux使用示例查找命令

linux基本命令示例

linux基本命令示例

Linux provides a lot of important tools to make the administrator’s life easy. find command is one of the most important commands in Linux world. Find command provides a search according to the file owner, file size, file change date, filename, etc. Now we can start with simple examples and then go to more complex examples.

Linux提供了许多重要的工具来简化管理员的工作。 find命令是Linux世界中最重要的命令之一。 Find命令提供了根据文件所有者,文件大小,文件更改日期,文件名等进行的搜索。现在,我们可以从简单的示例开始,然后转到更复杂的示例。

查找命令语法 (find Command Syntax)

Find command syntax is like below. First, the path we want to search is given and then different options are provided.

查找命令的语法如下所示。 首先,给出了我们要搜索的路径,然后提供了不同的选项。

find PATH OPTIONS

根据名称查找文件 (Find Files According Name)

The easy and fast method to find files is using their names. We search the current directory with a file named tmux. We will use -name option of the find command and the double dot  means we want to search the current working directory.

查找文件的简便方法是使用文件名。 我们使用名为tmux的文件搜索当前目录。 我们将使用find命令的-name选项,双点表示我们要搜索当前工作目录。

$ find . -name tmux

在主目录下查找文件 (Find Files Under Home Directory)

To search and find files under home directory of all normal users following find command can be issued.

可以执行以下find命令在所有普通用户的主目录下搜索和查找文件。

$ find /home -name ismail

查找忽略大小写或不区分大小写的文件 (Find Files Ignoring Case or Case Insensitive)

By default find command search provided text case sensitive. To make a search ignoring case sensitivity prefix -iname parameter with it like below. In this example, we will search for bash as case-insensitively in the whole file system.

默认情况下,find命令搜索提供的文本区分大小写。 如下进行忽略大小写前缀-iname参数的搜索。 在此示例中,我们将在整个文件系统中搜索不区分大小写的bash。

$ find / -iname bash
Find Files Ignoring Case or Case Insensitive
Find Files Ignoring Case or Case Insensitive
查找忽略大小写或不区分大小写的文件

仅搜索目录(Search For Only Directories)

Searching only directories can be done by specifying file type like below. We will use -type option and d to specify file type as the directory.

可以通过指定如下所示的文件类型来仅搜索目录。 我们将使用-type选项和d将文件类型指定为目录。

$ find / -type d -name bash

查找Python文件 (Find Python Files)

Searching python files with their extension can be done like below. The extension py can be changed anything we want.

可以按以下方式搜索带有扩展名的python文件。 扩展名py可以根据需要更改。

$ find . -name '*.py'

查找具有777权限的文件 (Find Files with 777 Permission)

777 permission can be a security problem if the files owned by root or high privileged users. We can search for files according to their permissions. We will use -perm option and the permission value.

如果root或高特权用户拥有文件,则777权限可能是一个安全问题。 我们可以根据文件的权限搜索文件。 我们将使用-perm选项和权限值。

$ find / -type f -perm 0777 -print

查找启用SUID的文件 (Find SUID Enabled Files)

We can search for files and directories according to their suid values. We will use -perm and /u=s to specify user suid flag.

我们可以根据其suid值搜索文件和目录。 我们将使用-perm/u=s来指定用户suid标志。

$ find / -perm /u=s
Find SUID Set Files
Find SUID Set Files
查找SUID设置文件

查找只读文件(Find Read-Only Files)

Searching read only files can be done below. We will use /u=r which means user is read-only.

搜索只读文件可以在下面完成。 我们将使用/u=r ,这意味着用户是只读的。

$ find / -perm /u=r

查找具有权限777的文件并更改为664 (Find Files with Permission 777 and change to 664)

Searching files with 777 permission and making them more secure with 664 can be done with the following command. We can execute a command in found files with -exec option. {} is used to specify the file name in -exec part.

可以使用以下命令来完成具有777权限的文件的搜索并使用664使其更安全。 我们可以使用-exec选项在找到的文件中执行命令。 {}用于在-exec部分中指定文件名。

$ find / -type f -perm 0777 -print -exec chmod 644 {} \;
Find Files with Permission 777 and change to 664
Find Files with Permission 777 and change to 664
查找具有权限777的文件并更改为664
  • -exec gives the ability to execute a command on the found file.

    -exec可以对找到的文件执行命令。

  • {} is used to specify founded files to run chmod on these files

    {}用于指定已创建的文件以在这些文件上运行chmod

查找和删除文件 (Find and Remove Files)

There is a simple shortcut to find files and remove them. In this example, we will remove files with .py extension by using rm command.

有一个简单的快捷方式来查找文件并删除它们。 在此示例中,我们将使用rm命令删除扩展名为.py文件。

$ find . -type f -name "*.py" -exec rm -f {} \;

查找空文件 (Find Empty Files)

Find can found empty files. We only need to specify the -empty option to the find command like below.

查找可以找到空文件。 我们只需要为find命令指定-empty选项,如下所示。

$ find / -type f -empty
Find Empty Files
Find Empty Files
查找空文件

查找所有隐藏文件(Find All Hidden Files)

Hidden files generally used by operating systems or applications for configuration issues. We can use a single dot as hidden file syntax because we know that hidden files in Linux start with . point.

操作系统或应用程序通常用于配置问题的隐藏文件。 我们可以使用单点作为隐藏文件语法,因为我们知道Linux中的隐藏文件以开头. 点。

$ find /tmp -type f -name ".*"

查找给定用户的文件 (Find Files For Given User)

Search files according to ownership. We will use -user option to specify the user name. In this example, we want to find root user files with .txt extension.

根据所有权搜索文件。 我们将使用-user选项指定用户名。 在此示例中,我们要查找扩展名为.txt root用户文件。

$ find / -user root -name *.txt
Find Files For a User
Find Files For a User
查找用户文件

查找组文件(Find Files For a Group)

Search files according to group ownership. If the group does not exists find give a message like find: ‘developer’ is not the name of an existing group.

根据组所有权搜索文件。 如果该组不存在,则find给出类似find的消息:'developer'不是现有组的名称。

$  find /home -group ismail
Find Files For a Group
Find Files For a Group
查找组文件

查找最近5天修改过的文件(Find Files Modified Last 5 Days)

Search files according to their modification date. We will use -mtime options which mean modification time to find files that have modified the last 5 days.

根据文件的修改日期搜索文件。 我们将使用-mtime选项,这意味着修改时间来查找最近5天已修改的文件。

$ find /home/ismail/ -mtime 5

查找最近5天的文件访问权限 (Find Files Access Last 5 Days)

Search files those accessed last 5 days. We will use -atime as option for access time.

搜索最近5天访问过的文件。 我们将使用-atime作为访问时间的选项。

$ find /home/ismail -atime 5

查找最近50到100天之间修改的文件 (Find Files Modified Between Last 50 and 100 Days)

Search for files modified last 50 and last 100 days.

搜索最近50天和最近100天修改的文件。

$ find /home -mtime +50 -mtime -100
Find Files Modified Between Last 50 and 100 Days
Find Files Modified Between Last 50 and 100 Days
查找最近50到100天之间修改的文件

查找最近2个小时内更改过的文件(Find Files Changed Last 2 Hours)

Search for files those changed last 2 hours

搜索最近2个小时内更改的文件

$ find /home/ -cmin -120
Find Files Changed Last 2 Hours
Find Files Changed Last 2 Hours
查找最近2个小时内更改过的文件

查找大小为10MB的文件(Find Files Sized 10MB)

We can find files according to their size. We will use -size option for this. In this example, we will find that size is 10MB. We can use - o + to specify  than lesser or bigger than the given size

我们可以根据文件的大小查找文件。 我们将使用-size选项。 在此示例中,我们发现大小为10MB。 我们可以使用- Ø +指定除比给定尺寸更小或更大

$  find /home -size 10M

根据大小查找文件并删除 (Find Files According to Size and Delete)

Search files those bigger than 100MB and delete them.

搜索大于100MB的文件并将其删除。

$ find /tmp -size +100M -exec rm -rf {} \;
Linux Find Command With Examples Infographing
Linux Find Command With Examples Infographing
Linux使用示例信息查找命令

翻译自: https://www.poftut.com/linux-find-command-with-examples/

linux基本命令示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值