قالب وردپرس درنا توس
Home / Tips and Tricks / How to Copy Files Using the Install Command on Linux

How to Copy Files Using the Install Command on Linux



  Concept Art of Bash on an Ubuntu Linux Desktop

install is a versatile command for copying files in Linux and Mac OS. It's perfect for power users looking for efficiency. Read this article to learn how to work smarter ̵

1; not harder.

Wait, it is not intended for the installation of software.

The command install may have the most misleading name of any Linux command. There is no software installed. If you are attempting to install a software package from the command line in Ubuntu or another Debian-based distribution, use the command apt-get . Instead, use the package management tool of your Linux distribution on other Linux distributions. For example, dnf under fedora or zypper under openSUSE.

What is being installed?

Briefly install combines elements from the cp (copy), chown (change ownership), chmod (change mode), ] mkdir (directory make) and strip (strip symbols). You can use functions from all these functions in a single action.

With the command install you can:

  • copy files like the command cp .
  • Choose if you want to do this Overwrite existing files.
  • Create the target directory if it does not exist, such as mkdir .
  • Set the user permission flags of the files exactly as they would with the chmod command.
  • Set the owner of the files, as well as the command chown .
  • Remove unused baggage from executable files, just like the strip command With all this functionality, the install command does not have too many options to fight with.

     Installation Guide

    When would you use them

    The ] The install command is not likely to be used every day. It is useful, but only for certain situations. A scenario where is installed is software development. For example, you program a new utility. You must test outside the development environment. To do this, you must copy the new program files to a test directory. You may need to create the test directory, and you must set the correct permissions and ownership for the files.

    Because the development is an iterative activity, you can do this sequence of actions very often. The command install does all the heavy work for you. When your new utility is ready for deployment, you can finally use it with install to copy it to the final workstation with the correct permissions.

    An Example

    A programmer is currently working on such a new utility with the name ana . It consists of an executable binary file and a database. After the test, it must be copied to / usr / local / bin to make it available to all users of the Linux system. You must replace the file names and directory paths in our example with the files and paths that you use on your computer if you install . use

    . Until it is ready to be published, it will be tested in a directory called ~ / test / ana . Members of the geek group have read and execute permissions. Other users also have read and execute permissions. The command install uses the same numeric representation for permissions as chmod . Our programmer has decided that the permissions must be set to the following:

    • Owner: Read, Write and Execute.
    • Group: Read and Execute.
    • Other: Run Only.

    Using the install Command

    The working directory of our fictional programmer is ~ / work . He has written the program, compiled and produced a binary file called ana . He has already created the database file with which ana Words.db works. Thus, both files are ready for testing. Let's take a look:

      ls -l ana Words.db 

    The utility ana that he just wrote creates anagrams from a phrase provided on the command line. Verification tests are pretty simple.

     Test of a utility ana in the working directory

    Our programmer called ana with the sentence "Biscuit" and everything seems to be good. He wants to copy these two files to the ~ / test / ana directory to see if the new utility is working properly outside the development environment. It issues the following command:

      install -D -v ana Words.db -t ~ / test / ana 

      install command for / test / ana

    The following options were used on the command line: [19659008] D : If necessary, create directories, including parent directories.

  • v : In detail, list each directory as it is created, and each file copy as it runs.
  • t : Target Directory

We can see that install creates the ~ / test directory and then creates the ~ /. Directory test / ana . The files are listed one after the other when copying to the destination directory.

