Informatics1-2017/Practice2

A MathWikiből
(Változatok közti eltérés)
(Feladatok)
(Windows)
239. sor: 239. sor:
  
 
== Windows ==
 
== Windows ==
A [https://hu.wikipedia.org/wiki/MS-DOS 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.
+
The terminal of windows [https://hu.wikipedia.org/wiki/MS-DOS DOS] didn't go through much improvement since its creation. It's far behind the linux shell.
* Parancssor indítása
+
* Starting the command prompt
** Start menü -> Parancssor
+
** Start menu -> Command prompt
** Start menü -> Futtatás -> cmd
+
** Start menü -> Run -> cmd
** Keresés -> cmd
+
** Search -> cmd
* [http://ss64.com/nt/ leírás]
+
* [http://ss64.com/nt/ description]
* használható: <code>help</code>, <code>help <parancs></code>
+
* useful: <code>help</code>, <code>help <command></code>
* Windows 10-en [http://withinrafael.com/new-experimental-console-features-in-windows-threshold/ új életre kelt a cmd].
+
* On Windows 10 [http://withinrafael.com/new-experimental-console-features-in-windows-threshold/ new features for the command prompt].
* A Windows 10 egy éves évfordulója óta [http://www.pcworld.com/article/3106463/windows/how-to-get-bash-on-windows-10-with-the-anniversary-update.html beépített a bash] (béta verzió).
+
* Since the one year anniversary of Windows 10 [http://www.pcworld.com/article/3106463/windows/how-to-get-bash-on-windows-10-with-the-anniversary-update.html bash is included].
  
Használatos a [https://msdn.microsoft.com/en-us/powershell PowerShell], ami nem mindegyik verzión beépített, de ingyenes és elterjedt.
+
Another usable windows shell: [https://msdn.microsoft.com/en-us/powershell PowerShell]
  
=== Különbségek ===
+
=== Differences from linux ===
 
Pár szembetűnő különbség
 
Pár szembetűnő különbség
* a Windows [https://hu.wikipedia.org/wiki/Kisbet%C5%B1-nagybet%C5%B1_%C3%A9rz%C3%A9kenys%C3%A9g case-insensitive], vagyis kis és nagybetű nem számít, ellenben a Linux case-sensitive.
+
* While linux is case-sensitive, windows is not.
* A könytár-, fájlnevek:
+
* Folder- and filenames:
 
** <tt>C:\Windows\System32\</tt>
 
** <tt>C:\Windows\System32\</tt>
 
** <tt>/usr/bin/</tt>
 
** <tt>/usr/bin/</tt>
* parancssori kapcsolók perrel versus kötőjellel:
+
* command options use / instead of -:
 
** Windows: <tt>dir /b</tt>
 
** Windows: <tt>dir /b</tt>
 
** Unix: <tt>ls -l</tt>
 
** Unix: <tt>ls -l</tt>
263. sor: 263. sor:
 
** Windows-on: <tt>.bat</tt>
 
** Windows-on: <tt>.bat</tt>
 
** Linux-on: <tt>.sh</tt>
 
** Linux-on: <tt>.sh</tt>
* pipe, redirect hasonló
+
* pipe, redirect is similar
  
Gyakran emulálják/helyettesítik a unix-os lehetőségeket windows-on:
+
Useful if you want to use linux bash on windows:
 
* [https://www.cygwin.com/ cygwin]
 
* [https://www.cygwin.com/ cygwin]
 
* [http://www.mingw.org/ mingw]
 
* [http://www.mingw.org/ mingw]

A lap 2017. szeptember 11., 06:56-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
$ _

The symbol > redirects the output of a command into a file, after this the contents of the file can be printed using cat-el. However:

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

The file myfile.txt remains empty, and we get an error message on the console (we didn't have permissions to list the files in the specified folder). This is the case, because the command ls did not produce an output onto the stardard output, but to the standard error (output) stderr. stderr can be redirected using the 2> symbol:

$ 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
$ _

This way the output of the command (stdout) and the error messages of the command (stderr) can be redirected separately.

Certain programs can read input from the standard input: stdin. There are so called non-interactive commands, where we don't need to do any input after issuing a command (most commands we discussed are like this). But there are some commands that can read input after being executed:

  • non-interactive command: grep "needle" haystack.txt
  • interactive command: grep "needle"

On the former command grep searches for the keyword needle in the given file. On the latter we need to provide input in which the command will search for the word.

$ 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
$ _

We can stop the input with Ctrl+D. The lines containing needle were repeated, every repeated line was the output of grep (it found the word).

Try it like this:

$ 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
$ _

With this only the lines containing needle were included in the file.

The output of a command can also be made to be the input of another using the pipe symbol: |.

Example: Print the files in our home directory that contain info in their name!
$ 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
$ _

Example: we have a lot of files in this folder, but we would like to look through them:

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

We can also pipe the contents of a file into a command (<), these two lines produce the same output:

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

Tasks

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

The terminal of windows DOS didn't go through much improvement since its creation. It's far behind the linux shell.

Another usable windows shell: PowerShell

Differences from linux

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

  • While linux is case-sensitive, windows is not.
  • Folder- and filenames:
    • C:\Windows\System32\
    • /usr/bin/
  • command options use / instead of -:
    • Windows: dir /b
    • Unix: ls -l
  • script
    • Windows-on: .bat
    • Linux-on: .sh
  • pipe, redirect is similar

Useful if you want to use linux bash on windows:

Személyes eszközök