OS Flashcards

1
Q

chmod (-R)
u g o a
- + =

chmod 777 filename
chmod a-x filename

A

-R (recursive)

u - The file owner.
g - The users who are members of the group.
o - All other users.
a - All users, identical to ugo.

- Removes the specified permissions.
\+ Adds specified permissions.
= Changes the current permissions to the specified permissions. If no permissions are specified after the = symbol, all permissions from the specified user class are removed.

Remove the execute permission for all users:
chmod a-x filename

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

cp (-R)

A

-R (recursive)
meaning perfect copy with all subfolders

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

cut (-f -d)

A

cut is a command-line utility that allows you to cut line parts from specified files or piped data and print the result to standard output. It can be used to cut parts of a line by delimiter, byte position, and character.

-f (–fields=LIST) - Select by specifying a field, a set of fields, or a range of fields. This is the most commonly used option.

-d (–delimiter) - Specify a delimiter that will be used instead of the default “TAB” delimiter.

245:789 4567 M:4540 Admin 01:10:1980
535:763 4987 M:3476 Sales 11:04:1978

cut test.txt -f 1,3
245:789 M:4540
535:763 M:3476

cut test.txt -d ‘:’ -f 1,3
245:4540 Admin 01
535:3476 Sales 11

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

file (-b )

file /etc/group
file -b /etc/group
file /bin/bash /opt/card.zip

A

Determines the type of a file (ex: ASCII text) by analyzing its content rather than its file extension. (if used without type or name, otherwise it just for finding files)

file /etc/group
/etc/group: ASCII text

file -b /etc/group (brief)
ASCII text

file /bin/bash /opt/card.zip
/bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=42602c973215ba5b8ab5159c527e72f38e83ee52, stripped
/opt/card.zip: Zip archive data, at least v1.0 to extract

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

expr
expr length
expr substr

A

The expr command in Linux is used for evaluating expressions. It supports a variety of arithmetic, logical, and string operations. Here’s a brief overview of how to use it:
Arithmetic Operations

Addition: expr 5 + 3
Subtraction: expr 10 - 2
Multiplication: expr 4 \* 3 (The * must be escaped)
Division: expr 10 / 2
Modulus: expr 10 % 3

Comparison Operations

Equality: expr 5 = 5
Greater than: expr 6 \> 4 (The > must be escaped)
Less than: expr 3 \< 5 (The < must be escaped)
Not equal: expr 5 != 3

String Operations

String length: expr length "hello"
Substring: expr substr "hello" 2 3 (Extracts "ell")

Logical Operations

AND: expr 1 \& 0 (The & must be escaped)
OR: expr 1 \| 0 (The | must be escaped)

Usage Tips

The command may require spaces between operators and operands.
Special characters like *, &, <, > must be escaped with a backslash (\).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

find .
find /path/to/directory -name “.txt”
find /path/to/directory -type d
find /path/to/directory -type f -executable -name “
.sh”

A

Running find . without any additional options will list all files and directories in the current directory and its subdirectories.

The -type option is used to specify the type of files to find. It allows you to filter the search results by file type.
Common Type Options

f: Regular file
d: Directory
l: Symbolic link
c: Character device
b: Block device
p: Named pipe (FIFO)
s: Socket The -name option is used to search for files and directories by name. It allows pattern matching using wildcards (*, ?, etc.).

Find Files by Pattern
find /path/to/directory -name “*.txt”

Find All Directories
find /path/to/directory -type d

Find All Executable Files
find /path/to/directory -type f -executable -name “*.sh”

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

grep -i -q -v -E
grep -q “error” log.txt && echo “Error found!” || echo “No error found.”

A

The grep command in Linux is used to search for text patterns within files. It is a powerful tool for text processing and filtering, offering various options to customize its behavior

The -i option enables case-insensitive searching. This makes grep ignore case distinctions in both the pattern and the input file(s).

The -q option runs grep in quiet mode. It suppresses all output, making grep only return an exit status to indicate whether a match was found.

Exit Status:
    0: Match found.
    1: No match found.
    2: An error occurred.

grep -q “error” log.txt && echo “Error found!” || echo “No error found.”

The -v option inverts the match, meaning it selects lines that do not match the given pattern. This is useful for filtering out specific lines.

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

head -n 5 example.txt

A

The head command is used to display the first few lines of a file or input from a command.

-n [num]: Specifies the number of lines to display from the start of the file. For example, head -n 10 filename.txt shows the first 10 line

10 is default

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

ls -l
format?

A

The ls command is used to list the contents of directories in Linux.
ls [options] [file…]
-l: Stands for “long listing format.” This option provides detailed information about each file or directory.

ex output:
-rw-r–r– 1 user group 2048 Aug 5 2024 file1.txt
drwxr-xr-x 2 user group 4096 Aug 5 2024 dir1

