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

How to use commands to operate files at high frequency in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to use commands for high-frequency manipulation of files in Linux? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

0. New operation:

Mkdir abc # create a new folder touch abc.sh # create a file

1. View operation

View the catalog:

Ll # displays catalog file details

View the contents of the file:

Cat | head | tail command

Cat abc.txt # View the contents of abc head-5 abc.txt # View the first five lines of abc. The default is 10 lines of tail [option] filename. The meaning of each option is as follows: + num: display after the num line-num: from the num line at the end of the file. If the num parameter is omitted, the system default is 10.murf: read in a loop, for example, when viewing the server log, you can see the last content of the # filename file displayed on the screen in real time and refresh constantly. Tail-f filename # View the last 20 lines of tail-f filename

More command:

The more command displays one screen of information at a time, and "- More- (xx%)" appears at the bottom of the screen if the message is not displayed.

Press the Space key at this time to display the next screen.

Press the enter key to display the next line

Press the B key to display the previous screen

Press Q to exit the more command.

Less command: similar to the more command, but more powerful than the more command. In many cases, you have to use less, such as pipes. For example:

Ll / etc | less

Stat command:

View the details of the file, such as creation modification time, size, etc.

[root@localhost zx] # stat index.html file: "index.html" size: 29006 block: 64 IO block: 4096 ordinary file device: fd00h/64768d Inode:17589607 hard link: 1 permission: (0644mob, (0644mob) Gid: (0 / root) Environment: unconfined_u:object_r:home_root_t:s0 recent visit: 2019-09-02 21JU 47JU 41.824053666 + 0800 recently changed : 2019-09-02 21 Suzhou 4433. 588587500 + 0800 recently changed: 2019-09-02 21 devise 44 ~ 33. 588587500 + 0800 creation time:-

Du command:

Option:-h is displayed in appropriate units (kb or M units are automatically selected according to the size of the file)

[root@localhost zx] # du-h index.html 32K index.html

two。 Delete operation

Rm-f aa.txt # Force deletion aa.txtrm-rf fileDir # Force deletion of the fileDir folder and all files in it

3. Copy operation

Replication of the same machine:

Cp: copy a file or directory

Syntax:

Cp [options] source dest

-a: this option is usually used when copying a directory, preserving links, file properties, and copying everything in the directory. Its function is equal to the combination of dpR parameters. -d: keep the link when copying. The links mentioned here are equivalent to shortcuts in the Windows system. -f: overwrite existing target files without prompting. -I: contrary to the-f option, prompt the user to confirm whether or not to overwrite the target file before overwriting, and the target file will be overwritten when answering "y". -p: copy the modification time and access rights to the new file in addition to copying the contents of the file. -r: if the source file given is a directory file, all subdirectories and files under that directory will be copied. -l: do not copy the file, just generate the linked file.

For example:

# copy.. / html/index.html to the current directory cp.. / html/index.html. # copy the files and subdirectories under the.. / html/ directory to the current tt directory, and if tt does not exist, automatically create cp-r.. / html/ tt/ # copy the file file to the directory / usr/men/tmp and rename it to file1cp file / usr/men/tmp/file1 # if the dir2 directory already exists, you need to use cp-r dir1/. If dir2# uses cp-r dir1 dir2 at this time, it will also copy the dir1 directory to dir2, which obviously does not meet the requirements. Just change ps:dir1 and dir2 to the corresponding directory path.

Remote replication

# copy the test.txt under the current directory to the / zx directory of the remote 111.12 machine. Scp test.txt root@192.168.111.12:/zx# copies the test.txt to the root directory of the remote user, and name it textA.txtscp test.txt root@192.168.111.12:testA.txt#. You may not specify the user, and type again in the following prompt As follows: scp test.txt 192.168.111.12:/zx# is copied remotely to local:-r is used to recurse the entire directory scp-r remote_user@remote_ip:remote_folder local_path

4. Move operation:

The move operation can be understood as deleting the original file after copying the file.

Eg1:

Mv / zx/soft/*. # copy all files in the / zx/soft directory to the current directory mv a.txt. / test/a.txt # copy the current directory a.txt to the current test directory. Mv / zx/soft/ / tmp/soft # copy the folder to / tmp/, you must make sure that tmp exists

5. Rename operation:

Rename or use the move operation command, such as:

# rename the directory (file) A to Bmv A B# move the / a directory (file) to / b and rename it to c. Make sure the b directory exists. Mv / a / b/c# moves the current test1 directory to the current test directory and names it bmv. / test1. / test/b

6. Decompression operation

Tar-c: create compressed files-x: extract-t: view content-r: append files to the end of compressed archives-u: update files in the original package

These five are independent commands, one of which should be used for compression and decompression, which can be used with other commands, but only one of them can be used. The following parameters are optional when compressing or decompressing files as needed.

-z: with gzip attribute-j: with bz2 attribute-Z: with compress attribute-v: show all procedures-O: unpack the file to standard output

The following parameter-f is required

-f: use the file name and remember that this parameter is the last parameter and can only be followed by the file name.

3. Give examples:

The command tar-cf all.tar * .jpg is to type all .jpg files into a package called all.tar. -c means to generate a new package, and-f specifies the file name of the package. The command tar-tf all.tar lists all the files in the all.tar package, and-t means tar-xf all.tar. The command is to extract all the files in the all.tar package. -x means to unzip tar-cvf jpg.tar * .jpg / / package all jpg files in the directory into jpg.tar eg2: tar-xzf nginx-1.14.0.tar.gz / / decompress to the current directory tar-zxf nginx-1.14.0.tar.gz-C / usr/local/nginx # to the corresponding directory eg3: tar-zxvf nginx...tar.gz # decompress and display the process

Note: some compressor prompt commands cannot be found and need to be installed, such as:

Yum install unzip or on ubuntu: apt-get install unzip

IV. Summary

1. * .tar decompression with tar-xvf 2, * .gz decompression with gzip-d or gunzip decompression 3, * .tar.gz and * .tgz decompression with tar-xzf 4, * .bz2 decompression with bzip2-d or bunzip2 decompression 5, * .tar.bz2 decompression with tar-xjf 6, * .Z decompression with uncompress 7, * .tar.Z decompression with tar-xZf 8, * .rar decompression with unrar e 9, * .zip decompression with unzip

When decompressing, sometimes you don't want to overwrite an existing file, then you can add the-n parameter.

Unzip-n test.zip unzip-n-d / temp test.zip only look at which files are contained in the zip package, and do not unzip unzip-l test.zip to see that the list of files displayed also includes the compression ratio unzip-v test.zip check zip file for corruption unzip-t test.zip if the same file already exists Ask the unzip command to overwrite the previous file unzip-o test.zip-d / tmp/ example: eg1: unzip mydata.zip-d mydatabak # extract to the mydatabak directory 10. Xz

This is a two-layer compression, with xz compression on the outside and tar compression on the inside, so you can decompress it in two steps.

$xz-d node-v6.10.1-linux-x64.tar.xz $tar-xvf node-v6.10.1-linux-x64.tar

7. Upload File tool

Uploading some files from the local windows to the remote Linux server can be done through xshell's xftp or through the following gadget lrzsz, which is more convenient to use.

Yum install lrzsz # installation tool

Common commands:

Sz dist.zip # downloading the file dist.zip to the local rz # opens a window to upload the file to the remote server

8.ln, file, and touch commands

Ln command: the name is used to create linked files, including hard links (Hard Link) and symbolic links (Symbolic Link). We often use symbolic links, also known as soft links. Soft connections are similar to shortcuts in windows.

Example:

# create a soft connection in the current directory, pointing to / etc/fastab, also named fastabln-s / etc/fastab#, create a soft connection to / boot/grub in the current directory, named gbln-s / boot/grub gb

Note: the correct way to delete a soft connection is:

Rm-rf. / gb

Wrong way:

Rm-rf. / gb/

This will delete the content under the original grub. Especially for soft connections of system files, you must be careful to delete them.

* * file command: * * used to identify the type of file

In Linux, the file suffix is only convenient for the user to identify, and there is no real constraint. The file command can see the actual type of file:

File [- bcLz] file | Directory

Option description:

File | Directory: file or directory to be identified-b: when the recognition result is displayed, the file name is not displayed-c: show the execution process-L: directly display the file type pointed to by the symbolic link file-z: try to interpret the contents of the compressed file

Example: as you can see, index.mp4 is essentially a HTML rather than an mp4 file

[root@VM_0_13_centos soft] # file index.mp4 index.mp4: HTML document, UTF-8 Unicode text, with very long lines

* * touch command: * * is used to change the access time and modification time of a file or directory.

Touch [- am] [- t] [directory | file]

If the specified directory file does not exist, an empty file is created directly, so touch is also often used to create a blank file.

# create a new file aa.txttouch aa.txt

Option description:

-a: modify access time only-m: modify only modification time-t: use the specified date time, not the system time. For example, it should be changed to 16:38 minutes and 13 seconds on October 20, 2019. The parameter is: '20191020163813'

Example:

You can check the timestamp of the file before you modify it: use the stat command to view

[root@VM_0_13_centos soft] # stat index.html File: 'index.html' Size: 17215 Blocks: 40 IO Block: 4096 regular fileDevice: fd01h/64769d Inode: 529352 Links: 1Access: (0644) Uid: (0 / root) Gid: (0 / root) Access: 2019-10-23 15 Blocks 15RV 37.280616254 + 0800Modify: 2019-10-23 15 Blocks 15 Blocks 37.280616254 + 0800Change: 2019-10-23 15 15 Blocks 37.290616257 + 0800 Birth:-

Start modification: change the access and modification time of the index.html file to the current system time.

Touch index.html

9. Find operation command:

There are times when you forget the files, directories, etc., that you want to use, so it is extremely necessary to find commands:

Find: find files or directories (commonly used)

The syntax is as follows:

Find [catalog …] [- amin] [- atime] [- cmin] [- ctime] [- empty] [- exec] [- fls] [- follow] [- fstype] [- gid] [- group] [- nogroup] [- mmin] [- mtime] [- name] [- nogroup] [- nouser] [- perm] [- size] [- uid] [- user] [- nouser]

Several common options describe:

-size: find files that match the specified size. The file size unit can be "c" for Byte; "k" for KB. If configured as "100k", the find command will look for files whose file size is exactly 100KB; if configured with "+ 100k", the find command will look for files whose file size is larger than 100KB; if configured with "- 100k", the find command will look for files with a file size smaller than 100KB. -user: find a file or directory whose owner is a specified user, or specify it with a user number-name: find the specified content, using "*" to indicate any character in the search; use "?" Indicates any character-mtime: finds a file or directory whose content has been changed at a specified time, in 24-hour units. If configured to 2 find command will look for files that changed content just 48 hours ago; configure to + 2 find command to find files that changed content more than 48 hours ago; configure to-2 find command to find files that changed content within 48 hours. -mmin: find files or directories whose contents have been changed at a specified time, measured in minutes. Cmin: find files or directories whose permission attributes have been changed at a specified time, measured in minutes. -ctime corresponds to the hour. -amin: find files or directories that have been accessed at a specified time. -atim corresponds to the hour. -perm: find files or directories that match the specified permission values (see Chapter 6 for permission values). If configured as "0700", the find command looks for files or directories whose permission value happens to be "0700"; if configured as "+ 0700", the find command looks for files or directories whose permission value is greater than "0700"; if configured to "- 0700", find

There are roughly the following categories of options:

1. Find by time ran

two。 Find by file size

3. Find by file name

4. Press other: such as permissions, user groups, types, etc.

Example:

# starting from the root directory, look for directories and files whose names begin with nginx /-name nginx* # look for files with a file size of more than 100m find /-size + 100M# look up / home/zx directory, files and directories that have been modified within 10 minutes find / home/zx/-mmin-10

Locate: find files or directories (rarely used)

Locate to find content

For example, locate nginx lists all directories and files that contain nginx. Can you use * or? Wait for a match.

Locate's lookup is very fast, because the command looks for a database, so some files and directories that have just been modified may not be found. You can update the database with the: updatedb command.

Which: finding files (rarely used)

Which [file]

The which command only looks in the paths and command aliases defined by the PATH environment variable, so the scope is limited.

Whereis: finding files (rarely used)

Whichis [- bu] [- B] [- M] [- S] [file]

Common options:

Files: commands to find

-b: only look for binaries

-u: find files that do not contain the specified type

-B: look for binaries only in the specified directory

-M: find help files only in the specified directory

-S: only look up the source directory in the specified directory

For example: by default, it will only be found in the specified directory (/ bin, / etc, / usr)

[root@VM_0_13_centos soft] # whereis nginxnginx: / usr/local/nginx/ usr/local/nginx/sbin/nginx / usr/local/nginx/sbin/nginx.bak what is the Linux system Linux is a free to use and freely spread UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, multi-threading and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

After reading the above, have you mastered how to use commands to manipulate files at high frequency in Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Wechat

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

12
Report