Linux delve 14- file manipulation

Posted by Eugene on Wed, 26 Jan 2022 19:14:28 +0100

-----Latest update [2022-01-23]-----

Preview of this article's directory structure:

  • 1, Introduction
  • 2, File operation
    1. Create file: touch
    2. Copying files: cp
    3. Move or rename files: mv
    4. Delete file: rm
  • 3, File permissions
    1. Permission Description: read, write and execute
    2. Permission Description: belongs to master, user group and other users
    3. Special permission: setuid
    4. File mode
    5. Permission modification: chmod
    6. User mask: umask
  • 4, Symbolic link
    1. Link Concept
    2. Create new link: ln
    3. How basic file commands work
    4. Symbolic links: ln -s
  • 5, File search
    1. View files related to a specific directory: where is
    2. Viewing files by searching the database: locate
    3. View files by searching the directory tree: find
  • 6, Reference

1, Introduction

This document mainly discusses the creation, copying, renaming, permission management and other operations of ordinary files in Linux.

2, File operation

1. Create file: touch

1) Automatically create files

In many cases, Linux will automatically create files for us. For example:

  • Many programs automatically create files when needed. (for example, use vim to open a file that does not exist.
  • When the output is redirected to a file that does not exist, the shell creates the file.
  • When a file is copied, the copying program creates a new file.

2) Create an empty file: touch

The touch program is often used to create a new empty file. For example:

[11:41 @nosee ~/Documents]$ ls -l
total 0
[11:41 @nosee ~/Documents]$ touch a
[11:41 @nosee ~/Documents]$ ls -l
total 0
-rw-rw-r-- 1 nosee nosee 0 Jan 22 11:41 a

The main purpose of touch is to change the modification time of the file without changing the file, just as you reach out and gently touch the file. (change file timestamps)

Syntax: touch [option] FILE...

Common options:

  • -t time, replace the current time with [[CC]YY]MMDDhhmm[.ss] (time format). By default, touch sets both the modification time and the access time to the current time.
  • -m. Only the modification time is changed.
  • -a. Only change the access time.

Example 1: modification time

[11:43 @nosee ~/Documents]$ ls -l
total 4
-rw-rw-r-- 1 nosee nosee 8 Jan 22 11:43 a
[11:45 @nosee ~/Documents]$ touch -t 01012359 a
[11:46 @nosee ~/Documents]$ ls -l
total 4
-rw-rw-r-- 1 nosee nosee 8 Jan  1 23:59 a

Example 2: create multiple files at the same time

[11:46 @nosee ~/Documents]$ touch b c
[11:47 @nosee ~/Documents]$ ls -l
total 4
-rw-rw-r-- 1 nosee nosee 8 Jan  1 23:59 a
-rw-rw-r-- 1 nosee nosee 0 Jan 22 11:47 b
-rw-rw-r-- 1 nosee nosee 0 Jan 22 11:47 c

2. Copying files: cp

cp - copy files and directories

1) The simplest example:

[12:04 @nosee ~/Documents]$ ls -l
-rw-rw-r-- 1 nosee nosee 0 Jan 22 12:03 a.txt
[12:04 @nosee ~/Documents]$ cp a.txt b.txt
[12:04 @nosee ~/Documents]$ ls -l
-rw-rw-r-- 1 nosee nosee 0 Jan 22 12:03 a.txt
-rw-rw-r-- 1 nosee nosee 0 Jan 22 12:04 b.txt

By default, when using the cp program to copy a file, if the target file does not exist, cp will create the file. If the directory already exists, cp will replace the file.

Syntax: CP [option] [-T] SOURCE DEST
Or CP [option] SOURCE... DIRECTORY
Or CP [option]- t DIRECTORY SOURCE...

Common options:

  • -i. (interactive) prompt before replacing the file (ignore the previous - n option)
  • -n. Do not overwrite existing files (ignoring the previous - i option)
  • -p. (preserve) the target file and the source file have the same modification time, access time and permissions
  • -R/ -r, (recursive) copies a directory and all its files to another directory
    copy directories recursively
    Example:
