Today I came across a requirement to copy couple of files and change permissions to execute those files when writing some ansible playbooks. We can do this by using cp, chmod, chown command as shown below. Ownership is changed from root user to normal user surendra.
ls -l abc.sh
output:
-rw-r–r– 1 root root 0 Jun 10 21:22 abc.sh
cp abc.sh /tmp/
chmod 755 /tmp/abc.sh
chown surendra.surendra /tmp/abc.sh
ls -l /tmp/abc.sh
Output:
-rwxr-xr-x 1 surendra surendra 0 Jun 10 21:23 /tmp/abc.sh
I can execute these commands without any problem, but if you want to do same activity on a regular basis then it is time consuming task and more over keeping these commands in ansible play-book is not a great option. How about if we have a command in Linux which do all these stuff in one shot? Yes, we have a command called "install" which do this all in one command. The equivalent install command of above three commands is below.
install -v -g surendra -o surendra -m a+x abc.sh /tmp/abc.sh
To understand above command we should know couple of options for install command. Below examples will help you to understand install command with ease.
Example: Just copy a file from one location to other and do not bother about permissions and ownership.
install abc.sh /tmp
Example: Copy a file with different permissions, for example every user in my machine should have execute permission on the file
install -m a+x abc.sh /tmp/
Note: -m option arguments will be similar to chmod arguments.
Example: Copy a file with different permissions, for example full permissions for a file for all the users.
install -m 777 abc.sh /tmp/
Example: Copy a file with different owner name
install -m 755 -o surendra abc.sh /tmp/
Example: Copy a file with different group owner
install -m 755 -o surendra -g surendra abc.sh /tmp/
Example: Copy a file with preserved time stamp
install -p -m 755 -o surendra -g surendra abc.sh /tmp/
Example: Copy file in verbose mode so that we can see what is happening.
install -v -p -m 755 -o surendra -g surendra abc.sh /tmp/
Output:
removed ‘/tmp/abc.sh’
‘abc.sh’ -> ‘/tmp/abc.sh’
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018