#Review: Basic Linux commands
mkdir mynewdir
cd mynewdir/
/mynewdir$ pwd
/mynewdir$ cp ../spider.txt .
/mynewdir$ touch myfile.txt
/mynewdir$ ls -l
#Output:
#-rw-rw-r-- 1 user user 0 Mai 22 14:22 myfile.txt
#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt
/mynewdir$ ls -la
#Output:
#total 12
#drwxr-xr-x 2 user user 4096 Mai 22 14:17 .
#drwxr-xr-x 56 user user 12288 Mai 22 14:17 ..
#-rw-rw-r-- 1 user user 0 Mai 22 14:22 myfile.txt
#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt
/mynewdir$ mv myfile.txt emptyfile.txt
/mynewdir$ cp spider.txt yetanotherfile.txt
/mynewdir$ ls -l
#Output:
#total 8
#-rw-rw-r-- 1 user user 0 Mai 22 14:22 emptyfile.txt
#-rw-rw-r-- 1 user user 192 Mai 22 14:18 spider.txt
#-rw-rw-r-- 1 user user 192 Mai 22 14:23 yetanotherfile.txt
/mynewdir$ rm *
/mynewdir$ ls -l
#total 0
/mynewdir$ cd ..
rmdir mynewdir/
ls mynewdir
#ls: cannot access 'mynewdir': No such file or directory
---------------------------------------------------------------
#Review: Redirecting streams
cat stdout_example.py
#!/usr/bin/env python3
print("Don't mind me, just a bit of text here...")
./stdout_example.py
#Output: Don't mind me, just a bit of text here...
./stdout_example.py > new_file.txt
cat new_file.txt
#Output: Don't mind me, just a bit of text here...
./stdout_example.py >> new_file.txt
cat new_file.txt
#Output: Don't mind me, just a bit of text here...
#Don't mind me, just a bit of text here...
cat streams_err.py
#!/usr/bin/env python3
data = input("This will come from STDIN: ")
print("Now we write it to STDOUT: " + data)
raise ValueError("Now we generate an error to STDERR")
./streams_err.py < new_file.txt
#This will come from STDIN: Now we write it to STDOUT: Don't mind #me, just a bit
of text here...
#Traceback (most recent call last):
#File "./streams_err.py", line 5, in <module>
#raise ValueError("Now we generate an error to STDERR")
#ValueError: Now we generate an error to STDERR
./streams_err.py < new_file.txt 2> error_file.txt
#This will come from STDIN: Now we write it to STDOUT: Don't mind #me, just a bit
of text here...
cat error_file.txt
#Traceback (most recent call last):
#File "./streams_err.py", line 5, in <module>
#raise ValueError("Now we generate an error to STDERR")
#ValueError: Now we generate an error to STDERR
echo "These are the contents of the file" > myamazingfile.txt
cat myamazingfile.txt
#These are the contents of the file
----------------------------------------------------------------------------
#Review: Pipes and pipelines
ls -l | less
#(... A list of files appears...)
cat spider.txt | tr ' ' '\n' | sort | uniq -c | sort -nr | head
# 7 the
# 3 up
# 3 spider
# 3 and
# 2 rain
# 2 itsy
# 2 climbed
# 2 came
# 2 bitsy
# 1 waterspout.
cat capitalize.py
#!/usr/bin/env python3
import sys
for line in sys.stdin:
print(line.strip().capitalize())
cat haiku.txt
#advance your career,
#automating with Python,
#it's so fun to learn.
cat haiku.txt | ./capitalize.py
#Advance your career,
#Automating with python,
#It's so fun to learn.
./capitalize.py < haiku.txt
#Advance your career,
#Automating with python,
#It's so fun to learn.
#Review: Signaling processes
-------------------------
ping www.example.com
#PING www.example.com(2606:2800:220:1:248:1893:25c8:1946
(2606:2800:220:1:248:1893:25c8:1946)) 56 data bytes
Press Control C:
--- www.example.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8013ms
rtt min/avg/max/mdev = 93.587/93.668/93.719/0.149 ms
ping www.example.com
#PING www.example.com(2606:2800:220:1:248:1893:25c8:1946
(2606:2800:220:1:248:1893:25c8:1946)) 56 data bytes
Press Control Z: the program stops.
fg
#ping www.example.com
#64 bytes from 2606:2800:220:1:248:1893:25c8:1946
(2606:2800:220:1:248:1893:25c8:1946): icmp_seq=5 ttl=51 time=93.6 ms
Press Control C:
--- www.example.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8013ms
rtt min/avg/max/mdev = 93.587/93.668/93.719/0.149 ms
#Redirections, Pipes, and Signals
Managing streams
These are the redirectors that we can use to take control of the streams of our
programs
command > file: redirects standard output, overwrites file
command >> file: redirects standard output, appends to file
command < file: redirects standard input from file
command 2> file: redirects standard error to file
command1 | command2: connects the output of command1 to the input of command2
---------------------------------------
Operating with processes
These are some commands that are useful to know in Linux when interacting with
processes. Not all of them are explained in videos, so feel free to investigate
them on your own.
ps: lists the processes executing in the current terminal for the current user
ps ax: lists all processes currently executing for all users
ps e: shows the environment for the processes listed
kill PID: sends the SIGTERM signal to the process identified by PID
fg: causes a job that was stopped or in the background to return to the
foreground
bg: causes a job that was stopped to go to the background
jobs: lists the jobs currently running or stopped
top: shows the processes currently using the most CPU time (press "q" to quit)