Saturday, June 17, 2017

Miscellaneous Linux Commands

Find All Files Containing Specific Text On Linux

Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude, --include, --exclude-dir or --include-dir flags could be used for efficient searching:
  • This will only search through those files which have .c or .h extensions:
    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
    
  • This will exclude searching all the files ending with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
  • Just like exclude files, it's possible to exclude/include directories through --exclude-dir and --include-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.

View List of Files in ZIP archive on Linux
  • unzip -l zipfilename ( where l is for list )
  • less zipfilename

Read Contents of Zipped Files without 
  1. first use -l to list
  2. Then 'unzip -c archive.zip file1.txt file2.txt | less'
Must pipe to less else the contents fly by , without you being able to see it.


Replacing a File inside an archive
  1. extract file : jar -xf abc.war $file
  2.  insert it back : jar -uvf abc.war $file

BASICS


tput
It is used to clear screen , if you give it as it it doesn't work

tput clear : it clears the screen, most unix systems simply offers clear command.


ps
See the processes

wc
word count

the file contains 6 line , 6 words and 42 characters
  • wc -l filename.txt : count lines
  • wc - w filename.txt : count words
  • wx -c filename.txt : count characters
cmp
Can compare two files
  • if two files identical no output message
  • if difference





comm : whats common in two files







diff
tells you what you need to do , to make files common


echo * & ls


ls options


-F : notice the * tells its an executable , the / tells its a directory and remaining are normal.
-x : multi columnar display



-R is recursive
-r is display in reverse order
-d : display only the directory name
-t : sort file by lat modification time
-u : sort file name by last access time
- i : display inode number

cat
- v : display non printing character
- n : display line numbers

cp & rm
-i : interactive
-R : recurisve

What is inode ?
An inode is an entry in inode table, containing information ( the metadata ) about a regular file and directory. An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4.



Note that
  • cp adds an inode number
  • mv doesn't disturb inode number but changes file name
  •  rm deletes both file and  inode

Different pagers
  • pg : now obsolete
  • more : berkley's original
  • less : mostly available on all linux machines, more powerful and is vi based
tar command
  • -c : create archive
  • -x : extract from archive
  • -t : display files in archive
  • -f arch : specify the archive 
  • -v : verbose
tar -zcvf tar-archive-name.tar.gz source-folder-name 
zip myZipFileName.zip abc.html xyz.txt

chmod

Similarly chmod ugo+x abc.txt





Similarly there is
  • chown
  • chgrp
VI Editor
  • execute mode
  • input mode
input mode commands
  • i : input to left of cursor
  • a : input to right of cursor
  • I : insert at begining of line
  • A : insert at end of line
  • o : opens line below
  • O : opens line above
  • rch :  replace single character under cursor with ch
  • R : replace text from cursor to Right
  • s : replace single character with cursor with any number of characters
  • S: Replace entire line
Quiting
  • :w saving your work ( writes buffer to disk )
  • :wq or :x : save and quit your work
  • :q : doesnt work if buffer is not saved
  • :q! : buffer is changed if you still want to abandon change
more
  • x : delete a character
  • dd : deletes a line
  • 6dd : deletes current line and 5 lines below
  • n : repeat search in current direction
  • N : search in opposite direction 
find   
find
  • find . -name abc.txt
  • find . -iname abc.txt (case insensitive search)
  • find . -name '*.txt'
  • find . -type f -name abc.txt (to find file with name abc.txt)
  • find . -type f -perm 0777 -print  (find file with all permissions) (777 = owner group others )
  • find . -type f ! -perm 0777 -print
  • find . -perm /a=x (find all executable)
  • find . -perm /u=r (find all read only)
  • find . -type f -perm 0777 -exec chmod 755 {} \; ( find files with permission 777 & change it to 755)
  • find . -type f -empty -exec rm -f {} \; ( find empty files and delete it )
  • find . -type f -name “.*” (find all hidden files )
  • find . -type f -mtime 10 ( find files that are modified 10 days back )
  • find . -type f -atime 10 ( find files that were accessed 10 days back )
  • find . -type f -mtime +10 -mtime -20 ( find files that are modified 10 days back & less than 20 days )
  • find . -mmin 1 (find files modified 1 min back )
  • ( -1 means less than 1 day , +1 more than 1 day)
  • find . -user root -name “*.txt”
  • find . -group developer -name “*.txt”
  • find . -size -1M ( find files that are less than 1 MB )
  • find . -size +1M . -size -3M ( find files that are more than 1MB & less than 3 MB )
  • find . -type f -name “*.txt” >> /dev/null (redirect error to /dev/null i.e. dont show on screen)
  • find . -type f -not -name “*.txt” ( find files not txt files)
  • find / -maxdepth 1 -type f -not -name “*.txt” (notice maxdepth)
  • find . \(-name “*.txt” -o -name “*.html” \) -exec rm -f {} \;
grep
  1. grep "regularExpression4string2search" "filePattern*.txt"
  2. -i : for case insensitive search
  3. -w : for full word search
  4. -A N : display N lines after match
  5. -B N : didplay N lines before it
  6. -C N : display N lines around 
  7. -r : recursive searching
  8. -v : invert searching (grep -v "main" * | wc -l  ... search files with not word main )
  9. -c : count number of matches
  10. -I : display filename with matches pattern
  11. -o : show position of matching word
  12. -n : show number while displaying output
llalc2@ABC~/temp
$ grep -c "one" *
abc.txt:1
hello.txt:1

llalc2@ABC ~/temp
$ grep -c "threes" *
abc.txt:0
hello.txt:1

llalc2@ABC ~/temp
$ grep -v "threes" *
abc.txt:one
abc.txt:two
abc.txt:three
abc.txt:four five
abc.txt:six
hello.txt:one
hello.txt:two
hello.txt:four five
hello.txt:six


shells
there are different types of shells
  • korn shell
  • sh : the primitive bourne shell
  • csh : C shell
  • bash shell : very powerfull and recommended for use.

No comments: