Linux Advanced

Advanced file permissions Linux

Sticky Bit

The sticky bit is set on a directory as a way to prevent users from removing files that they do not own. It is represented by the t over the x position on the other octal as in -rwxrwxrwt or -rwxrwxrwT when the x bit is not set on other.

You can set it by using chmod 1775 or chmod +t

Setgid bit on directory

The setuid can be set on a directory to ensure that all files inside the directory are owned by the group owner of the directory. It is represented by the s over the x position on the group octal as in -rwxrwsrwx or -rwxrwSrwx when the x bit is not set on group.

You can set it by using chmod 2775 or chmod g+s

Setgid and Setuid on files

When setuid is set on a file it causes the file to be executed by the file owner instead of the executing owner and when setgid is set on a file it causes the file to be executed by the file group instead of the executing owner.

 

To change permissions on all directories in a folder cd into it and run

sudo find . -type d -exec chmod xxxx {} \ ;

or for files

sudo find . -type f -exec chmod xxxx {} \ ;

where xxxx is your octals  like 2755 or u=rwx,g=rx,o=r for example chmod 2755 or chmod u=rwx,g=rx,o=r

Linux change permission on all directories within a directory not recursively

You can use the find command from terminal. CD info the directory you wnat to run the command in.

List the folder first to see  that we have the correct ones

sudo find . -maxdepth 1 -type d -ls

If we have a list of the directories we want then we can run 

sudo find . -maxdepth 1 -type d -exec chmod 755 {} \;

To change the permissions on all directories or files in a certain path linux

First we have to use find command to look for either directories of files and then we use the exec command on our find.

To change the permissions on all directories in a certain path

$ sudo find /path/you/want/to/search -type d -exec chmod 775 {} +

To change the permissions on all files  in a certain path

$ sudo find /path/you/want/to/search -type f -exec chmod 775 {} +

To Know what the chmod 775 does go here

Change extension of file type in multiple files in Linux

If your  interested in  converting all higher case "JPG" extensions to lower case "jpg" You could use the command line utility rename like so. CD into directory you want to change. Then

rename -n 's/\.JPG$/\.jpg/' *

Use -n option to test what will be changed, then when you happy with results use without like so

rename  's/\.JPG$/\.jpg/' *

List directory size one folder deep in human-readable form Linux

To list all the directories in a folder by size without  giving all subfolders, a nice clear list with no clutter use

$ du --max-depth 1 -h

du  = estimate file space usage

--max-depth 1 = only go 1 deep in the tree

-h = human-readable

For example

/var$ sudo du --max-depth 1 -h
21M    ./log
4.3M    ./backups
4.0K    ./mail
5.5M    ./crash
8.0K    ./www
588M    ./cache
4.0K    ./tmp
56K    ./spool
4.0K    ./local
4.0K    ./opt
551M    ./lib
1.2G    .

It also totals the whole directory at the end. Pretty cool.

Add text to .avi or mpeg4 video files on Ubuntu 12.04

In order to add text to a video file first we need to make sure we have all the editing tools and codecs

 

sudo apt-get install ubuntu-restricted-extras

sudo apt-get install ffmpeg x264

sudo apt-get install frei0r-plugins mjpegtools

 

Once all these are installed we can add text by running the command below

sudo ffmpeg -i /media/Verbatim/ty/new/130251.mp4 -vf drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSans.ttf: text='Your own text to add here':fontcolor=white@1.0:fontsize=70:x=300: y=600" -y 130251_num8.mp4

 

  • where -i specifies the input file
  • -vf specifies what you want to do to the file
  • drawtext adds the text Your own text to add here with a colour of white fontsize 70 and at the x y cooridainates  300 600 
  • output file name is 130251_num8
  • output file format is mp4

​​The code above will let ffmpeg choose the audio and video codecs which most times is not what you want because it can decrease the quality of the video. In order to select the codecs to apply, use the code below.

