Understanding file permissions and ownership on Linux

The files on a Linux system can have reading permissions, writing permissions, executing (running) permissions or no permissions for the user that owns that files, groups of users or the rest – users who does not own that files and they are not members of any group.

The usual file types are:

  • Directory – associated symbol d
  • Normal file – associated symbol (minus, dash)
  • Symbolic link (symlink) (like a shortcut on Windows) – associated symbol l

Permission types:

  • Reading – associated symbol r, or number 4
  • Writing – associated symbol w, or number 2
  • Executing (running) – associated symbol x, or number 1
  • No permission – associated symbol , or number 0

If a file has the reading permission you can open the file and read it, but you can not change the content. If a directory has the reading permission you can read the files in that directory, but you are not allowed to change their content.

If a file has the writing permission you can open the file for reading and for writing (you can change the file’s content and save it with the new content). You can not delete or rename a file unless the directory has the writing permission.

The execution permission allows the user to execute (run) the file (like a shell script).

User types:

  • User – the user name of the owner of the file or directory; if a user creates a file or directory it becomes the owner of that file of directory.
  • Group – a group of users (ftp, mysql), all group members have the same rights for the file or directory.
  • Other – all users that do not own the file or directory and they do not belong to any group that has right for the file or directory.

Setting permissions:

You can set the permissions using the chmod command. There two methods for changing file permissions:

  • Symbolic mode
  • Numeric mode

Symbolic mode

Setting the permissions is made using the associated symbols – rwx.

Actions are defined using mathematical symbols: the + (plus) symbol is used to add a permission, the – (minus) symbol is used to remove a permission, and the = (equal) symbol is used to remove the old permission and set a new one.

For the owners, associated symbols are u for user, g for group, o for others (the rest) and a for all.

To make a file executable type in a console:

chmod +x myfile

To remove the write permissions of the group:

chmod g-w myfile

Numeric mode

Instead of symbols, the associated number are used for setting permissions. The number for each owner will be the sum of the permissions for that owner.

To set the reading, writing and execution rights for the user you use the number 7 (4+2+1); to set the reading and writing rights for group will you use the number 6 (4+2); the reading permission for the rest (others) will be set using number 4.

The command for setting the permissions in numeric mode:

chmod 764 myfile

Here is the association between numbers and letters:

0  |  ---
1  |  --x
2  |  -w-
3  |  -wx
4  |  r--
5  |  r-x
6  |  rw-
7  |  rwx

Changing the owner:

It is done using the command chown.

To change the owner:

chown myusername myfile

To change the group and the owner:

chown mygroup:myowner myfile

To change only the group you use the command chgrp:

chgrp group myfile

Video tutorial

Check also a video tutorial about Unix File Permissions and Ownership (CHOWN, CHMOD, ETC) via Irongeek.com:


Extract audio from video file

Since there are so many tools (Minitube, youtube-dl) which allows you to download video files from YouTube or other video sharing sites, let’s go one step further and extract the sound from the downloaded video file to a mp3 file.

The FFmpeg tool will do the trick just fine. Thus assuming that you have the video file “My Video File.mp4” and you want to extract  the sound into “My Sound File.mp3 ” use the following command:

ffmpeg -i "My Video File.mp4"  -ab 128 "My Video File.mp3"

With the “-ab 128” parameter you specify that the audio bitrate should be 128k. Additionally the “-ar” parameter may be used for the audio sampling frequency but by default it is set to 44100, which should be ok.

On Ubuntu it may be necessary to add the Medibuntu repository and install the libmp3lame codec. For FFmpeg the audio codec can be specified using the parameter “-acodec”.

CLI search and replace in multiple files

Here is a quick tip about how to find and replace a text in multiple files, from CLI.

In this example you have some text files (.txt) with urls like “http://example.com, http://another-example.com…”. What you are trying to do is to replace all “http” references with “https”.

You can find all text files using the following command in the directory where the files are located:

find -type f -name *.txt

You can execute a command on the search result using the -exec parameter for the find command:

find -type f -name *.txt -exec my_command

The actual search and replace will be performed using sed command:

find -type f -name *.txt -exec sed -i 's/http/https/' {} \;

Find all folders with same name in a directory tree and execute commands on them

In a Linux CLI environment you can use the “find” command to search for files or folder. In this tutorial I will explain how to find folders having the same name in a directory tree and execute commands on them.

Since the best way to learn is by example, let’s consider the following scenario: a collection of images along with their corresponding thumbnails in directories named “thumbs”; so what you are trying to do is remove all “thumbs” directories from your images collection.

The first step is to determine the path of the directory tree. Assuming that “/home/cristian/wallpapers” is the path you can execute the command by providing the entire path as parameter for the “find” command. If you are already in this directory you can use the dot “.” instead of the path (the dot is the equivalent of the current directory).

Now that you have the path, you need two more parameters for the find command – the name of the directories and the type. For the type you must use the letter “d” since this is how the “find” command will know that you want to search for a directory. For the name just provide “thumbs” since this is what we want to remove (see above scenario).

So far the command is:

find "/home/cristian/wallpapers" -name thumbs -type d

You found the directories, now pass this list to the deleting command with “xargs”:

find "/home/cristian/wallpapers" -name thumbs -type d | xargs rm -rf

If you need root permissions to delete the files just add the “sudo”:

find "/home/cristian/wallpapers" -name thumbs -type d | xargs sudo rm -rf

then enter your password.