1. Overview

Despite its name, the install command isn’t actually used for installing software packages. Instead, it can be considered a fancy way to copy files to a target location, kind of like the basic cp command but with advanced features.

The install command lets us copy files and create directories like the cp and mkdir commands, but with more control. We can adjust permission modes like with chmod, ownership like with chown, as well as make backups and preserve metadata. These features make it a go-to tool for developers and power users alike.

In this tutorial, we’ll learn the basics of the install command, exploring its functionality and usage examples.

2. Basic Usage of the install Command

The install command is a versatile tool that combines several useful functions from other commands. Here, we’ll explore how it copies data and creates directories.

2.1. Copying Files

One of the simplest uses of the install command is to copy files from a source location to a target location:

$ install -v cicada.txt bouhannana_33project/
'cicada.txt' -> 'bouhannana_33project/cicada.txt'

Using the install command, we copied the cicada.txt file to a directory named bouhannana_33project. To display the details of the process, we included the -v option (abbreviation of –verbose).

2.2. Creating New Directories

Another basic function of install is creating directories like the mkdir command. But again, install offers extra features.

Let’s create a new directory:

$ install -d zaaiy_11project

The -d (short for –directory) option instructed the install command to create a zaaiy_11project directory if it doesn’t exist. If the directory already exists, install won’t overwrite it.

2.3. Creating Directories and Copying Files

When we want to create a new directory and copy files into it, this feature saves us time making separate calls to mkdir.

However, we don’t need -d to create a new directory and then another command to copy files into it. We can pair our command with the -D and the -t (short for –target-directory) options to achieve this with a single command:

$ install -v -D -t new_d/ cicada.txt
install: creating directory 'new_dir'
'cicada.txt' -> 'new_d/cicada.txt'

Voila! The command successfully created a new directory and copied our file into it.

3. Adjusting Files and Directories Attributes

The install command lets us avoid making separate calls to other tools to modify file and directory attributes.

3.1. Setting Owners and Groups While Copying

The install command is a real time-saver when it comes to setting ownership for files and directories. With the -o (short for –owner) and -g (short for –group) options, we can set the target owner and group without having to make separate calls to chown and chgrp.

Let’s copy a target file while changing its owner and group:

$ sudo install cicada.txt -o semara -g bouhannana new_dir/

The above command copies cicada.txt into new_dir and set its owner and group to semara and bouhannana respectively. If the user or group doesn’t exist, the command will fail.

3.2. Setting Owners and Groups While Creating Directories

Let’s use the same approach to create new directories while changing ownership:

$ sudo install -v -d -o semara -g sudo newest_dir

Same as before, the command creates a directory with a specific owner and group.

3.3. Setting User Permissions

We can also use install to set the user permission flags of files, similar to the chmod command. We can do this by using the -m (short for–mode) option followed by the permission flags in octal notation:

$ install -m 644 cicada.txt newest_dir/

The command applies new permissions to the copied file in the newest_dir directory. The permissions for the owner include read and write access, while the group and others will have read-only access as the mode dictates.

Just like creating a new directory with a new owner and group, this is also true for setting new user permissions:

$ install -v -m 700 -d test_dir/
install: creating directory 'test_dir'

In this case, our install command sets read, write, and execute permissions for the user only.

4. Preserving Timestamps

By default, the install command keeps the original ownership and permission mode but updates the timestamp of the files to the time of the copy operation.

The -p (short for –preserve-timestamps) option ensures that the access and modification times of the source files apply to the destination files as well.

This can be useful in situations when copying files for backup or archiving purposes:

$ install -v -p zaaiy.txt newest_dir/
'zaaiy.txt' -> 'newest_dir/zaaiy.txt'

The command copies a file named zaaiy.txt to a directory called newest_dir and keeps the same times when the file was last changed or opened.

5. Backing up Files

Creating file backups is essential to avoid data loss. The install command provides an easy way to create backups while copying files to a target location.

We can use the -b option with the install command to create a backup of a file we’re going to overwrite.

Let’s re-copy a file to our directory:

$ install -v -b cicada.txt bouhannana_33project/
'cicada.txt' -> 'bouhannana_33project/cicada.txt' (backup: 'bouhannana_33project/cicada.txt~')

The -b switch instructs install to backup the existing file in the target location with a tilde (~) appended to its filename as in cicada.txt~.

To avoid appending the default tilde suffix, we can add the -S (short for –suffix) to specify our own:

$ install -b -S .OLD cicada.txt bouhannana_33project/
$ ls bouhannana_33project/
cicada.txt  cicada.txt.OLD

Now, our command creates a backup of the existing file with a suffix of .OLD, then overwrites it. This is useful in situations where we want to avoid overwriting an important file.

6. Conclusion

In this article, we’ve explored the install command in Linux along with its various options.

We’ve learned that the install command can help us copy, create, and modify files and directories without the use of other commands like chmod, chgrp, and chown.

Furthermore, we’ve discussed several use cases, such as copying files, setting ownership, and even creating backups.

Comments are closed on this article!