Home > Article > System Tutorial > Linux file sharing artifact: usage and advantages of shar
If you often need to send and receive multiple files in a Linux system, you may encounter some troubles. You may need to compress the file into tar, gz, zip, etc. format and then send it to your contact. However, your contact may not know how to decompress these files or need to install additional programs to process them. Is there an easier and more convenient way to share Linux files? The answer is yes, that is to use the shar tool. shar is a command line tool that can "package" multiple files into an executable script file. If you send this file to your contacts, they just need to make sure it has execute permissions and then run it to automatically extract the original file. There are no complicated commands and no need to guide them through the process. This article will introduce you how to use shar to create self-extracting files in Linux, and introduce some of its usage and advantages.
Install Shar
Shar is not included by default in most Linux distributions, so you must install it first to create an automatically extracted Shar file archive. However, you won't find it in the Software Center, nor through apt yourself. Instead, you will have to install the larger "sharutils" package that contains it. To do this, launch a terminal and use the following command:
[linuxidc@linux:~/www.linuxidc.com]$ sudo apt install sharutils
Find and prepare files
Shar is a command line tool that processes a large number of files at once and places them in a single archive. So, for convenience and ease of use, create a temporary folder and then move or copy all the files you want to include in the shar archive into that temporary folder.
With your terminal still active, cd to your newly created directory.
Packaging file
To create your shar archive, run the following command:
[linuxidc@linux:~/www.linuxidc.com]$ cd linuxidc [linuxidc@linux:~/www.linuxidc.com/linuxidc]$ ls linuxidc.com.jpg linuxidc.com.py linuxidc.[Java](https://www.linuxidc.com/Java) linuxidc.com.png linuxidc.com.sh [www.linuxidc.com.ogg](http://www.linuxidc.com.ogg/) [linuxidc@linux:~/www.linuxidc.com/linuxidc]$ shar ./* > ../linuxidc.shar shar: 保存 ./linuxidc.com.jpg (文本文件) shar: 保存 ./linuxidc.com.png (文本文件) shar: 保存 ./linuxidc.com.py (文本文件) shar: 保存 ./linuxidc.com.sh (文本文件) shar: 保存 ./linuxidc.java (文本文件) shar: 保存 ./www.linuxidc.com.ogg (文本文件)
Change "Archive File Name" to your file name.
Let us "decompose" it, and let's see how it works.
Of course, the initial shar is the program itself.
The ./* is the input, and in this particular case it means "we are going to all the files in that directory."
> is the allocator between the input and output of the command. The program understands this as "merge each input on the left side of the bracket into a single file defined on the right side of the bracket".
../linuxidc.shar is the path and name of the output file. You can change it to whatever you want.
The process is very quick, usually taking no more than a few seconds (depending on the performance of your PC).
After creating the archive file, you can share the new file with your contacts. Although they also require sharutils to be installed for automatic extraction to work, as you will see in the next step, everything will be simpler than working with typical archives.
Extract shar file
When your friend receives the shar archive, all they have to do is make it executable and then run it.
Assuming your friend has sharutils installed, they can extract the files using the following command:
[linuxidc@linux:~/www.linuxidc.com/linuxidc.com]$ ls linuxidc.com.shar [linuxidc@linux:~/www.linuxidc.com/linuxidc.com]$ chmod +x linuxidc.com.shar [linuxidc@linux:~/www.linuxidc.com/linuxidc.com]$ ./linuxidc.com.shar x - created lock directory _sh33861. x - extracting linuxidc.com.jpg (文本文件) x - extracting linuxidc.com.png (文本文件) x - extracting linuxidc.com.py (text) x - extracting linuxidc.com.sh (文本文件) x - extracting linuxidc.java (文本文件) x - extracting www.linuxidc.com.ogg (文本文件) x - removed lock directory _sh33861.
That’s it – the files it contains are extracted into the same folder. We can now delete the original files they sent us.
Most Linux desktops provide full support for compression formats such as tar, gz, zip, etc., so shar is not very useful in this case. However, if you are using Linux in a server (or headless) environment, shar is very useful because you can easily extract archive files without having to remember various commands - no extra steps, no flags and switches, no Requires additional programs to be installed.
Through the introduction of this article, you should have mastered how to use shar to create self-extracting files in Linux. shar is a very useful tool that allows you to easily share Linux files without worrying about file formatting and decompression. shar also has some other features and options that give you more control and customization of your shar files. For example, you can add comments, passwords, checksums, and other information to your shar files to increase readability and security. You can also use the unshar command to extract the contents of a shar file instead of running it directly.
The above is the detailed content of Linux file sharing artifact: usage and advantages of shar. For more information, please follow other related articles on the PHP Chinese website!