Linux file management command: dirname \ find \ finds \ in \ indir

dirname: Display the path of the file except the name

  • Function: Display the path of the file except the name.
  • Usage: dirname name dirname option
  • The main options are as follows:
Order describe
--help Display this help message and exit.
--version Print version information and exit.

case study

(1) Check the /usr/bin/sort path.

# dirname /usr/bin/sort 
/usr/bin 

(2) Check the stdio.h path.

dirname stdio.h 

find: Find a directory or file

  • Role: Find a file or directory. Access rights: all users.
  • usage:find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
  • The main options are as follows:
Order describe
path...路径 Tell find where to find what the user wants.
-exec find Execute other linux commands given by this parameter on matching files. The form of the corresponding command is '
command - and' {} ;, pay attention to the spaces between { } and ;.
-ok It has the same function as - exec, except that it interacts with the meeting person. Before executing OK, it will confirm to the user whether it is
to be executed. The main parameters of the find command are as follows.
-name Find files by filename.
-perm Find files by file permissions.
-prune Use this option to make the find command not search in the currently specified directory. If the
-depth option is used at the same time, the -prune option will be ignored by the find command.
-user Find files by file owner.
-group Find files by the group they belong to.
-mtime -n +n Find the file according to the change time of the file, -n means that the file change time is
within , and +n means that the file change time is n days ago from now. The find command also has -atime and
-ctime options, but they are all similar to the -mtime option, so only the -mtime option is introduced here.
-nogroup Finds a file that has no valid owning group, ie the file belongs to a group that does not exist in /etc/groups.
-nouser Finds a file without a valid owner, that is, the file's owner does not exist in /etc/passwd.
-newer file1 ! file2 Find files that were changed more recently than file1 but older than file2.
-type Find files of a certain type.
-size n[c] Find files with a file length of n blocks, and c indicates that the file length is in bytes.
-depth When looking for files, first look for files in the current directory, and then look in its subdirectories.
-fstype Find files located in a certain type of file system. These file system types can usually be
found in the configuration file /etc/fstab, which contains information about the file system in this system.
-mount Do not cross filesystem mount points when looking for files.
-follow If the find command encounters a symbolic link file, it follows the file pointed to by the link.
-cpio Use the cpio command on matching files to back them up to a tape device.

case study

The 5 ways the find command finds files are as follows.

(1) File name search method.

find / -name named.conf

(2) Quickly find the file method.

If you know that the file is stored in a certain directory, you can save a lot of time just looking down in this directory. For example, the named.conf file can be judged from its file extension ".conf" that it is a configuration file, so it should be in the /etc directory. At this time, the following command can be used:

find /etc -name named.conf

(3) According to the partial file name search method. Sometimes it is only known that a file contains the 4 characters abdd, so to find all files containing these 4 characters in the system, you can enter the following command:

find / -name '*abdd*'

After entering this command, the Linux system will search for all files containing the 4 characters abdd in the "/" directory (where "*" is a wildcard), such as abddrmyz and other qualified files can be displayed.

(4) According to the feature search method of the file.

Sometimes you know the size, modification date, user and other characteristics of a file, you can also use the "find" command to find out the file. For example, to find files in the system that belong to a revoked user, you can use the following command:

find / -nouser 

(5) Mixed search method search method.

The find command can use mixed search methods. For example, if you want to search for files larger than 500,000 bytes in the "/home" directory and belonging to the cao user, you can use -and (and) to combine two search parameters to form a mixed search method.

find /home -size +500000c -and -user cao

findfs: Find filesystems by list or user ID

  • Function: used to find the file system of the specified volume label.

  • usage:

    findfs LABEL=<label>
    findfs UUID=<uuid>
    
  • The main options are as follows:

 LABEL=<label>:卷标名称。
 UUID=<uuid>:分区的 UUID 号。

Explanation With the increase of the capacity and number of hard disks in the Linux system, the number of partitions in the Linux system is also increasing. Use the findfs command to quickly locate the partition location through the volume label name or UUID number

