You are here

Training Day 2 - More Linux +

Day 2 was another review of commands available in Linux. Here are most of the answers for the exercise sheet:


1. Change to the /etc directory
#cd /etc

2. View your current directory
#ls or #ls -al (shows hidden files and in long format)

3. With one command go back to your home directory
#cd ~

4. Display what directory you are in
#pwd

5. Make a directory with your name
#mkdir EvanM or mkdir /tmp/some_directory/EvanM (the -p option creates all directories leading to EvanM if they do not exist)

6. Change to the directory using the relative path
#cd /tmp/some_directory/EvanM

7. Create 5 files in your home directory
#touch file.{1..5}

8. Create a new file in your home directory and add some text
#cat > /home/EvanM/new_file.txt
this is a test
[ctrl+d to exit]

9. View the content of the newly created file
#cat new_file.txt

10. Add some more text to the end of the new file
#cat >> test
adding more text
[ctrl+d to exit]

11. Create a subdirectory in the folder that has your name
#mkdir /tmp/some_directory/EvanM/new_subdirectory

12. Copy all your created files from the folder with your name into the new subdirectory
#cp file.* ..

13. List all the files in your subdirectory from the directory with your name
#ls ..

14. List all the files including those that are hidden in your subdirectory using the long format
#ls -al

15. Delete one of the files in your sub-directory
#rm /tmp/some_directory/EvanM/new_subdirectory/file.1

16. Change to the /etc and copy the passwd file to your subdirectory
#cd /etc; cp hosts /tmp/some_directory/EvanM/new_subdirectory/

17. View the passwd file one line at a time
#more /etc/passwd

18. View only the top 10 lines of the passwd file
#head -10 /etc/passwd

19. View the last 10 lines of the passwd file
#tail -10 /etc/passwd

20. View the passwd file with the command that permits you to scroll up or down
#less passwd

21. Execute the command to find the absolute path for the passwd file
#locate passwd

22. Copy your subdirectory and all its files to /etc
#cp -R /tmp/some_directory/EvanM/new_subdirectory /etc

23. Execute the command that will search a file for text and output the line containing it
#grep test //tmp/some_directory/EvanM/new_file.txt

24. Find the dmesg file using a single command
#find / -name "dmesg"

25. Copy the dmesg file found previously to your subdirectory and than move it to the tmp folder under a new name.
#cp /etc/dmesg /tmp; mv /tmp/dmesg /tmp/new_dmesg

26. Using a single command, delete the folder with your name and all of its subdirectories as well as files.
#rm -rf /tmp/some_directory/EvanM

A. Using VI, insert text at the end of the new dmesg file than save and exit
#vi /tmp/new_dmesg
[PRESS] i
[TYPE] this is some more text
[PRESS] ESC
[TYPE] :wq!

B. Copy a line and than past it using VI
yy
p

C. Delete the line that was copied using VI in a single command
dd

D. Undo the last delete
u

E. Using VI, use the command to delete a single word beginning with the first character under the cursor
#dw

F. Display all the line numbers in VI
#set number (set nonumber to remove)

G. Delete 5 consecutive lines in VI
#5dd

I. Create a soft symbolic link to a file
#ln -s file1 file2

II. View the permissions, date, ownership and pointers
#ls -al file*

III. Create a hard symbolic link to another file
#ln file1 file3

IV. Change the permissions of a file to match the following: (u=rwx, o=rx)
#touch file
#chmod 703 file

V. Note the differences between a hard and a soft symbolic link
INODES and Permissions are the same with hard links.
INODES\Permissions are different with soft links and a new permission bit "l" is displayed.

VI. Create a new user
#adduser EvanM

VII. Change file ownership of file to the newly created account
#chown EvanM:admin file

VIII. What is the effect of deleting the original file used in a symbolic link?
The link is broken and should be displayed in red.

1. Search for your name in the /etc/passwd file
#cat /etc/passwd | grep -i evan

2. Use grep to view the message file and search for error messages. Pipe the output and use another command in the same line to refine the search for something more specific.
#cat /var/log/messages | grep "kernel" | grep "eth0"

3. Sort the password file alphabetically and send the output to a new file
#cat /etc/passwd | sort > sorted_passwd_file

4. How many words exist in the sorted file
#wc -w sorted_passwd_file

5. Check which process is using the most % of the CPU
#ps aux

6. View the uptime of the system
#uptime

Uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.

7. Find the model of the cpu
#cat /proc/cpuinfo or #dmesg or #lspci

8. Find the amount of disk space being used
#df -h

9. View the last 20 lines of the boot up file
#dmesg | tail -20

10. Run tcpdump and ping as different jobs and in the background
#tcpdump -nn -i eth0 &
#ping google.ca &

11. Switch between the jobs
#jobs -l
#fg [job id]

12. kill the active tcpdump process only
#killall tcpdump

13. View the performace of the box in the last 5 minutes
#top

14. View the cronjobs are running
#crontab -e