Linux command file creation, deletion, movement and search

Order

Ctrl + a=

Date

date Function: Display Linux date or time.

Set Date

FORMAT parameters

In terms of display, users can set the format to be displayed. The format is set to a plus sign followed by several marks. The list of available marks is as follows:

%% output character %
%a abbreviation for day of the week (Sun..Sat)
%A The full name of the week (Sunday..Saturday).
%b Abbreviated month name (for example, Jan)
%B Full month name (for example, January)
...(part omitted)...
%Z time zone abbreviation (such as EDT)

If it does not start with a plus sign, it means that the time needs to be set, and the time format is MMDDhhmm[[CC]YY][. Ss], where MM is the month, DD is the day, hh is the hour, mm is the minute, and CC is The first two digits of the year, YY is the last two digits of the year, and ss is the number of seconds.

Access rights: All users.

When you don’t want meaningless 0s to appear (for example, 1999/03/07), you can insert the – symbol in the tag, such as date ' + %-H:%-M:%-S\ ' will remove the meaningless zeros in hours, minutes and seconds. For example, the original 08:09:04 will become 8:9:4. In addition, only those with permission (such as root) can set the system time.

After you change the system time as root, please remember to use clock -w to write the system time into CMOS, so that the system time will continue to be updated the next time you reboot. Correct value.

Example

Show current time

# date
Tue May 24 09:29:43 CST 2022
# date ' + %c'
Tue 24 May 2022 09:30:03 AM CST
# date ' + %D' //Display the complete time
05/24/22
# date ' + %x' //Display digital date
05/24/2022
# date ' + %T' //Display date, year represented by four digits
14:09:31
# date ' + %X' //Display 24-hour format
09:31:31 AM

Formatted output:

# date + "%Y-%m-%d"
2009-12-07

Output yesterday’s date:

# date -d "1 day ago" + "%Y-%m-%d"
201

2-11-19

Output the time after 2 seconds:

# date -d "2 second" + "%Y-%m-%d %H:%M.%S"
2012-11-20 14:21.31

The legendary 1234567890 seconds:

# date -d "1970-01-01 1234567890 seconds" + "%Y-%m-%d %H:%M:%S"
2009-02-13 23:02:30

or:

# date -d@1234567890 + "%F %T"
2009-02-13 23:02:30

Time format conversion:

# date -d "2009-12-12" + "%Y/%m/%d %H:%M.%S"
2009/12/12 00:00.00

Apache format conversion:

# date -d "Dec 5, 2009 12:00:37 AM" + "%Y-%m-%d %H:%M.%S"
2009-12-05 00:00.37

Time travel after format conversion:

# date -d "Dec 5, 2009 12:00:37 AM 2 year ago" + "%Y-%m-%d %H:%M.%S"
2007-12-05 00:00.37

Output in your own format

# date ' + usr_time: $1:%M %P -hey'
usr_time: $1:16 PM -hey

After displaying the time, skip a line and then display the current date.

date ' + %T%n%D'

Display month and day number

date ' + %B %d'

Display date and set time (12:34:56)

date --date '12:34:56'

Time addition and subtraction operations:

date + %Y%m%d # Display year, month and day
date -d " + 1 day" + %Y%m%d # Display the date of the next day
date -d "-1 day" + %Y%m%d # Display the date of the previous day
date -d "-1 month" + %Y%m%d # Display the date of the previous month
date -d " + 1 month" + %Y%m%d # Display the date of the next month
date -d "-1 year" + %Y%m%d # Display the date of the previous year
date -d " + 1 year" + %Y%m%d # Display the date of the next year

set time:

date -s # Set the current time. Only root authority can set it, others can only view it.
date -s 20120523 # Set to 20120523, which will set the specific time to 00:00:00
date -s 01:01:01 # Set the specific time without changing the date
date -s "01:01:01 2012-05-23" # This can set the entire time
date -s "01:01:01 20120523" # This can set the entire time
date -s "2012-05-23 01:01:01" # This can set the entire time
date -s "20120523 01:01:01" # This can set the entire time

Clock

The Linux clock command is used to adjust the RTC time.

