input license here

How to use the Install command to copy files in Linux

install is a flexible file copying command in Linux and macOS suitable for professional users. Read this article to discover how to use the smarter install command.
  • How to copy and rename files in Linux
  • Search for files and directories in Linux using the command line interface
  • Move files between Linux systems with SCP

Wait, isn't this the command to install the software?

The install command is a command that leads to the most misunderstanding of Linux commands . It does not actually install any software. If installing a software package from the command line in an Ubuntu distribution or other Debian-based distribution, use the apt-get command. On other Linux distributions , you can use the Linux package management tool, such as dnf on Fedora or zypper on openSUSE.

So what is the install command used for?

In short, the install command combines elements from cp (copy), chown (change owner), chmod (mode change), mkdir (create directory) and strip (remove the icon). It allows you to use functions from all the above commands in a single action.
The install command can:
  • Copy files like cp.
  • Choose whether to overwrite existing files.
  • Create destination directory if not available as mkdir command.
  • Assign user rights to files, just like the chmod command.
  • Set ownership of files as chown command.
  • Remove unnecessary things from executable files, just like the strip command.
Although there are all the above functions, the install command doesn't have too many options.
Install command

When to use the install command? 

The install command is not always used even though it is useful but only in certain cases. It is mainly used for software development. Suppose you are programming a new utility and need to check out the development environment. To do that, you need to copy the new program file into the test directory.
Because development is a repetitive activity, you can perform this series of actions many times. The install command will perform all these tasks for you. Finally, when your new utility is ready to be deployed, you can use the install command to copy it with the correct permissions to its final working location.

For example

For example, we are creating a new utility called ana. It includes a binary executable file and database. After testing, it must be copied to / usr / local / bin for users on the Linux system to use. You need to replace the file name, the path to the directory in this example with the file name and the folder you use.
Before it is ready for wide distribution, this utility will be checked in a folder called ~ / test / ana. Geek team members will have the right to read and execute. Other users have only the right to execute. The install command uses numeric representation to grant the same permissions as the chmod command. We will establish the following rights:
  • Owner : Read, write and perform.
  • Group : Read and perform.
  • Other users : Execute only.

How to use the install command

The working directory here is ~ / work. We will write, compile the program and create a binary file called ana and a database file to work with ana named Words.db.
ls - l ana Words . db
Compilation of the program
The written ana utility will reverse the phrase order in the command line.
Ana utility reverses word order
Here we experiment with the word biscuit and the results work quite well. Now we will copy these two files into the ~ / test / ana directory to see if this new utility works correctly when outside the development environment. To copy, use the following command:
install - D - v ana Words . db - t ~ / test / ana
Copy the two files into the ~ / test / ana directory
The options used on the command line are:
  • D : Create a directory, including the parent folder, if needed.
  • v : List each folder when it is created and each file copied when it is done.
  • t : Destination folder.
We can see the install command create the ~ / test directory and then create the ~ / test / ana directory. Files are listed one by one once they are copied to the target directory.
List files in ~ / test / ana to confirm that they have been copied correctly.
ls - l
List files in ~ / test / ana
The next step is to check the ana utility by using it in the ~ / test / ana directory.
Check the utility in the ~ / test / ana directory
Great, the gadget works as expected. However, the permissions assigned to groups are not correct. Here, geek team members have the right to read and execute; Other members are only executed.
We can solve both of these problems quite simply with the following command. Note, you need root access to use sudo, the -o, -g option and other options run this command. Enter the password when required.
sudo install - b - S . bak - o dave - g geek - m 751 ana Words . db - t ~ / test / ana
Redistribute rights
  • -B option (backup) creates backups of files before being overwritten.
  • -S (suffix) option defines the suffix for backup files. If no suffix is ​​provided, the ~ (tilde) option will be used. Here requires install using the .bak suffix.
  • We will set the owner of the file to dave using the -o (owner) option.
  • The -g (group) option requires the name of a group, which is the owner group of files. The group here used is called geek.
  • The -m (mode) option sets the file mode using standard chmod number syntax.
We do not need to use the -D option (create directory) anymore because we already have the test directory and also omit the -v option. List files in ~ / test / ana directory with the following command:
ls - l
List files and assign permissions correctly
This confirms that all of our requirements have been met.
  • The files have been copied to the test folder.
  • The rights have been set correctly.
  • dave is the owner of the file.
  • The geek group is the owner group of two files.
  • Backups have been created from each file, called ana.bak and Words.db.bak.
If you make some changes to the utility and recompile, the changed files should be copied into the ~ / test / ana directory from the ~ / work directory. We can do this using the -c option (compare). If the source file and destination file are the same, the source file cannot be copied.
sudo install - C - b - S . bak - o dave - g geek - m 751 ana Words . db - t ~ / test / ana
Copy changes
Listing the files in the destination directory tells us that the ana file size has changed, it is larger than the ana.bak file and the timestamp also changes. These changes are due to the new version of the file that has been copied here.
ls - l
List files when changed
The file size and timestamp on Words.db file does not change because it is not copied. On a project with many -c option files can save a lot of time and does not clutter the hard drive because it only copies changed files.
Again, we check the ana utility. It's time to use the install command to copy files to / usr / local / bin so that all users on this Linux computer can use the new utility. Since we have the / usr / local / bin directory, we will no longer create this directory. We can use the modified version in the last command.
We changed the destination directory to / usr / local / bin, deleting the -c option because it did not copy the files in the destination directory for comparison. Similarly, we do not need to use the -b (backup) and -s (suffix) option because there is no need to back up.
sudo install - o dave - g geek - m 751 ana Words . db - t / usr / local / bin
Change the destination directory to / usr / local / bin
Use the following command to list files in / usr / local / bin:
ls - l
List files in / usr / local / bin directory
Please perform the final test by changing this directory to the main directory and see if we can call the new utility from here.
Change directory
Note that we do not need to start the ana command with ./ meaning it is running from the / usr / local / bin directory.
As mentioned earlier, the install command can remove symbol tables and other unnecessary things in the binary file to reduce the file size. Let's do that. Note that the following command does not include Words.db file because Words.db is a database file that is not executable binary file. To copy and shrink ana file size, we use the following command, add the -s option (shrink) with lowercase s, option -b (backup) and -S (suffix) with text S capitalization.
sudo install - s - b - S . bak - o dave - g geek - m 751 ana - t / usr / local / bin
Reduce executable file size
Listing files in / usr / local / bin allows us to compare the size of ana file with the previous backup version. Ana file has decreased to nearly 60% of the previous size.
ls - l / usr / local / bin
Compare file
The install command has a lot of effects and helps you minimize many repetitive tasks.
I wish you all success! 
Related Posts
SHARE

Related Posts

Subscribe to get free updates

إرسال تعليق

Sticky