File Permissions: Displays the file’s permissions (e.g., -rwxr-xr-x).
Number of Links: Shows the number of hard links to the file.
Owner: Displays the username of the file owner.
Group: Displays the group name associated with the file.
Size: Shows the file size in bytes.
Modification Date: Displays the last modification date and time of the file.
File Name: Shows the name of the file or directory.

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

mkdir -p

A

mkdir command is used to create directories.

-p (or –parents): Allows the creation of parent directories as needed.

If any intermediate directories in the specified path do not exist, -p will create them.

mkdir -p /home/user/documents/projects/project1

This command creates the entire directory path, including any missing parent directories, such as documents, projects, and project1.

Without the -p option, mkdir will fail with an error if any parent directory in the path does not exist.

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

mv

A

mv is used to move files or directories from one location to another, or to rename files or directories.

mv file1.txt /home/user/documents

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

ps -e -f
format?

A

The ps command is used to display information about currently running processes on the system.

-e (or –all):
Lists all processes currently running on the system.
Includes processes from all users and all sessions.

-f (or –full):
Provides a full-format listing, showing detailed information about each process.

ps -ef example:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug05 ? 00:00:02 /sbin/init
user 1234 5678 0 12:34 pts/0 00:00:00 bash
user 2345 1234 1 12:35 pts/0 00:00:01 python script.py

C: CPU usage percentage.
STIME: The time when the process started.
TTY: The terminal associated with the process.
TIME: Total CPU time used by the process.

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

pwd

A

The pwd command stands for “print working directory” and is used to display the full path of the current directory in which the user is working.

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

rm (-f,-r),
rm -rf /path/to/directory

A

The rm command is used to delete files and directories from the filesystem.

-f (or –force): Forces the removal of files without prompting for confirmation and ignores nonexistent files.

-r (or –recursive): Recursively removes directories and their contents.

rm -rf /path/to/directory
This command deletes /path/to/directory and all its contents, even if they are write-protected or if there are errors encountered during removal.

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

read (-p)

A

The read command reads a line of input from standard input (stdin) or a file and assigns it to one or more variables.

-p: Prompts the user with a specific message before taking input.

read -p “Enter your name: “ name
echo “Hello, $name!”

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

sed (-E and commands d,s,y),

A

Stream Editing: sed is used to perform basic text transformations on an input stream (a file or input from a pipeline).

Extended Regular Expressions (-E: ERE): Support additional metacharacters like +, ?, |, and (), which aren’t available in basic regex without escaping.

d (Delete Lines):

sed ‘2d’ filename.txt => Deletes the second line from filename.txt.
sed ‘/error/d’ filename.txt => Deletes all lines containing the word “error” from filename.txt.

s (Substitute):
sed ‘s/apple/orange/’ filename.txt => Replaces the first occurrence of “apple” with “orange” on each line.

y (Transliterate):
Transliterates characters by performing one-to-one character mapping. It operates similarly to the tr command.

sed ‘y/abc/xyz/’ filename.txt => Translates characters a to x, b to y, and c to z in filename.txt.

sed ‘y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/’ filename.txt

!!!In-Place Editing: By default, sed does not modify files directly. Use -i for in-place editing with caution, as it can overwrite files

17
Q

sleep [NUMBER][SUFFIX]

A

Pause Execution: sleep is used to delay the execution of subsequent commands for a given period of time.

Syntax: sleep [NUMBER][SUFFIX]

Units of Time

s - seconds (default if no suffix is provided)
m - minutes
h - hours
d - days

ex: sleep 1.5m

18
Q

sort (-n,-r)

sort -nr numbers.txt

A

Sorting Text: sort is used to arrange lines of text files or input data in a specified order, either alphabetically or numerically.

-n (Numeric Sort)

Sorts lines based on numerical value rather than alphabetically. This option is particularly useful for sorting numbers in files

-r (Reverse Order)

Reverses the sorting order. When combined with other options like -n, it sorts in descending order.

sort -nr numbers.txt =>Combines -n for numeric sorting and -r for reverse order, resulting in a descending numeric sort.

19
Q

tail (-n)

A

The tail command is used to display the last few lines of a file or input from a command.

-n [num]: Specifies the number of lines to display from the end of the file.
10 is default

20
Q

test (numerical, string and file operators)

A

Evaluate Expressions: test checks conditions involving numbers, strings, and file attributes to decide the flow of a script based on those conditions.

test expression <=> Alternatively, [ expression ] is often used as a synonym for test.

Numerical Operators

-eq: Equal to
-ne: Not equal to
-gt: Greater than
-ge: Greater than or equal to
-lt: Less than
-le: Less than or equal to

String Operators

=: Equal to
!=: Not equal to
-z: String is null (zero length)
-n: String is not null

File Operators