case study

To find the location of the partition whose volume label is "/var/ftp", use the following command:

#findfs LABEL=/var/ftp 
/dev/hda9 

ln: link file or directory

  • Role: Link files or directories.

  • usage:

    ​ ln [option] [source file or directory] [target file or directory]

    ​ ln [option] [source file or directory...] [destination directory]

  • The main options are as follows:

Order describe
-b–backup Delete, overwrite the previous backup of the target file.
-d-F–directory Create a hard link to the directory.
-f–force Forcibly establish a link to the file directory, regardless of whether the file directory exists or not.
-i–interactive Ask the user before overwriting existing files.
-n–no-dereference Treat the target directory of a symbolic link as a normal file.
-s–symbolic Make symbolic links to source files instead of hard links.
-S<suffix backup string>–suffix=<suffix backup string> After backing up the target file with the "-b" parameter, a backup string will be added to the end of the backup file. The default backup string is the symbol "~", which can be changed through the "-S" parameter.
-v–verbose Display the command execution process.
-V<backup method>–version-control=<backup method> After backing up the target file with the "-b" parameter, a backup character string will be added to the end of the backup file. When using different backup methods, backup character strings with different suffixes will also be generated.
–help online help.
–version Display version information.

Description The ln command is used to link files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, all previously specified files or directories will be copied to the directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

链接有两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。 建立硬链接时,链接文件和被链接文件必须位于同一个文件系统中,并且不能建立指向目录的 硬链接。而对于符号链接,则不存在这个问题。默认情况下,ln 产生硬链接。

在硬链接的情况下,参数中的“目标”被链接至[链接名]。如果[链接名]是一个目录名, 系统将在该目录之下建立一个或多个与“目标”同名的链接文件,链接文件和被链接文件的内 容完全相同。如果[链接名]是一个文件,用户将被告知该文件已存在且不进行链接。如果指定 了多个“目标”参数,那么最后一个参数必须为目录。

案例练习

(1)为当前目录下的文件 lunch 创建一个符号链接/home/xu。

$ ln - s lunch /home/xu

说明 给 ln 命令加上-s 选项,则建立符号链接。如果[链接名]已经存在,但不是目录, 将不进行链接。[链接名]可以是任何一个文件名(可包含路径),也可以是一个目录, 并且允许它与“目标”不在同一个文件系统中。如果[链接名]是一个已经存在的目录, 系统将在该目录下建立一个或多个与“目标”同名的文件,此新建的文件实际上是指 向原“目标”的符号链接文件。

(2)创建到一个文件的另一个链接。

#ln -f chap1 intro 

这会将 chap1 链接到新的名称 intro。如果 intro 不存在,则会创建该文件名。如果 intro 已 经存在,那么这个文件会被替换为指向 chap1 的一个链接,然后 chap1 和 intro 文件名会指向同 一个文件。对这里面任何一个文件的更改都会出现在另一个文件中。如果一个文件名被 rm 命 令删除,那么该文件并没有完全被删除,因为它依旧以其他名字存在。

(3)将文件链接为另一个目录中的相同名字。

#ln index manual

这会将 index 链接到新名称 manual/index。

(4)将几个文件链接为另一个目录中的名称。

ln chap2 jim/chap3 /home/manual

这会将 chap2 链接到新名称/home/manual/chap2,将 jim/chap3 链接到新名称/home/ manual/chap3。

(5)在不指明目标文件参数的情况下得到相同的结果。

ln -s /tmp/toc

lndir:链接目录内容

  • 作用:链接目录内容。
  • 用法:lndir [-ignorelinks] [-silent] [源目录] [目的目录]

主要选项如下:

命令 描述
-ignorelinks 直接建立符号链接的符号链接。
-silent 不显示命令执行过程。

说明 执行 lndir 命令,可一口气把源目录下的文件和子目录统统建立起相互对应的符号 链接。

Guess you like

Origin blog.csdn.net/u014096024/article/details/131628257