Module 3: Linux Commands in the Bash Shell

Spread the love

Get a read on your analytical skills

This course will allow participants to practically implement Linux commands in the course of this extensive module using the Bash shell. It introduces basic Linux commands and provides experience operating with the file system to learn basic Linux Bash shell tasks to accomplish user authentication and authorization for a very practical user management experience in Linux environments.

Thus, a central area to be explored in the module is the function of the Bash shell in the manipulation process in a file system, so that members can at best manage and order data within Linux. It will also cover introducing participants to essential prophecies about seeking help when confronted with new and maybe difficult-to-comprehend Linux commands, thus creating an interactive environment in which students learn. At the end of this module, students will have learned practical applications of executing Linux commands through the Bash shell that will improve their confidence in using Linux file systems. This kind of hands-on approach strengthens performance by using command lines and prepares students to use Linux effectively in the real world.

Learning Outcomes:

  • Traverse a complete file system using Linux commands via the Bash shell.
  • Manage file systems entirely using Linux commands via the Bash shell.
  • Explain how files permissions are managed in Linux.
  • Authenticate and authorize users through Linux commands via the Bash shell.
  • Use sudo for root user permissions.
  • Access resources that support using Linux commands.

TEST YOUR KNOWLEDGE: NAVIGATE THE LINUX FILE SYSTEM IN BASH

1. What is a command?

  • The highest-level directory in Linux
  • A common shell in many Linux distributions
  • An instruction that tells a computer to do something (CORRECT)
  • A component of the Linux architecture

A command means a string of written instruction enabling a computer to perform the action specified therein. A command usually finds its way into a computer by user interfacing through the command-line interface (CLI) or shell. The computer takes the command and acts upon it.

2. Which of the following commands prints the working directory to the screen?

  • pwd (CORRECT)
  • head
  • ls
  • cat

Its meaning is to print working directory. It is used to find out what current directory the user is in. When executed, it just shows on the screen the absolute path of the working directory.

3. What does the cd command do?

  • Prints the working directory to the screen (CORRECT)
  • Outputs a specified string of text
  • Navigates between directories
  • Displays the names of files in the current directory

The cd command navigates between directories.

4. A security professional enters head access.txt into a shell. What are they telling the operating system to do?

  • Display the first 10 lines of access.txt (CORRECT)
  • Remove the first 5 lines of access.txt
  • Add a header to the file named access.txt
  • Return the content of access.txt one page a time

The head command is nothing but a command that prints the first few lines of a file. The default configuration is the first 10 lines of the specified file; for example, giving the command head access.txt will give you the head of the access.txt file, or the first 10 lines of it.

5. What is the difference between an absolute file path and a relative file path?

  • An absolute file path starts from the current directory, and a relative file path starts from the root.
  • An absolute file path ends with a forward slash (/), and a relative file path ends with a backslash (\).
  • An absolute file path ends with a backslash (\), and a relative file path ends with a forward slash (/).
  • An absolute file path starts from the root, and a relative file path starts from the current directory. (CORRECT)

A relative file path, in most cases, is defined as the path that does not lead to the root, but works from the current directory. It essentially determines the location of a file depending on where the user currently finds himself in the file system. Thus, if you are in the directory of /home/user/, documents/file.txt would be a relative path.

6. What is a Linux command?

  • An instruction telling the computer to do something (CORRECT)
  • The default shell in most Linux distributions
  • The information that is output from the shell
  • A dollar sign ($) before the cursor

The command in Linux is a command being given to a computer for performing any action or task: to execute it, that is, the command itself, in a terminal or shell. The system will process it accordingly. Commands in Linux can be very versatile, even simple commands dealing with files or advanced system management.

7. What does the ls command do?

  • It displays just the beginning of a file, by default 10 lines.
  • It displays the names of files and directories in the current working directory. (CORRECT)
  • It prints the working directory to the screen.
  • It displays the content of a file.

The command ls can be run to display the data such as the names of files or directories present in the working current directory. Thus, the output is the display of the content that allows a quick glance at the files and subdirectories present.

TEST YOUR KNOWLEDGE: MANAGE FILE CONTENT IN BASH

1. What two arguments commonly follow the grep command?

  • The file to write to and the string to add to it
  • The string to search for and the file to search through (CORRECT)
  • The file to move and the new file location
  • The file name to search for and the directory to search through

Such data seek commands as grep are widely used to find a specific string in or match it with patterns within files. Thus, the specific string/pattern followed by the file or files would be the arguments of this command. For instance, grep “error” log.txt would search for the string “error” in the log.txt file and display all lines containing it.