-e: File exists
-f: Regular file
-d: Directory
-r: Readable
-w: Writable
-x: Executable
-s: Size is greater than zero
-L: Symbolic link

Combined Logical Operators

Logical AND (&&): Both conditions must be true
Logical OR (||): At least one condition must be true
Negation (!): Inverts the condition

ex:

if [ -f “$file” ] && [ -r “$file” ]; then
echo “File is a regular file and readable.”
fi

if [ ! -w “$file” ]; then
echo “File is not writable.”
fi

21
Q

uniq (-c)

A

Remove Duplicates: uniq is designed to process sorted input and identify or filter out repeated lines, displaying unique lines only or counting occurrences.

uniq [options] [file]

-c (Count): precedes each line with the number of times it occurs in the input.

apple
banana
apple
cherry
banana
banana
apple

ex: sort data.txt | uniq -c => 3 apple 3 banana 1 cherry (each of new line)

22
Q

wc (-c,-l,-w)
wc -c -w -l example.txt

A

Count Lines, Words, and Characters: wc provides counts of lines, words, and characters, helping in text processing and data analysis.

wc -c -w -l example.txt

This command will display the number of characters, words, and lines in example.txt, respectively.

12 2 2 example.txt

23
Q

$0, $1,…, $9, $*,$#, $@, $?

A

$0 => The name of the script itself.

$1, $2, …, $9 => The first, second, …, ninth positional parameters (arguments) passed to the script.

$* => All positional parameters as a single word without quotes.

S# => Stores the number of command-line arguments that were passed to the shell program.

$@ => All positional parameters as separate quoted strings, preserving spaces.

$? => The exit status of the last executed command, with 0 indicating success.

./command -yes -no /home/username

$# = 3
$* = -yes -no /home/username
$@ = array: {"-yes", "-no", "/home/username"}
$0 = ./command, $1 = -yes etc.
24
Q

I/O redirections (|, >, > >, <, 2 >, 2 > >, 2 > & 1

ls | grep “.txt”
echo “Hello, World!” > output.txt
echo “text”&raquo_space; output.txt
wc -l < input.txt
ls non_existent_file 2> error.log
ls non_existent_file 2» error.log
ls non_existent_file > output.log 2>&1

A

> => Redirects the standard output to a file, overwriting the file if it exists.

> > => Redirects the standard output to a file, appending to the file if it exists.

< => Redirects the standard input from a file.

2> => Redirects the standard error to a file, overwriting the file if it exists.

2 > > => Redirects the standard error to a file, appending to the file if it exists.

2 > & 1 => Redirects the standard error (file descriptor 2) to the same location as standard output (file descriptor 1).

ls non_existent_file > output.log 2>&1

Redirects both the standard output and standard error of the ls command to output.log. This combines both streams into a single file, which is helpful when you want to capture all output (including errors) together.

The notation 2>&1 is used for redirecting the standard error (stderr) to the same destination as the standard output (stdout). Here’s the breakdown of the syntax:

2: Refers to the file descriptor number for standard error (stderr).

>: The redirection operator. It indicates that the output should be redirected.

&: A special character indicating that what follows is a file descriptor, not a file name. Without &, the shell would interpret 1 as a file name.

1: Refers to the file descriptor number for standard output (stdout).

=> Sends the output of one command as input to another command (pipe).

25
Q

/dev/null file

A

Special File: /dev/null is a device file that acts like a “black hole” for data. Any input sent to this file is discarded, and it doesn’t produce any output.

Character Device: It is a type of character device file, meaning it handles data streams character by character, and is present in the filesystem as a pseudo-device.

Location: It resides in the /dev directory, which contains device files representing hardware devices and other special files.

You can redirect the standard output or standard error of a command to /dev/null to suppress unwanted output.
Example: Suppressing all output from a command:
command > /dev/null 2>&1

26
Q

back-quotes ``

what does files=ls \dirname somefile`` do

A

Back-quotes (`) are a form of command substitution in Unix-like shells, such as Bash. They allow the output of a command to be used as part of another command or assignment. Here’s an overview of how back-quotes work and their typical use cases:

ex:
current_date=date
echo $current_date

nested ex:
files=ls \dirname somefile``

27
Q

!/bin/bash

shift [n]

while [ “$#” -gt 0 ]; do
echo “$1”
shift
done

A

Purpose: The shift command shifts the positional parameters to the left. This means that each positional parameter is moved one position down the list, and the first positional parameter ($1) is discarded.

ex:
#!/bin/bash
echo “Before shift: $1 $2 $3”
shift
echo “After shift: $1 $2 $3”

Before shift: arg1 arg2 arg3
After shift: arg2 arg3
Explanation: The shift command removes arg1 and shifts arg2 to $1, arg3 to $2.

ex2:
#!/bin/bash
while [ “$#” -gt 0 ]; do
echo “$1”
shift
done