__________________________________________________
** Before we start the lab, please Install and Launch Linux. **
__________________________________________________
Using grep to find patterns
** Command "grep" is often used to search for patterns within the text because it is convenient.
Following is an example on using "grep" :
1. Run command "cd /tmp"
- this command bring us to the folder tmp.
2. Run command "dmesg > dmesg1.txt"
- this command will create a file so that we can search for some information about the system after this.
3. Run command "grep network dmesg1.txt"
- this command help us to determine the network that the system is using.4. Run command "grep -i network dmesg1.txt"
5. Run command "echo $?"
- for my case, the output will be '0' because the command in 4. is success.
6. Run command "grep -i cdrom dmesg1.txt"
- this command help us to determine which network driver the system is using.
7. Run the previous command one more time
8. Run command "echo $?"
- in my case, the output is '1' because the command is failure.9. Run command "grep -i jimlewis dmesg1.txt"
10. Run command "echo $?"
- the output is nonzero because the command is a failure.
__________________________________________________
Compressing files using ZIP and TAR
**ZIP and TAR can do file packaging and compression.
1. Run command "cd /tmp"
- go to tmp.
2. Run command "mkdir lbooktemp"
- make a folder named lbooktemp.
4. Run command "ls > f1.txt; route > f2.txt; dmesg > f3.txt"
- create some files named "f1.txt", "f2.txt", and "f3.txt".
6. Run command "zip lbook1.zip f1.txtt"
- f1.txt f2.txt f3.txt will be zipped into file named lbook1.zip.
7. Run command "zip lbook1.zip *txt"
8. Run command "zip lbook1.zip *txt *dat"
- to include the other files into the zip file
9. Run command "mkdir test"
10. Run command "cp lbook1.zip.test"
11. Run command "cd test"
12. Run command "unzip lbook1.zip" (You can also use command unzip -l zipped-file to view the contents of ZIP file without extracting anything.)
13. Run command "ls -la" to see the list of files.
__________________________________________________
** Similar to ZIP, but TAR is a better alternative. **
1. Run command "cd /tmp/lbooktemp" to go back to the same files as before.
2. Run command "tar cvzf lbook1.tar.gz *.txt"
- this will create a gzip compressed archieve file.
3. Run command "file 1lbook1.tar.gz"
4. Run command "cp lbook1.tar.gz test"
- copy the file to test directory before we extract it.
5. Run command "cd test"
- You can see the gzip file are inside test folder.
6. Run command "tar xvzf lbook1.tar.gz" to check the files inside the gzip folder.
7. Run command "ls -la". It will show the same files as previous command.
8. Run command "tar tvzf lbook1.tar.gz" to view a tar archive.
9. Run command "cd /tmp" before we TAR the whole directory.
10. Run command "tar cvzf lbooktemp1.tar.gz lbooktemp"
- this will show the entire directory inluding the hidden files (if any).
__________________________________________________
** ZIP and TAR are usually used in system administration to back up everything. **
** To do back up > TAR a system > Copy the file to another machine > unTAR the file > Done! **
__________________________________________________
Other Helpful Commands such as stat, sum, touch, and so on
1. Run command "cd /tmp"
2. Run command "dmesg > file1.txt" to create a file named 'file1.txt'.
3. Run command "ls -la".
- Please remember the info. We will use this later.
4. Run command "stat file1.txt" - the info of file1.txt will be shown.
5. Run command "sum file1.txt"
- this is use to check the condition of file.
__________________________________________________
- Suppose you sent a file to someone that is running a Linux system. To ensure the file does not get corrupted along the way, you can run the command "sum".
- The first number is the checksum and the second number is the number of blocks for that file.
- If the other person runs command "sum" and has the same info, the files are the same.
** Note that the file names do not have to be the same. **
__________________________________________________
6. Run command "touch file2.txt"
- "touch" command can be used to create files.
7. Run command "file file2.txt" to prove that file2.txt is empty.
7. Run command "ls -la file1.txt"
8. Run command "touch file1.txt"
9. Run command "ls -la file1.txt" again.
** Notice that after "touch" command, the date and time change to current date and time on that file. **
__________________________________________________
** Suppose that you want to just view a text file. Run this command below. **
10. Run command "less file1.txt"
- You will see output as below :
** When using "less command", press Spacebar to scroll down, press Q to exit. **
** Suppose that you want to see just the first few lines in that file. Run the command below. **
11. Run command "head file1.txt"
- You will see output as below :** The "head" command shows the first 10 lines by default. **
__________________________________________________
**Suppose that you want to see just the last few lines in that file. Run the command below. **
12. Run command "tail file1.txt"
** The "head" command shows the first 10 lines by default. **
__________________________________________________
There are still many more commands that deal with files. If you are interested, you may search more for yourself.
__________________________________________________
Thank you for reading. Feel free to ask me if you have any questions.
__________________________________________________
Comments
Post a Comment