Basic Commands

Prompt

Learn the common basic commands used on the Linux command line.

Tutorial Video

Walk-Through

This challenge will give you experience running basic Linux commands. Additional learning materials about Linux commands can be found on Linux Journey. To solve these challenges, you will be using a Linux Command Line Interface (aka terminal or shell).

When you run a Linux command, you are be able to provide a variety of different options to get the specific behavior you want.

You can provide arguments - information that is provided to the program you are running. Arguments always follow the name of the program and could provide information such as the file you wish to run the program on. You may also see arguments referred to as “options” or “flags”.

The syntax for arguments is set by the author of the program. You can often type in the name of the command followed by --help to information on how to use the program. For some programs, you can use the man (manual) program to pull up the instructions. If a program has a manual entry, you can open the manual by typing man followed by the name of the program.

You can also use the output of one program as the input to another program. This utilizes a system in Linux called standard streams. There are three standard streams in Linux: standard input (stdin), standard output (stdout), and standard error (stderr). These represent data going into a program, data coming out of a program, and errors coming out of a program. You can take the stdout of one program and make that the stdin of another program. This is done by using the pipe operator, which is represented with the vertical bar. For example, you may want to calculate the md5 sum of some text. You can accomplish this by using the pipe operator to set the output of the echo command as the input to the md5sum command. The following command will calculate the md5sum of the text “test”.

echo “test” -n | md5sum
echo “test” -n | md5sum
⚠️
The -n option is necessary to prevent echo from adding a new line to the md5sum calculation

You can make stdin be the contents of a file by using the less-than character. The following command will calculate the md5sum of the test.txt file.

md5sum < test.txt
md5sum < test.txt

Likewise, you can save the output of a command to a file by using the greater-than character to redirect stdout to the file. If you use one greater-than character, you will overwrite the file (if it exists). If you use two greater-than characters, you will append to the file (if it exists). The following command will save the text “test” to a file called test.txt

echo “test” -n > test.txt
echo “test” -n > test.txt

Those are the building blocks of using the Linux command line. You should practice running different commands using these different customization tools. Below is a table of common Linux commands.

Program
Description
Example
Example Description
ls
“list” - Display files and directories
ls /
Display all files and directories in the “/” folder
cat
“concatenate” - Print out the contents of a file
cat example.txt
Print out the contents of the “example.txt” file
cd
“change directories” - Switch the current folder that the command line is working on
cd /
Change the terminal to the “/” folder
mv
“move” - Move a file or folder from one location to another or rename a file
mv /root/old.txt /tmp/new.txt
Move the “old.txt” from the the “/root” folder to the “/tmp” folder and rename the file to “new.txt”
cp
“copy” - Make a copy of a file or folder
cp original.txt copy.txt
Makes a duplicate of “original.txt” named “copy.txt”
mkdir
“make directory” - Makes a new folder
mkdir test
Makes a new folder named “test”
rm
“remove” - Deletes a file or folder permanently
rm example.txt
Permanently deletes the “example.txt” file
pwd
“print working directory” - Displays the absolute file path of the directory the command line is currently in
pwd
Prints the full path of the current directory
history
Prints a chronological log of the past commands that were entered
history
Prints the log of past commands
echo
Prints the provided string to standard output
echo "test"
Prints the string, “test” to standard output
grep
“global regular expression print” - search for text that matches a specific pattern
grep match example.txt
Prints lines that contain the text “match” in example.txt
wc
“word count” - Gets a line count (followed by a word count and a byte count) of a file or text stream
wc example.txt
Prints the number of lines in example.txt
cut
Extract column(s) from a file or text stream. Columns must be delineated by a consistent character
cut example.txt -d , -f 2
Prints out the column at index 2 from example.txt
sort
Sorts the lines from a file or text stream.
sort example.txt
Prints the sorted output of the lines from example.txt
uniq
“Unique” - Prints the result of removing duplicate lines from a file or text stream
uniq example.txt
Prints out the result of removing any duplicate lines from example.txt.
man
“manual” - Displays the manual for a program
man echo
Display the manual for the “echo” program

Chaining multiple Linux commands can become useful in manipulating and analyzing data. In the final question, you given the name.csv file and ask to find out how many people have the first name of Jordan. You can use a combination of cut, grep, and wc. The following command will pull just the firstName column from the file, search for rows that have “Jordan”, and then get a line count.

cut -d "," -f 1 < names.csv | grep Jordan | wc -l
cut -d "," -f 1 < names.csv | grep Jordan | wc -l

If we do a simple grep search without including the cut command, you would also match people who had Jordan as a last name, so it is important that cut is used to only select people who have Jordan as a first name. By chaining these commands using input/output redirection, we can accomplish our goal.

You should become comfortable and will be expected to search online for command line tools that can help you accomplish a task from the terminal. You can also use the built-in help or manual pages to learn how to use a tool. Please note that within the terminal we provide to you, you will only have access to the tools we pre-installed for you. You may not be able to install tools that are not already included in the terminal.

Questions

What character can you use to redirect the output of one program as the input to another program

Search online for this question as-is. Make sure to find an answer that is specific to redirecting from output from one program to another program (and not to a file)

What character can you use the redirect the output of a program to a file?

Search online for this question as-is. Make sure to find an answer that is specific to redirecting from output from one program to a file (and not to another program)

How many people have a first name of Jordan in names.csv?

cut -d "," -f 1 < names.csv | grep Jordan | wc -l

©️ 2024 Cyber Skyline. All Rights Reserved. Unauthorized reproduction or distribution of this copyrighted work is illegal.