Bash & Shell Flashcards

(61 cards)

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
How to sort a file? Is it possible to sort inlace?
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
Command to see who is currently logged in
who
27
Command to see information about the user
whoami
28
Command to archive a file/directory with common flags.
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
Command to create 'shortcuts' of commands. Does it stay after exiting terminal? If not, how to do it?
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
How to alter file permissions?
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
How to run a command in the background?
Add '&' to the end of it. command &
32
How to kill a process running in the foreground?
ctrl + c
33
How to suspend a process running in the foreground?
ctrl + z
34
How to send a process to the background?
bg %n Where n is the PROCESS NUMBER of it
35
How to list current processes of terminal?
jobs
36
How to send a process to the foreground?
fg %n Where n is the PROCESS NUMBER of it
37
How to kill a process? What's the default signal? Give one other signal as an example.
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
How to list the prime factors of a number?
factor n
39
How to open a file/directory?
xdg-open file
40
How to open the manual of a command?
man comman
41
How to search commands for a keyword?
apropos keyword Searches the header of each manual
42
How to list your command history?
history
43
How to redirect the output of a command as an input of another?
Use the pipe operator com1 | com2
44
How to execute a specific command from your history
! + commandNUM Ex: !233
45
Command to allow you to make HTTP requests?
curl It has A LOT of options and flags, take a look at them.
46
How to ping an IP address?
ping ip_num
47
How to make your terminal inactive for n seconds?
sleep n
48
What's the wildcard used to substitute any number of chars?
* Example: rm *.txt removes every .txt file in the pwd
49
What's the wildcard used to substitute a single char?
? 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
What should be the first line of a .sh file?
#/bin/bash
51
What do you do when you want to execute a newly created .sh file? And how to execute it?
chmod 744 file.sh And then run with ./file.sh
52
Describe the basics of a sh script
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
How to define a variable in .sh?
var_name=somehting Be aware! There can be no spaces before/after the equal sign.
54
How to get a variable from user input?
read -p "Whats your age? " AGE
55
What can you use to bash understand a line break?
Use the ; at the end. Example: echo "Hey"; sleep 2; Is equivalent to: echo "Hey" sleep 2
56
Describe the if/else structure
``` 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
What's the difference between verbose comparison commands like '-eq' and '=='?
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
What's the difference between [] and [[]]?
[] 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
What's the for loop structure in sh?
for i in * do something done
60
What's the switch/case structure in sh?
``` case $var in para1) cmd1; cmd2;; para2) cmd3; cmd4;; *) default command;; esac ```
61
How do you build and execute a function in sh?
myfunc() { cmd1 ... } myfunc // simply calling it's name executes it