df
you get "command not found". What could be wrong and how to fix it?
Most likely the default/generated $PATH was somehow modified or overridden thus not containing /bin/
where df would normally go.
This issue could also happen if bash_profile or any configuration file of your interpreter was wrongly modified, causing erratics behaviours.
You would solve this by fixing your $PATH variable:
As to fix it there are several options:
1. Manually adding what you need to your $PATH PATH="$PATH":/user/bin:/..etc
2. You have your weird env variables backed up.
3. You would look for your distro default $PATH variable, copy paste using method #1
Note: There are many ways of getting errors like this: if bash_profile or any configuration file of your interpreter was wrongly modified; causing erratics behaviours,
permissions issues, bad compiled software (if you compiled it by yourself)... there is no answer that will be true 100% of the time.
cron
and at
.
With cron, tasks are scheduled using the following format:
*/30 * * * * bash myscript.sh
Executes the script every 30 minutes.
crontab -e
Alternatively if you are using a distro with systemd it's recommended to use systemd timers.
yippiekaiyay 1>&2 die_hard
/
?777 - You give the owner, group and other: Execute (1), Write (2) and Read (4); 4+2+1 = 7. 644 - Owner has Read (4), Write (2), 4+2 = 6; Group and Other have Read (4). 750 - Owner has x+r+w, Group has Read (4) and Execute (1); 4+1 = 5. Other have no permissions.
chmod +x some_file
chmod -x $(which chmod)
. How to fix it?journalctl
dstat -t
is great for identifying network and disk issues.
netstat -tnlaup
can be used to see which processes are running on which ports.
lsof -i -P
can be used for the same purpose as netstat.
ngrep -d any metafilter
for matching regex against payloads of packets.
tcpdump
for capturing packets
wireshark
same concept as tcpdump but with GUI (optional).
dstat -t
is great for identifying network and disk issues.
opensnoop
can be used to see which files are being opened on the system (in real time).
strace
is great for understanding what your program does. It prints every system call your program executed.
top
will show you how much CPU percentage each process consumes
perf
is a great choice for sampling profiler and in general, figuring out what your CPU cycles are "wasted" on
flamegraphs
is great for CPU consumption visualization (http://www.brendangregg.com/flamegraphs.html)
~/.ssh/known_hosts
?ssh-keygen
is used for?ls [XYZ]
matchls [^XYZ]
matchls [0-5]
matchgrep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' some_file
* grep -E "error|failure" some_file
* grep '[0-9]$' some_file
mount
command but you get no output. How would you check what mounts you have on your system?top
and free
mem()
{
ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $@ | awk '{printf $1/1024 "MB"; $1=""; print }'
}
[Source](https://stackoverflow.com/questions/3853655/in-linux-how-to-tell-how-much-memory-processes-are-using)
The default signal is SIGTERM (15). This signal kills process gracefully which means it allows it to save current state configuration.
kill 0
does?kill -0
does?Running (R) Uninterruptible Sleep (D) - The process is waiting for I/O Interruptible Sleep (S) Stopped (T) Dead (x) Zombie (z)
strace
does? What about ltrace
?split
command this way: split -l 25 some_file
/etc/resolv.conf
is used for? What does it include?dig
* host
* nslookup
systemctl enable [service_name]
System V: update-rc.d [service_name]
and add this line id:5678:respawn:/bin/sh /path/to/app
to /etc/inittab
Upstart: add Upstart init script at /etc/init/service.conf
ssh 127.0.0.1
but it fails with "connection refused". What could be the problem?awk
command does? Have you used it? What for?lsof
command does? Have you used it? What for?ls -l
?ls -l *.log
?alias x=y
does?/my/file
and 5 is the file descriptor number.
ip a
you see there is a device called 'lo'. What is it and why do we need it?traceroute
command does? How does it works?201.7.19.90 - - [05/Jun/1985:13:42:99 +0000] "GET /site HTTP/1.1" 200 32421
access
, search
insert
and remove
for the following data structures:[] is not []
? explain the resultTrue-True
?lambda
expression is an 'anonymous' function, the difference from a normal defined function using the keyword `def`` is the syntax and usage.
The syntax is:
```lambda[parameters]: [expresion]```
**Examples:**
* A lambda function add 10 with any argument passed.
```py
x = lambda a: a + 10
print(x(10))
```
* An addition function
```py
addition = lambda x, y: x + y
print(addition(10, 20))
```
* Squaring function
```py
square = lambda x : x ** 2
print(square(5))
```
Generally it is considered a bad practice under PEP 8 to assign a lambda expresion, they are meant to be used as parameters and inside of other defined functions.
x = [1, 2, 3]
[['1', '2', '3'], ['4', '5', '6']]
# Note: :list and -> bool are just python typings, they are not needed for the correct execution of the algorithm.
Taking advantage of sets and len:
```
def is_unique(l:list) -> bool:
return len(set(l)) == len(l)
```
This one is can be seen used in other programming languages.
```
def is_unique2(l:list) -> bool:
seen = []
for i in l:
if i in seen:
return False
seen.append(i)
return True
```
Here we just count and make sure every element is just repeated once.
```
def is_unique3(l:list) -> bool:
for i in l:
if l.count(i) > 1:
return False
return True
```
This one might look more convulated but hey, one liners.
```
def is_unique4(l:list) -> bool:
return all(map(lambda x: l.count(x) < 2, l))
```
[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}]
Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'}{'a': {'b': {'c': 1}}}
/dir1/dir2/file1
print the file name (file1)/dir1/dir2/file1
1. Print the path without the file name (/dir1/dir2)
2. Print the name of the directory where the file resides (dir2)
/home
and luig
will result in /home/luigi
x = "pizza"
, what would be the result of x[::-1]
?"".join(["a", "h", "m", "a", "h", "a", "n", "q", "r", "l", "o", "i", "f", "o", "o"])[2::3]
yeild
? When would you use it?[['Mario', 90], ['Geralt', 82], ['Gordon', 88]]
How to sort the list by the numbers in the nested lists?return
returns?", line 2>)
2 LOAD_CONST 1 ('a')
4 MAKE_FUNCTION 0
6 STORE_NAME 0 (a)
5 8 LOAD_CONST 2 (", line 5>)
10 LOAD_CONST 3 ('b')
12 MAKE_FUNCTION 0
14 STORE_NAME 1 (b)
16 LOAD_CONST 4 (None)
18 RETURN_VALUE
Disassembly of ", line 2>:
3 0 LOAD_CONST 0 (None)
2 RETURN_VALUE
Disassembly of ", line 5>:
6 0 LOAD_CONST 0 (None)
2 RETURN_VALUE
```
An empty return
is exactly the same as return None
and functions without any explicit return
will always return None regardless of the operations, therefore
```
def sum(a, b):
global c
c = a + b
>>> None
```
assert
does in Python?assert
in non-test/production code?x = [1, 2, 3]
, what is the result of list(zip(x))?a.num2
assuming the following code
```
class B:
def __get__(self, obj, objtype=None):
reuturn 10
class A:
num1 = 2
num2 = Five()
```
some_car = Car("Red", 4)
assuming the following code
```
class Print:
def __get__(self, obj, objtype=None):
value = obj._color
print("Color was set to {}".format(valie))
return value
def __set__(self, obj, value):
print("The color of the car is {}".format(value))
obj._color = value
class Car:
color = Print()
def __ini__(self, color, age):
self.color = color
self.age = age
```
f = function(function_1())
f = function(function_1(function_2(*args)))
every time, that's what decorators do, they introduce syntax to write all of this on the go, using the keyword '@'.
These two decorators (ntimes and timer) are usually used to display decorators functionalities, you can find them in lots of
tutorials/reviews. I first saw these examples two years ago in pyData 2017. https://www.youtube.com/watch?v=7lmCu8wz8ro&t=3731s
```
Simple decorator:
def deco(f):
print(f"Hi I am the {f.__name__}() function!")
return f
@deco
def hello_world():
return "Hi, I'm in!"
a = hello_world()
print(a)
>>> Hi I am the hello_world() function!
Hi, I'm in!
```
This is the simplest decorator version, it basically saves us from writting a = deco(hello_world())
.
But at this point we can only control the before execution, let's take on the after:
```
def deco(f):
def wrapper(*args, **kwargs):
print("Rick Sanchez!")
func = f(*args, **kwargs)
print("I'm in!")
return func
return wrapper
@deco
def f(word):
print(word)
a = f("************")
>>> Rick Sanchez!
************
I'm in!
```
deco receives a function -> f
wrapper receives the arguments -> *args, **kwargs
wrapper returns the function plus the arguments -> f(*args, **kwargs)
deco returns wrapper.
As you can see we conveniently do things before and after the execution of a given function.
For example, we could write a decorator that calculates the execution time of a function.
```
import time
def deco(f):
def wrapper(*args, **kwargs):
before = time.time()
func = f(*args, **kwargs)
after = time.time()
print(after-before)
return func
return wrapper
@deco
def f():
time.sleep(2)
print("************")
a = f()
>>> 2.0008859634399414
```
Or create a decorator that executes a function n times.
```
def n_times(n):
def wrapper(f):
def inner(*args, **kwargs):
for _ in range(n):
func = f(*args, **kwargs)
return func
return inner
return wrapper
@n_times(4)
def f():
print("************")
a = f()
>>>************
************
************
************
```
tail
command in Python? Bonus: implement head
as wellvar x int = 2
and x := 2
?var x int = 2
we are setting the variable type to integer while with x := 2
we are letting Go figure out by itself the type.
y = string(x)
with y = strconv.Itoa(x)
const initializer x + y is not a constant
1 [5 5 5 5]
1 [5 5 5 5]
2 [5 5 5 5 5]
2 [1 2 3 4]
In `mod1` a is link, and when we're using `a[i]`, we're changing `s1` value to.
But in `mod2`, `append` creats new slice, and we're changing only `a` value, not `s2`.
[Aritcle about arrays](https://golangbot.com/arrays-and-slices/),
[Blog post about `append`](https://blog.golang.org/slices)
db.books.find({"name": /abc/})
db.books.find().sort({x:1})
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'{ "name": "John Doe" }'
someMultiLineString: |
to someMultiLineString: >
?