2. In Linux, what does the piping command (|) do?

  • It searches a specified file and returns all lines in the file containing a specified string.
  • It moves a file or directory to a new location.
  • It sends the standard input of one command as standard output to another command for further processing. (CORRECT)
  • It sends the standard output of one command as standard input to another command for further processing.

This command chaining can be achieved through the use of a piping command which allows the standard output of one command to become the standard input of another command. This would enable the task to be more complex by linking different commands together. The output of one command could then be processed by the next without the need for intermediate files. For example, ls | grep “file” displays all files present in the current directory and filters them so as to list only those that contain “file” in their names.

3. A security professional enters cp vulnerabilities.txt /home/analyst/projects into the command line. What do they want the operating system to do?

  • Remove the vulnerabilities.txt file from the projects directory
  • Copy the vulnerabilities.txt file into the projects directory (CORRECT)
  • Create a new file named vulnerabilities.txt in the projects directory
  • Search for the string vulnerabilities.txt in the projects directory

This command allows transfer of vulnerabilities.txt file to the projects directory but keeps the original file intact. Copy the file vulnerabilities.txt into the projects folder, leaving the source file in place.

4. What command creates a new file called failed_logins.txt?

  • find failed_logins.txt
  • rm failed_logins.txt
  • touch failed_logins.txt (CORRECT)
  • mkdir failed_logins.txt

Absolutely true! The touch command is used in Unix-based systems (like Linux and macOS) to create a new, empty file if it doesn’t already exist. In this case, touch failed_logins.txt will create an empty file called failed_logins.txt in the current directory.

5. What command can you enter to search the log.txt file for all lines containing the string error?

  • search error log.txt
  • grep log.txt error
  • search log.txt error
  • grep error log.txt (CORRECT)

This command will search the log.txt file and show lines containing the word “error.”

6. Which of the following commands can you use to create a new file?

  • mkdir
  • mv
  • touch (CORRECT)
  • rmdir

You can use the touch command to create a new file.

TEST YOUR KNOWLEDGE: AUTHENTICATE AND AUTHORIZE USERS

1. What is authorization?

  • The process of a user proving that they are who they say they are in the system
  • The process of temporarily granting elevated permissions to specific users
  • The concept of granting access to specific resources in a system (CORRECT)
  • The concept of granting only the minimal access and authorization required to complete a task or function

Login is the process whereby access is granted or denied to specific resources or actions in a certain system per the permissions assigned to a user, role, or entity. Authentication usually happens prior to this since an identification of the user using the system usually follows.

2. Which of the following statements correctly describe the file permissions string -rw-rw-rw-? Select two answers.

  • The user has write permissions. (CORRECT)
  • The group has read permissions. (CORRECT)
  • The file type is a directory.
  • The user and group have execute permissions.

The third character in a UNIX file access permission model – rw – rw – rw – indicates write permission for the group. The fifth character is an indicator of whether others have read permissions.

3. A security professional enters chmod g+w access.txt into the command line. What does this command tell the operating system to do?

  • Remove write permissions from the user for the access.txt file
  • Add write permissions to the group for the access.txt file (CORRECT)
  • Remove write permissions from the group for the access.txt file
  • Add write permissions to the user for the access.txt file

The command chmod g+w access.txt means to add write permission to the group for the file access.txt.

4. Which of the following commands typically must be used with sudo? Select three answers.

  • userdel (CORRECT)
  • chmod
  • chown (CORRECT)
  • useradd (CORRECT)

Normally, the commands useradd, userdel, and chown need to run with sudo privileges as these commands require administrative permission.

5. A security analyst is updating permissions on a directory named projects. The current permissions are drwxrw-r–. They want to add execute permissions for the group. What do they enter on the command line?

  • chmod g-x projects
  • chmod u-x projects
  • chmod g+x projects (CORRECT)
  • chmod x+x projects

The command chmod g+x projects gives execute permission for the group on the directory or file called projects.

6. When working with a directory, what do write permissions allow users to do?

  • Change the directory permissions
  • Create new files in that directory (CORRECT)
  • Read files in that directory
  • Enter into a directory and access its files

Permission to write with a directory enables users to create, delete, and rename filed entries in the directory. This permission allows for modification of the content of the directory, that is, for inserting files there.

7. If you want to change the permissions on an approved_users.txt file, which command can you use?

  • ls -la
  • head
  • chmod (CORRECT)
  • ls -l

Permission to write with a directory enables users to create, delete, and rename filed entries in the directory. This permission allows for modification of the content of the directory, that is, for inserting files there.

8. What is the purpose of the sudo command?

  • It changes permissions on files and directories.
  • It deletes a user from the system.
  • It adds a user to the system.
  • It temporarily grants elevated permissions to specific users. (CORRECT)

