"To be a warrior is not a simple matter of wishing to be one. It is rather an endless struggle that will go on to the very last moment of our lives. Nobody is born a warrior, in exactly the same way that nobody is born an average man. We make ourselves into one or the other." --Kokoro
Make sure that grub is looking at the correct location for the kernel. Â I just had a situation where I copied a VM grub menu which was using LVM to a physical host on accident , when I rebooted grub errored out. Â This is a work in progress..
Drop to grub command line.
esc
type ‘c’
grub> Â find /boot/vmlinuz
(hd0,2)
hit esc
select your correct kernel in grub, type ‘e’
Verify your config is pointing to correct partition, if not as was the case for me, change to whatever find found and hit enter, then type ‘b’ to boot.
(pulled from http://www.folkstalk.com/2011/12/101-examples-of-using-find-command-in.html)
Find Command in Unix and Linux Examples
Find is one of the powerful utility of Unix (or Linux)Â used for searching the files in a directory hierarchy. The syntax of find command is
1
find[pathnames][conditions]
Let see some practical exercises on using find command.
1. How to run the last executed find command?
1
!find
This will execute the last find command. It also displays the last find command executed along with the result on the terminal.
2. How to find for a file using name?
1
2
3
find-name"sum.java"
./bkp/sum.java
./sum.java
This will find all the files with name “sum.java” in the current directory and sub-directories.
3. How to find for files using name and ignoring case?
1
2
3
4
find-iname"sum.java"
./SUM.java
./bkp/sum.java
./sum.java
This will find all the files with name “sum.java” while ignoring the case in the current directory and sub-directories.
4. How to find for a file in the current directory only?
1
2
find-maxdepth1-name"sum.java"
./sum.java
This will find for the file “sum.java” in the current directory only
5. How to find for files containing a specific word in its name?
1
2
3
4
5
find-name"*java*"
./SUM.java
./bkp/sum.java
./sum.java
./multiply.java
It displayed all the files which have the word “java” in the filename
6. How to find for files in a specific directory?
1
find/etc-name"*java*"
This will look for the files in the /etc directory with “java” in the filename
7. How to find the files whose name are not “sum.java”?
1
2
3
4
5
find-not-name"sum.java"
.
./SUM.java
./bkp
./multiply.java
This is like inverting the match. It prints all the files except the given file “sum.java”.
8. How to limit the file searches to specific directories?
1
2
3
4
5
6
7
find-name"sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java
You can see here the find command displayed all the files with name “sum.java” in the current directory and sub-directories.
a. How to print the files in the current directory and one level down to the current directory?
1
2
3
4
find-maxdepth2-name"sum.java"
./tmp/sum.java
./bkp/sum.java
./sum.java
b. How to print the files in the current directory and two levels down to the current directory?
1
2
3
4
5
find-maxdepth3-name"sum.java"
./tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java
c. How to print the files in the subdirectories between level 1 and 4?
1
2
3
4
5
6
find-mindepth2-maxdepth5-name"sum.java"
./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
9. How to find the empty files in a directory?
1
2
find.-maxdepth1-empty
./empty_file
10. How to find the largest file in the current directory and sub directories
1
find.-typef-execls-s{}\;|sort-n-r|head-1
The find command “find . -type f -exec ls -s {} \;” will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.
11. How to find the smallest file in the current directory and sub directories
1
find.-typef-execls-s{}\;|sort-n-r|tail-1
Another method using find is
1
find.-typef-execls-s{}\;|sort-n|head-1
12. How to find files based on the file type?
a. Finding socket files
1
find.-types
b. Finding directories
1
find.-typed
c. Finding hidden directories
1
find-typed-name".*"
d. Finding regular files
1
find.-typef
e. Finding hidden files
1
find.-typef-name".*"
13. How to find files based on the size?
a. Finding files whose size is exactly 10M
1
find.-size10M
b. Finding files larger than 10M size
1
find.-size+10M
c. Finding files smaller than 10M size
1
find.-size-10M
14. How to find the files which are modified after the modification of a give file.
1
find-newer"sum.java"
This will display all the files which are modified after the file “sum.java”
15. Display the files which are accessed after the modification of a give file.
1
find-anewer"sum.java"
16. Display the files which are changed after the modification of a give file.
1
find-cnewer"sum.java"
17. How to find the files based on the file permissions?
1
find.-perm777
This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command “ls -l”.
18. Find the files which are modified within 30 minutes.
1
find.-mmin-30
19. Find the files which are modified within 1 day.
1
find.-mtime-1
20. How to find the files which are modified 30 minutes back
1
find.-not-mmin-30
21. How to find the files which are modified 1 day back.
1
find.-not-mtime-1
22. Print the files which are accessed within 1 hour.
1
find.-amin-60
23. Print the files which are accessed within 1 day.
1
find.-atime-1
24. Display the files which are changed within 2 hours.
1
find.-cmin-120
25. Display the files which are changed within 2 days.
1
find.-ctime-2
26. How to find the files which are created between two files.
1
find.-cnewer f1-and!-cnewer f2
So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.
27. How to find the permissions of the files which contain the name “java”?
1
find-name"*java*"|xargs ls-l
Alternate method is
1
find-name"*java*"-execls-l{}\;
28.  Find the files which have the name “java” in it and then display only the files which have “class” word in them?
1
find-name"*java*"-execgrep-Hclass{}\;
29. How to remove files which contain the name “java”.
(pulled from http://www.folkstalk.com/2011/12/good-examples-of-awk-command-in-unix.html)
Awk is one of the most powerful tools in Unix used for processing the rows and columns in a file. Awk has built in string functions and associative arrays. Awk supports most of the operators, conditional blocks, and loops available in C language.
One of the good things is that you can convert Awk scripts into Perl scripts using a2p utility.
The basic syntax of AWK:
1
awk'BEGIN {start_action} {action} END {stop_action}' filename
Here the actions in the begin block are performed before processing the file and the actions in the end block are performed after processing the file. The rest of the actions are performed while processing the file.
Examples:
Create a file input_file with the following data. This file can be easily created using the output of ls -l.
1
2
3
4
5
6
-rw-r--r--1center center 0Dec 821:39p1
-rw-r--r--1center center17Dec 821:15t1
-rw-r--r--1center center26Dec 821:38t2
-rw-r--r--1center center25Dec 821:38t3
-rw-r--r--1center center43Dec 821:39t4
-rw-r--r--1center center48Dec 821:39t5
From the data, you can observe that this file has rows and columns. The rows are separated by a new line character and the columns are separated by a space characters. We will use this file as the input for the examples discussed here.
1. awk ‘{print $1}’ input_file
Here $1 has a meaning. $1, $2, $3… represents the first, second, third columns… in a row respectively. This awk command will print the first column in each row as shown below.
1
2
3
4
5
6
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
-rw-r--r--
To print the 4th and 6th columns in a file use awk ‘{print $4,$5}’ input_file
Here the Begin and End blocks are not used in awk. So, the print command will be executed for each row it reads from the file. In the next example we will see how to use the Begin and End blocks.
2. awk ‘BEGIN {sum=0} {sum=sum+$5} END {print sum}’ input_file
This will prints the sum of the value in the 5th column. In the Begin block the variable sum is assigned with value 0. In the next block the value of 5th column is added to the sum variable. This addition of the 5th column to the sum variable repeats for every row it processed. When all the rows are processed the sum variable will hold the sum of the values in the 5th column. This value is printed in the End block.
3. In this example we will see how to execute the awk script written in a file. Create a file sum_column and paste the below script in that file
1
2
3
4
#!/usr/bin/awk -f
BEGIN{sum=0}
{sum=sum+$5}
END{print sum}
Now execute the the script using awk command as
awk -f sum_column input_file.
This will run the script in sum_column file and displays the sum of the 5th column in the input_file.
4. awk ‘{ if($9 == “t4”) print $0;}’ input_file
This awk command checks for the string “t4” in the 9th column and if it finds a match then it will print the entire line. The output of this awk command is
This will print the squares of first numbers from 1 to 5. The output of the command is
1
2
3
4
5
square of1is1
square of2is4
square of3is9
square of4is16
square of5is25
Notice that the syntax of “if†and “for†are similar to the C language.
Awk Built in Variables:
You have already seen $0, $1, $2… which prints the entire line, first column, second column… respectively. Now we will see other built in variables with examples.
FS – Input field separator variable:
So far, we have seen the fields separted by a space character. By default Awk assumes that fields in a file are separted by space characters. If the fields in the file are separted by any other character, we can use the FS variable to tell about the delimiter.
By default whenever we printed the fields using the print statement the fields are displayed with space character as delimiter. For example
7. awk ‘{print $4,$5}’ input_file
The output of this command will be
1
2
3
4
5
6
center0
center17
center26
center25
center43
center48
We can change this default behavior using the OFS variable as
awk ‘BEGIN {OFS=”:”} {print $4,$5}’ input_file
1
2
3
4
5
6
center:0
center:17
center:26
center:25
center:43
center:48
Note: print $4,$5 and print $4$5 will not work the same way. The first one displays the output with space as delimiter. The second one displays the output without any delimiter.
NF – Number of fileds variable:
The NF can be used to know the number of fields in line
8. awk ‘{print NF}’ input_file
This will display the number of columns in each row.
NR – number of records variable:
The NR can be used to know the line number or count of lines in a file.
9. awk ‘{print NR}’ input_file
This will display the line numbers from 1.
10. awk ‘END {print NR}’ input_file
This will display the total number of lines in the file.
String functions in Awk:
Some of the string functions in awk are:
The awk split function splits a string into an array using the delimiter.
The syntax of split function is
split(string, array, delimiter)
Now we will see how to filter the lines using the split function with an example.
The input “file.txt” contains the data in the following format
1
2
3
4
5
1U,N,UNIX,000
2N,P,SHELL,111
3I,M,UNIX,222
4X,Y,BASH,333
5P,R,SCRIPT,444
Required output: Now we have to print only the lines in which whose 2nd field has the string “UNIX” as the 3rd field( The 2nd filed in the line is separated by comma delimiter ).
The ouptut is:
There’s a bunch more you can do with systemd. You can plot out your boot up times, can mask off services so they don’t startup, look at kernel times etc.