Bash Commands Flashcards

1
Q

vim -r

A

list swap files

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

tail ‘file’

A

shows last 10 lines of the file

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

vim

A

open or creates file

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

mv ‘file1’ ‘file2’

A

move file to destination or rename file

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

cp ‘file1’ ‘file2’

A

copy contents from file1 to file2

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

rm ‘file’

A

remove a file

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

rm -r ‘file’

A

remove a file recursively

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

rm -d

A

remove empty directory

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

rm – -foo

A

rm file that starts with foo

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

find . -name ‘file1’ -type d

A

find the directory called file1. Without the -type d, it will search for files and directories

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

diff ‘file1’ ‘file2’

A

compares files and shows where they are different

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

sort -n -r ‘file’

A

sort files, -n for numeric sort and -r for reversing order

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

rev

A

reverse string character

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

chmod +wrx

A

reach, write and execute permissions

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

chmod -o -g -a -u

A

removes permission for others not in group, group, all users and user

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

lpr ‘filename’

A

prints the file

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

grep -i ‘hello’ ‘filename’

A

search for hello in filenames (this will not be case-sensitive)

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

grep -r ‘hello’ ‘directory

A

search recursively for pattern in the directory

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

sed ‘s/hello/goodbye/g’ ‘filename’

A

This will replace all occurrences of hello with goodbye in ‘filename’ without modifying the file

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

sed -i ‘s/dog/cat/g’ file.txt

A

replaces “dog” with “cat” in file.txt and saves the change

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

echo “this” | sed ‘s/is/at/g’

A

replaces “is” with “at” in the input string “this”

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

mkdir ‘filename’

A

makes a new directory

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

rmdir -rf

A

removes a non-empty directory

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

cp -r ‘dir1’ ‘dir2’

A

copy dir1 into dir2 including subdirectories

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

pwd

A

print working directory

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

cd ~

A

changes to home directory

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

whoami

A

returns your username

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

sudo command

A

execute command as the root user

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

lsof

A

lists all open files

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

varname=value

A

defines a variable

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

echo $varname

A

checks a variables value

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

read -p “Please enter your name: “ name

A

prompts the user for input with a specified message and stores the entered value in a variable

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

export -f ‘f1’

A

export function called f1

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

How do you define an array

A

array[0]=valA

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

How do you display the array’s value for a certain index

A

${array[i]}

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

what do you put at the beginning of your bash script

A

!/bin/bash

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

what are some arguments for chmod

A

–recursive - changes files and directories recursively
–changes - report when a change is made
–silent - suppress most error messages

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

what does apropos keyword mean?

A

Its when you don’t know the command name but roughly know what you want to do.

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

man 3 printf

A

this will open up the third man page of the prinf command

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

what does rm file* do?

A

removes files 1 to infinity that have digits after the filename. Ex: file1 file2

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

rm file?

A

removes files with one character after filename. files{0-9}

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

*(abc)

A

would match “”, “abc”, “abcabc”, “abcabcabc”, etc.

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

+(abc)

A

would match “abc”, “abcabc”, “abcabcabc”, but not an empty string “”

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

?(abc)

A

would match “” or “abc”.

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

@(abc|def)

A

would match “abc” or “def”, but not both or neither

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

!(abc)

A

would match any string that is not “abc”

45
Q

what is the AND operator and the OR operator

A

&& - AND || - OR

46
Q

str1 == str2

A

the strings match each other

47
Q

what are the terms for less than, greater than or equal to, not equal, etc..

A

-lt # less than
-le # less than or equal
-eq # equal
-ge # greater than or equal
-gt # greater than
-ne # not equal

48
Q

what is the criteria in starting and stopping a for loop

A

do and done

49
Q

what are the requirements in an if statement

A

if condition
then
statements
[elif condition
then statements…]
[else
statements]
fi

50
Q

how do you do an incremental for loop in bash

A

for (( initialisation ; ending condition ; update ))

51
Q

what does the pipe function do for this example cmd1 | cmd2

A

This takes the results from cmd1 and sends them to cmd2

52
Q

what happens here? ls | grep “.txt”

A

ls lists all the files, and grep “.txt” looks through that list to find only the text files.

53
Q

history

A

show command history

54
Q

source /file

A

runs every command one line at a time

55
Q

!!

A

repeats the last command

56
Q

echo $SHELL

A

displays the shell you are using

57
Q

echo $PATH

A

shows the entire path you are in

58
Q

whereis bash

A

shows location, source code and path

59
Q

ls

A

list files in directory

60
Q

env

A

displays environment variables

61
Q

what outputs when ls -l is printed to the console

A

displays
type of file
type of permission access
ACL flag
Links
Owner
Group
Size
Date and Time
Filename

62
Q

ls - a

A

displays hidden files

63
Q

ln -s ‘filename’ ‘link’

A

creates link to a file

64
Q

readlink ‘filename’

A

shows where the link points to

65
Q

tree

A

shows directories in the tree format

66
Q

touch file

A

creates or updates your file

67
Q

cat file

A

displays raw contents of file

68
Q

cat -n file

A

shows numbers of lines

69
Q

nl file

A

shows number of lines in file