Sudo is designed to temporarily provide specific user access rights to allow them to execute some command with elevated (administrative) privileges. For example, commands such as useradd (to add a user) and userdel (to delete a user) typically require elevated permissions because they alter the user management or system configuration aspects.

TEST YOUR KNOWLEDGE: GET HELP IN LINUX

1. Which of the following statements accurately describe Linux’s online global community? Select three answers.

  • Linux users can find support from the community for everyday tasks. (CORRECT)
  • Because Linux is open-source, the community can easily contribute. (CORRECT)
  • The community publishes online information to help users learn how to operate Linux. (CORRECT)
  • The community is focused on collecting feedback from advanced users of Linux.

Offering a rich treasure in the world community of online Linux users, the place also provides some resources and assistance for all that a user might need; it is great to know that there is also much information regularly published online: users can learn from various forms such as forums, documentation, and tutorials, but would know how to operate Linux themselves.

2. What does the man command do?

  • Display information on other commands and how they work (CORRECT)
  • Delete a user from the system
  • Temporarily grants elevated permissions to specific users
  • Display a description of a command on a single line

Running the man command will show you all the descriptions and usages of other commands. This allows you to find additional information about the command you would like to learn more about just by typing man followed by the command name.

3. What does the whatis command do?

  • Temporarily grants elevated permissions to specific users
  • Return the username of the current user
  • Display information on other commands and how they work
  • Display a description of a command on a single line (CORRECT)

whais is a very useful command because it simply provides a one-liner description of the command for use when you just want a quick overview of something and don’t care to see all the details in a complete man page.

4. What is an advantage of the apropos command?

  • It incorporates mandatory options for customized searching
  • Users can search for a command even if they do not know the specific command name. (CORRECT)
  • It condenses the description of a specific command to one line.
  • It can be used to search for descriptions of commands when you know the specific command name.

With the apropos command, users can search for a command without knowing its exact name. It queries the descriptions of manual pages for a string. There is one option, -a, which apropos offers, but it isn’t necessary for basic usage.

5. What has happened because Linux is an open-source operating system?

  • All Linux distributions are updated each year.
  • All Linux distributions are available for free.
  • A global community of Linux users has formed. (CORRECT)
  • Linux users work more independently from one another.

Being an open-source OS, Linux has developed a communities around the globe of users, providing valuable solutions of day-to-day tasks that Linux users can face and support to themselves.

6. How can you use the man command?

  • To display information on what other commands are and how they work (CORRECT)
  • To temporarily grant elevated permissions to specific users
  • To search the manual page descriptions for a specified string
  • To display a description of a command on a single line

The man command will display detailed information about a command concerning its various functionalities. For example, to know about the chown command and how it works, you can type man chown.

MODULE 3 CHALLENGE

1. What are the arguments in grep bmoreno Q4users.txt? Select two answers.

  • Q4users.txt (CORRECT)
  • grep
  • bmoreno (CORRECT)
  • .txt

2. What is the root directory?

  • The highest-level directory in Linux (CORRECT)
  • The command to navigate between directories
  • The home directory of a specific user with no privileges
  • A subdirectory of the home directory

3. A security analyst enters grep OS updates.txt into the command line. What does this tell the operating system to do?

  • Move the updates.txt file to the OS directory
  • Create a new file named updates.txt in the OS directory
  • Search through the updates.txt file and return all lines containing the string OS (CORRECT)
  • Create a new directory named OS and a new file named updates.txt

4. A security analyst enters touch updates.txt into the command line. What does this tell the operating system to do?

  • Open the updates.txt file
  • Create a new file named updates.txt in their current directory (CORRECT)
  • Move the updates.txt file out of their current directory
  • Create a new file named updates.txt and move it to the root directory

5. What are the three types of permissions?

  • Root, user, and group
  • User, group, and other
  • Read, write, and execute (CORRECT)
  • Read, write, and save

6. A security analyst enters chmod u+w,g-r access.txt into the command line. What does this command do?

  • It adds write permissions for the group and removes read permissions for the user on the access.txt file.
  • It adds write and read permissions for the user and group on the access.txt file.
  • It removes write and read permissions for the user and group on the access.txt file.
  • It adds write permissions for the user and removes read permissions for the group on the access.txt file. (CORRECT)

7. Which of the following commands require the user to be a root user or have sudo privileges? Select two answers.

  • cd
  • useradd (CORRECT)
  • grep
  • userdel (CORRECT)

8. Given the following permissions drw-rw-r–, what permissions does the group have? Select all that apply.

  • Write (CORRECT)
  • Use
  • Read (CORRECT)
  • Execute

