Files Management

July 5, 2016   

Here are the basics for dealing with files in Linux.

Create

Empty file:

touch file_name

Text file:

vi file_name

Remove

rm -rf folder

r: recurse for all files under folder f: force remove

Move or Rename

mv old_file new_file

Size of the directories

du -ch *

Total size of the current directory

du -ch | grep total

Link

ln -s origin_file location_of_linked_file

s: Symbolic link

Change owner

chown user:group -R folder

Change group

chgrp -R group_name folder

R: for folder

Add a user to a group

sudo usermod -a -G groupName userName

Mode Structure

ls -l
drwxr-xr-x file

Definition

d: directory
rwxr-xr-x: user group others (3u 3g 3o)
w: write
r: read
x: execute

You could change file mode by numeric or symbolic methods.

Numeric

Definition

7 -> full
6 -> read and write
5 -> read and execute
4 -> read only
3 -> write and execute
2 -> write only
1 -> execute only
0 -> none

Example

chmod 644 file
ls -l
-rw-r--r--   file

644: means make the user read and write, group read and other read.

Symbolic

Definition

u -> user
g -> group
o -> others
a -> all

  • -> add
  • -> remove
    = -> equal

Example

chmod u=rw,g+w,o-r file
ls -l
-rw-rw----   file

u=rw: let the user read and write just
g+w: add write permission to group
0-r: remove read permission to the others

if you want to add something to all you don’t need to type a

chmod +x file
ls -l
-rwxrwx--x   file

+x: same as a+x

Umask

To make default mode when creating a file you need to set the umask.Display current umask:

umask
0022
// or
umask -S
u=rwx,g=rx,o=rx

Set umask

umask 0077
// or
umask u-x,g=r,o+w

References:



comments powered by Disqus