[12:35 @nosee ~/Documents]$ ls -l a*
-rw-rw-r-- 1 nosee nosee 0 Jan 22 12:03 a.txt
[12:36 @nosee ~/Documents]$ cp -p a.txt aaa
[12:36 @nosee ~/Documents]$ ls -l a*
-rw-rw-r-- 1 nosee nosee 0 Jan 22 12:03 aaa
-rw-rw-r-- 1 nosee nosee 0 Jan 22 12:03 a.txt

2) Copy files to different directories (the usage of the tree command was introduced in my last file, which is mainly used to display the directory tree)

[12:45 @nosee ~/Documents]$ tree
.
├── ad
├── a.txt
└── b.txt

1 directory, 2 files
[12:46 @nosee ~/Documents]$ cp a.txt b.txt ad
[12:46 @nosee ~/Documents]$ tree
.
├── ad
│   ├── a.txt
│   └── b.txt
├── a.txt
└── b.txt

1 directory, 4 files

3) Copy a directory to another directory

Example: copy directory ad and its contents to directory ccc (the target directory ccc does not exist)

[12:54 @nosee ~/Documents]$ cp -r ad ccc
[12:54 @nosee ~/Documents]$ tree
.
├── ad
│   ├── a.txt
│   └── b.txt
├── a.txt
├── b.txt
└── ccc
    ├── a.txt
    └── b.txt

2 directories, 6 files

Example: copy directory ad and its contents to directory ccc (target directory ccc already exists)

[12:54 @nosee ~/Documents]$ cp -r ad ccc
[12:54 @nosee ~/Documents]$ tree
.
├── ad
│   ├── a.txt
│   └── b.txt
├── a.txt
├── b.txt
└── ccc
    └── ad
        ├── a.txt
        └── b.txt

3 directories, 6 files

If the target directory ccc already exists and you don't want to copy the original directory, you can use cp -r ad/* ccc.

3. Move or rename files: mv

mv - Rename (move) files. (move (rename) files)

Using mv program, you can "move" files from one location to another. If the new location is in the same directory as the original, the actual result is to "Rename" the original file. When mv moving a directory, all files and subdirectories in the directory are moved at the same time.

Syntax: MV [option] [-T] SOURCE DEST
Or MV [option] SOURCE... DIRECTORY
Or MV [option]- t DIRECTORY SOURCE...

Simple example:

[21:08 @nosee ~/Documents]$ tree
.
├── ad
├── a.txt
└── b.txt

1 directory, 2 files
[21:08 @nosee ~/Documents]$ mv a.txt ad/
[21:09 @nosee ~/Documents]$ tree
.
├── ad
│   └── a.txt
└── b.txt

1 directory, 2 files

Common options:

  • -i. (interactive) ask before replacing existing files.
  • -f. (force) replace files without asking, so use it carefully.

Example: move multiple files

[21:11 @nosee ~/Documents]$ tree
.
├── ad
├── a.txt
└── b.txt

1 directory, 2 files
[21:11 @nosee ~/Documents]$ mv a.txt b.txt ad/
[21:11 @nosee ~/Documents]$ tree
.
└── ad
    ├── a.txt
    └── b.txt

1 directory, 2 files

The above example can also use MV * Txt ad or MV txt ad.

Note: if the file already exists in the target directory, the source file will replace the target file.

Example: Rename

[21:22 @nosee ~/Documents]$ ls
ad
[21:22 @nosee ~/Documents]$ mv ad add
[21:22 @nosee ~/Documents]$ ls
add

4. Delete file: rm

rm - delete a file or directory. (remove files or directories)

Syntax: RM [option] [FILE]...

Common options:

  • -i. (interactive) request permission before deleting each file.
  • -f. (force mandatory) delete it directly without asking. Use it carefully!
  • -r. (recursive) deletes the entire directory tree

