Bash & Shell Flashcards

1
Q

Unix can be divided in … parts. What are they?

A

3 parts:

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

What happens when you type the command “rm file.txt”?

A

Shell is going to search for the program “rm”, check if the syntax is correct and send a ‘msg’ to kernel that could be translated to “execute program rm on file.txt”. Kernel is then going to allocate time and memory for the process and will execute it.

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

We can say everything in UNIX is a …. or a …..

A

A process or a file

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

Command to list content of present directory with common flags.

A

ls

flags:

  • a: includes hidden content
  • l: shows info of content
  • h: format sizes to ‘human-readable’ when using with l
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Command to create a directory

A

mkdir dir_name

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

Command to change directories with common usage

A

cd

some uses:
cd .. (goes back one)

cd ~ or just cd: goes to home directory

cd path: goes to path

cd - : goes to previous directory

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

Command to show full path

A

pwd

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

Command to copy a file

A

cp file1 file2

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

Commando to rename (or move) file

A

rename: mv file1 file2
move: mv oldPath newPath

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

Command to remove file

A

rm file

flags:

  • r: do it recursively (on a dir)
  • f: force removal (used so we’re not asked for every file inside the dir we want to remove)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Command to remove an empty directory

A

rmdir

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

Command(s) to show contents of a file.

A

cat file: prints the entire file on screen

less file: allows for a page-view of file content

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

Commands to show the first or last few lines of file

A

first: HEAD -n
last: TAIL -n

Where n is the number of lines. Default is 10

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

Command to search a file for key-words

A

grep ‘keyword’ file

flags:
-v: return non-matching lines

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

Command to count lines/words/chars of a file

A

wc file

flags:

  • l: lines
  • w: words
  • c: chars
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to redirect an output of a command to a file?

A

Command > file

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

What’s the problem of redirecting output ( > ) to a file?

A

When there’s a ( > ) on the entire command, the file receiving the output is TRUNCATED, that is, has all of it’s content erased, becoming a 0byte file.

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

What’s redirected to a file when using the ‘short syntax’ command > file and how to correctly use it?

A

When not specified, the redirection only handles the STDOUT (1) to the file. If an error happens, the STDERR (2) is not redirected, and thus is displayed on screen..

To fix that, we do:
command 1> file
or
command2>file

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

Where’s the common place to redirect output we want ‘gone’?

A

/dev/null

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

What does the ‘touch file’ command do

A

If the file doesnt exist, it creates it.

If the file exists, it just updates it’s “last modified” time

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

How to append the output of a command to a file?

A

command&raquo_space; file

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

How to create a file with some content already in it?

A

cat > file

And then start to type the content, hitting enter to jump lines and ctrl+d to exit and save it.

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

How to append some text to a file?

A

cat&raquo_space; file

And then start to type the content, hitting enter to jump lines and ctrl+d to exit and save it.

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

Command to concatenate files?

A

cat file1 file2 > file3

Warning: be aware of how a file is truncated when using the ( > )

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

How to sort a file?

Is it possible to sort inlace?

A

how to sort: sort file

To do it in place, pass in the -o flag fallowed by the name of the file (w/ extension), otherwise the content sorted will be displayed on screen.

It’s possible to do

sort file > newfile

But beaware that if you do

sort file > file

Your file will be truncated (erased) and nothing will be sorted.

26
Q

Command to see who is currently logged in

A

who

27
Q

Command to see information about the user

A

whoami

28
Q

Command to archive a file/directory with common flags.

A

tar -cv%f filename.tar file/dir

flags:
-c or -x: compress or extract

  • v: do it verbose-ly
  • %: nothing, z for gzip compression or j for bzip2 compression
  • f name of the tar file
29
Q

Command to create ‘shortcuts’ of commands.

Does it stay after exiting terminal? If not, how to do it?

A

alias shortcut=command

Be aware that no space should be used before/after the equal sign.

It does NOT stay after exiting. You can save it by appending the line to your .bashrc file or, better yet, creating or altering an already existing file .bashaliases and appending to it. Just make sure your .bashrc file sources it.

