In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to decompress, copy and move files in Linux shell". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Compress and decompress files individually
Common extensions for compressed files under Linux include .gz, .tar, .tar.gz, .zip and so on. These compression formats can be used across platforms (Windows/Mac/Linux). Let's take the .zip file as an example. We already know a text file compression package test.zip. If you want to extract it, simply run the unzip command:
Orion-orion@MacBook-Pro Learn-Linux% unzip test.zip Archive: test.zip inflating: test.txt
What if we want to recompress test.txt? You may be tempted to execute zip test.txt, and then we find a hint:
Orion-orion@MacBook-Pro Learn-Linux% zip test.txt zip warning: missing end signature--probably not a zip file (did you zip warning: remember to use binary mode when you transferred it?) Zip warning: (if you are trying to read a damaged archive try-F) zip error: Zip file structure invalid (test.txt)
In fact, it is wrong to pass parameters, causing zip to mistook test.txt for the compressed file name, which is certainly not legal. Let's look at the parameter composition of zip:
Zip [- options] [- b path] [- t mmddyyyy] [- n suffixes] [zipfile list] [- xi list]
[- b path] is the path to the compressed .zip file, and zipfile list is the list of files to be compressed. Therefore, we can successfully compress it by writing:
Orion-orion@MacBook-Pro Learn-Linux% zip test2.zip test.txt adding: test.txt (stored 0%)
Of course, zip also supports compressing multiple files:
Orion-orion@MacBook-Pro Learn-Linux% zip test3.zip test.txt test2.txt adding: test.txt (stored 0%) adding: test2.txt (stored 0%)
At this point, we find that if we extract the test3.zip, we will find that we have got two original files back:
Orion-orion@MacBook-Pro Learn-Linux% unzip test3.zipArchive: test3.zip extracting: test.txt extracting: test2.txt
Zip also supports directory compression, such as when we try to compress the test directory:
Orion-orion@MacBook-Pro Learn-Linux% zip test4.zip test adding: test/ (stored 0%)
Decompressing test4.zip at this time will regenerate the test directory:
Orion-orion@MacBook-Pro Learn-Linux% unzip test4.zip Archive: test4.zip creating: test/
However, zip is the operation of compressing the list of files entered separately, that is, compressing the directory and one-by-one all the files in the directory. So we need to package a lot of files into a single file, and then compress it? Tar is about to be used at this time.
Tar: packaging command
Many people misunderstand that tar is a compression command, but in fact, the compression commands are gzip, xz, and zip mentioned above. Tar is a packaging command, but with compression and decompression functions. There are so many options for tar. In order to lighten your memory burden, we will only introduce the following two options:
-c: create packaged files (can be paired with-v to visualize the packaged files in the process)
-x: unpack or decompress (can be extracted with-C in a specific directory)
(in fact, there are-z for compression / decompression through gzip,-j for compression / decompression through bzip2 support,-J for compression / decompression through xz support, etc., but we use .zip demonstration here to omit these parameters.)
So, we just need to remember the following command:
Compressed: tar-cv-f filename.zip the name of the file or directory to be compressed
Extract: tar-xv-f filename.zip-C directory to be unzipped (this directory must already exist)
Note that the compressed parameter transfer order is before the compressed .zip file and after the compressed file, make no mistake. (reminiscent of the gcc compiler, but this order problem is avoided when gcc passes parameters in the form of-o output_file.out to specify the output executable.)
For example, to compress the test folder (where there is a test.txt file), we can run the following command:
Orion-orion@MacBook-Pro Learn-Linux% tar-cv-f test4.zip testa testa test/test.txt
Then extract it to the current directory and run the following command:
Orion-orion@MacBook-Pro Learn-Linux% tar-xv-f test4.zip-C. X test/x test/test.txt
Compress multiple files:
Orion-orion@MacBook-Pro Learn-Linux% tar-cv-f test3.zip test.txt test2.txt a test.txta test2.txt
Then extract it to the current directory:
Orion-orion@MacBook-Pro Learn-Linux% tar-xv-f test3.zip-C.X test.txtx test2.txt
From the above, zip/unzip and tar are compressed what unzipped out is what, the original directory is a directory, there is no directory will not automatically generate a directory, but Linux or Mac system visual compression tool is not the same (in Mac known as the "archiving utility"). The compression command in Mac is equivalent to the tar command when compressing directories. For example, we want to use the compression tool that comes with Mac to compress the test folder:
The corresponding archive file will be generated
If you unzip it, you will get the same folder (it will automatically rename us), and it will not help us generate extra directories.
However, if we try to compress multiple files with the compression tool that comes with Mac
It will automatically generate a file called archive .zip for us.
Then, if we try to extract the archive .zip file at this time, we will find that the system will automatically generate a folder called archive for us:
The inside of this folder is the file we need.
This requires extra attention when operating on a large number of files, otherwise you will waste your time copying files!
File copy
Let's follow the scene above. Suppose our current directory is a project directory, and we manually use the visual decompression tool that comes with the system to generate an extra directory. Next, if we want to copy the files from the extra archive folders generated by the system to the current directory, we can use the cp command with the r parameter:
Orion-orion@MacBook-Pro Learn-Linux% cp-r Archive /. Orion-orion@MacBook-Pro Learn-Linux lstest.txt test2.txt Archive
Here the-r parameter represents a recursive copy command, which is used for recursive replication of directories. Note that the archive / in the command represents all files in the archive directory, which means the same as archive / *:
Orion-orion@MacBook-Pro Learn-Linux% cp-r Archive / *. Orion-orion@MacBook-Pro Learn-Linux lstest.txt test2.txt Archive
The option parameter-r is written as-R is equivalent:
Orion-orion@MacBook-Pro Learn-Linux% cp-R Archive / *. Orion-orion@MacBook-Pro Learn-Linux lstest.txt test2.txt Archive
However, if you pass in the parameter archive directly, you will copy the directory as a whole:
Orion-orion@MacBook-Pro Learn-Linux% cp-r file. Cp:. / archiving and archiving are identical (not copied).
It is impossible to have two subdirectories with the same name in the same directory, which, of course, will go wrong, and of course we can copy it to another directory:
Orion-orion@MacBook-Pro Learn-Linux% cp-r archiving / tmp orion-orion@MacBook-Pro Learn-Linux% ls / tmp | grep archiving
You might ask, what's the difference between plus r and no r? If you do not add r, directories are skipped by default, that is, only copy files are allowed:
Orion-orion@MacBook-Pro Learn-Linux% cp Archive /. Cp: archive / is a directory (not copied). Orion-orion@MacBook-Pro Learn-Linux% cp archive / tmp cp: archive is a directory (not copied). File movement
Let's just follow the scene above. Suppose we have successfully copied the test.txt and test2.txt in the archive folder to the current project directory. Now we have a new requirement: we have created a data subdirectory in the project directory, and now we need to move the test.txt and test2.txt in the project directory to the data subdirectory. This requires the following command:
Orion-orion@MacBook-Pro Learn-Linux mv test2.txt test.txt dataorion-orion@MacBook-Pro Learn-Linux ls datatest.txt test2.txt
Note that if you have multiple source files or directories, the last target file (that is, data here) must be the directory. When we move only one file, there is potential ambiguity. Here, because the data directory itself exists, we can move test.txt to the data directory to execute normally:
Orion-orion@MacBook-Pro Learn-Linux mv test.txt data orion-orion@MacBook-Pro Learn-Linux ls datatest.txt
But if the data directory does not exist, mv will be interpreted as renaming, for example, if we delete the data directory and then execute:
Orion-orion@MacBook-Pro Learn-Linux mv test.txt data
This is equivalent to renaming test.txt to data file:
Orion-orion@MacBook-Pro Learn-Linux% ls-l | grep data-rw-r--r-- 1 orion-orion staff 0 4 20 22:01 data
As you can see, the first letter is -, which means that data is a normal file, not a directory (if it is a directory, the first letter is d).
Therefore, you should be very careful when using the MV statement, because it has both the function of moving to the directory and renaming, and you may make mistakes if you don't pay attention to it!
This is the end of the content of "how to extract, copy and move Linux shell files". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.