Using Install Command in Linux
Nope, it does not install anything. Surprised?
Yes, despite the name "install", the install command does not install any package. It is an advanced way of copying files where you can set attributes such as file ownership.
For installing packages, you should use the package manager of your Linux distribution, like apt, dpkg, dnf, yum, zypper etc.
Most Linux users don't even know about the existence of this install command, let alone use it.
Take a look at some of the examples of install command usage and perhaps you may include it in your command arsenal.
How to use the install command
In this section, I will start with the basic examples and will gradually move to some advanced ones where the install command truly shines.
I will start with copying files with the install command.
1. Copy files using the install command
If you want to copy files like you do using the cp command, then all you have to do is specify the target file and location where the file needs to be copied:
install Filename Directory
For example, here, I copied the Test.txt
file to the Demo
directory:
install Test.txt ~/Demo
2. Prevent timestamps while copying files
By default, the install command will keep the original file permissions and ownership but update the timestamps with the time when the file was copied.
To prevent this, use the -p
flag as shown here:
install -p Filename Directory_name
For example, here I copied the same file Haruki.txt
with and without -p
flag to show the original and modified timestamps:
3. Create a directory using the install command
To create a new directory like you do with the mkdir command, you will have to use the -d
flag as shown here:
install -d Directory_name
Here's what I did to create a new directory named hello
inside my home directory:
install -d ~/hello
4. Create a new directory and copy files to it (altogether)
If you want to copy files to the new directory, then you can skip the directory creation (cough "productivity" cough) as you can do both of them in a single command.
For this purpose, you'd need to use two flags: -D
and -t
(will explain them in a moment):
install -D -t Directory_name Filename
For example, here, I copied the Test.txt
file to the My_dir
directory:
Saw that? Initially, there was no directory named My_dir
but when I used the install command, it created the directory and then copied the specified file.
Here,
-D
: This flag is used to create all leading components of the target destination. In simple terms, it will create the path if the specified path does not exist.-t
: Short for--target-directory
which is used to specify the target directory.
In addition to the above command chain, I would recommend adding the -v
flag to get the verbose output which will print what the command is doing:
install -v -D -t Directory_name Filename
5. Set permissions using the install command
Linux users typically use the chmod command to change the file permissions but the install command lets you do this while you copy the file to a different location or while creating a new directory.
For this purpose, you'd have to use the -m
flag with the install command so here, I will show you how you can use it while copying files and creating directories.
Set file permissions while copying it
Personally, this is my favorite use of the install command where you can change/set file permission while copying the file.
To do so, you can use the -m
flag in the following manner:
install -m <permission_numbers> Filename Directory
Set permissions while creating a directory
To set permissions while creating directories, use the -m
flag in the following manner:
install -m <permission_numbers> -d Directory_name
For example, here, I created a directory named LHB
with 777
permission:
install -m 777 -d LHB
6. Changing ownership using the install command
The install command lets you change the ownership while you are copying a file or creating a new directory. That's crazy. Right?
For this, you have to append the username to the -o
flag.
So let's take a how you can use the -o
flag with files and directories.
Change ownership while copying a file
To change file ownership while copying a file, use the -o
flag in the following manner:
sudo install -o <owner_user> Filename Directory
Assign directory owner while creating
To assign a directory owner while creating the directory, use the -o
flag in the following manner:
sudo install -d -o <owner_user> Directory_name
For example, here, I assigned a user milan
to the directory Bash_hash
:
sudo install -d -o milan Bash_hash
7. Change group ownership using the install command
You can use the install command to change the group ownership while copying a file or creating a directory.
For this purpose, you have to utilize the -g
flag and here's how you use it.
Change the group ownership of a file while copying it
To change the group ownership of a file while copying it, use the -g
flag with the install command in the following manner:
sudo install Filename -g <group_name> Directory
Create a new directory with specific group ownership
Using the install command, you can assign the group ownership to the directory while performing directory creation. For that, use the install command with the -g
flag as shown here:
sudo install -d -g <group_name> <directory_name>
8. Create backup files using the install command
This is not your traditional way of taking backup in Linux. When you use the install command to override the by copying it again to the same location with the -b
flag, it will add a tilde (~) at the end of the file pointing to the backup file.
To create a backup you'll have to follow 2 simple steps:
- Make sure you already have the same file at the target location and if not, first, make a copy of a file to the target location.
- Use the install command with the
-b
flag to create a backup file.
Sounds confusing? Let me help.
Step 1: Create a copy of a file (avoid if already done)
This is simple, just use the install command and specify the target file and target directory. Discussed in detail in the 1st example.
So here, I copied the Test.txt
file in the My_dir
directory:
install Test.txt My_dir/
Step 2: Use the -b
flag to create a backup of the file
Once you have a copy of a file at the target location, use the install command to copy the same file at the same location but with the -b
flag:
install -b Filename Directory_name
Previously, I copied the Test.txt
file and now I will use the same but with the -b
flag as shown here to create a backup:
install -b Test.txt My_dir/
It creates a file ending with a tilde (~) which is our backup file.
But you can change the suffix using the -S
flag and choose what your heart desires:
install -b -S <suffix> Filename Directory
For example. here, I used the .bkp
suffix:
install -b -S .bkp Test.txt My_dir/
Conclusion
If you are an advanced user and want to be more productive, then the install command is developed for users like you.
But if that looks too complex and confusing, then you can skip this command and use different tools instead such as using the cp command to copy files:
Or using the mkdir command to get more control over the creation of directories:
Once done, you can learn how you can change ownership and permissions in Linux:
I hope you will find this guide helpful.