Bash Commands Flashcards

(114 cards)

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
pwd
print working directory
25
cd ~
changes to home directory
26
whoami
returns your username
27
sudo command
execute command as the root user
28
lsof
lists all open files
29
varname=value
defines a variable
30
echo $varname
checks a variables value
31
read -p "Please enter your name: " name
prompts the user for input with a specified message and stores the entered value in a variable
32
export -f 'f1'
export function called f1
33
How do you define an array
array[0]=valA
34
How do you display the array's value for a certain index
${array[i]}
35
what do you put at the beginning of your bash script
#!/bin/bash
36
what are some arguments for chmod
--recursive - changes files and directories recursively --changes - report when a change is made --silent - suppress most error messages
37
what does apropos keyword mean?
Its when you don't know the command name but roughly know what you want to do.
38
man 3 printf
this will open up the third man page of the prinf command
39
what does rm file* do?
removes files 1 to infinity that have digits after the filename. Ex: file1 file2
40
rm file?
removes files with one character after filename. files{0-9}
41
*(abc)
would match "", "abc", "abcabc", "abcabcabc", etc.
42
+(abc)
would match "abc", "abcabc", "abcabcabc", but not an empty string ""
42
?(abc)
would match "" or "abc".
43
@(abc|def)
would match "abc" or "def", but not both or neither
44
!(abc)
would match any string that is not "abc"
45
what is the AND operator and the OR operator
&& - AND || - OR
46
str1 == str2
the strings match each other
47
what are the terms for less than, greater than or equal to, not equal, etc..
-lt # less than -le # less than or equal -eq # equal -ge # greater than or equal -gt # greater than -ne # not equal
48
what is the criteria in starting and stopping a for loop
do and done
49
what are the requirements in an if statement
if condition then statements [elif condition then statements...] [else statements] fi
50
how do you do an incremental for loop in bash
for (( initialisation ; ending condition ; update ))
51
what does the pipe function do for this example cmd1 | cmd2
This takes the results from cmd1 and sends them to cmd2
52
what happens here? ls | grep ".txt"
ls lists all the files, and grep ".txt" looks through that list to find only the text files.
53
history
show command history
54
source /file
runs every command one line at a time
55
!!
repeats the last command
56
echo $SHELL
displays the shell you are using
57
echo $PATH
shows the entire path you are in
58
whereis bash
shows location, source code and path
59
ls
list files in directory
60
env
displays environment variables
61
what outputs when ls -l is printed to the console
displays type of file type of permission access ACL flag Links Owner Group Size Date and Time Filename
62
ls - a
displays hidden files
63
ln -s 'filename' 'link'
creates link to a file
64
readlink 'filename'
shows where the link points to
65
tree
shows directories in the tree format
66
touch file
creates or updates your file
67
cat file
displays raw contents of file
68
cat -n file
shows numbers of lines
69
nl file
shows number of lines in file
70
cat f1 > f2
copy f1 to f2
71
cat f1 >> f2
merge 2 file texts together
72
echo "New entry" > file2
directs and overwrites New entry to file2
73
file1 >> file2
directions and appends contents of file1 to file2
74
more file1
shows the first portion of the file1
75
head file1
shows the first 10 lines of file1
76
tail file1
shows the last 10 lines of file1
77
What does filename=$(basename "$file" .txt) accomplish?
removes the .txt extension from the filename and stores the result in the variable filename
78
what does mkdir -p /path/to/directory
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
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
for "$filename" in .....
80
You must always do what 2 things when writing an if/elif/else statement in bash?
Use []; if [ "$file" -gt 6];
81
What does ./mean < "$input_file" > "$output_file" do?
executes the mean program, taking input from input_file and saving the output to output_file
82
what does echo "hello" | tr "aeiou" "12345"
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
what is an example of a file you are looking for when you say grep -r “multiply\(.*,.*,.*\)”
multiply(1, 2, 3)
84
what would this function be looking for? grep “multiply\(.*,.*,.*\)” *math* And what we be in the contents?
example_math Contents: multiply(1,2,3)
85
What does ‘s/[0-9]kg,/&&/g’ do?
finds occurrences of a digit followed by "kg," and a comma in a text and replaces them with the same matched content
86
What happens if you run multiple commands with the > operator sequentially on the same file?
The contents of the file will be overwritten by the output of the last command executed.
87
What does the command command 2> error_log do?
runs command and redirects any error messages (stderr) to a file named error_log
88
How can you redirect both standard output and standard error to a single file?
command > output_file 2>&1
89
what does this function do, ls | tee directory.txt | sort
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
what does, cat file | command do?
reads the contents of file and pipes that content into command for further processing
91
How does the uniq command work?
uniq filters out repeated lines in a sorted file, displaying only unique lines or the number of occurrences.
92
what will the results of this input be? seq 2 2 20
2 4 6 8 10 12 14 16 18 20
93
What is the purpose of the awk function?
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
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
Bob Eve
95
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
Top scorer: Eve with a score of 95
96
what does this while loop do? count=1 while [ $count -le 5 ]; do echo "Count $count" ((count++)) done
Count 1 Count 2 Count 3 Count 4 Count 5
97
What does the command wc file.txt do?
counts the number of lines, words, and characters in the file file.txt, displaying the results in three columns:
98
What does the command basename /home/user/documents/report.txt return?
report.txt
99
what does chmod -R do?
Apply the permission change recursively to all the files and directories within the specified directory.
100
what does chmod -c do?
It will display a message for each file that is processed. while indicating the permission change that was made.
101
what does chmod -f do?
It helps in avoiding display of error messages.
102
what does chmod -h do?
Change the permissions of symbolic links instead of the files they point to
103
What does the -c option do in tar?
It creates an archive by bundling files and directories together.
104
What does the -x option do in tar?
It extracts files and directories from an existing archive.
105
What does the -f option do in tar
It specifies the filename of the archive to be created or extracted.
106
What does the -v option do in tar?
It shows extra details about what's happening while files are being added to or taken out of the archive
107
What does the -r option do in tar?
It updates or adds files or directories to an already existing archive without recreating the entire archive.
108
what numbers represent the rwx commands?
A 1 gives execute permission, a 2 gives write permission, and a 4 gives read permission
109
what does the chown command do?
Change owner of file/dir (admin only)
110
what does chgrp do?
Change group of a file/dir
111
What's an ACL?
ACLs provide more granular control over file access than traditional permissions, but may not be necessary for all systems.
112