How to Copy Paste File and Update User Group of File in Linux (install command)
In Linux, if we want to copy a file and set the file permissions without using the chmod
command separately, we can use the install
command.
The install
command is used to copy files to a destination where we want to paste and set file permissions. If we want to download and install a ready-to-use package on a GNU/Linux system, we should use package managers like yum
or apt-get
.
In Linux, the install
command is similar to the cp
command but the install
command provides more options to set the DESTINATION file properties directly without using chmod
separately.
The general syntax of the install
command is:
install [OPTION]... [-T] SOURCE DESTINATION
install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY...
There are four forms of install
command.
The first three forms are used to copy the SOURCE to DESTINATION or multiple SOURCE to some existing DIRECTORY while setting the permission modes and owner/group.
But the fourth form is used to create all components of the given DIRECTORY.
Brief description of options available with the install
command.
Options |
Description |
--backup[=CONTROL] |
make a backup of each existing destination file |
-b |
like --backup but does not accept an argument |
-C , --compare |
compare each pair of source and destination files, in some cases, do not modify the destination at all |
-d , --directory |
compare each pair of source and destination files, in some cases, do not modify the destination at all |
-g , --group=GROUP |
Used to set group ownership, instead of processing the current group |
-m , --mode=MODE |
set permission mode (as in chmod ), instead of rwxr-xr-x |
-o , --owner=OWNER |
set group ownership, instead of process current group |
-t , -target-directory=Directory |
copy all SOURCE arguments into the Directory |
-T , --no-target-directory |
tread DESTINATION as a normal file |
--help |
display help and exit |
--version |
output version information and exit |
Let's see a few examples.
Copy and Paste a File into another Directory in Linux
In this example, we will copy a file textfile.txt from the shadow directory and paste it into the 'myfolder' directory.
install textfile.txt myfolder
If you run it on a Linux machine, it will look like this,
Copy Paste more than one File into a different directory in Linux
In this example, we will copy 3 files, namely, textfile.txt, snow.sh, and hello.cnf files from some directory and paste them into the shadow/folder directory.
install textfile.txt snow.sh hello.cnf shadow/myfolder
If you run it on a Linux machine, it will look like this,
Copy Paste File into another directory if the copied file already exists (use of -b
option)
In this example, a file already exists in the myfolder directory with the name textfile.txt, and another file with the same name is copied and pasted into the myfolder directory. In this case, a backup file is automatically created for the previous file.
Here is the command,
install -b textfile.txt myfolder
Let's see how this command runs on Linux OS,
How to Change the group of a File in Linux?
If we want, we can use the install command to change the group for any file in linux.
To check the ownership for any file, you can run the following command:
ls -l snow.sh
This command will return the access permissions for this file, user and user group too. (Check the image below to see the output for this command)
It is showing vikash as the user and user group.
But after copying snow.sh file to the myfolder directory using the install
command with --group = <group name>
flag value, the group ownership of snow.sh file is set to vash in the myfolder directory.
The install
command used is this,
sudo install --group=vash snow.sh myfolder
Here is the output when we run these commands on Linux OS,
To know more about the install command, run the below command,
install --help
This is how it will run,
After executing this command, you will get to see all the information related to the install
command with option and description displayed on the output screen.
Conclusion
In this tutorial, we covered how we copy and paste files to a destination folder and set file permissions using the install
command with the various options available in the install
command. The cp
command is also used for the same purpose.