Location>code7788 >text

Finding and Packing Files

Popularity:268 ℃/2024-10-30 09:21:08

File find

find is a real-time search tool that finds files by traversing a specified path.

Work Characteristics:

Slightly slower search speed

pinpoint search

Real-time search

Rich search criteria

May search only directories where the user has read and execute permissions

Format:

find [OPTION]... [find path] [find condition] [process action]

Find Path: Specify a specific target path; defaults to the current directory.

Find conditions: the specified search criteria, you can file name, size, type, permissions and other criteria; the default is to find out the specified path under all the

documented

Processing action: do the operation on the eligible files, default output to the screen

Specify the search directory level

-maxdepth level Maximum directory depth, specified directory files are level 1.
-mindepth level Minimum search depth.

For each directory, the files within the directory are processed first, and then the directory itself is processed

-depth
-d #warning: the -d option is deprecated; please use -depth instead, because the
latter is a POSIX-compliant feature

Search by filename and inode

-name "File name" # Support the use of glob, e.g. *, ? , [], [^], wildcards are enclosed in double quotes.
-iname "File name" #Not case sensitive.
-inum n #Search by inode number.
-samefile name #Files with the same inode number.
-links n #files with n links
-regex "PATTERN" #Match entire file paths by PATTERN, not by file name

Finding by Genus, Genus Group

-user USERNAME # Finds files belonging to the specified user (UID).
-group GRPNAME #Finds files belonging to the specified group (GID).
-uid UserID #Finds files belonging to the specified UID.
-gid GroupID #Finds files belonging to the specified GID number.
-nouser #Finds files that do not have an owner.
-nogroup #Finds files without a group.

Search by file type

-type TYPE
TYPE can be of the following form:
f: normal file
d: directory file
l: symbolic link file
s: socket file
b: Block device file
c: Character Device File
p: Pipe file

Empty file or directory

-empty

combinatorial condition

with: -a , default multiple conditions are with relationships
or: -o
Not: -not !

De Morgan's Law:

  • (not A) or (not B) = not (A and B)
  • (not A) and (not B) = not (A or B)

Exclude catalogs

# Find all files under /etc/ with a .conf suffix except for the /etc/security directory
find /etc -path '/etc/security' -a -prune -o -name "*.conf"
# Find all .conf files under /etc/ except for /etc/security and /etc/systemd,/etc/dbus-1 directories.
find /etc \( -path "/etc/security" -o -path "/etc/systemd" -o -path "/etc/dbus- 1" \) -a -a -path "/etc/dbus- 1" \) -a -path "/etc/dbus- 1" \)
1" \) -a -prune -o -name "*.conf"
# Exclude the /proc and /sys directories
find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1

Finding by file size

-size [+|-]#UNIT #Commonly used units: k, M, G, c (byte), note the case sensitivity.
#UNIT: # means (#-1, #], e.g.: 6k means (5k,6k)
-#UNIT # means [0,#-1],eg: -6k means [0,5k].
+#UNIT # means (#,∞), e.g.: +6k means (6k,∞)

Based on timestamp

# In days #
-atime
-ctime
-mtime # Modify time

# In "minutes".
-amin
-cmin
-mmin

# # means [#,#+1)
+# # means [#+1,∞]
-# # denotes [0,#)

Search by Permission

-perm [/|-]MODE
MODE # Exact permission match
/MODE #Any class (u,g,o) of objects must have a one-bit match in permissions, or relations, + obsolete since CentOS 7
-MODE #Each class of objects must also have the specified permissions, or the relation
0 means no concern

Description:

find -perm 755 will match files with a permission pattern of exactly 755

As long as find -perm /222 matches when any person has write permission

find -perm -222 only matches if everyone has write access

find -perm -002 will only match if someone else (other) has write permissions

regular expression (math.)

