Linux Files and Directories
Linux Files and Directories
Linux Files and Directories
LINUX FILES Each file belongs to a specific user and group. File names can be up to 256 characters . Every FILE/DIRECTORY in Linux has access permissions. There are three types of access READ/ WRITE/ EXECUTE. When listing a file, there are 10 characters that they indicate type and permissions of the file. 1 FILE Type 2 3 4 5 6 7 8 9 10
User permissions
Group Permissions
Other Permissions
read
write
execute
read
write
execute
read
write
execute
These commands cat/more/less/head/tail that can be used to view a file in linux. pico/vi/ued/ed/vim these are editors a file in linux. Commands are used to display content of files but different ways. $ cat script.sql Cat displays contents of the file. $ more script.sql More display contents page wise only in downward direction. (Hitting Enter = One Line Down, Spacebar = Page Down, q=quit) $ less script.sql Displays content pagewise in both upward and downward. Hit q = To quit. $ head script.sql display first few lines of the file. $ tail script.sql display last few lines of the file. Successful People in life seem to like LINUX
Page 1 of 29
create a new file. In vi the mouse cannot be used to move cursor. vi has two modes: the command mode and the insert mode. vi always starts out in command mode. Htting I to enter the insert mode. If we wish to leave from the insert mode and return to the command mode, hit the ESC key. Create a Text file using vi /vim Text Editor $ vi <filename.ext> $ vi samp.sh Text Editor Commands (Moving Within a file ) Moving one character at a time Direction keys to move up , down , left and right in text editor. Command k or j or + l or space h or backspace Description Move up a line Jump down a line Move to the right Move to the left
Page 2 of 29
Description
Delete a character from a file. Deletes the character to the left of the cursor Delete from the cursor position to the end of the line Deletes the current line Delete from the cursor position to the end of the word
5j= Would move down 5 characters 4dd = Deletes four lines. 5dw = Deletes 5 words 10x = Delete the character selected and the next 9 characters 10X = Deletes the 10 characters to the left of the currently selected character d^= Deletes from current cursor position to the beginning of the line Successful People in life seem to like LINUX
Page 3 of 29
ctrl f = scrolls down one screen ctrl b=scrolls up one screen L= Bottom line Editing FILE Commands Command
I I a A o O J
Description
Inserts text before current cursor location Inserts text at beginning of the current line Inserts text after current cursor location Inserts text at end of the current line Open a new line above cursor Open a new line below cursor Joins two lines together
Replacing Characters, Words, and Lines Command R R cw A u Description Replace character at cursor Overwrite current_line starting at cursor Changes and replaces the current word Inserts text at end of the current line Undo the last command
Page 4 of 29
Searching and Substitution Replacing Text : /oracle = search forward for the string 'oracle' ?oracle = search backward for the string 'oracle'
:1,$s/string1/string2/g or :%s/oracle/ORACLE/g substitution command (:s/) enables to quickly replace words or groups of words within the file. Here word oracle is changed to ORACLE. g stands for globally global replacement of sting1 withstring2. Some Additional commands set term=Prints terminal type << = shift contents of line one tab stop to the left >> = shift contents of line one tab stop to the right 2l = Go to 2nd character in the line from the current place. G or shift g = To last line of the file , gg = Top line of the file. $ = Move to the end of the line 0 = Move to beginning of the line Command touch is used to change the timestamp of a file. Using touch command we can create files. $ touch f1 f2 f3 . Successful People in life seem to like LINUX
Page 5 of 29
man/ info command provide a manual (long notes) about the command. manual entries online. Man command will be described in one sentence and some times in paragraphs. $ man rmdir $ info rmdir $ whereis $ORACLE_BASE Adding User and Setting Password # useradd sam Creating user and changing password can be done by # root user not by local user. # indicates root user i.e. SUPER USER . # passwd sam Changing password for user sam New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully
Page 6 of 29
Adding Groups & Finding Groups in Linux # groupadd <group_name> # groupadd oragroup $ less /etc/group , $cat /etc/group , $more /etc/group
Page 7 of 29
Linux Wildcard Search Wildcard allows users to search for or use multiple files with similar names. Wildcards with any command that accepts file/directory name as arguments.
Wildcard Matches
zero or more characters exactly one character exactly one character listed exactly one character in the given range any character that is not listed any character that is not in the given range exactly one entire word in the options given
Examples for wildcard Search $ cp [A-Z]* dir2 Copies all files, that begin with an uppercase letter, to directory dir2: $ rm *[!cehg] This deletes all files that don't end with c, e, h or g $ ls hda[0-9] list all files & dir names begin with hda, followed by exactly one numeral $ rm junk.??? removes all files names begin with junk., followed by exactly three characters Successful People in life seem to like LINUX
Page 8 of 29
Page 9 of 29
DIR 1
DIR 1
DIR 2
DIR 2
DIR 3
DIR 3
home directory
Hidden Files/Dir in LINUX create with a dot "." prefixed. Hidden files filename begins with a . period . To see hidden files using the ls command with -a option. Most of the Hidden files are text files so any text editor like "vim" or "nano" will display the text. "cat .foofile | less" to see it displayed within console. ls -altr lists hidden dir. Successful People in life seem to like LINUX
Page 10 of 29
First letter indicates what type of FILE OR DIRECTORY Remaining 9 characters are broken down into 3 groups of 3 characters. First three are the permissions for the owner, Middle three are permissions for the group which has access to the file , Last three are the permissions for everyone. Permissions are the first 10 characters of the line (-rwxrwx---) are Ex - FILE Permission (-rwxrwx---) rwx rwx --all 1 links root root 1304 size nov 2 space.sh
owner group
mod_dt file_name
First character on the line shows what type of file or directory - = Regular file , d = directory l = symbolic link b = block-type special file Successful People in life seem to like LINUX
Page 12 of 29
Add single permission to a file/directory $ chmod u+x filename Add multiple permission to a file/directory $ chmod u+r,g+x filename Remove permission from a file/directory $ chmod u-rx filename Remove all permission from a file/directory $ chmod a-r,a-w,a-x filename Change permission for all roles on a file/directory $ chmod a+x filename or chmod
Make permission for a file same as another file $ chmod --reference=file1 file2
Page 13 of 29
execute = 001 in binary = 1 in octal So , where does the 7 come from ? 7 in binary = 111 in decimal = read, write, execute Some Examples read write execute= 111=[1+2+4 =7] read write no execute =110=[4 +2=6] read no write execute =101=[4+1=5] read no write no execute=100=[4] no read write execute = 011=[2+1=3]
Page 14 of 29
read, write and executable for owner, group and all others read, write and executable for owner, group only read, write and executable for owner only read and write for owner, group and all others read and write by owner, read only for group and all others read, write and executable by owner, only read and executable by group and others
CHOWN Stands for CHANGE- FILE OWNER & GROUP Command chown used to change the owner and group of a file or directory. This is an admin command, root user only can change the owner of a file or dir Change the owner of a file : root to sam -rwxrwx--- 1 root root 1304 Nov 2 09:56 space.sh # chown <owner> filename # chown sam space.sh -rwxrwx--- 1 sam root 1304 Nov 2 09:56 space.sh space.sh file owner changed from root to sam. Here file group not changed. Successful People in life seem to like LINUX
Page 15 of 29
# chown owner:group <file_name> # chown sona:orasamp space.sh or # chown owner.group <fille_name> # chown sam.oragroup space.sh -rwxrwx--- 1 sona orasamp 1304 Nov 2 09:56 space.sh -rwxrwx--- 1 sam oragroup 1304 Nov 2 09:56 space.sh
Page 16 of 29
Change the owner/group of the sub directories files Recursively drwxr-xr-x 3 root root 4096 Nov 3 22:57 test
From root to sam (owner) From root to oragroup (Group) # chown -R owner:group '/dir_path' # chown -R sam:oragroup '/root/test/' drwxr-xr-x 3 sam oragroup 4096 Nov 3 22:57 test # cd test # ls -l -rw-r--r-1 sam oragroup 60 Nov 3 22:57 space.sh
drwxr-xr-x 2 sam oragroup 4096 Nov 3 22:57 test1 # cd test1 / # ls l -rw-r--r-- 1 sam oragroup 90 Nov 3 22:57 space2.sh
Page 17 of 29
Changing group from sona to dba using chown drwxr-xr-x 2 sona sona 4096 Nov 14 14:53 script
# chown dba script chown: `dba': invalid user # chown sona.dba script # ls -al drwxr-xr-x 2 sona dba 4096 Nov 14 14:53 script
# chgrp dba script # ls -al drwxr-xr-x 2 sona dba 4096 Nov 14 14:53 script
Page 18 of 29
# useradd -g oinstall chris # su - chris $ cd /home/chris/ $ umask 0022 022 is the normal umask, for root and user. umask 022 gives 755 for folders and 644 for files umask default (002) value differs 775/664 (folders/files) That's the default permission you get when you create new files/folders. Successful People in life seem to like LINUX
Page 19 of 29
An user is not assigned to specific group (sona), and no one else is a member of that group, can have 002 -(that would in effect be the same as umask 022) i.e (drwxrwxr-x). If user added with with any group as primary (g) , umask will be 0022(022). Usr1 with Primary Group and umask is 0022 # useradd -g oinstall usr1 usr1 umask : 0022 $ mkdir sample $ touch sample.txt $ ls -ld sample sample.txt drwxr-xr-x 2 usr1 oinstall 4096 Jan 13 20:32 sample -rw-r--r-- 1 usr1 oinstall 740 Jan 13 20:33 sample.txt DIR =777 022 = 755 (rwxr-xr-x) and FILE = 666-022=644 (-rw-r--r-- )
Page 20 of 29
Page 21 of 29
Page 22 of 29
Page 23 of 29
# ls -ld b.txt bb drwxr-xr-x 2 root root 4096 Nov 8 00:17 bdir -rw-r--r-1 root root 240 Nov 8 00:17 bfile.txt
Have a look here , Default settings are not changed , files are created with the access mode 666 and directories with 777. Directories 777 022 = 755 , Files 666 - 022 =644 DIRECTORIES DIR BASE PERMISSION IS 777 , UMASK VALUE= 022 so, 777- 022 = 755
Permissions of new DIR is (777-022) = 755 (rwxr-xr-x) Owner will have read, write, execute permissions Group will have read, execute permissions Others will have read, execute permissions FILES FILE BASE PERMISSIONS IS 666 , UMASK VALUE= 022 so , 666 - 022= 644 Subtract to get permissions of new file (666-022) = 644 (rw-r--r--) No other user can read or write your data, if umask is set to 077. BASE PERMISSION OF FILE IS 0666 and DIRECTORY IS 0777. So DIR = 777 - 077 =700 and FILE= 666 - 066 = 600. Successful People in life seem to like LINUX
Page 24 of 29
[oracle@localhost ~] $ umask 077 [oracle@localhost ~] $ vi script.txt [oracle@localhost ~] $ mkdir script [oracle@localhost ~]$ ls -al script.txt script -rw------- 1 oracle oinstall drwx------ 2 oracle oinstall 144 Nov 11 08:58 script.txt 4096 Nov 11 08:58 .
drwxrwxrwx 3 oracle oinstall 4096 Nov 11 08:58 .. [oracle@localhost ~] $ ls -ld script drwx------ 2 oracle oinstall 4096 Jan 13 07:07 script
If your umask is set to 077 i.e. 0077 , then do a subtraction . Umask sets automatically permissions on newly created files to 066 even we set 077. Newly created directory will be having permission 0777 (rwx rwx rwx) 0077 (---rwxrwx)= 0700 i.e. (rwx------). Newly created file will be having permission 0666 (rw-rw-rw-) 0077 (---rwxrwx) = 0600 i.e. (rw------). POINTS TO REMEMBER UMASK , is it's a mask and not a 'setting' chmod creates a setting. We can set umask in /etc/bashrc or /etc/profile for all users. umask -S displays the current mask in symbolic form. This is the default output. Successful People in life seem to like LINUX
Page 25 of 29
So if we have a program 'touch' create a file naturally with 666 as its permissions (rw-rw-rw-). The octal level for Linux starts at 666. 4 |2 |1 = r|w|x 4+2+1 =7 = rwx 4+0+1 = 5 = r-x Hence 4+2+0 = 6 = rwIf a file with -rwx rwx rwx , must have the octals 777 to match. If a file is -rwx r-x r-x then the octals that match are 755. We start with a base figure of 666 022 = 644 (rw- r-- r--) Change Octal way change to octal mode r-x rw- r-x to abc chmod 565 change to octal mode --x r-xr-to abc chmod 154
Page 26 of 29
[oracle@localhost ~]$ stat script.sql File: `script.sql' Size: 605 Blocks: 8 IO Block: 4096 regular file Links: 1
Device: 805h/2053d
Inode: 2583996
Access: (0644/-rw-r--r--) Uid: ( 500/ oracle) Gid: ( 500/oinstall) Access: 2012-10-15 00:06:17.000000000 +0530 Change: 2012-10-14 23:52:45.000000000 +0530
[oracle@localhost ~]$ stat -f script.sql File: "script.sql" ID: 0 Namelen: 255 Type: ext2/ext3
Blocks: Total: 6694774 Free: 1924314 Available: 1584239 Size: 4096 Inodes: Total: 3401216 Free: 3265909
The f flag shows file-system wide information (e.g. the total number of free inodes) while stat with no arguments shows information for the file itself. stat command gets the information it presents from the inodes stored on disks.
Page 28 of 29
[root@testorcl ~]# whoami root [root@testorcl ~]# id uid=0(root) gid=0(root) groups=0(root), 1(bin), 2(daemon), 3(sys), 4(adm), 6(disk), 10(wheel)
Page 29 of 29