Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of Linux File and Directory Management commands

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The operation of files and directories under Linux is the basis of learning linux, and the operation of files is nothing more than adding, deleting, changing and checking, and so on. The operation of the directory is also the addition, deletion, modification and modification of the directory and other operations. Before you understand directory management, you need to understand the structure of the directory. The directory under linux is a tree structure, all directories start from the root (/), and other file systems are mounted to the directory for use. When accessing a directory, there are relative paths and absolute paths.

Relative path: the path is written not from the root, but on the basis of the current path. For example, when / usr/share/doc wants to go to / usr/share/man, it can be written as: cd.. / man

Absolute path: the path is written from the root, for example: / usr/local

1. Common commands for file and directory management

Ls / cd / cp / mv / pwd / rm / rmdir / chgrp / chown / chmod / find / ln / mkdir / whereis / which / file / touch / locate / dd / updatedb / rename

Next I will summarize the common options for these commands

1.1 ls is used to display a list of targets and is a highly used command in Linux.

-a: displays all files and directories (ls internally calls the file name or directory name "." It is regarded as a shadow, no

Will be listed)

-A: show unshadowed files. And ".." List of all files except

-C: multiple columns display the output results. This is the default option

-l: contrary to the "- C" option, all output information is output in a single-column format, not as multiple columns

-c: when used with the "- lt" option, the output directory content is sorted according to the file status time, and the basis for sorting

Is the ctime field in the Inode of the file. When used with the "- l" option, the sorted sentence is the shape of the file

State change time

-d: displays only the directory name, not the list of contents under the directory. Displays the symbolic link file itself, while the

Do not display the list of directories it points to

-I: displays the file index node number (inode). An Inode represents a file;-- file-

Type: vs.

The-F "option has the same function, but does not display" * "

-k: displays the file size in KB (kilobytes)

-l: displays the list of contents under the directory in a long format. The output information includes files from left to right.

Name, file type, permission mode, number of hard connections, owner, group, file size and last modification of the file

Change the time, etc.

-m: separate the names of each file and directory with the "," sign; output a list of files horizontally

-r: sorts and outputs a list of directory contents in reverse order by file name

-s: displays the size of files and directories in chunks

-t: sort by the change time of files and directories

-R: recursive processing, processing all files and subdirectories under the specified directory together

-- full-time: list the complete date and time

-- color [= WHEN]: highlight different types with different colors.

-F: appends the type identifier of the file to each output item, which can be used to enter the file according to special characters

Row classification

Specific meaning: "*" indicates an ordinary file with executable permissions, "/" indicates a directory, and "@" indicates a symbol.

Link, "|" indicates command pipeline FIFO, and "=" indicates sockets socket. When a file is an ordinary file

Do not output any identifiers

1.2 cd is used to switch working directories.

Cd enters the user's home directory

Cd ~ enter the user's home directory

Cd-return to the directory where you were before entering this directory