-regextype type
Changes the regular expression syntax understood by -regex and -iregex tests
which occur later on the command line. Currently-implemented types are
emacs (this is the default), posix-awk, posix-basic, posix-egrep and posixextended.
-regex pattern
File name matches regular expression pattern. This is a match on the whole
path, not a search. For example, to match a file named `./fubar3', you can
use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'. The regular
expressions understood by find are by default Emacs Regular Expressions, but
this can be changed with the -regextype option.

Processing Action

-print: default processing action, displayed to the screen.
-ls: similar to the "ls -dils" command on the found files.
-fls file: find all the files of the long format information saved to a specified file, equivalent to -ls > file
-delete: delete the found files, use with caution!
-ok COMMAND {} \; Execute the command specified by COMMAND for each file found, before executing the command for each file, it will
Interactively ask the user for confirmation before executing the command for each file
-exec COMMAND {} \; Execute the command specified by COMMAND for each file found.
{}: Used to reference the name of the found file itself.

practice

# 1, find /var directory under the user root, and the group mail all the documents
find /var -user root -group mail

# 2, find /var directory does not belong to root, lp, gdm all the files
find /var ! \(-user root -o -user -o lp -user gdm\)

# 3, find /var directory in the last week, its content has been modified, while the owner is not root, nor is the postfix file
find /var -mtime -7 -type f -a ! \( -user root \) -ls

# 4. Find files on the current system that have no owner or group and have been accessed in the last week.
find / -nouser -nogroup -atime -7

# 5, find /etc directory is larger than 1M and the type of ordinary file all the files
find /etc -size +1M -type f -ls

# 6, find /etc directory all users do not have write access to the file
find /etc -perm

# 7, find /etc directory at least one class of users do not have execute permission of the file
find /etc -type f -perm /022

# 8, find /etc/ directory, all users have execute permission, and other users have write permission file
find /etc/ -type f -perm -113

Compression and decompression

compress and uncompress

gzip and gunzip

bzip2 and bunzip2

xz and unxz

zip and unzip

zip can pack directories and multiple files into a single file and compress it, but it may lose file attribute information such as owner and group information.

message, it is generally recommended to use tar instead.

Command Format:

# zip
zip [options] target_name source_name
-r # Recursive compression, compresses all subdirectories and files in the specified folder.

# Unzip
unzip [options] -d
-d # Unzip to the specified directory
-v # View the contents of the tarball
-t # Verify that the package is complete

pack and unpack

tar

tar that Tape ARchive tape archive, you can directories and multiple files packaged in a file, and can be compressed to retain the file attributes are not lost, commonly used in backup functions, we recommend using the

The corresponding file has a .tar extension

specification

tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ][-b <Number of blocks>][-C <Destination directory>][-f <Backup file>][-F
<Script file>][-K <File>][-L <Media capacity>][-N <Date time>][-T <Model file>][-V <Volume name>][-X
<model file>][-<device-number><storage-density>][--after-date=<datetime>][--atime-preserve][--
backuup=<backup-method>][--checkpoint][--concatenate][--confirmation][--delete][--
exclude=<paradigm-style>][--force-local][--group=<group-name>][--help][--ignore-failedread][--
new-volume-script=<Script file>][--newer-mtime][--no-recursion][--null][--numeric-owner
numeric-owner][--owner=<user-name>][--posix][--preserve][--preserve-order][--preservepermissions][--
record-size=<number-of-blocks>][--recursive-unlink][--remove-files][--rshcommand=<
execute-command>][--name-owner][--suffix=<backup-end-string>][--totals][--usecompress-
program=<execute command>][--version][--volno-file=<numbered file>][file or directory...]
-

selection

-A or --catenate Add new file to an existing backup file.
--b<number-of-blocks> or --blocking-factor=<number-of-blocks> Sets the number of blocks per record, each with a block size of 12 Bytes.
-B or --read-full-records Resets the block size when reading data.
-c or --create Create a new backup file.
-C<destination-directory> or --directory=<destination-directory> Switch to the specified directory.
-d or --diff or --compare Compare differences between files within the backup file and on the file system.
-f<backup file> or --file=<backup file> Specifies the backup file.
-F<Script file> or --info-script=<Script file> Execute the specified Script file each time the tape is changed.
-g or --listed-incremental Handles large backups in GNU format.
-g or --incremental Handles large backups in the old GNU format.
-h or --dereference Do not create a symbolic link, copy the original file pointed to by the link directly.
-i or --ignore-zeros Ignore 0 Byte blocks in backup files, that is, EOF.
-k or --keep-old-files Unwrap the backup file without overwriting existing files.
-k<file> or --starting-file=<file> Restore from the specified file.
-l or --one-file-system The file or directory being copied must reside on the same file system as the one on which the tar command was executed, otherwise it will not be copied.
or it will not be copied.
-L<media-length> or --tape-length=<media-length> Sets the per-body capacity in 1024 Bytes.
-m or --modification-time Restore a file without changing the file's modification time.
-M or --multi-volume Use multi-volume mode when creating, restoring, or listing the contents of a backup file.
-N<date-format> or --newer=<datetime> Save only files updated from the specified date to the backup file.
-o or --old-archive or --portability Use V7 format when writing information to the backup file.
-o or --stdout Outputs files restored from a backup file to a standard output device.
-p or --name-permissions Restore a file with the original file permissions.
-P or --absolute-names Use absolute names for file names, without removing the "/" sign before the file name.
-r or --append Adds a new file to the end of an existing backup file.
-R or --block-number Lists the block number of each message in the backup file.
-s or --name-order Restore files in the same order as they are stored within the backup file.
-S or --sparse If a file contains a large number of consecutive 0 bytes, store this file as a sparse file.
-t or --list List the contents of the backup file.
-T<paradigm-file> or --files-from=<paradigm-file> Specifies a paradigm file containing one or more paradigm styles that lets tar unpack or
create files that match the set conditions.
--u or --update Replaces only files that are newer than those in the backup file.
-U or --unlink-first Unlinks files before unzipping them to restore them.
-v or --verbose Display the command execution process.
-V<volume-name> or --label=<volume-name> Creates a backup file using the specified volume name.
-w or --interactive Ask the user first if a problem is encountered.
-w or --verify After writing a backup file, verify that the file is correct.
-x or --extract or --get Restore files from a backup file.
-X<model-file> or --exclude-from=<model-file> Specifies a model file containing one or more model styles, allowing ar to exclude
files that match the set criteria.
-z or --gzip or --ungzip Processes backup files with the gzip command.
-z or --compress or --uncompress Process backup files through the compress command.
--<device-number><storage-density> Sets the number of the peripheral device to be used for backup and the density of the data to be stored.
--after-date=<datetime> This parameter has the same effect as specifying the "-N" parameter.
--atime-preserve Do not change the file access time.
--backup=<backup-method> or --backup Backup the file before removing it.
--checkpoint List directory names when reading backup files.
--concatenate This parameter has the same effect as specifying "-A".
--confirmation This parameter has the same effect as specifying the "-w" parameter.
--delete Removes the specified file from the backup file.
--exclude=<model style> Exclude files that match the model style.
--group=<group name> Sets the group of files added to the device file to the specified group.
--help Online help.
--ignore-failed-read Ignore data read errors without interrupting program execution.
--new-volume-script=<Script file> This parameter has the same effect as specifying the "-F" parameter.
--newer-mtime Save only changed files.
--no-recursion No recursion, i.e., all files and subdirectories in the specified directory are not processed.
--null Reads file names from null devices.
--numeric-owner Replaces user names and group names with user identifiers and group identifiers.
--owner=<user name> Sets the owner of files added to the backup file to the specified user.
--posix Use POSIX format when writing data to the backup file.
--preserve This parameter has the same effect as specifying the "-ps" parameter.
--preserve-order This parameter has the same effect as specifying the "-A" parameter.
--preserve-permissions This parameter has the same effect as specifying the "-p" parameter.
--record-size=<number-of-blocks> This parameter has the same effect as specifying the "-b" parameter.
--recursive-unlink Unlink all files in the entire directory before unlinking compressed files to restore the directory.
--remove-files Removes files once they have been added to the backup file.
--rsh-command=<execute-command> Sets the command to be executed on the remote host in place of the rsh command.
---same-owner Attempts to restore a file with the same file owner.
--suffix=<backup string> Make a backup before removing the file.
---totals List file sizes after backup files are created.
--use-compress-program=<Execute command> Process the backup file with the specified command.
--version Displays version information.
--volno-file=<number-file> Replaces the preset volume number with the number in the specified file.

split

The split command can split a file into multiple files

# Split a large tar file into multiple smaller files
split -b Size -d tar-file-name prefix-name
Example.
split -b 1M mybackup-parts
#Split into multiple smaller files with numeric suffixes
split -b 1M -d mybackup-parts

Combine multiple small cut files into one large file

cat mybackup-parts* > 

Commonly used commands:

tar -zcvf test/ # Packaging
tar -zxvf test/ # unpack it

cpio

cpio is a long-established packaging and unpacking tool, but it is also less commonly used today.

The cpio command is a tool to package files for backup and restore by means of redirection, and it can decompress files ending in ".cpio" or ".tar".

Format:

cpio [options] > filename or device name
cpio [options] < filename or device name

Commonly used options:

-o output mode, pack, pack the standard input file name and send it to the standard output.
-i input mode, unpack, unpacks the standard input packaged file names to the current directory.
-t preview, view the list of files contained in the standard input packaged file.
-O filename Output to a specified archive filename.
-A Append files to an existing archive.
-I filename Decompresses the specified archive filename.
-F filename Replaces standard input or output with the specified filename.
-d Unpacks a directory, which is automatically created during a cpio restore.
-v Displays the names of the files being packed.

Tip

Please, this article for the author to organize the study notes, only for personal reference and combing ideas, errors and omissions, please provide more guidance.

Such as, this article refers to a variety of resources to organize not, and therefore did not apply for the original statement, if any infringement, please contact to delete.

If, if you need to reprint please indicate the source of the big brother, the garden is not much more generous.

Also, this article is for learning purposes only, please do not use it in illegal ways.