Note:
1) If you try to delete a file without write permission, rm will request permission, and the system will ignore the file permission protection mechanism with the user's permission.
2) The - f option will ignore the file permissions and the - i option and delete the file directly.
3) When using the - r option, you must pay special attention. It is best to use it together with the - i option. Before deleting, you'd better use the pwd command to ensure your working directory.

Example: delete the entire directory tree

[21:43 @nosee ~/Documents]$ tree
.
├── add
│   ├── a.txt
│   └── b.txt
└── ccc
    ├── a.txt
    └── b.txt

2 directories, 4 files
[21:43 @nosee ~/Documents]$ rm -r ccc/
[21:44 @nosee ~/Documents]$ tree
.
└── add
    ├── a.txt
    └── b.txt

1 directory, 2 files

Last question: is it possible to recover deleted files\ (\ bigg({impossible!} \bigg)\)

be careful:
Once the file is deleted, there is no way to retrieve the file. However, the actual disk space used by the file has not been cleared. The file system only identifies this part of disk space as reusable. Finally, this part of disk space will be reused and the old data will be overwritten by the new data. In a busy large Unix system, this can take only a few seconds. However, it is uncertain when this will happen. Sometimes, old data may be hidden in the unused part of the disk for a long time. In fact, there are some special tools: the "restore delete" tool can view the unused part of the disk and restore the old data.
In addition, even if the data is overwritten, it is possible to recover the data in extreme cases, as long as the data is not overwritten multiple times. If the hard disk is taken to the laboratory with very expensive data recovery equipment, it is possible to recover the covered old data on the hard disk by analyzing the magnetic traces on the magnetic surface of the hard disk.

3, File permissions

Unix maintains a set of file permission s for each user, commonly known as permissions.

1. Permission Description: read, write and execute

There are three kinds of permissions, including read permission, write permission and execute permission. These three permissions are independent of each other.

The exact meaning of file permissions depends on the type of file. For a non executable file, having execution permission makes no sense.

jurisdiction explain
Ordinary file -
Read (r) read file
Write (w) write file
Execute (x) Executive document
catalogue -
Read (r) Read directory
Write (w) Create, move, copy, or delete directory entries
Execute (x) search for directory

The three permissions are different from each other, but can be used in combination. For example, in order to modify a file, you need to have both read and write permissions on the file. In order to run a shell script, you need to have both read and execute permissions.

For directories, the permissions of directories are different from ordinary files. Read permission allows the user to read the files in the directory, write permission allows the user to modify the directory (create, move, copy and delete), and execution permission allows the user to search the directory.

If you only have read permission, you can only enumerate the file names in the directory, that's all. You cannot view the size of a file, view subdirectories, or change directories using a cd unless you have execute permission.

Note:
A directory entry contains only a file name and a pointer to the file, not the actual file. Therefore, you can't view the file size when you have only read permission to the directory.

2. Permission Description: belongs to master, user group and other users

File permissions control access to files by user IDs. Each file has three groups of permissions: one for the owner, one for the group, and one for other users. Then each group of permissions is divided into three parts: read, write and execution.

When using ls -l to view the file, you will see a string similar to - rwsr-xr-x, which is the permission of the file. For example:

[nosee@instance-4 ~]$ ls -ld test
drwxr-xr-x 3 nosee nosee 4096 Jan 21 01:52 test

In the above example, the owner of the file is the first nose viewed from left to right, and the second nose is the user group.
The permission of the file is rwxr-xr-x (the first character d indicates that the file type is directory). It is divided into three groups:
The first three characters (the first group of permissions) indicate the permissions of the user owner on the file.
The middle three characters (the second group of permissions) are used to indicate the permissions of the user group on the file.
The last three characters (the third group of permissions) represent the permissions of other user IDs on the file.

Note:
1) Owner is the unique user ID that can change file permissions. (with the exception of root, root can do anything)
2) Other users in the third group do not include the user's owner and members in the user group.
3) Use the command id or groups to view the user group to which your user id belongs. Generally, it is a user group with the same name as the user id.
4) Unix systems allow a user ID to belong to multiple groups at the same time.
5) Unless you really need to share files with other users in the group, you can ignore this idea because it is rarely needed.