RTC is the computer’s built-in hardware time. Executing this command can display the current time, adjust the time of the hardware clock, set the system time to be consistent with the time of the hardware clock, or save the system time back to the hardware clock.

Parameter description:

  • --adjust: The first time you use --set or --systohc parameters to set the hardware clock, it will be displayed in /etcadjtime is generated in the code> directory. When these two parameters are used to adjust the hardware clock again, this file will record the difference between the two adjustments. When the clock command plus the --adjust parameter is executed in the future, the program will It will automatically calculate the average value based on the numerical difference of the recorded files and automatically adjust the hardware clock time.

  • --debug: Display the instruction execution process in detail, which is convenient for troubleshooting or understanding the program execution situation.

  • --directisa: Tells the clock command not to directly access the hardware clock through the /dev/rtc device file. This parameter is suitable for older computers with only ISA bus structure.

  • --getepoch: Present the new era value of the hardware clock in the system core to the standard output device.

  • --hctosys: Hardware Clock to System Time, set the system time to be consistent with the hardware clock. Since this action will cause the system to fully update the file access time, it is best to execute it when the system starts.

  • --set --date: Set the date and time of the hardware clock.

  • --setepoch --epoch=: Set the new era value of the system core hardware clock. The year is represented by four digits.

  • --show: Read the time of the hardware clock and present it to the standard output device.

  • --systohc: System Time to Hardware Clock, store the system time back into the hardware clock.

  • --test: Only for testing, it does not actually write the time to the hardware clock or system time.

  • --utc: Treat the time on the hardware clock as CUT, sometimes called UTC or UCT.

  • --version: Display version information.

Example

Get the current time:

clock # Get the current time

Display UTC time:

clock -utc # Display UTC time

System time reads the hardware time.

Hwclock

Check the system time:

hwclock -show

View the hardware time.

Time synchronization, convert hardware time into system time:

hwclock -w

Synchronize hardware to system time:

hwclock -s

Cal

Calendar query:

cal 10 2024 # View the calendar for October 2024
cal 2024 # View the 2024 year calendar

** File type flag**

  1. - : Ordinary file

    • This means that the file is an ordinary data file and contains actual content, which can be text, images, etc.
  2. D: Directory file

    • This means that the file is a directory used to organize and store other files and subdirectories.
  3. L : Link file

    • This is a link file, also called a soft link, that points to the location of another file or directory.
  • b : block device file

    • This is a block device file used to access data stored on disk or other block devices.
  • c : character device file

    • This is a character device file used to access character stream devices such as serial ports, terminals, and printers.
  • p : Pipeline equipment file

    • This is a pipe device file used to transfer data between processes.
  • s : socket device file

    • This is a socket device file used to communicate on the network

File Properties

  • Metadata (inode)

    • Metadata is a data structure in the file system that uniquely identifies a file. Similar to an ID number, it solves the file sharing role.
  • User Data

    • User data is the content actually recorded in the file, which can be text, images, etc.

File link

  • Soft links (symbolic links)

    • A soft link is a shortcut to real data. It is similar to a shortcut and can point to a file or directory.
  • Hard link

    • A hard link is equivalent to a backup of a file, and multiple files share the same real data. They share the same inode, so modifications to one of the files are reflected on all hard links.

File Relationships

  • The relationship between the original file and the hard link. When the original file is deleted, all hard links associated with it become invalid.

Characteristics of hard links and soft links

  • Soft links can be deleted, and the number of references to hard links can be represented by a number. At the same time, the specific file it points to can be found through a soft link.

