Linux – Permission and Search Commands (Faq)

1. What is umask?

umask file-creation mask is set to mode. If mode begins with a digit, it is interpreted as an octal number.

[sdbt ~]$ umask
0002

[sdbt ~]$ umask -p
umask 0002

[sdbt ~]$ umask -S
u=rwx,g=rwx,o=rx

2. Which command is used to change the permission of a file?

chmod

used options :

777 (4+2+1 = r+w+x)

or

ugo(user ,group,others)

ex.[sdbt ~]$ chmod u+x test.sql

[sdbt ~]$ ls -l|grep test.sql
-rwxr–r– 1 jlaxmi oinstall 158 May 21 10:58 test.sql

[sdbt ~]$ chmod -R 775 shellss/[sdbt ~]$ ls -l shellss/
total 20
-rwxrwxr-x 1 jlaxmi oinstall 156 Jun 26 10:52 check.sh
drwxrwxr-x 2 jlaxmi oinstall 4096 Jun 26 10:53 dir1
-rwxrwxr-x 1 jlaxmi oinstall 0 Jun 26 10:54 f1
-rwxrwxr-x 1 jlaxmi oinstall 41 Jun 26 10:41 file1.sh
-rwxrwxr-x 1 jlaxmi oinstall 41 Jun 26 10:59 for.sh
-rwxrwxr-x 1 jlaxmi oinstall 171 Jun 26 10:48 ifshell.sh
-rwxrwxr-x 1 jlaxmi oinstall 0 Jun 26 11:02 while.sh

3. chgrp Change the group of each file to group.(root)

[root@sdbt]# chgrp sdbt file2.txt[root@sdbt]# ls -l file2.txt
-rw-r–r– 1 sdbt sdbt 0 Jul 5 13:02 file2.txt

[root@sdbt]# chgrp -R oinstall shellss/

[root@sdbt]# ls -l shellss/
total 20
-rw-r–r– 1 sdbt oinstall 156 Jun 26 10:52 check.sh
drwxr-xr-x 2 sdbt oinstall 4096 Jun 26 10:53 dir1
-rw-r–r– 1 sdbt oinstall 0 Jun 26 10:54 f1
-rw-r–r– 1 sdbt oinstall 41 Jun 26 10:41 file1.sh
-rw-r–r– 1 sdbt oinstall 41 Jun 26 10:59 for.sh
-rw-r–r– 1 sdbt oinstall 171 Jun 26 10:48 ifshell.sh
-rw-r–r– 1 sdbt oinstall 0 Jun 26 11:02 while.sh

4. chown – Change the user or owner to given file.(root)

[root@sdbt]# chown oracle file1.txt[root@sdbt]# ls -l file1.txt
-rw-r–r– 1 oracle oinstall 0 Jul 5 13:00 file1.txt

[root@sdbt]# chown -R oracle shellss/[root@sdbt]# ls -l shellss/
total 20
-rw-r–r– 1 oracle oinstall 156 Jun 26 10:52 check.sh
drwxr-xr-x 2 oracle oinstall 4096 Jun 26 10:53 dir1
-rw-r–r– 1 oracle oinstall 0 Jun 26 10:54 f1
-rw-r–r– 1 oracle oinstall 41 Jun 26 10:41 file1.sh
-rw-r–r– 1 oracle oinstall 41 Jun 26 10:59 for.sh
-rw-r–r– 1 oracle oinstall 171 Jun 26 10:48 ifshell.sh
-rw-r–r– 1 oracle oinstall 0 Jun 26 11:02 while.sh