3. Special permission: setuid

Usually, whenever you run a program, the program will run under the authorization of your user ID. This means that your program has the same permissions as the user ID.

Sometimes, ordinary user IDs may need to run programs with special permissions. To make this possible, people designed a special file setting permission to allow other users to access the file as if they were the owner (creator) of the file. This special permission is called setuid or suid.

In most cases, setuid is used to allow the user to identify and run programs selected from programs owned by root. This means that no matter which user ID runs the program, it runs with root privileges. This allows the program to perform tasks normally performed by the super user.

For example, in order to change the password, you need to use the passwd program. However, to change the password, the program must modify the / etc/passwd file and / etc/shadow file, and modifying these two files requires the special permission of root. For this reason, the passwd program itself is stored in a file owned by root with setuid open.

setuid permission is to replace the letter "x" with the letter "s" in the file permission. For example:

[nosee@instance-4 ~]$ ls -l /usr/bin/passwd 
-rwsr-xr-x 1 root root 63736 Jul 27  2018 /usr/bin/passwd

4. File mode

Unix uses a compact 3-digit code to represent a complete set of file permissions. This code is called file mode, or simply mode.

For example, rw ------- indicates the mode of 600.
6 = permission of owner
0 = group permissions
0 = permissions of other user IDs

For read, write and execute, each permission has a corresponding permission value:
Read permission = 4
Write permission = 2
Execution Authority = 1
No permission = 0

For example rwxrw-r-- the corresponding permission mode can be calculated as follows: (4 + 2 + 1) = 7
rwx = 4+2+1= 7
rw- = 4+2+0 = 6
r-- = 4+0+0 = 4
Therefore, the permission mode of the file is: 764

5. Permission modification: chmod

To change the permissions of the file, you need to use the chmod command. (change file mode bits)

Syntax: Chmod [option] MODE[,MODE]... FILE...
Or Chmod [option] OCTAL-MODE FILE...
Or Chmod [option]-- reference=RFILE FILE...

Only the owner and super user can change the permission mode of the file.

Example:

[nosee@instance-4 ~]$ ls -l letter.txt 
-rw-r--r-- 1 nosee nosee 611 Jan 15 18:19 letter.txt
[nosee@instance-4 ~]$ chmod 666 letter.txt 
[nosee@instance-4 ~]$ ls -l letter.txt 
-rw-rw-rw- 1 nosee nosee 611 Jan 15 18:19 letter.txt

6. User mask: umask

When Unix creates a new file, the following modes will be specified for the file according to the file type:
666: non executable ordinary file
777: executable ordinary file
777: Contents

In this initial mode, Unix subtracts the user mask value, which is the value of the permission of the new file.

umask - Display or set file mode mask. View the usage instructions of umask: help umask. Umask is a built-in command.

Syntax: umask [-p] [-S] [mode]

Example: (you can put this command into the initial file)

umask 022

If the user mask is 022, the default mode for creating a new ordinary file is 644, that is, rw-r--r --. The mode of creating executable files and directories is 755, namely rwxr-xr-x.

To make the file more secure, you can set the user mask to 077, that is:

umask 077

4, Symbolic link

1. Link Concept

When Unix creates files, it does two things. First, Unix reserves a space in the storage node to store data. Second, Unix creates a structure called index node or i-node to store the basic information of files.

The i node contains all the file system information required to use the file. On Unix systems, you can use the stat command to easily view the contents of the i node of a specific file.

Syntax: stat [option] FILE..., display file or file system status.

For example:

[nosee@instance-4 ~]$ ls -lshi letter.txt 
405854 4.0K -rw-rw-rw- 1 nosee nosee 611 Jan 15 18:19 letter.txt
[nosee@instance-4 ~]$ stat letter.txt 
  File: letter.txt
  Size: 611       	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 405854      Links: 1