timedatectl command:

  • Features:

    • System commands related to time and date.
  • Option parameters:

    • (Specific option parameters are not provided and may be given in subsequent content.

Hard link

Features: The original file must be created in the same file system

Soft link

Features: Create files and directories that do not exist
Create existing files and directories
Can be created across file systems
You can delete soft links without affecting the original files.

In

Create soft link

Steps

Touch f 1
Touch f 2
In -s f 1 f 2
Ln -s f 3 f 5
Ln -s original file soft link file
Ln original file hard link

cd
Mkdir bb //Create directory
Ll
c d
Touch file 1
Touch file 2
-s
Ln –

Ls-i

B lock block device file (fast file system)
C character device()
P (Pipe Equipment File)
S socket socket file ip + port number

Domain name-port number

    • rw- – – .
  • A: The file is owned by
  • B The permissions of the group to which the file belongs
  • C Others’ permissions on the file
  • D extended permissions
    L=soft connection
    D=directory file
    How many citations are there for files and directories?
    Ls -a
    Cd.
    cd…
    Ll:
    Root: Administrator account (has full access to all files and directories on a Unix or Linux system)
    Right root: the group to which the current file belongs
    The number after Root: the size of the file

Ll -h /: Displays all files and displays their sizes in a human-readable format
Behind the number: time modification time
Stat anaconda-ka. Cfg
Access: Access time: This indicates the last time the user read the contents of the file.
Modify modification time: This indicates the time when the contents of the file were last modified.
Change: This indicates when the file’s metadata (such as permissions, owners, etc.) changed

Birth: This represents the creation time of the file, i.e. when the file was originally created.

- The first group `rw-` represents the owner's permissions:
    
    - `r` means read permission (Read), allowing the owner to view the contents of the file.
    - `w` means write permission (Write), allowing the owner to modify or edit the file.
    -x means execute the file. Execute permissions on the file
    -
    - `-` means that without execution permission (Execute), the owner cannot execute the file as a program.
- The second group `---` represents the permissions of the group it belongs to:
    
    - Three consecutive `-` means that there is no permission, that is, the group cannot read, write or execute the file.
- The third group `---` represents the permissions of others:
    
    - Likewise, three consecutive `-` means no permissions, i.e. others cannot read, write or execute this file.
- The final `.` indicates that this is a file, not a directory.
    

This way of expressing permissions has a total of nine characters, and each group of three characters represents the permissions of the owner, the group to which it belongs, and other people. In each group, the first character represents read permission, the second character represents write permission, and the third character represents execute permission.

So, for the permissions you provide:

- The owner has read and write permissions, but no execute permissions.
- The group you belong to and others do not have any permissions.

stat command

stat is used to display the status of a file or file system. It displays the metadata content of the file.

Use stat

Use stat to query file information.

ll -h /

This command will display all files in the root directory in a human-readable format and display the file sizes. The number after the number indicates the last modification time of the file.

File time attribute:

  • Access Time: This represents the last time a user read the contents of the file.

  • Modify Modification Time: This indicates the last time the file’s content was modified.

  • Change Time: This represents the time when the file’s metadata (such as permissions, owner, etc.) changed.

  • Birth Time: This represents the creation time of the file, i.e. when the file was originally created.

Time stamp of files modified by Touchu

If you touch an existing file, in addition to the creation time of the file, the touch command will only affect the access time and modification time of the file.
Touch a file that does not exist to create a file

File naming rules

  1. Slashes (/) cannot be used: Slashes are used to represent root directory and path separators and cannot be used as part of the file name.

  2. File name length limit: The file name cannot exceed 255 characters. This includes all characters in the file name, including letters, numbers, symbols, etc.

  3. Case Sensitive: In most Unix and Linux systems, file names are case sensitive. For example, file and File are considered two different files.

  4. Directories are also files: In Unix and Linux systems, directories are also treated as files. Files and directories cannot have the same name under the same path.

File creation

touch

To create a file using the touch command, you can enter the following command at the command line:

touch file name

This will create an empty file named “filename”. If the file already exists, the touch command will update its access time and modification time.

You can use this command to create multiple files by listing their names in the command. For example:

touch file 1 file 2 file 3

This will create three empty files named “File1”, “File2” and “File3”.

Also, for the additional example you provided:

touch {<!-- -->1..5}{<!-- -->a..b}

This command will create the following files:

  • 1a
  • 1b
  • 2a
  • 2b
  • 3a
  • 3b
  • 4a
  • 4b
  • 5a
  • 5b

Here is an example of using brace expansion to quickly generate a series of similar file names. Here, {1..5} represents numbers from 1 to 5, and {a..b} represents letters from a’ to b’.

>

is a redirection symbol that creates a file

Vim f 3

Vim opens a file or creates a f 3

Mkdir (mark directory)

Cascading Directories: Create one or more directory structures containing subdirectories.

Mkdir -p 1/2/3/4/5
Mkdir -p
View tree trees

What is a directory?
In the computer field, Alias is usually used to give a short alias to a command, program, file path, etc. to facilitate quick access or execution by users. For example, you can create an alias to shorten a long command into a short word or phrase, saving time and typing.

Delete command

rm: Represents a delete command.

If you want to delete a file, you can use the rm command, for example:

bashCopy code

rm filename
**#### -r: Represents recursive deletion, used to delete the directory and all files and subdirectories under it.
If you want to delete a directory and all the files and subdirectories it contains, use the -r option, for example:

bashCopy code

rm -r directoryname

Be careful when using the rm command as deleted files cannot be recovered. Make sure you delete the file or directory you really want to delete.

-f: indicates forced deletion, that is, the user will not be asked for confirmation.

Use in combination

rm -f -r a is a command used on the command line to delete a directory named a and all the files and subdirectories it contains.
So, rm -f -r a will force a recursive deletion of the directory named a, including all files and subdirectories in it.

alias

For example, on Unix/Linux systems, you can use the alias command to create aliases, such as:

bashCopy code

alias ll='ls -l'

In this way, entering ll in the command line in the future is equivalent to executing the ls -l command, which facilitates user operations.

In different contexts, “Alias” can also refer to other things, such as an alias used in a database to give a table or field an alias to simplify queries, or an alias used in a network to identify a device.

Copy and move file commands

Linux》

The Linux cp command is used to copy files and directories. Its basic syntax is:

cp [option] source file target file

or

cp [options] source file... target directory

Among them, [options] represents some optional parameters for controlling the method and effect of copying; source file represents the file or directory to be copied; target file or destination directory represents the destination of the copy.

Common options

The following are some commonly used cp command options:

  • -r: Recursive copy, that is, copy the source directory and all its contents to the target directory. If the target directory already exists, it will be merged.
  • -i: Interactive copying, that is, asking the user whether to overwrite the existing target file before copying.
  • -p: Preserve attribute copy, that is, copy the source file to the target file, and retain the attributes of the source file (including owner, group, permissions and time).
  • -a: Archive copy, equivalent to the set of -d, -p, -r options, can be copied All types of files and directories, with links and attributes preserved.
  • -d: Link copy, if the source file is a soft link (similar to the shortcut in Windows), the copied target file is also a soft link, pointing to the same location.
  • -l: Hard link copy, establish the target file as a hard link to the source file (similar to a copy in Windows). The two files share the same data block, and modifying one will affect the other.
  • -s: Soft link copy, establish the target file as a soft link to the source file (similar to the shortcut in Windows), the two files point to the same location, deleting one of them will not affect the other .
  • -u: Update copy, copy only when the source file is newer than the target file, can be used to compare and update two versions of files.
  • -v: Detailed copy, displays the detailed copy process, and can be used to view the copy progress and results.

Example

Here are some examples of cp commands:

  • cp file1.txt file2.txt: Copy file1.txt to file2.txt. If file2.txt already exists, it will be overwritten.
  • cp -r dir1 dir2: Copies dir1 and all its contents to dir2, or merges dir2 if it already exists.
  • cp -i file1.txt file2.txt: Copy file1.txt to file2.txt. If file2.txt already exists, you will be asked whether to overwrite it.
  • cp -p file1.txt file2.txt: Copy file1.txt to file2.txt, and retain the attributes of file1.txt (including owner, group, permissions and time).
  • cp -a dir1 dir2: Archive copies dir1 and all its contents to dir2, preserving dir1’s links and attributes.
  • cp -d link1 link2: Link copy link1 to link2. If link1 is a soft link, link2 is also a soft link, pointing to the same location.
  • cp -l file1.txt file2.txt: Hard link copies file1.txt to file2.txt. The two files share the same data block, and modifying one will affect the other.
  • cp -s file1.txt link1: Copy the soft link file1.txt to link1. Link1 is a soft link pointing to the location of file1.txt. Deleting one of them will not affect the other.
  • cp -u file1.txt file2.txt: Update copy file1.txt to file2.txt, copy only when file1.txt is newer than file2.txt.
  • cp -v file1.txt file2.txt: Copy file1.txt to file2.txt in detail, and display the copy process and results.

How to use the cp command to transfer files under administrator privileges to ordinary users

cp [option] source file target file

or

cp [options] source file... target directory

Among them, [options] represents some optional parameters used to control the method and effect of copying; source file represents the path of the file or directory to be copied; Target file or Target directory represents the path of the copied file or directory.

If you want to copy a normal file, you can directly specify the path of the source and destination files, for example:

cp /home/root/file.txt /home/user/file.txt

This command will copy the file.txt file in the /home/root/ directory to the /home/user/ directory and keep the original file name.

If you want to copy a directory and all its contents, you need to add the -r option, which means recursive copying, for example:

cp -r /home/root/dir/ /home/user/dir/

This command will copy the dir/ directory under the /home/root/ directory and all its subdirectories and files to /home/user/ directory and retain the original directory name.

If you want to copy multiple files or directories, you can specify multiple source files or directories in a single command, followed by a destination directory, for example:

cp -r /home/root/file1.txt /home/root/file2.txt /home/root/dir/ /home/user/

This command will delete file1.txt, file2.txt and dir/ in the /home/root/ directory. Copy to the /home/user/ directory and keep the original file name and directory name.

If you want to confirm whether the existing target file is overwritten before copying, you can add the -i option to indicate interactive copying, for example:

cp -i /home/root/file.txt /home/user/file.txt

This command will prompt you whether to overwrite the /home/user/file.txt file before copying it. If you answer y, it will overwrite it; if you answer n, the copy will be cancelled.

If you want to retain the attributes of the source file (including owner, group, permissions and time), you can add the -p option to indicate that the attributes are copied, for example:

cp -p /home/root/file.txt /home/user/file.txt

This command will copy the /home/root/file.txt file to the /home/user/file.txt file while retaining the attributes of the source file.

In addition to these common options, the cp command also has some other options that can achieve different functions, such as:

  • -a: Archive copy, equivalent to the set of -d, -p, -r options, can be copied All types of files and directories, with links and attributes preserved.
  • -d: Link copy, if the source file is a soft link (similar to the shortcut in Windows), the copied target file is also a soft link, pointing to the same location.
  • -l: Hard link copy, establish the target file as a hard link to the source file (similar to a copy in Windows). The two files share the same data block, and modifying one will affect the other.
  • -s: Soft link copy, establish the target file as a soft link to the source file (similar to the shortcut in Windows), the two files point to the same location, deleting one of them will not affect the other .
  • -u: Update copy, copy only when the source file is newer than the target file, can be used to compare and update two versions of files.
  • -v: Detailed copy, displays the detailed copy process, and can be used to view the copy progress and results.
Query the name of ordinary users

ID-un
Rhace

Mv

Standard input

Stdin (standard input)

Get input from keyboard
code is 0

Standard correct output stdout (standard)

The correct information after the command is executed is grayed out on the screen.
Code 1

Standard error output stderr

Information after the command execution fails will be output to the screen.
Code 2
Data flow redirection

Redirect operator

Output redirection operator
Just a symbol, not a command

> Output redirection symbol
  
> > Append output redirection character
Append output redirection symbol e

Enter redirection symbol

Termination command Ctrl + c

Echo

Echo [string][$shell]

Tee

Vim Editor

Why learn vim
Built-in vim editor
Others do not exist
Many software interfaces support vim

Have programming skills

In Linux everything is a file, so you can use vim

Command mode

Switch mode a i o input mode

s
\t
S
A
\t
a
\t
i
\t
I
o
\t
O
\t

.s delete

Input symbols to file

Echo character > file name

Find file path

Whereis

Whereis parameter command name

-m only checks the path
-b only checks binary files

Which

Which ls
Find ls current path

Locate

Find files

Find

Find path, match expression (specify file type)

-name filename (*,?)*d (find the file with so d)

-type file type (f ordinary file. D directory file. I link file)

-size file size + 1 m -1 M c bytes w number of words b fast number
-uszr file owner
-group The group the file belongs to
-atime n Find files accessed within n minutes

-ctime n

Find file path -exec ls -l -h {};
Find /home/rhace -exec ls -l -h {};