The install command in Linux – Copy files, Create Directories, Change User permissions and File ownerships

Install

The install command in Linux copies files and sets file attributes. It is not to be used for installing packages. For installing packages on to your Linux system you should use apt command or yum command. In this tutorial, we will learn how to use the install command for copying files and changing file attributes.

Let’s go over a list of things that you can do using the install command.

  • Copy files (cp command)
  • Create the target directory if it does not exist( mkdir command).
  • Set the user permission flags of the files (chmod command).
  • Set the ownership of the files (chown command).

How to copy files using install?

To copy files using the install command use the following syntax :

install [OPTION]… SOURCE… DIRECTORY

This will copy the file at the source to the destination directory with the same name.

Let’s see an example :

install sample.txt test_directory

To check the contents of the directory we will cd into it.

cd test_directory
ls

Output :

Test Directory install command
Test Directory

You can also get a verbose output from the install command. Let’s see how to do that in the next section.

How to get a verbose output?

To get a verbose output from the install command use the -v flag along with it.

install -v sample.txt test_directory

Output :

'sample.txt' -> 'test_directory/sample.txt'

You can also use the install command for creating directories. Let’s learn about it in the next section.

How to create directories using the install command?

Let’s try running the following command in Linux :

 install sample.txt new_parent/new_file

The directory ‘new_parent‘ does not exist. Therefore the output we get is :

install: cannot create regular file 'new_parent/new_file': No such file or directory

However, if we use the -D flag along with the command above then install command creates the parent directory and then performs the copy operation.

install -D -v sample.txt new_parent/new_file

Output :

install: creating directory 'new_parent'
'sample.txt' -> 'new_parent/new_file'

Using the -v flag provided a verbose output.

How to set user permissions?

To set user permission you can use the -m flag along with install command. This works just like the chmod command.

 install -m 700 sample.txt new_directory

This will copy the contents of the old file to the new file. The permission for the new file is set to -rwx——.

To check use :

ls -l new_directory

Output :

-rwx------ 1 root root 314 Nov 20 09:05 new_directory

How to change ownership of files?

To change the ownership of files you can use the -o flag along with the install command. This works exactly like the chown command.

install -o Adam sample.txt new_directory

Output:

This copies the contents of the old file to the new file. The ownership of the new file is set to ‘Adam‘. To check use :

ls -l new_directory

Output :

-rwxr-xr-x 1 Adam root 314 Nov 20 09:04 new_directory

Conclusion

This tutorial was about install command in Linux. We covered different utilities provided by the command. We copied files, created directories, changed user ownerships and file permissions all using just one command. Hope you had fun learning with us!