Introduction to Bash Flashcards

1
Q

What is Bash

A

Bash is a vital tool for managing Linux machines. The name is short for “Bourne Again Shell.”

A shell is a program that commands the operating system to perform actions. You can enter commands in a console on your computer and run the commands directly, or you can use scripts to run batches of commands. Shells like PowerShell and Bash give system administrators the power and precision they need for fine-tuned control of the computers they’re responsible for.
In Unix and Linux, everything is a file. That means you can use the same commands without worrying about whether the I/O stream—the input and output—comes from a keyboard, a disk file, a socket, a pipe, or another I/O abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

full syntax for a Bash command

A

command [options] [arguments]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Bash treats the first string it encounters as a command. The following command uses Bash’s ls (for “list”) command to display the contents of the current working directory:

A

ls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Arguments often accompany Bash commands. For example, you can include a path name in an ls command to list the contents of another directory:

A

ls /etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Most Bash commands have options for modifying how they work. Options, also called flags, give a command more specific instructions. As an example, files and directories whose names begin with a period are hidden from the user and aren’t displayed by ls. However, you can include the -a (for “all”) flag in an ls command and see everything in the target directory:

A

ls -a /etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

You can even combine flags for brevity. For example, rather than enter ls -a -l /etc to show all files and directories in Linux’s /etc directory in long form, you can enter this instead:

A

ls -al /etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which options and arguments can be used, or must be used, varies from command to command. Fortunately, Bash documentation is built into the operating system. Help is never more than a command away. To learn about the options for a command, use the man (for “manual”) command. For instance, to see all the options for the mkdir (“make directory”) command, do this:

A

man mkdir

man is your best friend as you learn Bash. man is how you find the information you need to understand how any command works.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Most Bash and Linux commands support the –help option. This shows a description of the command’s syntax and options. To demonstrate, enter mkdir –help. The output looks something like this:

A

Usage: mkdir [OPTION]… DIRECTORY…
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
-m, –mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, –parents no error if existing, make parent directories as needed
-v, –verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
–context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
–help display this help and exit
–version output version information and exit

GNU coreutils online help: http://www.gnu.org/software/coreutils/
Report mkdir translation bugs to http://translationproject.org/team/
Full documentation at: http://www.gnu.org/software/coreutils/mkdir
or available locally via: info ‘(coreutils) mkdir invocation’

Help obtained this way is typically more concise than help obtained with man.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Wildcards are symbols that represent one or more characters in Bash commands. The most frequently used wildcard is the asterisk. It represents zero characters or a sequence of characters. Suppose your current directory contains hundreds of image files, but you only want to see the PNG files; the ones whose file names end with .png. Here’s the command to list only those files:

A

ls *.png

Linux has no formal concept of a file-name extension as other operating systems do. This doesn’t mean that PNG files won’t have a .png extension. It simply means Linux attaches no special significance to the fact that the file names end with .png.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Now let’s say the current directory also contains JPEG files. Some end in .jpg, while others end in .jpeg. Here’s one way to list all the JPEG files:

A

ls *.jpg *.jpeg

And here’s another:

ls .jpg

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The * wildcard matches on zero or more characters, but the ? wildcard represents a single character. If the current directory contains files named 0001.jpg, 0002.jpg, and so on through 0009.jpg, the following command lists them all:

A

ls 000?.jpg

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Yet another way to use wildcards to filter output is to use square brackets, which denote groups of characters. The following command lists all the files in the current directory whose names contain a period immediately followed a lowercase J or P. It lists all the .jpg, .jpeg, and .png files, but not .gif files:

A

ls .[jp]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In Linux, file names and the commands that operate upon them are case-sensitive. So to list all the files in the current directory whose names contain periods followed by an uppercase or lowercase J or P, you could enter this:

A

ls .[jpJP]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Expressions in square brackets can represent ranges of characters. For example, the following command lists all the files in the current directory whose names begin with a lowercase letter:

A

ls [a-z]*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

