How to copy files using the command “install” en Linux

Contents

Bash conceptual art on an Ubuntu Linux desktop

install is a versatile file copy command on Linux and macOS. It is perfect for the power user looking for efficiency. Read this post to find out how to work smarter, no more difficult.

Wait, Not to install software?

the install command can have the most misleading name of any of the Linux commands. Actually, does not install any software. If you are trying to install a software package from the command line on Ubuntu or other Debian-based distribution, use el apt-get command. On other Linux distributions, use the package management tool of your Linux distribution, as an example, dnf in Fedora or zypper and openSUSE.

Then, What does the installation do?

In one word install combines items from the cp (Copy), chown (Change of owner), chmod (change mode), mkdir (make directory), and strip (strip symbols) commands. It allows you to use functions of all of them in a single action.

the install command can:

  • Copy files like the cp command.
  • Choose if you want to overwrite existing files.
  • Create the destination directory if it does not exist, What mkdir.
  • Set user permission flags for files, in the same way as the chmod command.
  • Set the owner of the files, in the same way as the chown command.
  • Remove non-essential baggage from executable files, in the same way as the strip command.

Despite all that functionality, the install the command doesn't have too much Many options to deal with.

install man page

When would you use it

the install The command will probably not be used every day. It is useful, but only for certain situations. A stage where install comes into play is software development. Let's say you are programming a new utility. You will need to test outside of the development environment. To do that, you need to copy the new program files to a test directory. You may need to create the test directory and determine the correct permissions and ownership for the files.

Because development is an iterative activity, you may end up doing this sequence of actions many, usually. the install The commando does all the heavy lifting for you. In summary, when your new utility is ready to deploy, you can use install to copy it with the correct permissions to your final working location.

An example

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

Until you are ready to launch, will be tested in a directory called ~/test/ana. The members of the geek group will have read and execute permissions. Other users will also have read and execute permissions. the install The command uses the same numeric representation for permissions as chmod does it. Our programmer decided that the permissions should be set to:

  • Owner: Read, write and execute.
  • Group: Read and execute.
  • Others: Run only.

How to use the install Command

The working directory of our fictitious programmer is ~/work. Has written the program, you have compiled it and produced a binary called ana. He has already created the database file that ana It works with, Words.db. Then both files are ready to try. Let's take a look at them:

ls -l ana Words.db

the ana the utility you just typed creates anagrams from a phrase provided on the command line. Verification tests are pretty straightforward.

testing the ana utility in the working directory

Our programmer has invoked ana with the phrase "cookie" and everything seems to be fine. Now you want to copy these two files to the ~/test/ana directory to see if the new utility works correctly outside the development environment. Issues the following command:

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

Install the command in / test / mother

The alternatives used on the command line were:

  • D: Create directories, including main directories, if required.
  • v: Detailed, list each directory as it is created and each file copy as it is made.
  • t: Destination directory.

We can see that install create the ~/test directory, and then create the ~/test/ana directory. The files are listed one by one as they are copied to the destination directory.

File listing in ~/test/ana confirm that they have been copied correctly.

ls -l

ls in the directory / test / mother

The next step is to test the ana utility by invoking it in the ~/test/ana directory.

test the ana utility in the directory / test / mother

The utility works as expected, which is great. Despite this, the permissions are not correct. The requirement is to determine group members geek have read and execute permissions, and that other users have just run.

We can tackle both problems quite simply with the following command. Please note the use of sudo to run the command with root permissions. the -o and -g and the alternatives require it. 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

install command with backup options and permission settings

  • the -b The option (backup) backs up files before they are overwritten.
  • the -S The option (suffix) set suffix for backup files. If you don't provide a suffix to ~ (tilde) used. We are asking install use a suffix of .bak.
  • We set the owner of the file to be dave using the -o (owner) option.
  • the -g The option (group) need the name of a group. This becomes the group that owns the files. The group that we are going to use is called geek.
  • the -m The option (way) sets file modes for files, using standard chmod numeric syntax.

We no longer need to use the -D (create directories), because we know that the test directory already exists. We have also omitted the -v option (detailed). Listing the files in our ~/test/ana directory shows us the details of the file:

ls -l

ls in the directory / test / mother

This confirms that all our requirements have been met.

  • the files were copied to the test directory.
  • Permissions have been configured correctly.
  • dave is the owner of the files.
  • the geek group is the group that owns the two files.
  • Each file has been backed up, called ana.bak and Words.db.bak.

All of that was achieved through the use of a command.. Cleansed.

Our programmer makes some final changes to the utility and recompiles. Files that have changed should be copied to the ~/test/ana directory of the ~/work directory. We can do this using the -C (compare) option. Whether the source file and the destination file are the same, the source file is not copied.

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

install the command with the -C compare option

Enumerating the files in the destination directory shows us that the file size of the ana the file has changed. It is larger than the ana.bakproceedings. The timestamp in ana it has also changed. These changes are due to the new version of the file being copied here.

ls -l

ls en / test / ana con test fo ana utility

The file size and timestamp of the Words.db file has not changed. No changes were made to the Words.db file, so it was not copied. In a project with many files, -C The option (compare) it can save a lot of time and waste of hard disk time, when copying only files that have been modified.

The programmer has again tested that the ana utility still works.

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

We have changed the destination directory to be /usr/local/bin. We have removed the -C (compare) because there are no copies of these files in the destination directory yet, so there is nothing to compare with. In the same way, there is nothing to back up, so we can remove the -b (backup) and the -S (suffix) option.

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

install by copying files to / usr / local / bin

We can list that the files have reached /usr/local/bin:

ls -l

ls of / usr / local / bin

And as a final test, let's change the directory to our home directory and see if we can invoke our new utility from there.

ana utility test

Please note that it does not need to precede the ana command with ./ which means it is running from /usr/local/bin. Mission accomplished.

We mentioned that the installation can remove redundant symbol tables and other baggage within the binary file, to reduce its size. Let's do it now. Note that the following command does not include Words.db. this is because words.db is a database file, not a binary executable. To copy and shrink the binary file ana we can use the following command. We've added the -s option (shrink) with a “s” lower case. We've added the -b option again (backup) and the -S option (suffix), with a “S” capital letter

sudo install -s -b -S .bak -o dave -g geek -m 751 ana -t /usr/local/bin

installation option with -s strip option

File listing in /usr/local/bin allows us to compare the size of the ana file with its backup version. the ana The file has been reduced to almost the 60% of its previous size.

ls -l /usr/local/bin

ls in the working directory

In summary

The install The command adapts to a very specific use. For many people, will not be used day after day, or possibly from month to month. Despite that,, the install The command is a good tool to get familiar and have in your arsenal of tricks. For those occasions when you need it, rewards your learning curve with efficiency improvements, simplicity and simply fewer keystrokes.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.