Listing the files in ~ / test / ana confirms that they were copied correctly.

  ls -l [19659040lsim/test/ana" width="646" height="132" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

The next step is to test utility ana by calling it in ~ / test / ana ,

 Testing an ana utility in the / test / ana directory

The utility works as expected, which is great. The permissions are not correct. The prerequisite is that members of the geek group have Read and Execute permissions and other users only have Execution.

We can easily address both issues with the following command. Note the use of sudo to execute the command with root privileges. The options -o and -g require this. We will be asked for our password when we issue the command.

  sudo install -b -S .bak -o dave -g geek -m 751 ana Words.db -t ~ / test / ana 

  Installation command with settings for backup options and bonuses

  • The option -b (backup) creates backup copies of the files before they are overwritten.
  • The -S (suffix) option defines the suffix for the backup files. If you do not specify a suffix, a ~ (tilde) is used. We ask install to use a suffix of .bak .
  • We set the owner of the file to dave - with the - o (owner) option.
  • The option -g (group) requires the name of a group. This becomes the owner of the files. The group we will use is called geek .
  • The option -m (mode) sets the file modes for the files, using the default chmod ] numeric syntax.

The option -D (create directories) no longer needs to be used because the test directory already exists. We have also omitted the option -v (verbose). Listing the files in our directory ~ / test / ana shows us the file details:

  ls -l 

  ls in the directory / test / ana

This confirms that all our requirements were met.

  • The files have been copied to the test directory.
  • The permissions have been set correctly.
  • dave is the owner of the files.
  • The group geek is the owner group of the two files.
  • Backups of each file were created, named ana.bak and Words.db.bak.

Everything that was achieved by using a command. Neat.

Our programmer makes some recent changes to the utility and compiles again. The changed files must be copied to the directory ~ / test / ana from the directory ~ / work . You can do this with the option -C (Compare). If source file and destination file are the same, the source file will not be copied.

  sudo install -C -b -S .bak -o dave -g geek -m 751 ana Words.db -t ~ / test / ana 

  Installation command with option -C Compare

The Listing the files in the destination directory tells us that the file size of the file ana has changed. It is larger than the file ana.bak . The timestamp on ana has also changed. These changes are due to the fact that the new version of the file has been copied here.

  ls -l 

  ls in / test / ana with test for a utility

The file size and timestamp of Words.db file has not been changed. No changes were made to the file Words.db so it was not copied. For a project with many files, the -C (Compare) option can save a lot of time and disk loss by copying only the changed files.

The programmer has retested that ana utility is still running.

It is time to use install to copy the files to the / usr / local / bin directory. This will make the new utility available to all users of this Linux computer. We know that / usr / local / bin exists, so we do not need to create this directory. We can use a modified version of our last command.

We changed the destination directory to / usr / local / bin . We have removed the option -C (Compare) because there are not any copies of these files in the target directory, so there is nothing to compare. Likewise, there is nothing to backup, so we can remove the option -b (backup) and the option -S (suffix).

  sudo install -o dave -g geek -m 751 ana Words.db -t / usr / local / bin 

  Install copying files to / usr / local / bin

We can list the files that arrived in / usr / local / bin :

  ls -l 

  ls of / usr / local / bin

As a last test we change that Directory in our home directory and see if we can call our new utility from there.

 Test of an ana utility

Note that we do not run the command ana with it / usr / local / bin . Mission accomplished.

We mentioned that the installation can remove redundant symbol tables and other baggage from the binary file to reduce size. Let's do this now. Note that the following command does not contain Words.db. This is because Words.db is a database file and not a binary executable. To copy and shrink the binary file ana you can use the following command. We added the option -s (reduction) with a lowercase letter "s". We have added the option -b (backup) and the option -S (suffix) with a capital letter "S" (19659027). sudo install -s -b -S .bak -o dave -g geek -m 751 ana -t / usr / local / bin

  Installation option with -s-stripoption

Listing files in ] / usr / local / bin allows you to compare the size of the ana file with its backup version. The file ana has been reduced to almost 60% of its previous size.

  ls -l / usr / local / bin 

  ls in working directory

In Summary

] The command install is suitable for a pretty niche use. For many people, it is not used day to day or possibly month to month. Nevertheless, the command install is a good tool to familiarize yourself with tricks and to have in your arsenal. In the cases you need it, it rewards your learning curve with efficiency gains, simplicity, and fewer keystrokes.




Source link