Introduction To Oracle Linux - Shell and Command Line

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Help Center Introduction to Oracle Linux: Shell and Comman… Search

Learn

Introduction to Oracle Linux: Shell and Command Line


Duration Level Audience Products and Services Technologies Released
1 hour 30 minutes Beginner DevOps Engineer, Developer, IT Administrator Oracle Linux Oracle Linux, Administration, Introduction Mar 27, 2023

    
Note:
This tutorial is available in an Oracle-provided free lab environment. Introduction to Oracle Linux: Shell and
Command Line
It uses example values for Oracle Cloud Infrastructure credentials, tenancy,
and compartments. When completing your lab, substitute these values
Introduction
with ones specific to your cloud environment.

Objectives
Introduction
What Do You Need?
This lab explores introductory tasks for using Oracle Linux.

Set Up Lab Environment


Objectives
In this lab, you’ll: Execute Commands Using a Shell and
Command Line
Execute commands using a shell and the command line.
Work with Files and Directories
Work with files and directories.

Edit files with vim. Overview


Learn about file permissions.
Edit Files with the Vim Text Editor
Monitor system processes.

What Do You Need? Overview

A system with Oracle Linux installed Learn About File Permissions

Set Up Lab Environment Overview

Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and Monitor System Processes
other usage instructions.
Overview

Information: The free lab deploys a running Oracle Linux system.


For More Information

1. If not already connected, open a terminal and connect via ssh to the ol-server system. More Learning Resources

ssh oracle@<ip_address_of_ol-server>  Copy

Execute Commands Using a Shell and Command Line


Overview
In this section, you will use shell metacharacters to simplify commands, structure, and output. Bash
(/bin/bash) is the default shell. You will view and set values to manage command-line history.

1. Verify that the default shell, /bin/bash, is running, and use the echo command on Oracle
Linux to display the contents of the SHELL variable.

echo $SHELL  Copy

2. Make certain you’re in the user’s home directory using the tilde (~) metacharacter with the
cd command.

cd ~  Copy

3. Ensure the current number of command lines maintained by the history command.

echo $HISTFILESIZE $HISTSIZE  Copy

4. Set HISTSIZE to 20 using the following command.

HISTSIZE=20  Copy

5. Confirm that the command-line history for an open terminal window size is set to 20.

echo $HISTSIZE  Copy

6. View the page-wise output of the history command.

history | less  Copy

7. Press the q key to quit the previous command.

8. View the preceding 10 commands from the history database.

history 10  Copy

9. Use the –c option to clear previous history.

history -c  Copy

10. View the cleared history using the history command again.

history  Copy

Work with Files and Directories


Overview
In this section, you will use file and directory access commands.

1. Display the present working directory using the pwd command.

pwd  Copy

2. Display a long list of all the contents of the current working directory running ls command.

ls -la  Copy

3. Display the file types in your current directory using the ls command.

ls -Fa  Copy

4. If a temp directory doesn’t already exist, create a directory called temp using the mkdir
command.

mkdir temp  Copy

5. Change to the temp directory using the cd command.

cd temp  Copy

6. Display the present working directory using the pwd command.

pwd  Copy

7. Display the files and directories under the root directory using the ls command.

ls /  Copy

8. Return to your home directory using the cd command.

cd ~  Copy

9. Change the directory to the parent directory using the cd command.

cd ..  Copy

10. Display the present working directory using the pwd command.

pwd  Copy

11. Quickly return to your previous directory using the cd command.

cd -  Copy

12. Create a new file called myfile using the echo and redirect commands.

echo hello world > myfile  Copy

13. Verify the new myfile file was created using the ls command.

ls  Copy

14. Display the contents of the myfile file using the cat command.

cat myfile  Copy

15. Copy the myfile file to another file name using the cp command.

cp myfile myfile2  Copy

16. Display a long list of the contents of the current working directory using the ls command.

ls -l  Copy

17. Display the myfile2 file type using the file command.

file myfile2  Copy

18. Display the file contents of myfile2 using the cat command.

cat myfile2  Copy

19. Copy the myfile2 file to the temp directory using the cp command.

cp myfile2 temp/  Copy

20. Display the contents of the temp directory using the ls command.

ls -l temp/  Copy

21. Rename the temp directory using the mv command.

mv temp temp2  Copy

22. Display the file types in your current directory using the ls command.

ls -F  Copy

23. Copy all files in the temp2 directory to a new directory, temp3, using cp command.

cp -R temp2 temp3  Copy

24. Display all files recursively using the ls command.

ls -R  Copy

25. Create a new temp directory using the mkdir command.

mkdir temp  Copy

26. Create a new file called alpha using the touch command.

touch alpha  Copy

27. Display the contents of your current directory using the ls command.

ls  Copy