Access: (0666/-rw-rw-rw-)  Uid: ( 1001/   nosee)   Gid: ( 1002/   nosee)
Access: 2022-01-21 03:53:20.464013524 +0000
Modify: 2022-01-15 18:19:35.982654205 +0000
Change: 2022-01-22 18:11:03.352578229 +0000
 Birth: -

When dealing with a directory, it is as if the directory actually contains files. In fact, the directory only contains the name of the file and the I node number of the file. Therefore, the contents of the directory are quite small: there is only one column of names, and each name corresponds to an i node number.

The connection between the file name and the i node is called a link. Conceptually, links connect the file name to the file itself.

In fact, an i node can be referenced by more than one file name. One of the best features of Unix is to allow multiple links. The unique identifier of a file is its i node, not its name. By using the ln command, you can create multiple files with the same I node.

2. Create new link: ln

Whenever a new file is created, the file system automatically creates a link between the file name and the file. However, sometimes you may want to create a new link for an existing file, and you can use the ln (link link) command. (make links between files)

Syntax: ln [option] [-T] TARGET LINK_ NAME
Or ln [option] TARGET
Or ln [option] TARGET... DIRECTORY
Or ln [option]- t DIRECTORY TARGET...

1) Create a new link for a single file

Example: (ls command with - i option can display the i node number of the file)

[nosee@instance-4 ~]$ ls -li l*
405854 -rw-rw-rw- 1 nosee nosee 611 Jan 15 18:19 letter.txt
[nosee@instance-4 ~]$ ln letter.txt letter
[nosee@instance-4 ~]$ ls -li l*
405854 -rw-rw-rw- 2 nosee nosee 611 Jan 15 18:19 letter
405854 -rw-rw-rw- 2 nosee nosee 611 Jan 15 18:19 letter.txt

At this time, both file names (letter and letter.txt) point to the same file (i.e. the same I node: 405854). The number 2 after the file permission is the number of links of the file.

2) Create a new link for one or more normal files and place the new link in the specified directory.

[nosee@instance-4 ~]$ ls -li l*
405854 -rw-rw-rw- 2 nosee nosee 611 Jan 15 18:19 letter
405854 -rw-rw-rw- 2 nosee nosee 611 Jan 15 18:19 letter.txt
[nosee@instance-4 ~]$ mkdir ld
[nosee@instance-4 ~]$ ln letter* ld
[nosee@instance-4 ~]$ ls -liR l*
405854 -rw-rw-rw- 4 nosee nosee  611 Jan 15 18:19 letter
405854 -rw-rw-rw- 4 nosee nosee  611 Jan 15 18:19 letter.txt

ld:
total 8
405854 -rw-rw-rw- 4 nosee nosee 611 Jan 15 18:19 letter
405854 -rw-rw-rw- 4 nosee nosee 611 Jan 15 18:19 letter.txt

Command ln letter Txt LD has the same effect as the above command ln letter* ld. At this time, the number of file connections is already 4.

3. How basic file commands work

1) Create file / directory
When creating a new file or directory, Unix will set aside the corresponding storage space and create an i node. Unix then places a new entry in the appropriate directory by using the specified file or directory name and the new I node number.

2) Copying files: cp
When copying the existing content, Unix replaces the content of the target file with the content of the source file, but the i node number is not modified.
When copying a nonexistent file, Unix first creates a new file with its own i node, and then copies the contents of the old file to the new file. After copying, the contents of the two files are the same, but the i nodes are different.

3) Rename / move files: mv
When renaming or moving a file, Unix changes the file name, or moves the directory entry, or both, but saves the same i node number.

4) Create link: ln
When creating a new link to an existing file, Unix creates a new directory entry with the specified file name and points to the i node of the original file. Such a file has two file names, but points to the same i node number.

5) Remove links: rm, rmdir
When removing a link, Unix removes the connection between the file name and the i node by removing the directory entry. Unix will delete the file if it has no link. In most cases, a file has only one link, which is why rm and rmdir are used as delete commands most of the time.

4. Symbolic links: ln -s

The link types discussed earlier allow us to specify more than one name for the same file, but such links have two limitations. First, you cannot create links for directories. Second, you cannot create links for files in different file systems.

When creating directory or file links in different file systems, you need to create so-called symbol link s. To do this, you need to use the ln command with the - s option.

The symbolic link does not contain the i node number of the file, but the pathname of the original file. (similar to Windows shortcut)

Example:

[nosee@instance-4 ~]$ ls -li l*
405854 -rw-rw-rw- 1 nosee nosee 611 Jan 15 18:19 letter.txt
[nosee@instance-4 ~]$ ln -s letter.txt lt
[nosee@instance-4 ~]$ ls -li l*
405854 -rw-rw-rw- 1 nosee nosee 611 Jan 15 18:19 letter.txt
406425 lrwxrwxrwx 1 nosee nosee  10 Jan 22 20:47 lt -> letter.txt

In order to distinguish between the two types of links, conventional links are generally called hard links, while symbolic links are called soft link s. When only the "link" itself is used, it refers to a hard link.

Note: in order to display the number of hard links of a file, you can use the ls -l command to view. However, there is no way to display the number of soft links in a file, because the file system itself does not know.

5, File search

1. View files related to a specific directory: where is

The whereis program is used to view files related to specific Unix commands: binary (executable) files, source files, and document files. Where is does not search the entire file system, but only looks at the directories where such files are most likely to exist, such as / bin, / sbin, / etc, / user/share/man, etc.

whereis - locate the binary, source, and manual page files for a command.

Common commands:

  • -b. (binary) displays only the path name of the executable file
  • -m. Find only files in the online manual
  • -s. Find only the pathname of the source file

Example:

[nosee@instance-4 ~]$ whereis  mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/share/man/man1/mysql.1.gz
[nosee@instance-4 ~]$ whereis -m mysql
mysql: /usr/share/man/man1/mysql.1.gz

2. Viewing files by searching the database: locate

If there is no locate program in the system, you can use sudo apt install mlocate command to install it.

The task of the locate program is to search a special database (the database contains the pathnames of all publicly accessible files) and find all the pathnames containing specific patterns.

locate - find files by name.

Syntax: locate [option] PATTERN...

Common options:

  • -r. Use regular tabular expressions.
  • -c. Count the total number of matching files without displaying the actual file name.
  • -i. Ignore case
  • -S. (capital s) displays the information of locate database on the system

Example:

[05:48 @nosee ~/Documents]$ locate a.txt
/home/nosee/Documents/add/a.txt
/snap/snap-store/558/usr/share/gnupg/help.ca.txt
/snap/snap-store/558/usr/share/gnupg/help.da.txt
/snap/snap-store/558/usr/share/gnupg/help.ja.txt
/usr/lib/firmware/brcm/brcmfmac43340-sdio.pov-tab-p1006w-data.txt
/usr/share/doc/xorg/howto/build-mesa.txt.gz
/usr/share/gnupg/help.ca.txt
/usr/share/gnupg/help.da.txt
/usr/share/gnupg/help.ja.txt
[06:08 @nosee ~/Documents]$ locate -S
Database /var/lib/mlocate/mlocate.db:
	47,432 directories
	496,075 files
	34,387,530 bytes in file names
	12,503,803 bytes used to store database

Disadvantages: mlocate DB, this database is not updated in real time. Some newly added files may need to wait for the next database update to find them.

3. View files by searching the directory tree: find

Find is one of the most complex Unix tools. The general idea of find is to search one or more directory trees and find files that meet specific criteria according to the test conditions. Once the search is complete, find will perform some action on the found file. The action can be as simple as the display of file name.

find - search for files in a directory hierarchy

Syntax: find [- H] [- l] [- P] [- D debugopts] [- Olevel] [starting point...] [expression]

1) Common options

2) Common patterns

