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.

  1. How to copy and rename files in Linux
  2. Search for files and directories in Linux using the command line interface
  3. 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:

  1. Copy files like cp.
  2. Choose whether to overwrite existing files.
  3. Create destination directory if not available as mkdir command.
  4. Assign user rights to files, just like the chmod command.
  5. Set ownership of files as chown command.
  6. 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.

How to use the Install command to copy files in Linux Picture 1

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:

  1. Owner : Read, write and perform.
  2. Group : Read and perform.
  3. 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 

How to use the Install command to copy files in Linux Picture 2

The written ana utility will reverse the phrase order in the command line.

How to use the Install command to copy files in Linux Picture 3

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 

How to use the Install command to copy files in Linux Picture 4

The options used on the command line are:

  1. D : Create a directory, including the parent folder, if needed.
  2. v : List each folder when it is created and each file copied when it is done.
  3. 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 

How to use the Install command to copy files in Linux Picture 5

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

How to use the Install command to copy files in Linux Picture 6

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 

How to use the Install command to copy files in Linux Picture 7

  1. -B option (backup) creates backups of files before being overwritten.
  2. -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.
  3. We will set the owner of the file to dave using the -o (owner) option.
  4. The -g (group) option requires the name of a group, which is the owner group of files. The group here used is called geek.
  5. 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 

How to use the Install command to copy files in Linux Picture 8

This confirms that all of our requirements have been met.

  1. The files have been copied to the test folder.
  2. The rights have been set correctly.
  3. dave is the owner of the file.
  4. The geek group is the owner group of two files.
  5. 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 

How to use the Install command to copy files in Linux Picture 9

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 

How to use the Install command to copy files in Linux Picture 10

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 

How to use the Install command to copy files in Linux Picture 11

Use the following command to list files in / usr / local / bin:

 ls -l 

How to use the Install command to copy files in Linux Picture 12

Please perform the final test by changing this directory to the main directory and see if we can call the new utility from here.

How to use the Install command to copy files in Linux Picture 13

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 

How to use the Install command to copy files in Linux Picture 14

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 

How to use the Install command to copy files in Linux Picture 15

The install command has a lot of effects and helps you minimize many repetitive tasks.

I wish you all success!

4.3 ★ | 3 Vote

May be interested

  • How to run 2 or more Terminal commands at the same time on LinuxHow to run 2 or more Terminal commands at the same time on Linux
    if you use linux, you probably know how to use useful commands to work with files, install software and launch programs. however, one thing you don't know is that you can still run multiple commands at the same time.
  • How to install and use GNU nano to edit files on LinuxHow to install and use GNU nano to edit files on Linux
    one of the most popular command-line text editors on linux is gnu nano, which comes pre-installed on most modern linux distributions.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • 20+ essential Linux security commands20+ essential Linux security commands
    here are some of the most important security commands for everyday work on linux systems. if you're interested in security issues on your linux system, don't ignore these helpful security commands.
  • How to copy files from Mac to USBHow to copy files from Mac to USB
    when copying files from macos to a usb drive with normal copy paste or drag and drop files, the process will not work properly as on a windows computer. in the article below, tipsmake will guide you how to copy files from a mac to a usb drive.
  • How to use the stat command on LinuxHow to use the stat command on Linux
    the linux stat command shows you more details than the ls command. the following article will show you how to use the stat command in linux. like ls, the stat command has many options. this makes it a great candidate for the use of alias.
  • 14 interesting Linux commands in Terminal14 interesting Linux commands in Terminal
    terminal is a very powerful tool, but it can become 'interesting' through a few bash commands that quantrimang will introduce to you later. let's follow up and find out because some commands are quite useful.
  • Basic Linux commands everyone needs to knowBasic Linux commands everyone needs to know
    when using linux, you should also learn basic linux commands to make operations quicker and easier. here are some basic commands on linux you should know.