28. Delete the alpha file using the rm command.

rm alpha  Copy

29. Delete the temp directory using the rmdir command.

rmdir temp  Copy

30. Display the file types in your current directory using the ls command.

ls -F  Copy

31. Starting in your home directory, find all files named myfile using the find command.

find ~ -name myfile  Copy

32. Starting in your home directory, find all files named temp2 using the find command.

find ~ -name temp2  Copy

Edit Files with the Vim Text Editor


Overview
In this section, you will use the vim editor which is the default editor for Oracle Linux 8.

1. Type the vim command from your home directory.

vim  Copy

2. Press the i key to change into insert text mode and type the following text.

Hello World
What is your  Copy

Waht id today's date?`

3. Append text to the line What is your.

Press Esc to enter normal mode.

Use the h, j, k, l or arrow keys to navigate to the last character of the line.

Press the a key to append and insert a space with the next string name?.

4. Replace the d character with s in the line, Waht id today’s date?.

Press Esc to return to normal mode.

Move the cursor to the third line by pressing the j or down arrow key. This will
move the cursor down.

To move the cursor to the left, press h or the left arrow key.

Bring the cursor to the d character in the string id.


Press the r key and then insert character s. This will replace the character d with
the character s.
5. Change the word Waht to What.

Press Esc and move the cursor to the third line.

Place your cursor on the character a of the word Waht and execute the cw
command.

Enter the text hat. This will change the whole word Waht to what.

Press ESC when finished modifying the word.

6. Copy and paste the line Hello World.

Press ESC to return to command mode.

Move the cursor to the beginning of the Hello World line.

Execute the yy command to copy the string.

Execute the p command to paste the string. The whole line is copied and pasted.

7. Delete the additional Hello World line.

Press Esc to enter command mode.

Move the cursor to the beginning of the second line Hello World and execute
the dd command. The entire line is deleted.

8. To search for the string What.

Press Esc to enter command mode.

Press the forward slash / key.

Enter the text What and press Enter. The cursor automatically moves to the
first string in the file that it encounters. Notice that /What appears at the bottom
of the terminal window screen.

9. Search for the next occurrence of the same string by pressing the n key.

The cursor will move to the second string in the file.


10. Customize the session by displaying the line numbers.
Press Esc to enter command mode.

Enter the :set nu command and press Enter. Notice that :set nu appears
at the bottom of the terminal window screen.
11. Remove the line numbers.
Press Esc to enter command mode.

Type the :set nonu command and press Enter. The line numbers disappear.

12. Quit and save the file with the changes.


Press Esc to enter command mode.

Type :w intro.txt and press Enter to save the file.

Type :q to quit. The command prompt returns.

Learn About File Permissions


Overview
In this section, you will view and change file ownership. You will also view and change permissions on
files.

1. Ensure you are in your home directory using the pwd command.

pwd  Copy

2. To find the owner of the existing directory, use the ls command.

ls -ld  Copy

The owner of the existing directory is displayed in the third column of the output.

3. Identify the owner of the contents in the temp2 directory using the ls command.

ls -l temp2  Copy

4. Change the ownership of the temp2 directory to the root user.

Use sudo su command to switch to root user/role

Run the change owner chown command.

sudo su
chown root temp2  Copy

5. Confirm the ownership of the contents of the temp2 directory.

ls -l  Copy

6. Change the user and group ownership of its contents to root and root, by running the
chown command again with the recursive -R option.

chown -R root:root temp2


ls -lR temp2  Copy

7. Exit su using the exit command.

exit  Copy

8. Ensure that the umask value is set to 0022 on your system. To verify, run the umask
command.

umask  Copy

9. If the umask is not set to 0022, then set the umask value to 0022 running the umask
command.

umask 0022  Copy

10. Create a new directory called perm in the temp3 directory using the mkdir command.

mkdir temp3/perm  Copy

11. Change to the etc directory and list these four files – group, motd, shadow, fstab for
Oracle Linux using the ls command.

cd /etc
ls -l group motd shadow fstab  Copy

Note: In Oracle Linux, there are no permissions on the shadow file.

12. Copy the four files to your ~/temp3/perm directory. The shadow file will fail to copy.

cp group motd shadow fstab ~/temp3/perm  Copy

Example Output:

 Copy
cp: cannot open ’shadow’ for reading: Permission denied

13. Go to your temp3 directory and verify the contents of its ~/temp3/perm directory.

cd ~/temp3
ls -l perm  Copy

14. Change directories to your home directory using the cd command.

cd  Copy

15. Create a new directory called test and a new file called test1.

mkdir test
touch test/test1  Copy

16. Examine the default permissions of the new file test1.

ls –l test/test1  Copy

17. Check the default permissions of the new directory test.

ls –ld test  Copy

