LinuxFilters
LinuxFilters
----------------------------------------------------------------------------------------
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~
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
-------------------------------------------------------
3. Basic ‘tee’ Command in Linux --tee command read from std ip and write to op and files
------------------------------------------------------------
4.Basic ‘grep’ Command in Linux---searches for pattern in a 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 Product,Manufacturer and Serial details from var directory where syslog store
sudo cat kern.log | grep -i -w "product\|serial\|manufacturer"
--------------------------------------------------------------------------------------------
-r, --reverse
reverse the result of comparisons
-------------------------------------------------------------------------
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
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
------------------------------------------------------------------
---------------------------------------------------------
7. Basic ‘uniq’ Command in Linux-uniq - report or omit repeated lines
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.
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
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
------------Find Command-------
Find is one of the most powerful cmd in linux and can help with to do no of things in
systesm
find /home -iname filename >find the file case insensitive i.e ignore case
// 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 /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