Informatics1-2017/Practice2

A MathWikiből
(Változatok közti eltérés)
(Linux permissions)
(Bash)
72. sor: 72. sor:
  
 
== Bash ==
 
== Bash ==
Néhány hasznos link:
+
Some useful links:
 
* https://www.codecademy.com/learn/learn-the-command-line
 
* https://www.codecademy.com/learn/learn-the-command-line
 
* http://ss64.com/bash/
 
* http://ss64.com/bash/
  
=== Alapvető parancsok ===
+
=== Basic commands ===
* '''pwd''': aktuális könyvtár kiíratása.
+
* '''pwd''': path to the current folder.
* '''cd''': könyvtárváltás. Ha paraméter nélkül hívjuk meg, akkor a saját mappánkba visz. Pl '''cd /mnt''',  '''cd''', '''cd ..'''
+
* '''cd''': change directory
* '''ls''': könyvtár tartalmának listázása. Pl: '''ls''', ''ls -h'', '''ls /dev''', '''ls -h -l ~'''
+
* '''ls''': list the contents of the directory: '''ls''', ''ls -h'', '''ls /dev''', '''ls -h -l ~'''
* '''mkdir''': könyvtár létrehozása. '''mkdir NewFolder'''
+
* '''mkdir''': create folder: '''mkdir NewFolder'''
* '''cp''': másolás. '''cp mit.txt hova.txt'''
+
* '''cp''': copy: '''cp what.txt where.txt'''
* '''mv''': mozgatás/átnevezés (az eredeti nem marad meg). '''mv mit.txt hova.txt'''
+
* '''mv''': move: '''mv what.txt where.txt'''
* '''rm''': törlés, Pl: '''rm alma.txt''', rekurzívan töröl minent a mappával együtt: '''rm -r NewFolder'''
+
* '''rm''': remove: '''rm alma.txt''', with recursion (delete a whole folder): '''rm -r NewFolder'''
* '''quota''': A felhasznált/megmaradt helyet írja ki. Ha a kvótánk betelik, akkor nem kapjuk meg a nekünk címzett leveleket és a grafikus terminálra sem tudunk belépni; ilyenkor a parancssoros terminálra belépve tudjuk kitakarítani a home-unkat.
+
* '''quota''': Shows the currently used storage space and our maximum allowed storage. If this fills up, we won't be able to login to the graphical linux screen. But we can still login to the command line linux screen to clean out our home folder.
* '''df''', '''du''': lemezen szabad / felhasznált helyet írja ki. A '''-h''' kapcsolóval olvasható ''GB'', ''MB'', ''kB'' értékeket kapunk, de lehet fix byte-okban is kiírni. Pl: '''df -h ~'''
+
* '''df''', '''du''': disk space used, with the '''-h''' parameter it will be made more readable: '''df -h ~'''
* '''mc''': midnight commander fájlböngésző.
+
* '''mc''': midnight commander explorer.
* segíts magadon
+
* help yourself
 
** '''info <parancs>'''
 
** '''info <parancs>'''
** '''man <parancs>''' az adott parancsról leírást ad. A leírásban navigálni a 'fel', illetve a 'le' billentyűkkel, továbbá a 'PageUp', 'PageDown'-nal lehet. Kereséshez '/' jel után kell beírni a kifejezést. Kilépés 'q'-val. '''man ls'''
+
** '''man <parancs>''' shows detailed help for the given command, navigation with the arrow keys, quit with '''q''' search with '''/''' : '''man ls'''
** '''<parancs> --help'''
+
** '''<command> --help'''
 
** '''help'''
 