18. Using the chmod command and symbolic mode, add write (w) permission for the group
permission set to the motd file.

chmod g+w temp3/perm/motd


ls -l temp3/perm  Copy

Note: Symbolic mode uses a combination of letters and symbols to add or remove
permissions for each type of user.

19. Using octal mode, change the permissions on the motd file to -rwxrw----.

chmod 760 temp3/perm/motd


ls -l temp3/perm  Copy

Octal mode is the use of values with a base 8, in this case 0-7.

20. Using octal mode, add write (w) permission for other on the file named group.

chmod 646 temp3/perm/group


ls -l temp3/perm  Copy

21. Identify the GID and UID for the motd file using the ls command.

ls -n temp3/perm/motd  Copy

22. Create a new directory called notes using the mkdir command.

mkdir notes  Copy

23. Create a new file called memo in your dir4 directory.

touch notes/memo
ls -l notes/memo  Copy

24. Remove the read (r) permission for the owner from the memo file in the notes directory.
You can use symbolic mode to do this.

chmod u-r notes/memo


ls -l notes/memo  Copy

25. Use the cat command to view the memo file.

cat notes/memo  Copy

Note: This fails because read permission has been removed from the user. Even
though you are part of the group, the permissions are viewed in the order in which
they appear.

Monitor System Processes


Overview
In this section, you will determinea the process identifier (PID), view a processes tree, and kill processes.

1. Use the following ps command to list the processes currently running on your system.

ps  Copy

2. Use the –f option to print a full listing for the ps command.

ps -f  Copy

3. Use the –e option to print information about every process running. Then use the ps and
wc commands to show the total number of processes.

ps -e
ps -e | wc -l  Copy

4. Once again, use the ps command.

ps -f  Copy

Note: Observe the TTY column where the controlling terminal is pts/0.

5. Open a second terminal window and connect via ssh to the ol-server system.

ssh oracle@<ip_address_of_ol-server>  Copy

6. Execute the ps command in the new terminal window.

ps -f  Copy

Note: Observe the TTY column in the second terminal window, where the controlling
terminal is pts/1. This is because you now have two separate and concurrent terminal
window sessions open at the same time.

7. In your first terminal window, enter the sleep 100 command:

sleep 100  Copy

8. In the second terminal window, use the ps and grep commands to identify the PID of the
sleep process.

ps -ef | grep sleep  Copy

The PID can be found under the second column of the output.

9. From the second terminal window, use the kill command with the PID argument to
terminate the sleep process.
This example uses a PID of 29987.

Your PID may differ from the command presented.

kill 29987  Copy

Notice the sleep 100 is terminated in the first terminal window.

10. In the second terminal window, enter the tty command to identify the name of this
terminal window. The name appears as /dev/pts/, where n is a number (for example,
/dev/pts/1).

tty  Copy

11. Return to your first terminal window. Use the pgrep -t (terminal option) command to find
the PID associated with the second terminal window.

pgrep -t pts/1  Copy

12. In your first terminal window, use the kill command to terminate the ssh session and log
off the ol-server system in your second terminal window.
This example uses a PID of 29957.

Your PID may differ from the command presented.

Notice you are logged off the ol-server system in the second terminal window

kill 29957  Copy

13. Run the following kill -l (list option) commands to identify the signal names and signal
values.

kill -l 9
kill -l kill  Copy

kill -l 15
kill -l term

Note: signal value 9, the signal name is KILL, and for the signal name kill, the signal
value is 9. For signal value 15, the signal name is TERM, and for the signal name term,
the signal value is 15.

14. In the terminal window, enter the sleep command and place it in the background.

sleep 600 &  Copy

15. Use the ps command to identify the bash shell process running in that window.

ps  Copy

16. In the second terminal connect via ssh to the ol-server system.

ssh oracle@<ip_address_of_ol-server>  Copy

17. In the second terminal window, display the process tree and provide the PID of the sleep
process running in the first terminal window as an argument, using the pstree -p <PID>
command.
This example uses a PID of 1252.

Your PID may differ from the command presented.

pstree -p 1252  Copy

18. In the second terminal window, terminate the first terminal window using the kill
command with the bash shell PID.

kill -9 1252  Copy

19. In the first terminal window, enter the ps command and notice that the sleep 600
process was killed.

ps  Copy

For More Information


Oracle Linux 8 Documentation Oracle Linux 9 Documentation Oracle Linux Training Oracle Linux
Training Station

More Learning Resources


Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle
Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an
Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.

Content on Oracle Help Center Learn is published for your convenience. It is not part of the Product
Documentation for any Oracle product or service.

 Title and Copyright Information

© Oracle About Oracle Contact Us Products A-Z Terms of Use & Privacy Cookie Preferences Ad Choices

You might also like