9. What are the arguments in cp vulnerabilities.txt /home/analyst/projects? Select two answers.

  • vulnerabilities.txt (CORRECT)
  • cp
  • /home
  • /home/analyst/projects (CORRECT)

10. Which of the following items represents the root directory?

  • *home
  • *
  • /home
  • (CORRECT)

11. Which command searches a specified file and returns all lines in the file containing a specified string?

  • sudo
  • pwd
  • grep (CORRECT)
  • mkdir

12. Which of the following are types of permissions? Select all that apply.

  • Write (CORRECT)
  • Execute (CORRECT)
  • Read (CORRECT)
  • Authorize

13. A security analyst enters chmod u+w,g-r access.txt into the command line. What does this command tell the operating system to do? Select all that apply.

  • Remove read permissions from the user for the access.txt file
  • Add write permissions to the user for the access.txt file (CORRECT)
  • Add write permissions to the group for the access.txt file
  • Remove read permissions from the group for the access.txt file (CORRECT)

14. What does sudo do?

  • Changes the owner associated with a particular file
  • Temporarily grants elevated permissions to specific users (CORRECT)
  • Adds users to the system
  • Deletes users from the system

15. Which of the following commands searches the manual page descriptions for a specified string?

  • cp
  • apropos (CORRECT)
  • man
  • pwd

16. Given the following permissions drw-rw-r–, what does the fourth character represent?

  • The group does not have execute permissions for this directory
  • The user has execute permissions for this directory
  • The group has execute permissions for this directory
  • The user does not have execute permissions for this directory (CORRECT)

17. What are the arguments in mv Q1users.txt /home/analyst/reports? Select two answers.

  • mv
  • Q1users.txt (CORRECT)
  • /home/analyst/reports (CORRECT)
  • .txt

18. Fill in the blank: The highest-level directory in Linux is called the _____.

  • sudo
  • permissions
  • root directory (CORRECT)
  • home directory

19. A security analyst is updating permissions on the file access.txt. They want to add write permissions for the user and remove read permissions for the group.  What do they enter on the command line?

  • chmod u+w,g-r access.txt (CORRECT)
  • chmod u+rw,g-rw access.txt
  • chmod access.txt u+w,g-r
  • chmod u-w,g+r access.txt

20. Which command can you use to change your current directory?

  • ls
  • cat
  • pwd
  • cd (CORRECT)

21. What does the touch command do?

  • Moves a file or directory to a new location
  • Creates a new file (CORRECT)
  • Opens a file editor
  • Changes permissions on files and directories

22. What are read, write, and execute?

  • Specific Linux commands used to change file permissions
  • Different methods for editing files
  • The three types of permissions for authorized users (CORRECT)
  • The three types of owners for files and directories

23. A user is not a root user, but needs elevated privileges to use certain commands. What should they do?

  • Use the sudo command (CORRECT)
  • Use the chmod command
  • Assign themselves write permissions
  • Assign themselves execute permissions

24. A security analyst enters apropos password into the command line. What does this tell the operating system to do?

  • Output all the commands that contain the word “password” in their man page descriptions (CORRECT)
  • Display detailed information on the password command and how it works
  • Display a description of the password command on a single line
  • Change your current directory to /password

25. Given the following permissions drw-rw-r–, which character indicates if this is a file or directory?

  • Fifth
  • Second
  • Tenth
  • First (CORRECT)

26. What should you specify in the argument following the cd command?

  • The string you want to search for
  • Your current directory
  • The file you want to create
  • The directory you want to navigate to (CORRECT)

27. Which of these commands creates a new file?

  • chmod
  • touch (CORRECT)
  • cd
  • mkdir

CONCLUSION – Linux Commands in the Bash Shell

This module will look deeply into the Linux commands that can be used through the Bash shell with regard to their usefulness in the Linux operating system. In a hands-on and experiential learning way with the scope of application, users will eventually know how to use the file system and manage permissions and users. Last but not least, it will be taught how to look for and use available resources to counter the unknown Linux command so that it puts the final shape into equipping the participants with tools and strategies for lifelong learning.

This will greatly ensure the development of a practical understanding of operations in a command line and therefore enable one to use the Bash shell effectively and confidently. This would be the foundation upon which all other applications of the skill would be built to make it relevant in the real world. The wholly experiential way of learning would lead to improved self-confidence in using Linux commands and greater proficiency in working in a Linux environment. Trainees have the theoretical understanding that is complemented with hands-on learning experience towards a solid foundation for the use of Linux commands in the fields of cybersecurity and other relevant areas.

Leave a Comment