Table of Contents
Preserve file permissions using -p option in cp command
To preserve the file permissions when copying files or directories using the cp command, you can use the -p or –preserve option.
The -p option will preserve the following attributes of the source file or directory:
- File mode (permissions)
- Access time
- Modification time
- User ID (UID)
- Group ID (GID)
Here’s an example command to copy a file while preserving its permissions:
cp -p source_file.txt destination/
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!And to copy a directory while preserving its permissions:
cp -rp source_directory/ destination/
Note that the -r option is used to copy directories recursively, and the -p option is used to preserve the permissions and other attributes.
Here is another example:
Copy a file and rename it while preserving its permissions:
cp -p /path/to/source/file.txt /path/to/destination/newfile.txt
This will copy the file.txt from the source directory to the destination directory, but rename it to newfile.txt, while preserving the file permissions.
preserve file permissions using -a option in cp command
The -a option in the cp command is used to archive files and directories, which means it preserves the following attributes of the source files or directories:
- Permissions
- Ownership
- Timestamps
- Symbolic links
- Special files and devices
To use the -a option with the cp command, you can run the following command:
cp -a source_file.txt destination/
Or, to copy a directory and all its contents while preserving their attributes, run:
cp -a source_directory/ destination/
The -a option is equivalent to using the options -pR, which preserves permissions and recursively copies subdirectories and their contents.
Additionally, the -a option preserves ownership and timestamps of the copied files or directories.
Check file permissions on Linux
There are different ways to check the permissions of a file in Linux. Here are two commonly used methods:
The ls -l command: You can use this command to display the file permissions, among other details. For example:
ls -l filename
The stat command: This command displays more detailed information about the file, including its permissions. For example:
stat filename
Both methods will give you information about the file’s permissions, ownership, and other attributes.
Let’s move to the details.
To check the permissions of a file in Linux, you can use the ls -l command, which will display detailed information about the file, including its permissions.
Here’s an example command to check the permissions of a file named example.txt:
ls -l example.txt
This command will output something similar to the following:
-rw-r--r-- 1 user user 0 May 5 12:00 example.txt
The first field -rw-r–r– represents the file permissions.
The first character indicates the type of file (- for a regular file, d for a directory, etc.), and the next three characters represent the owner’s permissions (r for read, w for write, and x for execute).
The next three characters represent the group’s permissions, and the final three characters represent the permissions for everyone else.
In this example, the owner has read and write permissions (rw-), and everyone else has read-only permissions (r–).
You can also use the stat command to display more detailed information about a file’s permissions and other attributes. For example:
stat example.txt
This will output a more detailed list of attributes for the example.txt file, including its permissions.