File operations

According to documents

cat

Display the entire file content at once

Cat [option] File name -b Indicates the number of non-empty output lines. -n Indicates the number of all output linesCopy the code

Connect multiple files

Cat file1 file2 > file // Merge file1 and file2 into fileCopy the code

more

Compared with CAT,more displays content in separate screens, one page at a time

More [option] file name +n display from line n -n Defines the screen size to be n linesCopy the code

Operation command space, display the next page Enter, display the next line B, return to the previous page Q, exit

less

Less can browse through files at will, while MORE can only move forward, not backward, and LESS doesn’t load the entire file before viewing it.

Less [option] File name -n Displays the line number of each lineCopy the code

Operation command pageUp, scroll upa page pageDown, scroll downa page ↑\uparrow↑, scroll upa line ↓\downarrow↓, scroll downa line Q to exit

head

Displays the first few lines of the file, 10 by default

Head [option] File name -n n Displays the first n lines -c n displays the first n bytesCopy the code

tail

Displays several lines at the end of the file, 10 by default

Tail [option] File name -n n Displays the last N lines -c n Displays the last n bytesCopy the code

Look for more

find

Used to find files in the file tree, and make the corresponding processing.

Find pathName [options] [-print-exec...] -name Searches for files by file name -perm Searches for files by file permission -user Searches for files by file owner -group Searches for files by file group. -type Searches for files of a certain typeCopy the code

Pathname: Path of the directory searched by the find command. For example,. Is used to represent the current directory, and/is used to represent the system root directory. -print: find prints the matched files to standard output. -exec: find Executes the shell command given by this parameter on the matched files. The corresponding command is of the form ‘command’ {}; , note {} and \; The space between.

For example, find the current directory. C file

grep

Text search command, search results are sent to standard output, does not affect the original file content.

-f Read keywords from the file. -n Displays the number of lines in the file where the matched content is located. -r Searches for folders recursivelyCopy the code

For example, find a line that does not contain test in a file whose filename contains test

grep -v test *test*
Copy the code

Displays the content line ending taile

grep  'tail$' test.txt

Copy the code

Displays all lines that contain strings with at least 5 consecutive lowercase characters per line

grep '[a-z] \ {5, \}' test.txt
Copy the code

  

Copy, delete, move

cp

Copy a source file or directory to a destination file or directory

Cp [option] Source file or directory Target file or directory -f overwrites the existing target file. -I prompts you before overwriting the target file. -r: If the source file is a directory file, all subdirectories and files under the directory are copiedCopy the code

Example: Copy the file myfile to the root directory /home/mengqc and rename it newfile

cp  myfile	/home/mengqc/newfile
Copy the code

Copy all files under /home/mengQC and their subdirectories to directory /home/liu

cp  -r	/home/mengqc	/home/liu
Copy the code

rm

Delete one or more files (directories cannot be deleted by default unless option is added)

Rm [options] file1file2. -f Ignores nonexistent files. -r Recursively deletes the specified directory and subdirectories and their files. -I Determines whether to delete the directories one by one before deleting themCopy the code

mv

Rename a file or directory or move a file to another directory

Rm [Option] Source file or directory name Target file or directory name -i The same name of the file, prompting whether to overwrite the fileCopy the code
  • When the destination file is the directory file path, the source file is moved to this directory
  • If the target file is not a directory file, the source file name changes to the target file name and overwrites existing files with the same name

Example: Change file ex1 to new1

mv ex1 new1
Copy the code

Move all files in /home/ts to the current directory (“.” indicates the current directory)

mv /home/ts/ *.Copy the code

Directory operations

Create a Delete directory

mkdir

Mkdir [option] dirname -m Set access permission for a new directory,8-p Multiple subdirectories can be created at one timeCopy the code

Example: create subdirectory WFK under /home/ts. Only the file owner can read, write, and execute files

mkdir -m 700 /home/ts/wfk
Copy the code

Create test and test1 under test in the current directory

mkdir -p test/test1
Copy the code

rmdir

Delete the specified directory (empty directory). If the directory is not empty, the command fails

Mkdir [option] dirname -p Recursively deletes directory dirname. When the subdirectory is deleted, the parent directory is empty and also deleted. If it is not empty, it is reservedCopy the code

Change working directory and display directory contents

cd

Switch the current directory to dirName

CD [Directory name]Copy the code

