CLI Flashcards
What is Bash?
Bash is an sh-compatible shell that allows us to run complex commands and perform different tasks from a terminal window. It incorporates useful features from both the KornShell and C shell.
How to view content of given enviroment variables?
echo $PATH
Useful enviroment variables
$USER
$PWD
$HOME
How to display the process ID of the current shell instance?
echo “$$”
What “export” command does?
The export command makes the variable accessible to any subprocesses we might spawn from our current Bash instance. If we set an environment variable without export it will only be available in the current shell.
How to view enviroment variables defined by default in Linux?
By env command.
How to check history of commands that have been entered?
By history command.
How to re-run first command from your history?
!1
How to repeat last command that was executed during terminal session?
!!
Where is command history saved to?
.bash_history in the user home directory.
Two enviroment variables control the history size
HISTSIZE and HISTFILESIZE
HISTSIZE
Controls the number of commands stored in memory for the current session.
HISTFILESIZE
Configures how many commands are kept in the history file.
How to invoke reverse-i-search facility?
CTRL + R
How to inspect your bash history?
By history command
How to use history expansion to re-run a command from it?
!number
Standard Input (STDIN)
Data fed into the program
Standard Output (STDOUT)
Output from the program (defaults to terminal)
Standard Error (STDERR)
Error messages (defaults to terminal)
Standard Error (STDERR)
Error messages (defaults to terminal)
Piping operator
|
Redirection Operators
<>
Which operator is used to save the output to a file to keep it for future?
>
How to save “test” string to file “redirection_test.txt”?
echo “test” > redirection_test.txt
Which operand is used to append additional data to an existing file?
> >
How to append two lines of strings, to one file names “redirection_test.txt”?
echo “1”»_space; redirection_test.txt
echo “2”»_space; redirection_test.txt
What is doing “wc” command?
Print newline, word, and byte counts for each file.
How to redirect standard error (STDERR) to file?
ls ./test 2>error.txt
File Descriptor for STDIN?
0
File Descriptor for STDOUT?
1
File Descriptor for STDERR?
2
What is POSIX?
Portable Operating System Interface for UNIX
How to count by “wc” file.txt and return the data to “count.txt” using piping?
cat file.txt | wc -m > count.txt
How to use a cat command in conjuction with sort to reorder the content of the /etc/passwd?
sudo cat /etc/passwd | sort 1>passwd.txt
Text Searching and Manipulation main commands
grep
sed
cut
awk
What is regular expression?
A regular expression is a special text string for describing a search pattern.
What is grep?
In a nutshell, grep searches text files for the occurrence of a given regular expression and outputs any line containing a match to the standard output, which is usually the terminal screen.
grep -r stands for?
Recursive searching
grep -i stands for?
Ignore test case.
How to list all files in the /usr/bin directory and pipe the output, which searches for any line containing the string “zip”?
ls -la /usr/bin | grep zip
What is sed?
Sed is a powerful stream editor. It is also very complex. At a very high level, sed performs text editing on a stream of text, either a set of specific files or standard output.
How to create a stream of text “I need to try hard”?
echo “I need to try hard”
How to replace word “hard” with “harder” by sed?
sed ‘s/hard/harder/’
How to create a stream of text “I need to try harder” using the echo command and then pipe it to sed in order to replace the word “hard” with “harder”?
echo “I need to try hard” | sed ‘s/hard/harder’
What is cut?
The cut command is simple, but often comes in quite handy. It is used to extract a section of text from a line and output it to the standard output.
Most commonly-used switches for cut?
- f
- d
What -f switch means in cut command?
Field Number
What -d switch means in cut command?
Field Delimiter
How to extract list of users on Linux system?
cut -d “:” -f 1 /etc/passwd
What is the output of this command?
echo “I hack binaries,web apps,mobile apps, and just about anything else”| cut -f 2 -d “,”
web apps
What is the field delimiter in this command?
echo “I hack binaries,web apps,mobile apps, and just about anything else”| cut -f 2 -d “,”
(,)
What is AWK?
AWK is a programming language designed for text processing and is typically used as a data extraction and reporting tool. It is also extremely powerful and can be quite complex.
Commonly used switch with AWK?
-F
What is -F switch in awk?
Field separator, and the print command, which outputs the result text.
What is the output of this command?
echo “hello::there::friend” | awk -F “::” ‘{print $1, $3}’
hello friend
What is the difference between cut and awk?
Awk is much more flexible.
What does the head command do?
The head command displays the first 10 lines in a file.
What does wc -l command do?
Display the total number of lines in a file.
What gunzip command do?
Comrpess or expand files.
Gunzip alternatives
gzip, zcat
How to unzip “access_log.txt.gz”
gunzip access_log.txt.gz
How to print first 10 lines of file “access.log”?
head access.log
What sort -u do?
Sorts and showing only unique lines.
What does uniq command do?
Report or omit repeated lines.
What the -c option for uniq do?
This will prefix the output line with the number of occurrences.
What command using /etc/passwd, extract the user and home directory fields for all users on your Linux.
cat /etc/passwd | awk -F “:” ‘{print “The user “ $1, “ home directory is “ $6}’
How to copy the /etc/passwd file to your home directory (/home/kali)?
cp /etc/passwd /home/kali/passwd
How to use cat in a one-liner to print the output of the /kali/passwd and replace all instances of the “Light Display Manager” string with “LDM”.
cat passwd | sed -i ‘s/Light Display Manager/LDM/g’ passwd
Linux Text Editors
gedit
leafpad
nano
vi
What is nano?
Nano is one of the simplest-to-use text editors.
What does CTRL + O shortcut in nano?
Write changes to the file.
What does CTRL + K shortcut in nano?
Cut the current line.
What does CTRL + U shortcut in nano?
Un-cut a line and paste it at the cursor location.
What does CTRL + W shortcut in nano?
Search.
What does CTRL + X shortcut in nano?
Exit.
What is vi?
Vi is an extremely powerful text editor, capable of blazing speed especially when it comes to automating repetitive tasks. However, it has a relatively steep learning curve and is nowhere near as simple to use as Nano.