sudo ffmpeg -i /home/drupalpro/Desktop/ty/130257.mp4 -vf drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSans.ttf: text='130257':fontcolor=red@1.0:fontsize=70:x=350: y=650" -vcodec libx264 -crf 18 -acodec copy 130257.mp4

  • only change is -vcodev libx264 which select the h.264 codec which is the best
  • -crt which sets the quality (high is bad and low is good) -crt 16 is supposed to be visually lossless to the human eye but -crt 0 is true lossless and will take a long time to encode
  • and -acodec copy which just copies the original

 

FFmpeg also alows yout to sync your audio to video.

ffmpeg -i input.avi -itsoffset 0.2 -i input.avi -map 0:0 -map 1:1  -acodec copy -vcodec copy synced.avi

Check out http://alien.slackbook.org/blog/fixing-audio-sync-with-ffmpeg/ for a detailed explanation.

 

Restart Networking Ubuntu Linux

If using ifconfig eth0 down and up on ubuntu it wont renew the IP from the DHCP server. For this we need to restart the network manager.

To restart the networking service in ubuntu in order to get new IP from DHCP

sudo service network-manager stop

sudo service network-manager start

 

Linux Permissions

Linux permissions are set for three groups on each file or folder. The three groups are Owner, Group and World. If I open a terminal and type;

ls -la

drwxr-xr-x 44 drupalpro drupalpro  4096 Mar 27 18:03 .
drwxr-xr-x  4 root      root       4096 Jun 17  2012 ..
drwx------  3 drupalpro drupalpro  4096 Jun 22  2012 .adobe
drwxr-xr-x  8 drupalpro drupalpro  4096 Jun 22  2012 Aptana_Studio_3
-rw-rw-r--  1 drupalpro drupalpro  5188 Jun 22  2012 .bash_aliases
-rw-rw-r--  1 drupalpro drupalpro 16168 Mar 27 19:00 .bash_eternal_history
-rw-------  1 drupalpro drupalpro  6935 Mar 27 17:33 .bash_history
-rw-r--r--  1 drupalpro drupalpro   220 Jun 17  2012 .bash_logout
-rw-r--r--  1 drupalpro drupalpro  3486 Jun 17  2012 .bashrc
drwx------ 12 drupalpro drupalpro  4096 Mar 27 16:31 .cache
drwxr-xr-x  7 drupalpro drupalpro  4096 Mar 12 07:09 commerce_kickstart
drwxrwxr-x  7 drupalpro drupalpro  4096 Mar 26 16:40 commons

So looking at the last one on the list we see  the first part of the listing drwxrwxr-x give the permissions for the directory commons. The permissions work from left to right in this order ( Owner, Group and World). What this tells us is that its a directory (drwxrwxr-x) and that the owner  has rwx permissions (read,write,execute), the group has rwx permissons  (read,write,execute) and the world has r-x (read,execute).

 

 

Linux File Permissions
Octal Decimal Permission  
000 0 (0+0+0) no permission ---
001 1 (0+0+1) execute --x
010 2 (0+2+0) write -w-
011 3 (0+2+1 write and execute -wx
100 4 (4+0+0) read r--
101 5 (4+0+1) read and execute r-x
110 6 (4+2+0) read and write rw-
111 7 (4+2+1) read and write and execuute rwx

 

Create a Concateneted Volume

Initialize the disk or partition for use by the Logical Volume Manager LVM

pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4
pvs
pvdisplay

Create a volume group

vgcreate myvg /dev/sdb1 /dev/sdb2
vgdisplay

Create a logical volume in an existing volume group

lvcreate --size 1g myvg (creates /dev/myvg/lv01
lvs
lvdisplay

Build the file system ext4 on our new logical volume /dev/myvg/lv01

mkfs -t ext4 /dev/myvg/lv01

 

Software Packages

To list all of the packages installed on you system

rpm -qa

Different flavors of linux have different package managers that do a lot of the backgroung work like installing dependencies.

SuSe uses zypper

To install a package named nmap

zypper in nmap

To search for a package using zypper

zypper se nmap

Where is the package installed

which nmap

/usr/bin/nmap

What version of the package is installed

rpm -qf /usr/bin/nmap

Pages