5. ls Listout the files and directories (learn about wild card characters (? and *)

ls -lrt *a -> list out the files that ends with “a”

ls -lrt c*v? -> list out the files that starts with c and the last second character of the file is “v”

* meand one or more characters
? means single character

ex.[sdbt@acs ~]$ ls ?a*
case.sh xaa xab xac

6. To print a variable or string

echo $PATH -> where PATH is a variable, and $ listes the contents of the PATH variable.

echo “Welcome to sakthidbtechnology”; -> prints the characters that listed in the quotes.

7. xargs

xargs
build and execute command lines from standard input. It converts input from standard input into arguments to a command.

[sdbt ~]$ echo s d b t | xargs #limit output per line using -n option
s d b t

[sdbt ~]$ echo s d b t t b d s | xargs -n 4
s d b t
t b d s

[sdbt ~]$ echo s d b t | xargs -p /bin/echo #prompt user before execution using -p option.

s d b t ?…y
s d b t
[sdbt ~]$ echo s d b t | xargs -p /bin/echo
s d b t ?…n
yes

8. which – Display standard output the full path of the executable commands.

[sdbt ~]$ which sqlplus
/oracle/product/11.2.0/db_1/bin/sqlplus

9. whereis- Locate the binary, source, and manual page files for a command

[sdbt ~]$ whereis bin
bin: /usr/local/bin

10. locate – Find files by name.

[sdbt ~]$ locate glogin.sql
/oracle/product/11.2.0/db_1/sqlplus/admin/glogin.sql

11. find

[sdbt ~]$ find . -name test1.sh #find files using name
./script/test1.sh

[sdbt ~]$ find -iname documents #Ignoring case
./Documents

[sdbt ~]$ find ~ -size 1024 #find files which matches the exact size
/home/jlaxmi/.mozilla/firefox/gpic2ksk.default/cookies.sqlite

[sdbt ~]$ find ~ -mmin -180 #find files which modified within 180min.
/home/jlaxmi
/home/jlaxmi/wdb.sh
/home/jlaxmi/.viminfo

[sdbt ~]$ find ~ -mtime -1 #find files which modified within 1 day.

Find Last 10 Days Modified Files
# find / -mtime 10

Find Last 5-10 Days Modified Files
# find / -mtime +5 –mtime -10

Find Specific log files which is greated than 5M and delete

# find / -type f -name *.log -size +5M -exec rm {} \;

Find teh alert file and remove it
# find . -type f -name “alertsdbt.txt” -exec rm -f {} \;

Find and remove multiple files

# find . -type f -name “*.txt” -exec rm -f {} \;

12. grep – grep, egrep, fgrep – print lines matching a pattern

[sdbt~]$ grep animals fileinfo.txt
animals

[sdbt~]$ grep -v animals fileinfo.txt
fruits
vechicles
fruits
vegetables

[sdbt~]$ grep -i animals fileinfo.txt
animals
Animals

13. egrep == grep -E Interpret PATTERN as an extended regular expression

[sdbt~]$ egrep -C 0 ‘(v|w)echicle’ fileinfo.txt
vechicles
Wechicles

[sdbt~]$ egrep [E-I] alphabets.txt
eE
fF
gG
hH
iI

14. fgrep == grep –F Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

[sdbt~]$ fgrep -f fileinfo.txt filedummy.txt
fruits
Vechicles
fruits
vegetables

15. gawk pattern scanning and processing language. awk was implemented by Alfred Aho, Peter Weinberger, and Brian Kernighan

[sdbt~]$ ls -lrt|grep sdbt
-rw-rw-r– 1 sdbt sdbt 279 Jul 9 12:58 sdbt04
-rw-rw-r– 1 sdbt sdbt 36 Jul 9 12:58 sdbt03
-rw-rw-r– 1 sdbt sdbt 35 Jul 9 12:58 sdbt02
-rw-rw-r– 1 sdbt sdbt 22 Jul 9 12:58 sdbt01
-rw-rw-r– 1 sdbt sdbt 0 Jul 9 12:58 sdbt00

[sdbt~]$ ls -lrt|grep sdbt|awk ‘{print $9 }’
sdbt04
sdbt03
sdbt02
sdbt01
sdbt00

16. cut – remove sections from each line of files

[sdbt~]$ ls -lrt|grep sdbt
-rw-rw-r– 1 sdbt sdbt 279 Jul 9 12:58 sdbt04
-rw-rw-r– 1 sdbt sdbt 36 Jul 9 12:58 sdbt03
-rw-rw-r– 1 sdbt sdbt 35 Jul 9 12:58 sdbt02
-rw-rw-r– 1 sdbt sdbt 22 Jul 9 12:58 sdbt01
-rw-rw-r– 1 sdbt sdbt 0 Jul 9 12:58 sdbt00

[sdbt~]$ ls -lrt|grep sdbt|cut -d ‘:’ -f2
58 sdbt04
58 sdbt03
58 sdbt02
58 sdbt01
58 sdbt00

17. tr – translate or delete characters.

[sdbt~]$ cat fileinfo.txt | tr ‘s’ ‘$’
fruit$
animal$
vechicle$
animal$
fruit$
Vegetable$

[sdbt ~]$ cat fileinfo.txt |tr ‘s’ ‘ ‘
fruit
animal
vechicle
animal
wechicle
fruit
Vegetable