This command, by contrast, lists all the files in the current directory whose names begin with an uppercase letter:

A

ls [A-Z]*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

And this one lists all the files in the current directory whose names begin with a lowercase or uppercase letter:

A

ls [a-zA-Z]*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

If you need to use one of the wildcard characters as an ordinary character, you make it literal or “escape it” by prefacing it with a backslash. So, if for some reason you had an asterisk as part of a file name—something you should never do intentionally—you could search for it by using a command such as:

A

$ ls *

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

ls lists the contents of your current directory or the directory specified in an argument to the command. By itself, it lists the files and directories in the current directory:

A

ls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Files and directories whose names begin with a period are hidden by default. To include these items in a directory listing, use an -a flag:

A

ls -a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

To get even more information about the files and directories in the current directory, use an -l flag:

A

ls -l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Suppose you want to see what’s inside a file. You can use the cat command for that. The output won’t make much sense unless the file is a text file. The following command shows the contents of the os-release file stored in the /etc directory:

A

cat /etc/os-release

22
Q

This is a useful command because it tells you which Linux distribution you’re running:

A

NAME=”Ubuntu”
VERSION=”18.04.2 LTS (Bionic Beaver)”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=”Ubuntu 18.04.2 LTS”
VERSION_ID=”18.04”
HOME_URL=”https://www.ubuntu.com/”
SUPPORT_URL=”https://help.ubuntu.com/”
BUG_REPORT_URL=”https://bugs.launchpad.net/ubuntu/”
PRIVACY_POLICY_URL=”https://www.ubuntu.com/legal/terms-and-policies/privacy-policy”
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

The /etc directory is a special one in Linux. It contains system-configuration files. You don’t want to delete any files from this directory unless you know what you’re doing.

23
Q

Some Bash commands can only be run by the root user; a system administrator or superuser. If you try one of these commands without sufficient privileges, it fails. For example, only users logged in as a superuser can use cat to display the contents of /etc/at.deny:

A

cat /etc/at.deny

24
Q

at.deny is a special file that determines who can use other Bash commands to submit jobs for later execution.

You don’t want to run as root most of the time; it’s too dangerous. To run commands that require admin privilege without logging in as a superuser, you’ll preface the commands with sudo:

A

sudo cat /etc/at.deny

sudo stands for “superuser do.” When you use it, you’re telling the shell that for this one command, you’re acting with the root-user level of permission.

25
Q

cd stands for “change directory,” and it does exactly what the name suggests: it changes the current directory to another directory. It enables you to move from one directory to another just like its counterpart in Windows. The following command changes to a subdirectory of the current directory named orders:

A

cd orders

26
Q

You can move up a directory by specifying .. as the directory name:

A

cd ..

27
Q

This command changes to your home directory; the one you land in when you first sign in:

A

cd ~

28
Q

You can create directories by using the mkdir command. The following command creates a subdirectory named orders in the current working directory:

A

mkdir orders

29
Q

If you want to create a subdirectory and another subdirectory under it with one command, use the –parents flag:

A

mkdir –parents orders/2019

30
Q

The rmdir command deletes (removes) a directory, but only if it’s empty. If it’s not empty, you get a warning instead. Fortunately, you can use the rm command to delete directories that aren’t empty in combination with the -r (recursive) flag. The command would then look like so, rm -r.

The rm command is short for “remove.” As you’d expect, rm deletes files. So this command puts an end to 0001.jpg:

A

rm 0001.jpg

31
Q

And this command deletes all the files in the current directory:

A

rm *

32
Q

Be wary of rm. It’s a dangerous command.

Running rm with a -i flag lets you think before you delete:

A

rm -i *

Make it a habit to include -i in every rm command, and you might avoid falling victim to one of Linux’s biggest blunders. The dreaded rm -rf / command deletes every file on an entire drive. It works by recursively deleting all the subdirectories of root and their subdirectories. The -f (for “force”) flag compounds the problem by suppressing prompts. Don’t do this.

33
Q

