
- Operating System Tutorial
- OS - Home
- OS - Overview
- OS - Components
- OS - Types
- OS - Services
- OS - Properties
- OS - Processes
- OS - Process Scheduling
- OS - Scheduling algorithms
- OS - Multi-threading
- OS - Memory Management
- OS - Virtual Memory
- OS - I/O Hardware
- OS - I/O Software
- OS - File System
- OS - Security
- OS - Linux
- OS - Exams Questions with Answers
- OS - Exams Questions with Answers
- Operating System Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
How to Copy File Permissions and Ownership to Another File in Linux?
Many times while taking back up of data from one location to another or configuring software, we need to maintain the same level of ownership and permission of the files. Creating those permissions and granting ownership to individual files can be error prone of done by typing commands for each of the files. So we use some arguments with the chown and chmod commands.
Ownership
We use the –-reference swiych in the chown function to specify the ownership cloning from the source file to the target file.
Syntax
chown --reference=source_reference_file target_file
In the below example we have a source file whose ownership gets cloned to the target file after executing the above command. The Ownership details are shown both before and after applying the command.
# Before cloning the ownership $ ls –lt # Applying the ownership $sudo chown --reference=ref_file.txt all_rivers.txt # After Applying the ownership $ls -lt
Running the above code gives us the following result −
# Before applying ownership -rw-r--r-- 1 root root 19 Jan 1 08:40 all_rivers.txt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt # After applying ownership -rw-r--r-- 1 ubuntu ubuntu 19 Jan 1 08:40 all_rivers.txt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt
File Permission
In a similar manner the file permissions also get copied from one file to another with a similar syntax, but involving chmod.
# Before cloning the permission $ ls –lt # Applying the permission $sudo chmod --reference=ref_file.txt all_rivers.txt # After Applying the permission $ls -lt
Running the above code gives us the following result −
# Before Cloning permission -rw-r--r-- 1 ubuntu ubuntu 19 Jan 1 08:40 all_rivers.txt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt # After cloning permission -rw-rw-r-- 1 ubuntu ubuntu 19 Jan 1 08:40 all_rivers.txt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt
- Related Articles
- Golang program to copy one file into another file
- How to Copy a File to Multiple Directories in Linux?
- How to Copy Odd Lines of Text File to Another File using Python
- How to change file permissions in Python?
- How to set file permissions in Java?
- C program to copy the contents of one file to another file?
- File Permissions in C#
- File Permissions in java
- How to copy a file, group of files, or directory in Linux?
- Getting root permissions on a file inside of vi on Linux
- How to write a program to copy characters from one file to another in Java?
- How to check the permissions of a file using Python?
- How to include another HTML file in an HTML file?
- How to read data from one file and print to another file in Java?
- Remove Lines Which Appear in File B From Another File A in Linux
