Shell Script to List Files that have Read, Write and Execute Permissions
Last Updated :
20 Apr, 2021
In this article, We will learn how to list all files in the current directory that have Red, Write and Execute permission.
Suppose, we have the following files in our current directory :
Here, We have a total of 8 files in our current directory. Out of 8, we have Read, Write and Execute permission on 6 files and 2 have only Read and Write permissions.
Let's write the script for List the Files that have Read, Write and Execute Permissions
Approach :
- We have to check every file in the current directory and display the name that has Read, Write and Execute permission,
- To traverse through all files, we will use for loop
for file in *
Here, we are using * which represent all files in current working directory and we are storing the current file name on file variable.
- Now we will check whether the chosen file is actually a file or not using if statement
- If it is a file, then we will check whether it has Read, Write and Execute permission,
- We will use an if statement to check all permissions.
- If the file has all permissions then we will print the file name to the console.
- Close the if statement
- If it is not a file, then we will close the if statement and move to the next file.
Before moving forward we will see what these operators do :
- -f $file -> returns true if file exists.
- -r $file -> returns true if file has Read permission
- -w $file -> returns true if file ha write permission.
- -x $file -> returns true if file has Executed permission.
- -a -> it is used for checking multiple conditions, same as && operator.
Below is the implementation:
# Shell script to display list of file names
# having read, Write and Execute permission
echo "The name of all files having all permissions :"
# loop through all files in current directory
for file in *
do
# check if it is a file
if [ -f $file ]
then
# check if it has all permissions
if [ -r $file -a -w $file -a -x $file ]
then
# print the complete file name with -l option
ls -l $file
# closing second if statement
fi
# closing first if statement
fi
done
Now, our code writing work is done but still, we can't run our program because when we create a file in Linux, It has two permissions i.e Read and Write for the User who created the file. To execute our file, we must give the Execute permission to the file.
Assigning Execute permission to main.sh:
$ chmod 777 main.sh
Use the following command to run the script:
$ bash main.sh
Similar Reads
Shell Script to Displays All the Lines Between the Given Line Numbers In this article, We will write a shell script to display all lines between given line numbers to console. We have a file name and start and end line, We have to write a script that will print all the lines from the specified start line to the ending line of the file. Example: File : a.txt Line 1 : H
2 min read
How to use exit code to read from terminal, from script and with logical operators Scripting is an important part of automation in Linux and it is one of the key tools every system administrators use to manage day-to-day activities like taking backups, adding/removing users/groups, updating/removing packages, etc. Since bash is the default shell in most of the Linux distributions,
7 min read
SetUID, SetGID, and Sticky Bits in Linux File Permissions As explained in the article Permissions in Linux, Linux uses a combination of bits to store the permissions of a file. We can change the permissions using the chmod command, which essentially changes the 'r', 'w' and 'x' characters associated with the file. Further, the ownership of files also depen
6 min read
Shell Script to List all Hidden Files in Current Directory Here we are going to see how to write a script to list all hidden files in the current directory, But before starting we will see how to hide the file in the current directory. Hide Files in Linux: In Linux, the files which start with a period (.) sign are the hidden files. We will write a small scr
2 min read
Finding Files With SUID and SGID Permissions in Linux SUID(Set-user Identification) and SGID(Set-group identification) are two special permissions that can be set on executable files, and These permissions allow the file being executed to be executed with the privileges of the owner or the group. SUID: It is special file permission for executable files
3 min read