** '''help'''
* '''history''': parancs történet lekérdezése.
+
* '''history''': history of issued commands.
* '''apropos''': parancs/program nevek keresése. ''apropos math''
+
* '''apropos''': search for commands: ''apropos math''
* '''top''': futó alkalmazások.
+
* '''top''': shows running applications.
* '''kill''', '''killall''', '''xkill''': folyamatok leállítása. Egy adott felhasználó összes folyamatának leállítása: '''killall -u username'''
+
* '''kill''', '''killall''', '''xkill''': stop the execution of a given or all programs, stopping all programs for a given user: '''killall -u username'''
* '''cat''': fájl tartalmának kiíratása. Pl: '''cat .forward'''
+
* '''cat''': prints the contents of the given file: '''cat .forward'''
* '''echo''': kiírja az argumentumait, Pl: '''echo Hello World'''. Figyelem, beugratós: '''echo --help'''
+
* '''echo''': prints the argument: '''echo Hello World'''
* '''grep''': szöveg mintázatok keresése állományokban. '''grep "tű" szénakazal.txt'''
+
* '''grep''': searching with regular expressions
* '''less''': szöveges állományok megjelenítése, könnyebb olvashatóság nagy fájloknál, Pl '''less valami.txt'''
+
* '''less''': a smarter form of cat, easier to navigate larger text files: '''less something.txt'''
* '''head''' (-n ), '''tail''' (-n ): fájl elejének és végének a kiiratása. '''head -n 100 valami.txt'''
+
* '''head''' (-n ), '''tail''' (-n ): print the beginning and the end of the given file: '''head -n 100 something.txt'''
* '''sort''' (-g -k): szövegállomány rendezése.
+
* '''sort''' (-g -k): sorting text.
* '''wc''' (-l): byte/karakterek/sorok számolás adott fájlra.  
+
* '''wc''' (-l): counting bytes/lines/characters.  
* '''ssh''': távoli gépre való bejelentkezést teszi lehetővé. [[Informatika1-2015/Gyakorlat1#Leibniz_el.C3.A9r.C3.A9se_otthoni_linuxr.C3.B3l|Linux-ról]] [[Informatika1-2015/Gyakorlat1#Leibniz_el.C3.A9r.C3.A9se_otthoni_windowsr.C3.B3l|Windows-ról]]
+
* '''ssh''': secure connection to a remote machine: ssh username@leibniz.math.bme.hu
* '''scp''': távoli gépre/gépről való másolás. [[Informatika1-2015/Gyakorlat1#Leibniz_el.C3.A9r.C3.A9se_otthoni_linuxr.C3.B3l|Linux-ról]] [[Informatika1-2015/Gyakorlat1#Leibniz_el.C3.A9r.C3.A9se_otthoni_windowsr.C3.B3l|Windows-ról]]
+
* '''scp''': secure copy from (or to) a remote machine scp from.txt username@leibniz.math.bme.hu:~/to.txt
* '''wget''': file-ok letöltése internetről, HTTP, HTTPS és FTP használatával.
+
* '''wget''': download files from the internet
* '''exit''': kilépés terminálból (vagy ctrl+d).
+
* '''exit''': exit the terminal (or ctrl+d).
* '''sudo''': programok futtatása superuserként. (Mat intézetben erre nem lesz lehetőségünk) Pl. '''sudo rm /home/someone_I_hate/important_file.txt'''
+
* '''sudo''': execute the command as a superuser: '''sudo rm /home/someone_I_hate/important_file.txt'''
* '''halt, reboot''': kikapcsol, újraindít (Mat intézetben erre sem lesz lehetőségünk)
+
* '''halt, reboot''': shuts down, reboots
  
 
=== Pipe, redirect ===
 
=== Pipe, redirect ===
Minden program írhat a konzolra, vagy az <tt>stderr</tt>-re, ez az, amit a konzolon szoktunk látni. Azonban ezeket a képernyő helyett fájlba is írhatjuk.
+
Every program (command) can print to the console, or to <tt>stderr</tt> this is what we see on the console. These can be redirected into a file:
  
 
<code>
 
<code>

A lap 2017. szeptember 11., 06:09-kori változata

Tartalomjegyzék

Linux permissions

  • Permission groups
    • superuser (su), pl. system administrators
    • owner, the owner of the given file, who created it, e.g. kkovacs
    • group, a group of users that the owner is a part of, e.g. student
    • others, everyone else
  • Permission types
    • w: Write, can write or read the file
    • r: Read, can read the file, or access the folder's contents
    • x: eXecute, can execute the file (script or program), or access the files / folders inside the folder
    • -: nothing

We can pick and choose from these types and groups, for example we can set the permissions for a file so that

  • the owner can read, write and execute the file
  • the owner's group can read and execute the file
  • everybody else can't do anything with the file

Superusers can always do everything with all files.

We can use alphabetic and numerical codes for these permissions:

  • u: user, owner
  • g: group
  • o: others
  • a: everyone: user, group and others.
  • read: r or 4
  • write: w or 2
  • execute: x or 1
  • nothing: - or 0

The symbol for a differs from o for example it is possible to give execution permissions to others, but deny it to my group.

In the alphabetic denotion, the permissions are usually represented by a 10 character long string:

1 2 3 4 5 6 7 8 9 10
type owner group others
read write execute read write execute read write execute
$ ls -l ~
drwxr-xr-x 8 borbely student 4096 Aug 30 23:24 Desktop
drwxr-xr-x 2 borbely student 4096 Mar 27  2012 Downloads
drwxr-xr-x 2 borbely student 4096 Oct 20  2009 Drives
drwx------ 2 borbely student 4096 Apr 20 10:42 mail
drwxr-xr-x 7 borbely student 4096 Sep  6 13:01 public_html
$ _

The owner of the directory public_html is: borbely, his group: student, the permission: drwxr-xr-x:

  • type d: directory
  • owner rwx, borbely can do anything with the folder
  • other students r-x, they can't write the file, but they can read or execute it
  • everyone else r-x, they can do the same as the student group

In a numeric form a 3 digit number represents the permissions. The first number from the left represents the permissions of the owner, then the group then others. The numbers 4: read, 2: write, 1: execute. The sum of these is the permission. E.g. /home/student/borbely/public_html permissions: 755:

  • owner (borbely) 7=4+2+1: read, write, execute
  • group (student) 5=4+1: read, execute
  • other 5=4+1: read, execute

chmod

Permissions for a file or folder can be set with the chmod command. Some examples:

  • chmod 700 ~/info_homework: my homework can only be seen by me (and superusers of course)
  • chmod 750 ~/important_work: this is some important work, I don't want others to erase or rewrite it, but my group is allowed to look into it
  • chmod 754 -R ~/public_html: the keyword -R makes the command recursive so that this permission applies to all files inside this directory

More details

Bash

Some useful links:

Basic commands

  • pwd: path to the current folder.
  • cd: change directory
  • ls: list the contents of the directory: ls, ls -h, ls /dev, ls -h -l ~
  • mkdir: create folder: mkdir NewFolder
  • cp: copy: cp what.txt where.txt
  • mv: move: mv what.txt where.txt
  • rm: remove: rm alma.txt, with recursion (delete a whole folder): rm -r NewFolder
  • quota: Shows the currently used storage space and our maximum allowed storage. If this fills up, we won't be able to login to the graphical linux screen. But we can still login to the command line linux screen to clean out our home folder.
  • df, du: disk space used, with the -h parameter it will be made more readable: df -h ~
  • mc: midnight commander explorer.
  • help yourself
    • info <parancs>
    • man <parancs> shows detailed help for the given command, navigation with the arrow keys, quit with q search with / : man ls
    • <command> --help
    • help
  • history: history of issued commands.
  • apropos: search for commands: apropos math
  • top: shows running applications.
  • kill, killall, xkill: stop the execution of a given or all programs, stopping all programs for a given user: killall -u username
  • cat: prints the contents of the given file: cat .forward
  • echo: prints the argument: echo Hello World
  • grep: searching with regular expressions
  • less: a smarter form of cat, easier to navigate larger text files: less something.txt
  • head (-n ), tail (-n ): print the beginning and the end of the given file: head -n 100 something.txt
  • sort (-g -k): sorting text.
  • wc (-l): counting bytes/lines/characters.
  • ssh: secure connection to a remote machine: ssh username@leibniz.math.bme.hu
  • scp: secure copy from (or to) a remote machine scp from.txt username@leibniz.math.bme.hu:~/to.txt
  • wget: download files from the internet
  • exit: exit the terminal (or ctrl+d).
  • sudo: execute the command as a superuser: sudo rm /home/someone_I_hate/important_file.txt
  • halt, reboot: shuts down, reboots

Pipe, redirect

Every program (command) can print to the console, or to stderr this is what we see on the console. These can be redirected into a file:

$ ls -l ~
drwxr-xr-x 8 borbely student 4096 Aug 30 23:24 Desktop
drwxr-xr-x 2 borbely student 4096 Mar 27 2012 Downloads
drwxr-xr-x 2 borbely student 4096 Oct 20 2009 Drives
drwx------ 2 borbely student 4096 Apr 20 10:42 mail
drwxr-xr-x 7 borbely student 4096 Sep 6 13:01 public_html
$ ls -l ~ > folder.txt
$ cat folders.txt
drwxr-xr-x 8 borbely student 4096 Aug 30 23:24 Desktop
drwxr-xr-x 2 borbely student 4096 Mar 27 2012 Downloads
drwxr-xr-x 2 borbely student 4096 Oct 20 2009 Drives
drwx------ 2 borbely student 4096 Apr 20 10:42 mail
drwxr-xr-x 7 borbely student 4096 Sep 6 13:01 public_html
$ _

A > jellel egy parancs kimenetét egy fájlba irányítjuk át, majd a fájlt kiírathatjuk a cat-el. Ugyanakkor figyeljük meg a következőt:

$ ls -l /home/algebra/wettl/ > myfile.txt
ls: cannot open directory /home/algebra/wettl/: Permission denied
$ cat myfile.txt
$ _

A myfile.txt üresen maradt, de a konzolon hibaüzenet jelent meg (nem volt jogunk a mappa listázására). Ez azért van, mert az utóbbi ls parancs nem a standard kimenetre, hanem a hiba-kimenetre, azaz stderr-re írt. Az stderr-t a 2> jelel irányíthatjuk át:

$ ls -l /home/algebra/wettl/ > myfile.txt 2> error.log
$ cat myfile.txt
$ cat error.log
ls: cannot open directory /home/algebra/wettl/: Permission denied
$ _

Így az egyik fájlba a standard kimenet (stdout), a másikban az stderr került bele.

Az egyes programok olvashatnak a standard bemenetükről: stdin. Az ú.n. nem-interaktív parancsok olyanok, hogy a parancs kiadása után a befejezéséig nem nyúlunk a billentyűzethez. Ezekkel csak az argumentumaikon keresztül kommunikálhatunk. Interaktív parancsok a futásuk közben olvasnak a standard bemenetükről: stdin

  • nem-interaktív parancs: grep "needle" haystack.txt
  • interaktív parancs: grep "needle"

Az előbbi esetben a grep egy megadott fájlban keresi a needle kifejezést. Az utóbbi esetben a beírt szövegben keresi ugyanazt.

$ grep "needle"
I hate this
why can't I find anything
why can't I find a needle
why can't I find a needle
oh, a needle
oh, a needle
again
$ _

A Ctrl+D billentyű-kombinációval megszakítjuk a futást. A needle-t tartalmazó sorok duplán vannak, mert azokat a grep kérésemre kiírta.

Próbáljuk ki így:

$ grep "needle" > needle.txt
I hate this
why can't I find anything
why can't I find a needle
oh, a needle
again
$ cat needle.txt
why can't I find a needle
oh, a needle
$ _

Ezzel kiválasztottuk a bemenetről a needle-t tartalmazó sorokat.

Az egyik parancs kimenete lehet a másik bemenete is, ez a pipe, jele egy függőleges vonal: |.

Példa: írjuk ki a home mappánk azon elemeit, melyek nevükben az info szövegrészletet tartalmazzák.
$ ls -1 ~
Desktop
Downloads
Drives
info1
info_hf.txt
mail
myfile.txt
myfolder
needle.txt
public_html
regi_info
regi_info_zh.txt
$ ls -1 ~ | grep "info"
info1
info_hf.txt
regi_info
regi_info_zh.txt
$ _

Példa: sok elem van egy mappában és apránként végig akarjuk nézni:

$ ls /home/student/ | wc -l
821
$ ls /home/student/ | less

Egy fájlból kivehetjük a különböző sorokat (rendezés és duplikált sorok törlése):

$ cat nevek.txt | sort | uniq > nevek.sorted.txt

Ha egy fájl tartalmát akarjuk pipe-olni egy parancsba, azt így is megtehetjük (<):

$ cat nevek.txt | sort
$ sort < nevek.txt

Ez kombinálható > -el.

$ sort < nevek.txt | uniq > nevek.sorted.txt

Feladatok

1)

  • Töltsük le a A1nevsor.txt állományt a borbely felhasználó oldaláról
    • wget
  • Távolítsuk el a neveket az első oszlopból.
    • cut
  • Rendezzük a sorokat Neptun kód szerint (a fejléc kivételével).
    • sort
  • Nagybetűsítsük a Neptun kódokat.
    • tr
  • Írjuk ki az eredményt egy külön fájlba (segédfájlokat használhatunk útközben).
    • cat, echo, paste, head, tail

Megoldás

wget math.bme.hu/~borbely/A1nevsor.txt
tail -n+2 A1nevsor.txt | cut -f2,3 -d" " | sort | tr [:lower:] [:upper:] > sorted.txt

Windows

A DOS után a Windows-os parancssori lehetőségek nem nagyon fejlődtek. A parancssor közelében sincs a Linux-nak, sem használhatóságban, sem funkcióiban, de azért van valami.

  • Parancssor indítása
    • Start menü -> Parancssor
    • Start menü -> Futtatás -> cmd
    • Keresés -> cmd
  • leírás
  • használható: help, help <parancs>
  • Windows 10-en új életre kelt a cmd.
  • A Windows 10 egy éves évfordulója óta beépített a bash (béta verzió).

Használatos a PowerShell, ami nem mindegyik verzión beépített, de ingyenes és elterjedt.

Különbségek

Pár szembetűnő különbség

  • a Windows case-insensitive, vagyis kis és nagybetű nem számít, ellenben a Linux case-sensitive.
  • A könytár-, fájlnevek:
    • C:\Windows\System32\
    • /usr/bin/
  • parancssori kapcsolók perrel versus kötőjellel:
    • Windows: dir /b
    • Unix: ls -l
  • script
    • Windows-on: .bat
    • Linux-on: .sh
  • pipe, redirect hasonló

Gyakran emulálják/helyettesítik a unix-os lehetőségeket windows-on:

Személyes eszközök