Cd.. Return to the parent directory (if the current directory is "/", it will still be "/"; ".." after execution. " For the meaning of the superior directory)

Cd.. /.. Return to the upper two-level directory

Cd! $uses the parameter of the previous command as the cd parameter.

1.3 cp copy command

-a: the effect of this parameter is the same as specifying the "- dpR" parameter at the same time

-I: (default option) ask the user before overwriting existing files

-f: forcibly copy a file or directory, regardless of whether the target file or directory already exists

-p: preserve the attributes of the source file or directory

-Rbarb r: recursive processing that processes all files in the specified directory with subdirectories

-l: establish a hard connection to the source file instead of copying the file

-s: establish a symbolic connection to the source file instead of copying the file

-d: when copying a symbolic connection, the target file or directory is also established as a symbolic link and points to the source text

The original file or directory to which a piece or directory is connected

-u: after using this parameter, only when the change time of the source file is newer than that of the target file, or when the names are mutual

Copy the file only when the corresponding target file does not exist

-b: back up the target file before overwriting the existing file destination

-v: displays the actions performed by the command in detail.

1.4 mv is used to rename files or directories, or to move files from one directory to another.

-I: (default) interactive operation, asking the user before overwriting, if the source file and the target file or destination

If the file in the directory has the same name, ask the user whether to overwrite the target file. The user enters "y" to indicate that the target will be overwritten

File; enter "n" to cancel the movement of the source file. This avoids mistakenly overwriting the file.

-b: create a backup for the file before it is overwritten when it exists

-f: if the target file or directory duplicates with the existing file or directory, the existing file or directory is directly overwritten

Record

-u: the move operation is performed only when the source file is newer than the target file or when the target file does not exist.

1.5 rm can delete one or more files or directories in a directory, or you can set a directory

All files and their subdirectories under them are deleted. For linked files, just delete the entire link

File, while the original file remains unchanged.

-d: delete the hard connection data of the directory to be deleted directly to 0, and delete the directory

-f: force deletion of files or directories

-I: ask the user before deleting an existing file or directory

-r or-R: recursive processing that processes all files under the specified directory with subdirectories

-v: displays the detailed execution of the instruction.

1.6 chgrp is used to change the user group to which a file or directory belongs. This command is used to change the specified file location

The user group to which it belongs. Where the group name can be the id of the user group or the group name of the user group. File name

It can be a list of files to be changed to a group separated by spaces, or a set of files described by wildcards

Close. If the user is not the file owner or superuser (root) of the file, the

Group.

-R or-- recursive: recursive processing that juxtaposes all files and subdirectories under the instruction directory

Reason

-f or-- quiet or-- silent: no error message is displayed

-h or-- no-dereference: only make changes to symbolic linked files, not any other related ones

File

-v or-- verbose: displays the instruction execution process

-- reference=: sets all the groups to which the specified file or directory belongs to and parameters

The test file or directory belongs to the same group

1.7 chown changes the owner of a file or directory and the group to which it belongs, and this command can change a file or directory to a

User authorization to make the user the owner of the specified file or to change the group to which the file belongs. User groups can

So the group name or group id. A file name can make a list of files separated by spaces, which can be included in the file name

Wildcards.

-f or-- quite or-- silent: no error message is displayed

-h or-- no-dereference: only changes are made to symbolic linked files, not to any other related ones

File

-R or-- recursive: recursive processing that processes all files and subdirectories under the specified directory together

-v or-- version: displays the instruction execution process

-- dereference: the effect is the same as the "- h" parameter

-- reference=: integrates the owner of the specified file or directory with the group to which it belongs

The owner of the part and reference file or directory is the same as the group to which it belongs.

1.8 chmod is used to change the permissions of a file or directory. In the family of UNIX systems, files or directories

The control of permissions is distinguished by three kinds of general permissions: read, write and execute, and there are three special permissions.

For use. Users can use the chmod instruction to change the permissions of files and directories.

Either a word or a numeric code is fine. The permissions of symbolic links cannot be changed if the user modifies the rights of symbolic links

Limited, its changes will be applied to the linked original file.

The scope of permissions is expressed as follows:

U User, the owner of a file or directory

G Group, the group to which the file or directory belongs

O Other, except for the owner of the file or directory or the group to which it belongs, all users belong to this category

Encirclement

An All, that is, all users, including owners, groups, and other users

R read permission, numeric code "4"

W write permission, numeric code "2"

X execute or switch permissions, numeric code "1"

-without any permission, the number code is "0"

S Special function description: change the permissions of a file or directory.

-f or-- quiet or-- silent: no error message is displayed

-R or-- recursive: recursive processing, processing all files and subdirectories under the instruction directory together

-v or-- verbose: displays the instruction execution process

-- reference=: sets all the groups belonging to the specified file or directory to and

The reference file or directory belongs to the same group

+: open the permission setting of this option for files or directories with permission scope

-: turn off this option permission setting for files or directories with permission scope

=: this option permission setting for files or directories with specified permission scope

1.9 mkdir create directories / folders

-m set directory properties

-p establish a parent directory to determine whether the directory exists, or if it does not exist

-parenls establishes a parent directory

-v displays detailed processing information

1.10 whereis find files / view command path

-b locate the executable file.

-m locate the help file.

-s locate the source code file.

-u search for files under the default path other than executable files, source code files, and help files.

-B specifies the path to search for executable files.

-M specifies the path to search for help files.

-S specifies the path to search for source code files.

-f displays only the real name of the file

1.11 file-determine the file type

-b do not output file name (brief mode)

-z attempted to view the internal information of the compressed file.

The-L (on systems that support symbolic links) option displays the original file of the symbolic link file, just like the like-named option of the ls (1) command.

1.12 the time touch is used to update files or directories, files that do not exist will be created as empty files, unless you use the-c or-h option. If the file name is "-", it is specially handled to change the access time of files related to standard output.

The parameters that must be used for long options are also required for short options.

-an only change the access time

-m only change the modification time

-c,-- no-create does not create any files

-d,-- the date= string replaces the current time with the specified string.

-r,-- the reference= file replaces the current time with the time attribute of the specified file

-t STAMP uses [[CC] YY] MMDDhhmm [.ss] format instead of when-time=WORD uses the time specified by WORD: access, atime, use are all equal to the effect of the-an option, while modify and mtime are equal to the effect of the-m option

1.13 rename is used for naming and managing files. It can be used to name files in batches and support regular expressions. There are differences between two versions of the rename command, one is that the C language version supports wildcards, the other is the Perl version. Support for regular expressions

-n operation preview to display the results after execution without changing the file name

-f Enforcement

[common wildcard description]

? Represents an arbitrary character

* represents one or a string of arbitrary characters--

[description of common regular expression symbols]

^ match the start position of the input

$matches the end of the input

. Match any character except a newline character

+ match the previous character one or more times for example, "zo+" can match "zoo" but not "z"

[amurz] represents a range of characters, for example, "[amurz]" matches any lowercase character between "a" and "z".

[^ mmurz] negative character range. Matches characters that are not within the specified range.

The 1.14 updatedb command is used to create or update database files necessary for the slocate command. The updatedb command takes a long time to execute because it traverses the directory tree of the entire system and writes all the file information to the slocate database file. Note: slocate itself has a database that stores information about files and directories in the system.

-o: ignore the default database file and use the specified slocate database file

-U: update the slocate database of the specified directory;-v: show the detailed process of execution.

1.15 dd convert and copy files

When making an unforced conversion, copy the file using the specified input and output block size (default is from standard input to standard output. )

It reads one block of the specified size at a time from the input (512 bytes by default). If you use the bs=bytes option and there is no conversion, except for specifying sync, noerror, or notrunc, dd will write all read data (which can be less than the requested read) to a separate output block. The length of this output block is exactly the same as the data read, unless you specify to use sync (synchronous) conversion, in which case NUL characters (or spaces, see below) will be appended to the end of the data.

In other cases, the input is read one block at a time, then processed, and the output is collected and finally written to the data block of the specified size. The final output block may be shorter than the specified size.

If=file is read from file instead of standard input.

Of=file is written to file instead of standard output. Unless conv=notrunc is specified, dd will truncate the file to O bytes (or the size specified by the seek= option)

Ibs=bytes reads bytes bytes at a time. The default is 512.

Obs=bytes writes bytes bytes at a time. The default is 512.

Bs=bytes reads and writes bytes bytes at once. This overrides the values set by ibs and obs (and setting bs does not mean setting ibs and obs to the same value at the same time, at least when using only sync, noerror, or notrunc conversions. Because bs stipulates that each input block should be copied to the output as a separate block of data, rather than grouping the shorter blocks together.

Count=blocks copies only the first blocks blocks of the input file (each block is ibs-byte bytes in size), not all of the contents until the end of the file. Conv= conversion [, conversion]. Converts the file as specified by the conversion parameter (there are no spaces on either side of the lq,rq).

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report