70
Q

cat f1 > f2

A

copy f1 to f2

71
Q

cat f1&raquo_space; f2

A

merge 2 file texts together

72
Q

echo “New entry” > file2

A

directs and overwrites New entry to file2

73
Q

file1&raquo_space; file2

A

directions and appends contents of file1 to file2

74
Q

more file1

A

shows the first portion of the file1

75
Q

head file1

A

shows the first 10 lines of file1

76
Q

tail file1

A

shows the last 10 lines of file1

77
Q

What does filename=$(basename “$file” .txt) accomplish?

A

removes the .txt extension from the filename and stores the result in the variable filename

78
Q

what does mkdir -p /path/to/directory

A

If /path/to/ does not exist, it will create both path, to, and directory. If directory already exists, it won’t raise an error.

79
Q

When calling a variable in your bash script that has already been initialized, what 2 things must you always include? Lets call the variable filename

A

for “$filename” in …..

80
Q

You must always do what 2 things when writing an if/elif/else statement in bash?

A

Use []; if [ “$file” -gt 6];

81
Q

What does ./mean < “$input_file” > “$output_file” do?

A

executes the mean program, taking input from input_file and saving the output to output_file

82
Q

what does echo “hello” | tr “aeiou” “12345”

A

translates the vowels a, e, i, o, u in the string “hello” into the numbers 1, 2, 3, 4, and 5. The output would be h2ll4

83
Q

what is an example of a file you are looking for when you say grep -r “multiply(.,.,.*)”

A

multiply(1, 2, 3)

84
Q

what would this function be looking for? grep “multiply(.,.,.*)” math
And what we be in the contents?

A

example_math

Contents:
multiply(1,2,3)

85
Q

What does ‘s/[0-9]kg,/&&/g’ do?

A

finds occurrences of a digit followed by “kg,” and a comma in a text and replaces them with the same matched content

86
Q

What happens if you run multiple commands with the > operator sequentially on the same file?

A

The contents of the file will be overwritten by the output of the last command executed.

87
Q

What does the command command 2> error_log do?

A

runs command and redirects any error messages (stderr) to a file named error_log

88
Q

How can you redirect both standard output and standard error to a single file?

A

command > output_file 2>&1

89
Q

what does this function do, ls | tee directory.txt | sort

A

Lists the contents of the current directory, saves that list to a file called directory.txt, and displays the sorted output in the terminal.

90
Q

what does, cat file | command do?

A

reads the contents of file and pipes that content into command for further processing

91
Q

How does the uniq command work?

A

uniq filters out repeated lines in a sorted file, displaying only unique lines or the number of occurrences.

92
Q

what will the results of this input be? seq 2 2 20

A

2
4
6
8
10
12
14
16
18
20

93
Q

What is the purpose of the awk function?

A

It helps you look at specific sets of data and do things with that information. You can tell it what to find and what to do with it.

94
Q

If your file contains:
Alice 88
Bob 92
Charlie 67
Diana 85
Eve 95

What will this function produce?

awk ‘$2 > 90 { print $1 }’ grades.txt

A

Bob
Eve

95
Q

If your file contains:
Alice 88
Bob 92
Charlie 67
Diana 85
Eve 95

What will this prompt produce?

awk ‘{
if ($2 > max) {
max = $2;
name = $1;
}
} END { print “Top scorer:”, name, “with a score of”, max }’ grades.txt

A

Top scorer: Eve with a score of 95

96
Q

what does this while loop do?

count=1
while [ $count -le 5 ]; do
echo “Count $count”
((count++))
done

A

Count 1
Count 2
Count 3
Count 4
Count 5

97
Q

What does the command wc file.txt do?

A

counts the number of lines, words, and characters in the file file.txt, displaying the results in three columns:

98
Q

What does the command basename /home/user/documents/report.txt return?

A

report.txt

99
Q

what does chmod -R do?

A

Apply the permission change recursively to all the files and directories within the specified directory.

100
Q

what does chmod -c do?

A

It will display a message for each file that is processed. while indicating the permission change that was made.

101
Q

what does chmod -f do?

A

It helps in avoiding display of error messages.

102
Q

what does chmod -h do?

A

Change the permissions of symbolic links instead of the files they point to

103
Q

What does the -c option do in tar?

A

It creates an archive by bundling files and directories together.

104
Q

What does the -x option do in tar?

A

It extracts files and directories from an existing archive.

105
Q

What does the -f option do in tar

A

It specifies the filename of the archive to be created or extracted.

106
Q

What does the -v option do in tar?

A

It shows extra details about what’s happening while files are being added to or taken out of the archive

107
Q

What does the -r option do in tar?

A

It updates or adds files or directories to an already existing archive without recreating the entire archive.

108
Q

what numbers represent the rwx commands?

A

A 1 gives execute permission, a 2
gives write permission, and a 4
gives read permission

109
Q

what does the chown command do?

A

Change owner of file/dir
(admin only)

110
Q

what does chgrp do?

A

Change group of a file/dir

111
Q

What’s an ACL?

A

ACLs provide more granular control over file access than traditional permissions, but may not be necessary for all systems.

112
Q
A