Main Menu

Linux Install Command Tutorial

The Basics Use Cases of Install Command

As previously stated, the install command is an effective tool that can perform multiple tasks at once. For example, it can copy files, create folders in Linux, and alter file permissions. Here, we show you the basic usage of the install command. 

Copy Files

Using the Linux install command, you can copy files to wherever you want. In this case, you have a source and target location. And this is how you can do this:

$ install -v file.txt tar_33project/

'file.txt' -> 'tar_33project/file.txt'

In these commands, the `-v` option (short for `--verbose`) showcases the details of the copying process. As an example, we used `file.txt` as the name of the file and copied that file to the `tar_33project` directory. 

How to Copy Files Without Timestamps

Do you want timestamps not to be updated when copying a file using the install command? if yes, you can use the `-p` flag. This is the command that you need to run, replacing the source file with the file you want to copy and destination_directory with the directory that you want to copy the file. 

sudo install -p source_file destination_directory

The -p flag preserves the timestamps of the source file during the copying process.

Create New Directories

You need to use the `-d` option to create a new directory using the install command Linux. To do that, type the following command in your terminal:

sudo install -d new_directory_name

Don’t forget to replace `new_directory_name` with your directory name. 

Attention: To create a directory in a desired location, you can use the following command to specify the full path:

sudo install -d /path/to/desired/location/new_directory_name

Example: Imagine you want to create a directory with the name "data" in a directory named "/home/user". In this case, you will use this command:

sudo install -d /home/user/data

Create Directories and Copy Files

Now, we want to do both use cases, creating a directory and copying files at once. Using this single command, you can create a new directory and copy a file into it. All you need to do is use the `-D` and `-t` options, just like this command:

sudo install -D -t new_directory_name your_file.txt

Don’t forget to replace `new_directory_name` with your desired name and `your_file.txt` with the actual file you want to copy. 

Notes:

  • If the target folder has already been created, the file will be copied to it.
  • This method avoids the requirement for individual'mkdir' and 'cp' commands.
  • '-D': Creates all needed parent directories.
  • '-t': specifies the destination directory to which files will be copied.

To provide multiple files to copy, separate them with spaces:

  sudo install -D -t my_project file1.txt file2.txt file3.txt

Setting File and Directory Attributes with `install` 

With the install Linux command, you can control ownership and permissions of files and directories when the installation process is performed. 

Setting Ownership and Group

You can set the owner and group of a file or directory by using the `-o` and `-g` options, respectively. 

sudo install -o user_name -g group_name file_or_directory

This command will copy or create the provided file or directory, assigning the owner 'user_name' and group 'group_name'.

Setting Permissions

Also, the install command in Linux enables you to set file permissions. To do that, you need to use the `-m` option followed by the desired permission in octal format. 

sudo install -m 755 file_or_directory

This command sets the permissions of the file or directory to 755, which means:

  • Owner: Read, Write, and Execute
  • Group: Read and Execute
  • Others: Read and execute.

Combining Options

Now, you can combine all the above actions if you want. In this way, you can create directories with specific permissions and ownership. To combine these options, use this command:

sudo install -D -m 755 -o user_name -g group_name new_directory

This command will create the 'new_directory' with permissions 755, owned by 'user_name', and assigned to the 'group_name' group. Remember to use'sudo' before the 'install' command to run it with root rights, which is frequently required when altering system files and folders.

Assign directory owner while creating

If you want to assign a directory owner while creating the directory, you need the -o flag in the following order:

sudo install -d -o <owner_user> Directory_name

For example, to assign a user named “john” to the directory “Bash” you use this command:

sudo install -d -o john Bash

Change File Group Ownership

You need the `-g` flag to change the group ownership of a file or directory. This is the command you need to use, replacing the `group_name` with the name you want:

sudo install -g group_name file_or_directory

This flag specifies the new group owner. 

If you want to change both the owner and group, you can combine this with the `-o` flag:

  sudo install -o user_name -g group_name file_or_directory

Note: Ensure that the specified group exists on your system.

Set Group Ownership While Copying a File

To modify the group ownership of a file when copying it, use the '-g' flag again:

sudo install -g group_name source_file destination_directory

In this command, the -g flag specifies the new group owner for the copied file. source_file is the file you want to copy and the destination_directory is the place you want to copy that. 

Example: Imagine you want to copy a file with the name `my_script.sh` to the `/usr/local/bin/` directory as well as setting its group ownership to the `my_group` group:

sudo install -g my_group my_script.sh /usr/local/bin/

This will copy the file to the destination directory and assign group ownership to the'my_group' group.

Create a Directory with Specific Group Ownership

To assign a specific group ownership to a newly formed directory, run the install command with the -g option. Here's how.

sudo install -d -g <group_name> <directory_name>

Using the install Command to Make Backups

This may seem like a strange technique to back up files in Linux. When you use the install command which is one of the essential Linux commands with the -b flag to copy a file to its current directory, it generates an automated backup of the original file. This backup file's name includes a ~ (tilde). To do this, there are two steps to follow:

  1. Ensure the file exists: In the first step, you need to ensure the file you want to back up already exists in the target location. If it doesn’t exist, you need to copy that file to the targeted location first. 
  2. Create the backup: After ensuring the file exists, you can create a backup using the -b flag. It will copy the file to its existing location to trigger the backup process.

Let’s start to see how to perform these steps in detail. 

Step 1: Create a File Copy if Needed

This is easy! All you need to do is specify the source file and the target directory using the install command. In this example, we want to copy Test.txt to My_dir. So, we need to use this command: 

install Test.txt My_dir/

Step 2: Create a File Backup with the -b Option

Creating a backup of a file with `install` command is easy too! You need to use the -b flag to copy the file in the existing location. It is considered as the backup of the original file. In this example, we use a file named `important_file.txt` in the `/etc` directory to create a backup:

sudo install -b important_file.txt /etc/

If you want to customize the suffix of the backup file, you need to use the `-S` flag:

sudo install -b -S .backup important_file.txt /etc/

Using this command, you will create a backup file named `important_file.txt.backup` in the same directory.

Notes:

  1. To alter system files, you will need 'sudo' rights.
  2. The '-b' flag is a useful technique to create backups, but it's crucial to understand how it works to avoid unwanted consequences.

Category: Tutorials Server

Write Comment