Introduction
In this lab, you will use the Linux command line to manage files and folders, as well as perform some basic administrative tasks. Part 1: Shell Basics
Part 2: Copying, Deleting, and Moving Files
Instructions Part 1: Shell Basics
The shell is the term used to refer to the command interpreter in Linux. Also known as Terminal, Command Line and Command Prompt, the shell is very powerful way to interact with a Linux computer.
Step 1: Access the Command Line
Step 2: Display Manual Pages from the command line.
You can display command line help using the man command. A man page, short for manual page, is a builtin documentation of the Linux commands. A man page provides detailed information about a given command and all its available options.
[analyst@secOps ~]$ man man
Question:
Name a few sections that are included in a man page. Name, Synopsis, Configuration
[analyst@secOps ~]$ man cp
Question:
What is the function of the cp command? Copy files from one location to another location in the local filesystem.
What command would you use to find out more information about the pwd command? What is the function of the pwd command? The man pwd command is used to access the man page about pwd. The pwd command prints the name of the current or working directory.
Step 3: Create and change directories.
In this step, you will use the change directory (cd), make directory (mkdir), and list directory (ls) commands. Note: A directory is another word for folder. The terms directory and folder are used interchangeably throughout this lab.
[analyst@secOps ~]$ pwd
/home/analyst
Question:
What is the current directory? /home/analyst answers
[analyst@secOps ~]$ cd /home/analyst
[analyst@secOps ~]$ ls -l total 20
drwxr-xr-x 2 analyst analyst 4096 Mar 22 2018 Desktop drwxr-xr-x 3 analyst analyst 4096 Apr 2 14:44 Downloads drwxr-xr-x 9 analyst analyst 4096 Jul 19 2018 lab.support.files drwxr-xr-x 2 analyst analyst 4096 Mar 21 2018 second_drive
[analyst@secOps ~]$ mkdir cyops_folder1 [analyst@secOps ~]$ mkdir cyops_folder2 [analyst@secOps ~]$ mkdir cyops_folder3
[analyst@secOps ~]$
[analyst@secOps ~]$ ls -l total 32
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:01 cyops_folder1 drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:02 cyops_folder2 drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:02 cyops_folder3 drwxr-xr-x 2 analyst analyst 4096 Sep 26 2014 Desktop drwx—— 3 analyst analyst 4096 Jul 14 11:28 Downloads drwxr-xr-x 8 analyst analyst 4096 Jul 25 16:27 lab.support.files drwxr-xr-x 2 analyst analyst 4096 Mar 3 15:56 second_drive
[analyst@secOps ~]$ cd /home/analyst/cyops_folder3
[analyst@secOps cyops_folder3]$
Question:
Which folder are you in now? /home/analyst/cyops_folder3
Note: In the [analyst@secOps ~]$ prompt above: The tilde symbol ~ represents the current user’s home directory. In this example, the current user’s home directory is /home/analyst. After the cd /home/analyst/cyops_folder3 command, the current user’s home directory is now /home/analyst/cyops_folder3.
Note: $ (dollar sign) indicates regular user privilege. If a ‘#’ (hashtag or pound sign) is displayed at the prompt, it indicates elevated privilege (root user).
Note: While these symbols, conventions and main concepts remain the same, the prompt of a terminal window is highly customizable in Linux. Therefore, the prompt structure seen in the CyberOps Workstation VM will likely differ from the prompt in other Linux installations.
Challenge: Type the command cd ~ and describe what happens.
Question:
Why did this happen? The directory is changed to the home directory. Because the shell interprets the ~ as a shortcut for the current user’s home directory, cd ~ changes to the current user’s home.
[analyst@secOps ~]$ mkdir /home/analyst/cyops_folder3/cyops_folder4
[analyst@secOps ~]$
analyst@secOps ~]$ ls –l /home/analyst/cyops_folder3 total 4
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:04 cyops_folder4
analyst@secOps ~]$ ls –la /home/analyst/cyops_folder3 total 12 drwxr-xr-x 3 analyst analyst 4096 Aug 16 15:04 . drwxr-xr-x 20 analyst analyst 4096 Aug 16 15:02 ..
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:04 cyops_folder4
The -a option tells ls to show all files. Notice the . and .. listings shown by ls. These listings are used by the operating system to track the current directory (.) and the parent directory (..) You can see the use of the . and .. when using the cd command to change directories. Using the cd command to change the directory to the . directory incurs no visible directory change as the . points to the current directory itself.
[analyst@secOps ~]$ cd /home/analyst/cyops_folder3
[analyst@secOps cyops_folder3]$
[analyst@secOps cyops_folder3]$ cd .
[analyst@secOps cyops_folder3]$
Question:
What happens? Apparently nothing but the command interpreter has changed the directory to the current directory itself.
[analyst@secOps cyops_folder3]$ cd ..
[analyst@secOps ~]$
Question:
What happens? The directory was changed to /home/analyst, which is the directory immediately above cyops_folder3, also known as parent directory.
What would be the current directory if you issued the cd .. command at [analyst@secOps ~]$? /home
What would be the current directory if you issued the cd .. command at [analyst@secOps home]$? /
What would be the current directory if you issued the cd .. command at [analyst@secOps /]$? /here.
Step 4: Redirect Outputs.
Another powerful command line operator in Linux is known as redirect. Represented by the > symbol, this operator allows the output of a command to be redirected to some location other the current terminal window (the default).
[analyst@secOps /]$ cd /home/analyst/
[analyst@secOps ~]$
analyst@secOps ~]$ echo This is a message echoed to the terminal by echo.
This is a message echoed to the terminal by echo.
analyst@secOps ~]$ echo This is a message echoed to the terminal by echo. > some_text_file.txt No output was shown.
Question:
Is that expected? Explain. Yes. The output was redirected to the some_text_file.txt file.
Notice, that even though the some_text_file.txt file did not exist, prior to the echo command, it was automatically created to receive the output generated by echo. Use the ls -l command to verify if the file was really created:
[analyst@secOps ~]$ ls –l some_text_file.txt
-rw-r–r– 1 analyst analyst 50 Feb 24 16:11 some_text_file.txt
[analyst@secOps ~]$ cat some_text_file.txt
This is a message echoed to the terminal by echo.
analyst@secOps ~]$ echo This is a DIFFERENT message, once again echoed to the terminal by echo. > some_text_file.txt
[analyst@secOps ~]$ cat some_text_file.txt
This is a DIFFERENT message, once again echoed to the terminal by echo.
Question:
What happened to the text file?
The text file was completely replaced by the new message. The > operator destroyed the contents of the txt file before writing the message echoed by echo.
Step 5: Redirect and Append to a Text File.
[analyst@secOps ~]$ echo This is another line of text. It will be APPENDED to the output file. >> some_text_file.txt
[analyst@secOps ~]$ cat some_text_file.txt
This is a DIFFERENT message, once again echoed to the terminal by echo.
This is another line of text. It will be APPENDED to the output file.
Question:
What happened to the text file? Explain The new message was appended to the end of the file, keeping the original contents intact.ur answers here.
Step 6: Work with hidden files in Linux.
Note: Do not confuse dot-files with the current directory indicator “.” symbol. Hidden file names begin with a dot (period), followed by more characters while the dot directory is a hidden directory comprised of only a single dot.
[analyst@secOps ~]$ ls –l
Question:
How many files are displayed? 5
[analyst@secOps ~]$ ls –la
Questions:
How many more files are displayed than before? Explain
Many more as ls -la displays, in addition to regular files, all the hidden files in folder..
Is it possible to hide entire directories by adding a dot before its name as well?
Are there any directories in the output of ls -la above? Yes, there are many hidden directories in the output.
Give three examples of hidden files shown in the output of ls -la above. .config, .bash_history, .xinitrc
[analyst@secOps ~]$ man ls
Part 2: Copying, Deleting, and Moving Files
Step 1: Copying Files
[analyst@secOps ~]$ cp some_text_file.txt cyops_folder2/
Identify the parameters in the cp command above.
Question:
What are the source and destination files? (use full paths to represent the parameters) Source: /home/analyst/some_text_file.txt.
Destination: /home/analyst/cyops_folder2/some_text_file.txt
[analyst@secOps ~]$ ls cyops_folder2/ some_text_file.txt
[analyst@secOps ~]$ ls -l total 36
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:01 cyops_folder1 drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:11 cyops_folder2 drwxr-xr-x 3 analyst analyst 4096 Aug 16 15:04 cyops_folder3 drwxr-xr-x 2 analyst analyst 4096 Sep 26 2014 Desktop drwx—— 3 analyst analyst 4096 Jul 14 11:28 Downloads drwxr-xr-x 8 analyst analyst 4096 Jul 25 16:27 lab.support.files drwxr-xr-x 2 analyst analyst 4096 Mar 3 15:56 second_drive
-rw-r–r– 1 analyst analyst 142 Aug 16 15:09 some_text_file.txt
Step 2: Deleting Files and Directories
[analyst@secOps ~]$ rm some_text_file.txt
[analyst@secOps ~]$ ls -l total 32
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:01 cyops_folder1 drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:11 cyops_folder2 drwxr-xr-x 3 analyst analyst 4096 Aug 16 15:04 cyops_folder3 drwxr-xr-x 2 analyst analyst 4096 Sep 26 2014 Desktop drwx—— 3 analyst analyst 4096 Jul 14 11:28 Downloads drwxr-xr-x 8 analyst analyst 4096 Jul 25 16:27 lab.support.files drwxr-xr-x 2 analyst analyst 4096 Mar 3 15:56 second_drive
[analyst@secOps ~]$ rm –r cyops_folder1
[analyst@secOps ~]$ ls -l total 28
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:11 cyops_folder2 drwxr-xr-x 3 analyst analyst 4096 Aug 16 15:04 cyops_folder3 drwxr-xr-x 2 analyst analyst 4096 Sep 26 2014 Desktop drwx—— 3 analyst analyst 4096 Jul 14 11:28 Downloads drwxr-xr-x 8 analyst analyst 4096 Jul 25 16:27 lab.support.files drwxr-xr-x 2 analyst analyst 4096 Mar 3 15:56 second_drive
Step 3: Moving Files and Directories
a. Moving files works similarly to copying files. The difference is that moving a file removes it from its original location. Use the mv commands to move files around the local filesystem. Like the cp commands, the mv command also requires source and destination parameters. Issue the command below to move the some_text_file.txt from /home/analyst/cyops_folder2 back to the home directory:
[analyst@secOps ~]$ mv cyops_folder2/some_text_file.txt .
[analyst@secOps ~]$ ls –l cyops_folder2/ total 0
[analyst@secOps ~]$ ls –l /home/analyst/ total 32
drwxr-xr-x 2 analyst analyst 4096 Aug 16 15:13 cyops_folder2 drwxr-xr-x 3 analyst analyst 4096 Aug 16 15:04 cyops_folder3 drwxr-xr-x 2 analyst analyst 4096 Sep 26 2014 Desktop drwx—— 3 analyst analyst 4096 Jul 14 11:28 Downloads drwxr-xr-x 8 analyst analyst 4096 Jul 25 16:27 lab.support.files drwxr-xr-x 2 analyst analyst 4096 Mar 3 15:56 second_drive -rw-r–r– 1 analyst analyst 142 Aug 16 15:11 some_text_file.txt
Question:
What command did you use to accomplish the task? rm -r cyops_folder2/
Reflection
What are the advantages of using the Linux command line?
Its give me more control over the graphical interface and is faster.