-------------------Will start with very basic Linux Filters-----------------
1. Basic ‘ls’ Command in Linux
Ls ---list directory contents or list info about file
ls path- list files at path you specified
ls -l > list in long listing format
ls -a > list also hidden files
ls -author > list author of each file
ls -S > sort by file size largest first
ls .*filetype > list particular file type
ls -lh > list with human readbele i,e in kb size
ls -r > reverse order while sorting
ls -R > list subdirectories recursively
ls -LS > symbolic link+ sort by file size
ls -i > print inode of each file
----------------------------------------
2. Basic ‘cat’ Command in Linux
cat -b >> nonempty output lines
cat -n >> number all output lines
cat -s >> ignore empty or repeated lines
cat -e >> equivalent to -vE
----------------------------------------------------------------------------------------
practice: working with directories
1.Stay where you are, and list the contents of /bin and /sbin ?
Sol --- ls /bin
ls /sbin
2.Stay where you are, and list the contents of ~
ls~
3.List the files in /boot in a human readable format?
ls -lh /boot
4.Change to the /etc directory, stay here and create a directory newdir in your home
Directory
cd /etc
Sudo mkdir /home/newdir
5.Create a directory ~/etcbackup and copy all *.conf files from /etc into it. Did you include
all subdirectories of /etc ?
mkdir ~/etc backup
ls /etc/*.conf
cp /etc/*.conf etcbackup
6.Use rename to rename all *.conf files to *.backup .
rename ' s/conf/backup/' *.conf
-------------------------------------------------------
3. Basic ‘tee’ Command in Linux --tee command read from std ip and write to op and files
command | tee file.out >> ls -la | tee xyz.txt
command | tee -a file.out >> -a basically append to files instead of overwriting
------------------------------------------------------------
4.Basic ‘grep’ Command in Linux---searches for pattern in a file
grep options file.txt >> grep “xyz” xyz.txt
grep -i options file.txt >> -i is basically for case insensitive
grep -n options file.txt >> -n means with line no
grep -v options file.txt >> -v select non-matching lines
grep -c options file.txt >> only print a count of matching lines
grep "option" file1.txt file2.txt >> search pattern in two files
grep "REGEX" file >>
grep -w "string" file >> forces pattern to match only whole words
grep -A <N> "string" FILENAME >> after conext of matching how many lines to show specify
grep -B <N> "string" FILENAME >> before content line show
grep -C <N> "String" Filename >> specify line of context
grep -r "ramesh" * >> recurse subdirectories
grep -v -e "pattern" -e "pattern" >> -v is invert -e for regex pattern
Grep OR Operator > grep 'pattern1\|pattern2' filename
Grep AND Operatror > grep -E 'pattern1.*pattern2' filename
-------------------------------------------------------------------------
grep Product,Manufacturer and Serial details from var directory where syslog store
sudo cat kern.log | grep -i -w "product\|serial\|manufacturer"
grep cpu details from kern.log from var directory
sudo cat kern.log | grep -i -w "cpu"
-----Regular Expressions in Grep Command-----
Example 1. Beginning of line ( ^ )
grep "^Nov 10" messages.1
Example 2. End of the line ( $)
grep "pattern.$" file
Example 3. Count of empty lines ( ^$ )
grep -c "^$" messages anaconda.log
grep -c "^$" rohit
Example 4. Single Character (.)
grep ".pattern" filename
Starting me kuch bhi then patttern
One or more occurrence (\+)
grep "hi \+hello" file
Zero or one occurrence (\?)
grep "hi \?hello" input
Character Class ([0-9])
grep [0123] file
--------------------------------------------------------------------------------------------
4.Basis ‘sort’ Command in Linux- sort - sort lines of text files
sort file --sort file in increasing order
sort -r file-- -r, --reverse
reverse the result of comparisons
sort -n file -- -n, --numeric-sort
compare according to string numerical value
sort -nr file --
-n, --numeric-sort
compare according to string numerical value
-r, --reverse
reverse the result of comparisons
sort -k coulmn file >>> -k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
sort -u file >> -u, --unique
with -c, check for strict ordering; without -c, output only the first of an equal run
-------------------------------------------------------------------------
5. Basis ‘cut’ Command in Linux-cut - remove sections from each line of files
Print selected parts of lines from each FILE to standard output.
cut -c2 file >> cut second characters from each line from start
cut -c1-3 file >> cut first -three characters from start
cut -c3- file >> cut third character from start
cut -d':' filed_name file_name >
-d is delimiter how you are separating something
-f is used to select field on basis of delimiter
grep expression file name | cut -d':' -f1,6 > grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
-complement- --complement
complement the set of selected bytes, characters or fields
-s, --only-delimited
do not print lines not containing delimiters
grep expression file name | cut -d':' --complement -s -f5 >> grep "/bin/bash" /etc/passwd | cut -
d: --complement -f1,6 >> print all fields except field 1 and 6
grep "/bin/bash" /etc/passwd | cut -d':' --complement -s -f5 >> do not print field which has not
delimiter
------------------------------------------------------------------
6. Basis ‘wc’ Command in Linux
wc -l : Prints the number of lines in a file.
wc -l filename
wc -w : prints the number of words in a file.
wc -w filename
wc -c : Displays the count of bytes in a file.
wc -c filename
wc -m : prints the count of characters from a file.
wc -m filename
---------------------------------------------------------
7. Basic ‘uniq’ Command in Linux-uniq - report or omit repeated lines
uniq -c example.txt > -c, --count
prefix lines by the number of occurrences
uniq -d example.txt > -d, --repeated
only print duplicate lines, one for each group
uniq -u example.txt > -u, --unique
only print unique lines
-------------------------Locatecommand-------- locate - find files by name
locate reads one or more databases prepared by updatedb(8) and writes file names matching at
least one of the PATTERNs to standard output, one per line.
locate filename--The locate tool is very different from find in that it uses an index to locate files.
This is a lot faster than traversing all the directories, but it also means that it is always outdated.
If the index does not exist yet, then you have to create it (as root on Red Hat Enterprise Linux)
with the updatedb command.
locate -S > -S, --statistics
Write statistics about each read database to standard output instead of searching for
files and exit successfully.
Imp link -https://linux-training.be/funhtml/ch20.html
------------------------------- practice: filters-----------------------------------
Put a sorted list of all bash users in bashusers.txt.
grep bash /etc/passwd | cut -d: -f1 | sort > bashusers.txt
Put a sorted list of all logged on users in onlineusers.txt > hint who cmd will be used
who | cut -d' ' -f1 | sort > onlineusers.txt
Make a list of all filenames in /etc that contain the string conf in their filename
ls /etc | grep conf
Make a sorted list of all files in /etc that contain the case insensitive string conf in their
filename
ls /etc | grep -i conf | sort
Count the number of *.conf files in /etc and all its subdirs
Use find and -exec to rename all .txt files to .tx
find . -name '*.htm'
find . -name '*.htm' -exec mv {} {}l \;
find . -name '*.htm*'
------------Find Command-------
Find is one of the most powerful cmd in linux and can help with to do no of things in
systesm
lets see some options with find
find . -name filename >find the file in the current directory
find /home -name filename >find the file in home directory
find /home -iname filename >find the file case insensitive i.e ignore case
find / -type d -name DirName > Dir using name
find . -type f -name Filename > File Using name or extension
find . -type f -perm 0755 -print > File Permissions
find / -type f ! -perm 755 > File Permissions without
find . -type f -perm 777 -exec chmod 400 {} \; > execute 777 to 400
// find / -type f -perm 0755 -print -x chmod 777 {} \; > execute 755 into 777
find . -type f -name "File.txt" -exec rm -f {} \; > execute and remove File.txt
find /tmp -type f -name ".*" > check all hidden files and quotes is necessary
find / -user USERNAME -name FILENAME > find all the file of that particular user --user check
cat /etc/passwd
find /home -user USERNAME > find home directory of current user
find /home -group GROUPNAME > find all file under this group
find / -user olduser -type f -exec chown newuser {} \; > to create olduser type sudo adduser
oldrule and also create newuser login into olduser and then you canolnly change the ownership
to newuser
find `pwd` -type f -execdir /bin/chmod 500 {} \; > command working
find / -atime 1 > Last 1 Days Accessed Files
find / -cmin -60 > changed file in last 1 hour
find / -size 5M > find all file with size
+n More than n. +60 means more than 60 min
n Exactly n. 60 means exactly
-n Less than n. -60 means less than 60 min
find /home/you -iname "*.txt" -mtime -60 -print // find all file that are modified in less than 60 min
find /home/you -iname "*.txt" -ctime -60 -print //find change time last accessed
find /home/you -iname "*.txt" -atime -60 -print //find file that has been accessed in less 60 min