In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the method of "Linux file directory management and VIM editor". In daily operation, I believe that many people have doubts about the method of Linux file directory management and VIM editor. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "Linux file directory management and VIM editor". Next, please follow the editor to study!
File and directory management, at the beginning of learning this piece, I feel a lot of content is very complicated, but after learning to sum up, I found that it is actually very organized and not difficult, just proficient in these commonly used commands on the line. As for the Vim editor, I have to say that after using this editor, I feel that the notepad of windows is very untechnical.
First briefly summarize the commands commonly used in files and directories, and skip the simple usage.
File manipulation commands: touch, file, which, find, cp, rm, mv, ln
File content manipulation commands: cat, more, less,head, tail,wc, grep
Directory operation commands: pwd, cd, ls, mkdir, du
Archiving and compression commands: gzip, bzip2, tar
The code is as follows:
[jzhou@localhost ~] $pwd = = > Show the current directory
/ home/jzhou
[jzhou@localhost ~] $mkdir dirtest = = > create a directory
[jzhou@localhost ~] $cd dirtest = = > enter this directory
[jzhou@localhost dirtest] $touch testfile = = > create a file
[jzhou@localhost dirtest] $mkdir dirtest1 = = > create a subdirectory
[jzhou@localhost dirtest] $ls = = > list the contents of the current directory
Dirtest1 testfile
[jzhou@localhost dirtest] $echo hello linux > > testfile = = > append content to the file
[jzhou@localhost dirtest] $cat testfile = = > display the contents of the file
Hello linux
[jzhou@localhost dirtest] $file testfile = = > View file types
Testfile: ASCII text
[jzhou@localhost dirtest] $du-sh testfile = = > shows the space occupied by the file
8.0K testfile
[jzhou@localhost dirtest] $wc testfile = = > Statistics on the number of lines, words and characters of a file
2 12 testfile
[jzhou@localhost dirtest] $echo ,I love Linux > > testfile = = > append content
[jzhou@localhost dirtest] $echo no,no, I hate C plus plus > > testfile
[jzhou@localhost dirtest] $echo OK,the end > > testfile
[jzhou@localhost dirtest] $cat testfile = = > View content
Hello linux
Haha,I love Linux
No,no, I hate C plus plus
OK,the end
[jzhou@localhost dirtest] $head-2 testfile = = > View the first two lines of the file
Hello linux
Haha,I love Linux
[jzhou@localhost dirtest] $tail-2 testfile = = > View the last two lines of the file
No,no, I hate C plus plus
OK,the end
[jzhou@localhost dirtest] $cat testfile | grep "Linux" to find specific keywords
Haha,I love Linux
[jzhou@localhost dirtest] $
The above only shows the simple use of some commands, many options are not added, head and tail commands default to display the first 10 lines and the last 10 lines of records, du is to view the space occupied by the directory or file, usually larger than the actual size, and usually an integer multiple of 4.
More and less command is also a way to view the contents of the file, but less has gradually replaced more, because all the functions of more less have, and less can turn the page up to view, more is not, cat is directly a screen display of the contents of the file, no matter how long, all if the file is very long, then use the less command, similarly, press the Q key to exit.
The code is as follows:
[jzhou@localhost dirtest] $cd dirtest1 = = > enter the subdirectory you just created
[jzhou@localhost dirtest1] $touch testfile1 = = > create a new file in a subdirectory
[jzhou@localhost dirtest1] $echo > > testfile1
[jzhou@localhost dirtest1] $cd.. Return to the previous directory
[jzhou@localhost dirtest] $ls
Dirtest1 testfile
[jzhou@localhost dirtest] $cp testfile. / dirtest1/ = = > copy the file testfile to the subdirectory dirtest1
[jzhou@localhost dirtest] $cd dirtest1/ = = > go to the subdirectory
[jzhou@localhost dirtest1] $ls = = > check that there is an extra file in the subdirectory that has just been copied
Testfile testfile1
[jzhou@localhost dirtest1] $cd..
[jzhou@localhost dirtest] $ls
Dirtest1 testfile
[jzhou@localhost dirtest] $rm-f testfile = = > forcibly delete the testfile file in the dirtest directory
[jzhou@localhost dirtest] $ls = > testfile file has been deleted
Dirtest1
[jzhou@localhost dirtest] $cd. / dirtest1/ = = > go to the subdirectory
[jzhou@localhost dirtest1] $mv testfile. / testfile = = > the target directory I'm trying to move here is wrong
Testfile testfile1
[jzhou@localhost dirtest1] $pwd = = > so I want to look at the current directory to use the absolute path
/ home/jzhou/dirtest/dirtest1
[jzhou@localhost dirtest1] $mv testfile / home/jzhou/dirtest/== > move the testfile file to the dirtest directory
[jzhou@localhost dirtest1] $cd..
[jzhou@localhost dirtest] $ls = = > good, the testfile file has been moved over
Dirtest1 testfile
[jzhou@localhost dirtest] $ln-s testfile linkfile = = > create a soft link
[jzhou@localhost dirtest] $ls-l = = > notice how the soft link file is displayed below
Total 20
Drwxrwxr-x 2 jzhou jzhou 4096 03-05 22:43 dirtest1
Lrwxrwxrwx 1 jzhou jzhou 8 03-05 22:45 linkfile-> testfile
-rw-rw-r-- 1 jzhou jzhou 67 03-05 22:40 testfile
[jzhou@localhost dirtest] $
The only difference between a rm file acting on a file and a directory is whether it has the-r option, because when deleting a directory, there may be files and directories nested in the directory, so you must have the-r option. The format of cp and rm is: cp/rm original file target file (note the path problem here)
Ln link file: divided into soft links and hard links, soft links, also known as symbolic links, that is, with the-s option. Soft links are equivalent to shortcuts under windows. If the original file is damaged, the shortcut is invalid, while hard links are equivalent to a copy of the original file. In general, hard links are rarely used. Therefore, when establishing a linked file, you usually add the-s option to establish a soft link. The file type bit of the linked file is: l, which will be described in the subsequent note file permissions.
It is also important to note that a hard link file cannot be established for a directory, and the hard link must be in the same partition (file system) as the original file.
The code is as follows:
[jzhou@localhost ~] $cd dirtest/
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile
[jzhou@localhost dirtest] $tar cf test.tar dirtest1 testfile = = > Archive directories and files
[jzhou@localhost dirtest] $ls = = > A newly created archive file test.tar has been added
Dirtest1 linkfile testfile test.tar
[jzhou@localhost dirtest] $rm-rf dirtest1 testfile = = > Delete the original file to make it easier to confirm whether the file is archived later
[jzhou@localhost dirtest] $ls
Linkfile test.tar
[jzhou@localhost dirtest] $pwd = = > check the current directory, and then unarchive it in this directory.
/ home/jzhou/dirtest
[jzhou@localhost dirtest] $tar xf test.tar-C / home/jzhou/dirtest/ = = > unlock the archive, and the testfile file is released
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile test.tar
[jzhou@localhost dirtest] $rm-f test.tar = = > Delete this archive package to facilitate later testing
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile
[jzhou@localhost dirtest] $gzip-9 testfile = = > compress this file in gz format
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile.gz = = > this is the file name automatically generated after compression
[jzhou@localhost dirtest] $gzip-d testfile.gz = = > unpack the newly compressed package
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile = = > look, testfile is decompressed.
[jzhou@localhost dirtest] $bzip2-9 testfile = = > compress this file in bz2 format
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile.bz2 = = > look, this bz2 is just generated
[jzhou@localhost dirtest] $bzip2-d testfile.bz2 = = > unlock this compressed package
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile = = > look, it's released.
[jzhou@localhost dirtest] $tar jcf test.tar.bz2 testfile = = > this is archive compression in bz2 format. Note that the option is j
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile test.tar.bz2
[jzhou@localhost dirtest] $rm-r testfile
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile test.tar.bz2
[jzhou@localhost dirtest] $tar jxf test.tar.bz2-C / home/jzhou/dirtest/ = = > unlock archive compression
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile test.tar.bz2
[jzhou@localhost dirtest] $tar zcf test.tar.gz dirtest1 = = > this is archive compression in gz format. Note that the option is z.
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile test.tar.bz2 test.tar.gz
[jzhou@localhost dirtest] $rm-rf dirtest1
[jzhou@localhost dirtest] $ls
Linkfile testfile test.tar.bz2 test.tar.gz
[jzhou@localhost dirtest] $tar zxf test.tar.gz-C / home/jzhou/dirtest/ = = > unlock archive compression
[jzhou@localhost dirtest] $ls
Dirtest1 linkfile testfile test.tar.bz2 test.tar.gz
[jzhou@localhost dirtest] $
The above command display format is not very friendly, because in the real environment, if you delete the original file, the soft-linked file will be unavailable and the background will turn red. But this does not affect understanding, hehe.
Note that archiving only puts files or directories in a package and is not compressed, while gzip and bzip2 are compressed. The last few lines of the above commands combine the two, that is, archive first and then compress.
The command format for tar and gzip bzip2 is as follows:
The code is as follows:
Tar [options]... Archive file name source file or directory = "make an archive file"
Tar [options]... Archive file name [- C target directory] = "unlock archive file"
Gzip/bzip2 [- 9] File name or directory = "make a compressed file"
Compressed file in gzip/bzip2-d .gz / .bz2 format = "unzip the compressed file
For the above command, only to cite the simplest usage, as to achieve more powerful functions, it is necessary to check each command with which options, or directly to the man command for help, those options are too many, so I think as long as you know a command, as for the specific usage when used to check and do not need to remember all the meaning of the option.
Common shortcut editing methods of VIM editor
A text editor can be used to create or modify text files and to maintain various configuration files in Linux systems. When you first come into contact with this editor, it will affect the editing efficiency because you are not proficient, but after mastering the common shortcut keys, it is very fast. The following is just a brief introduction to the Vim editor, as for more in-depth usage, you can look for it online.
The default text editor used in Unix and early Linux is Vi, and now it is an enhanced version of vi vim. Because vi is used to it, it is still called vi. In fact, it is an alias alias vi='/usr/bin/vim', that can be seen through the command which vi.
The Vim editor has three working modes: command mode, input mode, and last-line mode. In some materials, it may be said that there are 2 working modes, which does not include the 'last-line mode'. This is not important, because the first two modes are really important; because there are a lot of things you can do in these two modes. The switching between the various modes is shown below:
The conversion of these modes should also be very skillful.
Basic operations in command mode:
(1) display line number:: set nu cancel line number:: set nonu
(2) Fast line-to-line jump: # G: jump to line # in the file; G: jump to the end line of the file; 1G or gg: jump to the beginning of the file line.
(3) Fast in-line jump: Home End
About deleting copy and paste: (in command mode)
Delete
X or Del
Delete a single character at the cursor
Dd
Delete the line of the current cursor
# dd
Delete # lines starting at the cursor
D ^
Delete all characters from the beginning of the line before the current cursor
D $
Delete all characters from the current cursor to the end of the line
Copy
Yy
Copy the contents of the entire current line to the clipboard
# yy
Copy # lines starting at the cursor
Paste
P
After pasting the contents of the buffer to the cursor position
P
Before pasting to the cursor position
Look in the contents of the file:
Operation key
Function
/ word
Look for the string "word" in the file from the top down
? word
Look for the string "word" in the file from the bottom up
N
Locate the next matching found string
N
Locate the last matching found string
Undo edits and save exit:
U
Press once to cancel the most recent operation
Press the u key repeatedly to resume the multi-step operation that has been done
U
Used to cancel all edits made to the current row
ZZ
Save the current file contents and exit the vi editor
Save the file and exit the vi editor: (in last line mode)
Save Fil
: w / root/newfile
Save as another file
Exit vi
: q
Exit without modification
: q!
Discard changes to the contents of the file and exit vi
Save the file to exit vi
: wq
File content replacement: (in last line mode)
: s / old/new
Replace the first character "old" string found in the current line with "new"
: s / old/new/g
Replace all strings found in the current line "old" with "new"
: #, # s/old/new/g
Replace all strings "old" with "new" within the line number "#, #"
:% s/old/new/g
Replace all strings "old" with "new" throughout the file
: s / old/new/c
Adding the c command at the end of the replacement command will prompt the user for confirmation of each replacement action
At this point, the study on the method of Linux file directory management and VIM editor is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.