The general syntax of the find program is: find [path] [test]... [action]...

In order to run find, you need to specify three things (in the following order): directory path, test and action.

Example:

[nosee@instance-4 ~]$ find test -name a -print
test/a
test/mydoc/text/a

As mentioned above, this command can be divided into three parts:
Path: test, from/ Test starts by searching all files and subdirectories
Test: - name a, for each file, apply test - name a (this means to find the file named a)
Action: - print, execute action - Print (display pathname) for each file passing the test

No matter how complex the find command is, it can be analyzed in this way.

3) find command: Path

In general, the beginning of the find command consists of one or more paths that indicate where find starts its search.

Example:

[nosee@instance-4 ~]$ find ./test/
./test/
./test/A
./test/a
./test/z
./test/Z
./test/mydoc
./test/mydoc/haha.txt
./test/mydoc/text
./test/mydoc/text/a
./test/mydoc/text/a/b

4) find command: Test

Use the find command to search one or more directory trees, find files that meet the specified criteria, and then perform specific actions on the found files. To define a standard, we can specify one or more tests. Common tests are as follows:

test explain
file name -
-name pattern File name containing pattern
-iname pattern File name containing pattern (case insensitive)
File characteristics -
-type [df] File type: d = directory, f = ordinary file
-perm mode File permissions set to mode
-user userid Owner is userid
-group groupid Group is groupid
-size [-+]n[cbkMG] Size is n [character (byte), block, kilobyte, megabyte, Gigabyte]
-empty Empty file (size = 0)
Access time and modification time -
-amin [-+]n Accessed n minutes ago
-anewer file Access after file
-atime [-+]n Visited n days ago
-cmin [-+]n Status changed n minutes ago
-cnewer file Status changes after file
-ctime [-+]n Status changed n days ago
-mmin [-+]n Modified n minutes ago
-mtime [-+]n Modified n days ago
-newer file Modify after file

Example: find all directories under the test directory

[nosee@instance-4 ~]$ find test -type d
test/
test/mydoc
test/mydoc/text
test/mydoc/text/a
test/mydoc/text/a/b

Example: find the file whose permission mode is 644 in the test directory

[nosee@instance-4 ~]$ find test -perm 644
test/A
test/a
test/z
test/Z
test/mydoc/haha.txt

5) find command: actions

Action tells find what to do with the found file. Common actions are as follows:

action explain
-print Write the pathname to standard output (the default action for most find is - print)
-fprint file The result is the same as - print. Write the output to the file file
-ls Displays a long list of file details
-fls file The result is the same as - ls, and the output is written to the file file
-delete Delete search results
-exec command {} \; Execute command, {} to indicate the matching file name
-ok command {} \; Same as - exec, but confirm before running command

Example:

[nosee@instance-4 ~]$ find test -perm 644 -ls
   406488      0 -rw-r--r--   1 nosee    nosee           0 Jan 21 01:52 test/A
   406243      0 -rw-r--r--   1 nosee    nosee           0 Jan 21 01:52 test/a
   406504      0 -rw-r--r--   1 nosee    nosee           0 Jan 21 01:52 test/z
   406509      0 -rw-r--r--   1 nosee    nosee           0 Jan 21 01:52 test/Z
   393708      4 -rw-r--r--   1 nosee    nosee           7 Jan 20 19:54 test/mydoc/haha.txt

Example: delete file z

[nosee@instance-4 ~]$ tree test/
test/
├── A
├── Z
├── a
├── mydoc
│   ├── haha.txt
│   └── text
│       └── a
│           └── b
└── z

4 directories, 5 files
[nosee@instance-4 ~]$ find test/ -iname z -delete
[nosee@instance-4 ~]$ tree test/
test/
├── A
├── a
└── mydoc
    ├── haha.txt
    └── text
        └── a
            └── b

4 directories, 3 files

6, Reference

Bookcase: Chapter 25 of UNIX & Linux University Course (US): Harley Hahn, translated by Zhang Jieliang

Topics: Linux Unix