Understanding Linux File Permissions and Ownerships

From one of our previous articles "How to manage Linux Users and Groups" we discussed on how Linux becomes a multi-user OS, what is a user and a group with their configurations. By design, even though Linux allows multiple users can use the same computer in the same time without affecting others, Linux doesn't allow you to access or modify files belonging to other users. If Linux allows you to do it, that would be a security risk. But somehow they have implemented a security measure to mitigate that security risk. With that we can make sure only desired users and groups can access the relevant files and directories.

If we take a small demonstration. Here, we will log in as a normal user and try to access a root directory.

It gave a permission denied error when accessing. Why ? That's because /root directory is owned by user root. Only a privileged user can access or modify that.

So, Linux introduces two kind of factors which tells who can access or modify a file as w…

Wildcards - Linux

What is a wildcard ? It's like a pattern of characters. You guess a pattern of characters and can be used wildcards to represent that. Simply we use wildcards to represent a set of characters with a pattern. It can be used with any commands. You can grab more referring below examples.

There is a basic set of wildcards,

  • * : This represents zero or more characters ( note that it's any character )
  • ? : This represents a single any character 
  • [ ] : This represents a range of characters matches for any occurrence enclosed within bracket. 
  • { } : More Similar as above. 
Now we will take one by one with examples.

1) * : This represents any number of any character. 

eg_1 : * can be helpful in listing files inside of a directory.

There are some files named foo1,foo2,foo3,foo4 in my home directory. If we need to list files inside of all those directories, can be used the * wildcard as foo*. It checks the matches that starts with foo and the rest of the name can be anything. So foo1,foo2,foo3,foo4 is selected.

How to use wildcards in Linux.

eg_2 : Guess we have several text files ( a.a.txt, a.b.txt, a.c.txt... )

So we need to list all the text files. Wildcards helps us with *. Text files end with .txt file extension. So *.txt means all the files ending with ".txt" and whatever character in the front.

How to use wildcards in Linux.

eg_3 : Guess we need to remove all text files we created before. So, we can use rm -f  *.txt. It means remove files ending with a .txt file extension.

How to use wildcards in Linux.

eg_4 : Guess you need to move all the text files to another directory.

How to use wildcards in Linux.

*** mv is the linux command used to move a file. That will we discussed in a next article. 

In the above example, it moves all the files ending with ".txt" to the directory named foo1.

2) ? : Used to represent a single any character. 

eg_1 : Guess we have some files named marble, marks, mark, mercy and subjects. So, we need to list files  which starting from 'm' and seconds character can be anything and third character should be 'r' while the rest of the characters can be any.

In this case, we can use ? wildcard. ls m?r* What does it means ?

m - starting from letter m
? - second character can be any one character. It can be a number, string or any character. 
r - third character should be r
* - rest of the characters can be anything. ( can be zero characters or any number of character)

How to use wildcards in Linux.

eg_2 : Guess the below example.

How to use wildcards in Linux.

ll *.??? 

Above command will display any named character in the start and it should be end with 3 characters.

3) [ ] : Used to list files. 

eg_1 : Guess we have some files as below.

So, we need to list fruit names that starts with 'a' or 'b' or 'l'. So it should list apple, banana and lemon. Because those are starting with a or b or l.

How to use wildcards in Linux.

* So what [ ] wildcard do ? If we put one or more characters into [ ], it will list the files containing that character. 

eg_2 : Guess we have some files as below.

We need to list files having below conditions.
i) starting with any characters
ii) In the middle of the name there should be a number in between 3 to 9.
iii) In the end of the name, it can be anything.

So, below shows how we can use wildcards for our conditions. If we need a range of numbers, we can give that range inside of the square brackets like below. It can be alphabetic letters also. If we need names having a to m, we can use [a-m].

How to use wildcards in Linux.

eg_3 : Guess we need to delete the above mentioned files.

How to use wildcards in Linux.

4) { } : Used to create multiple files. 

eg_1 : Guess we need to create a set of text files starts from 0 to 9. We can use { } wildcard. To indicate the range we should use ".."  as below.

How to use wildcards in Linux.

*** Wildcards are very useful in day-to-day work with Linux. This is a very basic knowledge with wildcards. Refer more and learn. 



Popular posts from this blog

Different types of users in linux

Understanding Linux File Permissions and Ownerships

SUDO: What is it in Linux ?