30
Q

How to alter file permissions?

A

chmod [0-7][0-7][0-7] file

The 3 numbers represent, in order: user, group and others permissions.

4 = read
2 = write
1 = execute

You can do like chmod u+x file as well but thats just harder to remember

31
Q

How to run a command in the background?

A

Add ‘&’ to the end of it.

command &

32
Q

How to kill a process running in the foreground?

A

ctrl + c

33
Q

How to suspend a process running in the foreground?

A

ctrl + z

34
Q

How to send a process to the background?

A

bg %n

Where n is the PROCESS NUMBER of it

35
Q

How to list current processes of terminal?

A

jobs

36
Q

How to send a process to the foreground?

A

fg %n

Where n is the PROCESS NUMBER of it

37
Q

How to kill a process?

What’s the default signal?

Give one other signal as an example.

A

kill %n
when n is the process number

or

kill PID

The default signal is -15, SIGNTERM. You can also use -9 (SIGNKILL) to terminate a process that refuses to quit on -15

38
Q

How to list the prime factors of a number?

A

factor n

39
Q

How to open a file/directory?

A

xdg-open file

40
Q

How to open the manual of a command?

A

man comman

41
Q

How to search commands for a keyword?

A

apropos keyword

Searches the header of each manual

42
Q

How to list your command history?

A

history

43
Q

How to redirect the output of a command as an input of another?

A

Use the pipe operator

com1 | com2

44
Q

How to execute a specific command from your history

A

! + commandNUM

Ex:
!233

45
Q

Command to allow you to make HTTP requests?

A

curl

It has A LOT of options and flags, take a look at them.

46
Q

How to ping an IP address?

A

ping ip_num

47
Q

How to make your terminal inactive for n seconds?

A

sleep n

48
Q

What’s the wildcard used to substitute any number of chars?

A

*

Example:
rm *.txt

removes every .txt file in the pwd

49
Q

What’s the wildcard used to substitute a single char?

A

?

Example:
rm index.?s

removes every file that has that structure. If you have index.js and index.ts, it’s going to remove both.

50
Q

What should be the first line of a .sh file?

A

/bin/bash

51
Q

What do you do when you want to execute a newly created .sh file?

And how to execute it?

A

chmod 744 file.sh

And then run with ./file.sh

52
Q

Describe the basics of a sh script

A

Every teminal command typed in the script is executed as such, so you can cd to a directory, perform operations, check if something is valid, if a file exists, alter a file… basically everything you can do with the terminal, but in a automated manner.

53
Q

How to define a variable in .sh?

A

var_name=somehting

Be aware! There can be no spaces before/after the equal sign.

54
Q

How to get a variable from user input?

A

read -p “Whats your age? “ AGE

55
Q

What can you use to bash understand a line break?

A

Use the ; at the end.

Example:
echo “Hey”; sleep 2;

Is equivalent to:
echo “Hey”
sleep 2

56
Q

Describe the if/else structure

A
if [ something ]; then
    something
elif [ something ]; then
    something
else
    somehting
fi

Attention to the spaces after each bracket and to the ‘fi’ at the end

57
Q

What’s the difference between verbose comparison commands like ‘-eq’ and ‘==’?

A

The verbose version is used when dealing with numbers, while the ‘==’ can be used to check text equality.

If you use [[ if ]], a more powerful BASH EXCLUSIVE tool, you can use ‘==’ to check numbers, strings, etc.

58
Q

What’s the difference between [] and [[]]?

A

[] is a command in shell, and as such you can’t use operators like ‘&&’ and ‘!’, since you have to pass flags (like -lt) to test what you want.

[[]] Is a bash exclusive interpretation, and handles operators inside of it. And more!

59
Q

What’s the for loop structure in sh?

A

for i in *
do
something
done

60
Q

What’s the switch/case structure in sh?

A
case $var in
    para1) cmd1; cmd2;;
    para2) cmd3; cmd4;;
        *) default command;;
esac
61
Q

How do you build and execute a function in sh?

A

myfunc() {
cmd1

}

myfunc // simply calling it’s name executes it