If you want to delete a subdirectory named orders that isn’t empty, you can use the rm command this way:

A

rm -r orders

This deletes the orders subdirectory and everything in it, including other subdirectories.

34
Q

The cp command copies not just files, but entire directories (and subdirectories) if you want. To make a copy of 0001.jpg named 0002.jpg, use this command:

A

cp 0001.jpg 0002.jpg

35
Q

If 0002.jpg already exists, Bash silently replaces it. That’s great if it’s what you intended, but not so wonderful if you didn’t realize you were about to overwrite the old version.

Fortunately, if you use the -i (for “interactive”) flag, Bash warns you before deleting existing files. This is safer:

A

cp -i 0001.jpg 0002.jpg

36
Q

You can use wildcards to copy several files at once. To copy all the files in the current directory to a subdirectory named photos, do this:

A

cp * photos

37
Q

To copy all the files in a subdirectory named photos into a subdirectory named images, do this:

A

cp photos/* images

38
Q

This assumes that the images directory already exists. If it doesn’t, you can create it and copy the contents of the photos directory by using this command:

A

cp -r photos images

The -r stands for “recursive.” An added benefit of the -r flag is that if photos contains subdirectories of its own, they too are copied to the images directory.

39
Q

The ps command gives you a snapshot of all the currently running processes. By itself, with no arguments, it shows all your shell processes; in other words, not much. But it’s a different story when you include a -e flag:

A

ps -e

-e lists all running processes, and there are typically many of them.

40
Q

For a more comprehensive look at what processes are running in the system, use the -ef flag:

A

ps -ef

This flag shows the names of all the running processes, their process identification numbers (PIDs), the PIDs of their parents (PPIDs), and when they began (STIME). It also shows what terminal, if any, they’re attached to (TTY), how much CPU time they’ve racked up (TIME), and their full path names.

41
Q

w command

A

To find out who’s on your servers, Linux provides the w (for “who”) command. It displays information about the users currently on the computer system and those users’ activities. w shows user names, their IP addresses, when they logged in, what processes they’re currently running, and how much time those processes are consuming. It’s a valuable tool for sysadmins.

42
Q

Bash I/O operators

A

< for redirecting input to a source other than the keyboard
> for redirecting output to destination other than the screen
» for doing the same, but appending rather than overwriting
| for piping output from one command to the input of another

43
Q

Suppose you want to list everything in the current directory but capture the output in a file named listing.txt. The following command does just that:

A

ls > listing.txt

44
Q

If listing.txt already exists, it gets overwritten. If you use the&raquo_space; operator instead, the output from ls is appended to what’s already in listing.txt:

A

ls&raquo_space; listing.txt

45
Q

The piping operator is powerful (and often used). It redirects the output of the first command to the input of the second command. Let’s say you use cat to display the contents of a large file, but the content scrolls by too quickly for you to read. You can make the output more manageable by piping the results to another command such as more. The following command lists all the currently running processes. But once the screen is full, the output pauses until you select Enter to show the next line:

A

ps -ef | more

46
Q

You can also pipe output to head to see just the first several lines:

A

ps -ef | head

47
Q

Or suppose you want to filter the output to include only the lines that contain the word “daemon.” One way to do that is by piping the output from ps to Linux’s useful grep tool:

A

ps -ef | grep daemon

48
Q

You can also use files as input. By default, standard input comes from the keyboard, but it too can be redirected. To get input from a file instead of the keyboard, use the < operator. One common sysadmin task is to sort the contents of a file. As the name suggests, sort sorts text in alphabetical order:

A

sort < file.txt

49
Q

To save the sorted results to a new file, you can redirect input and output:

A

sort < file.txt > sorted_file.txt

50
Q

You can use I/O operators to chain Linux commands as needed. Consider the following command:

A

cat file.txt | fmt | pr | lpr

The output from cat goes to fmt, the output from fmt goes to pr, and so on. fmt formats the results into a tidy paragraph. pr paginates the results. And lpr sends the paginated output to the printer.