Linux file folder manage example

To introduce permissions as they apply to both directories and files in CentOS Linux, let’s look at the following command output.

Listing files and folders: Use the ls command to list the contents of a directory.

  • ls

Creating directories: Use the mkdir command to create a new director

  • mkdir new_folder

Creating files: Use the touch command to create a new empty file.

  • touch new_file.txt
Copying files and folders: Use the cp command to copy files and folders.
  • cp file.txt /path/to/destination
Moving files and folders: Use the mv command to move files and folders.
  • mv file.txt /path/to/destination
Renaming files and folders: Use the mv command to rename files and folders.
  • mv old_name.txt new_name.txt
Removing files and folders: Use the rm command to remove files and folders.
  • rm file.txt
To remove a directory and its contents recursively, use the -r option:
  • rm -r directory

Changing file permissions: Use the chmod command to change file permissions.

  • chmod 644 file.txt
Searching for files and folders: Use the find command to search for files and folders.
  • find /path/to/search -name “filename”
Applying Permissions to Directories and Files

In Linux, file and directory permissions are managed using the chmod command. Permissions are represented by three sets of characters: owner, group, and others, denoted as “r” for read, “w” for write, and “x” for execute. Here’s how you can apply permissions:

Symbolic Method
  • chmod [who][operator][permissions] file/directory
  • [who] specifies whose permissions are to be changed.
  • [operator] can be + to add permissions, - to remove permissions, or = to set permissions explicitly.
  • [permissions] can be r, w, or x, optionally combined.
Example:
  • chmod u+x file.txt (Add execute permission for the owner on file.txt)
  • chmod g-w file.txt (Remove write permission for the group on file.txt)
  • chmod a=rw file.txt (Set read and write permissions for all)
Numeric Method:

Each permission has a numeric representation:

You can add up these values for each permission set.

Example

chmod 755 file.txt (Owner has read, write, and execute permissions; group and others have only read and execute permissions)

Applying Permissions to Directories

When applying permissions to directories, there’s an additional permission that has special meaning.

  • r (read): Allows viewing the contents of the directory.
  • w (write): Allows creating, deleting, or renaming files within the directory.
  • x (execute): Allows accessing files and subdirectories within the directory.

For directories, the execute permission (x) is crucial. Without it, you cannot access files or subdirectories within, even if you have read (r) permission on the directory itself.

Examples:

Giving full access to the owner, read-only access to the group and others:

  • chmod 755 directory_name
Giving full access to the owner, read and execute access to the group, and no access to others:
  • chmod 750 directory_name

Remember, when applying permissions recursively, use the -R flag with chmod to apply changes to all files and subdirectories within a directory. However, be cautious with recursive changes, as they can affect many files and directories at once.

Leave a Comment