Go to the root directory

cd /
Copy the code

Go to the “home” directory

For the root user, CD ~ is the same as CD /root for the common user, CD ~ is the same as CD /home/current user name

cd ~
Copy the code

Return to parent directory

cd ..
Copy the code

pwd

View the current working directory path

pwd
Copy the code

ls

View file information, or files

Ls [options] directory or filename -l lists the details of the file -a lists everything in the directory. The original implied file -r recursively lists all subdirectoriesCopy the code

Total indicates the total space occupied by all files in the current directory

The following fields are represented in sequence

File Properties Number of hard links Group to which the owner belongs File size Establishment date File name

Change file or directory access permission

Each file or directory has three groups of access permissions. Each group is represented by R, W, and X, respectively, indicating the read, write, and execute permissions of the file owner and the read, write, and execute permissions of the users in the same group as the owner. Read, write, and execute permissions of other users in the system

chmod

Chmod [Operation object] [Permission modification] File name U: current users of a directory or file G: current group of a directory or file O: users or groups except the current users or groups of a directory or file A: all users or groups r: read permission. The number is 4. X: indicates the execute permission. The value is 1. - : indicates the delete permissionCopy the code
chmod a-x  text	// Revoke execution permissions for all users
chmod go-w text	// Revoke the write permission of the same group and other users
chmod g+w  text		// Grant write permission to the same group of users
chmod 777  text		// Grant read/write execute permission to all users
chmod 644  text		// Grant read and write permissions to the owner user, and read permissions to the same group and other users
Copy the code

Change the user group and file owner

chgrp

CHGRP [option] Group name File name -r Recursively changes the user group of all subdirectories and files under the specified directoryCopy the code

Example: Change the user groups of all files in /home/ts and its subdirectories to lj

chgrp -R lj /home/ts
Copy the code

chown

Chown [option] File main name file name -r Recursively changes the file owners of all subdirectories and files in the specified directoryCopy the code

Process management

ps

Displays the status of the current process

Ps [Option] a Displays all processes of all users (including other users) u Displays processes by user name and startup time -f Displays all information about processes -e Displays information about all processes x Displays information about processes on non-control terminals r Displays processes running on the current terminalCopy the code

Common instructions:

Display all processes

ps aux
Copy the code

Finds all information about the specified process

Ps - ef | grep process keywordsCopy the code

kill

Kill [parameter][process number]Copy the code

Common commands: Forcibly terminate a process

kill 9 -Process of no.Copy the code

Use ps to find process P1 and kill it

kill 9 - $(ps -ef | grep p1)
Copy the code

File compression and decompression

tar

Usually tar is used for packaging, and then other commands are called for compression and decompression

tar -cvf  test.tar  /etc/test  // Package the files in the /etc/test directory into the test.tar file and store it in the current directory
tar -xvf test.tar	// Decompress the test.tar file
tar -rvf  test.tar /etc/test1	// Add the files in /etc/test1 to the test.tar file.
tar -tvf test.tar	// Check the contents of the test.tar compressed file-c Creates a new document. -v Displays detailed information about the tar file. -f Name of the file to be operated -r Indicates that the file to be added is added to the end of the compressed file. -x Decompresses the fileCopy the code
tar  -czvf  sysconfig.tar.gz /etc/sysconfig/ // Package the /etc/test/ directory into a tar file and compress it into the test.tar.gz file
tar -xzvf sysconfig.tar.gz // Unzip the test.tar.gz file-z Calls the gzip program to compress the file. The compressed file name ends in. Gz.Copy the code

other

su,sudo

Su: When you switch to a user and are prompted to enter the password, the password is the password of the user after the switch. The usage is as follows: “Su” when the user name is not added to the user, the system uses the root account by default and the password is also the password of the super account. There is no time limit.

Su [user name]Copy the code

Sudo: Temporarily switch to superuser mode to enforce superuser privileges. When prompted for a password, it will be the password of the current user, not the password of the super account. But there is a time limit

sudo
Copy the code

date

Displays or sets the system date and time.

cal

Calendar showing the year or month from AD 1 to 9999

(If there is only one parameter, the year is displayed. If there is no parameter, the calendar of the current month is displayed.)

CAL Month yearCopy the code

man

Find the description and usage of a command

Man command nameCopy the code

passwd

To change the user password, enter the new password again as prompted

Passwd [user name]Copy the code