3 Mayıs 2020 Pazar

/proc/meminfo - procfs Dosya Sistemi

Giriş
Bu dizin ile de sistem hafıza kullanımı bilgisine erişmek mümkün.

MemTotal Alanı
Toplamda ne kadar RAM olduğunu gösterir.

1 Mayıs 2020 Cuma

dpkg komutu - Debian/Ubuntu Sistemlerde Kullanılır

Giriş
Debian tabanlı sistemlerde .deb uzantılı paketlerin kurulmasını sağlar. Açıklaması şöyle.
dpkg can also be used as a front-end to dpkg-deb(1) and dpkg-query(1). The list of supported actions can be found later on in the ACTIONS section. If any such action is encountered dpkg just runs dpkg-deb or dpkg-query with the parameters given to it, but no specific options are currently passed to them, to use any such option the back-ends need to be called directly.
apt gibi internetten paket indiremez, sadece diskteki dosyaları kurabilir. 

dpkg-query komutu ve dpkg-deb komutu ile ilişkilidir.

Açıklaması şöyle.
apt is for managing remote repositories, dpkg - for locally installed packages. They're related. apt is front end to dpkg. When you run apt-get install package it gets .deb file, and installs it via dpkg.
Paketlerin Dizini
Paketler şu dizindedir
/var/lib/dpkg/
Lock Dosyası
dpkg bir lock dosyası kullanır. Dosyanın yolu şöyle
/var/lib/dpkg/lock-frontend
Bazen bu dosya silinmez şöyle bir hata alabiliriz.
dpkg: error: dpkg frontend lock is locked by another process
Dosyayı kimin kilitlediğini görmek için şöyle yaparız. Eğer dosyayı kilitleyen uygulama yoksa, dosya silinebilir ve dpkg tekrar başlatılabilir.
sudo lsof /var/lib/dpkg/lock-frontend
Log Dosyası
Şöyle
/var/log/dpkg.log
--get-selections seçeneği
Kurulu tüm paketleri gösterir. Şöyle yaparız.
dpkg --get-selections > my_package_list.txt
-i seçeneği
install anlamına gelir. Şöyle yaparız.
dpkg -i linux-firmware_1.161_all.deb
-l seçeneği
Beliritlen örüntüye uyan kurulu paketleri gösterir. Açıklaması şöyle.
dpkg -l shows your installed package versions (starting with ii), removed ones (rc), and some others (e.g. installed but not configured, see the manpage).
Örnek
Şöyle yaparız.
$ dpkg -l terminator
dpkg-query: no packages found matching terminator
Örnek
Şöyle yaparız.
$ dpkg -l gedit
||/ Name              Version        ...
+++-==========================================
ii  gedit             3.22.0-1       ...
-L seçeneği
Kurulu paket hakında bilgi verir.
Örnek
Şöyle yaparız
dpkg -L elixir
Çıktı olarak şunu alırız.
/.
/usr
/usr/bin
/usr/lib
/usr/lib/elixir
/usr/lib/elixir/bin
/usr/lib/elixir/bin/elixir
/usr/lib/elixir/bin/elixirc
/usr/lib/elixir/bin/iex
/usr/lib/elixir/bin/mix
/usr/lib/elixir/lib
/usr/lib/elixir/lib/eex
/usr/lib/elixir/lib/eex/ebin
/usr/lib/elixir/lib/eex/ebin/Elixir.EEx.Compiler.beam
(etc...)
-S seçeneği
Belirtilen dosyanın hangi pakette olduğunu bulur. Şöyle yaparız.
dpkg -S /usr/bin/gdb
veya şöyle yaparız.
dpkg -S $(which gdb)


29 Nisan 2020 Çarşamba

xxd komutu

Giriş
Hexadecimal veriyi byte dizisi haline getirir. echo -e kullanmak ile aynı şeydir

Örnek
xxd kullanarak şöyle yaparız.
echo "00 FF AB" | xxd -r -p | program
echo kullanarak şöyle yaparız.
echo -e "\x41\x42\x43\x44" | program

28 Nisan 2020 Salı

Pre-emptive Scheduling Nedir?

Giriş
Çalışan uygulamayı yarıda kesme işlemine Pre-emptive multitasking deniyor. Türkçesi ise sanırım Geçişli Çoklu Görev olarak kullanılıyor. Linux'a mahsus açıklama şöyle.
... so Linux uses pre-emptive scheduling. In this scheme, each process is allowed to run for a small amount of time, 200ms, and, when this time has expired another process is selected to run and the original process is made to wait for a little while until it can run again. This small amount of time is known as a time-slice.
Pre-emptive Scheduling İçin CPU Interrupt Kullanılır
Açıklaması şöyle.
All practical approaches to preemption will use some sort of CPU interrupt to jump back into privileged mode, i.e. the linux kernel scheduler.

If you look at your /proc/interrupts you'll find the interrupts used in the system, including timers.
Sistem Çağrısı Olunca
Sistem çağrısı olunca aslında bu çağrı bir şekilde scheduler'ı tetikler. Açıklaması şöyle.
I understand that processes jump to syscalls and those jump back to the scheduler, so it makes sense how processes can be “swapped” in that regards
Bir başka açıklama şöyle.
Also, when a program issues a system call (Usually by a software interrupt - "trap"), the kernel is also able to preempt the calling program, this is especially evident with system calls waiting for data from other processes.


Completely Fair Scheduler (CFS)

Giriş
Açıklaması şöyle
the Completely Fair Scheduler (CFS). Integrated into Linux 2.6.23., CFS performs the following duties:

- CFS ensures that the CPU is allocated equitably.
- If the CPU access time provided to different tasks isn’t balanced, CFS gives the shortchanged tasks the time they need to execute.
- CFS keeps track of the balance between tasks by maintaining CPU access times in the virtual runtime. The smaller a task’s virtual runtime, the greater its recognized CPU need.
- CFS uses “sleeper fairness” to make sure even tasks that aren’t currently running will still receive their fair share of CPU when required.
-CFS doesn’t directly use priorities.
CFS bu  işleri gerçekleştirmek için arka tarafta bir tane "Red Black Tree" kullanır. Açıklaması şöyle
CFS uses nanosecond granularity accounting and does not rely on any jiffies or other HZ detail. Thus the CFS scheduler has no notion of “timeslices” in the way the previous scheduler had, and has no heuristics whatsoever.

cp komutu

Giriş
copy anlamına gelir. mv komutu kopyalama yerine taşımak için kullanılabilir. cp komutu çaılırşen Ctrl+C ile kesilse bile sorun çıkmıyor.

cp komutu hedef dosya varsa bile onu uyarı vermeden ezer.

Örnek
Görmek için şöyle yaparız.
$ cp first.html second.html

$ cat second.html
first
-a seçeneği
Açıklaması şöyle.
-a Same as -pPR options. Preserves structure and attributes of files but not directory structure.
Sembolik linkeri de takip eder. 
Örnek
Şöyle yaparız.
cp -a src_dir another_destination/
Örnek
Bir diski başka diske kopyalamak için şöyle yaparız
cp -a /media/external/disk1/. /media/external/disk2/
-n/--no-clobber seçeneği
Hedef dosya varsa onu değiştirmez. Açıklaması şöyle
--no-clobber do not overwrite an existing file
Nasıl çalıştığının açıklaması şöyle. Eğer cp komutu dosyayı yok olarak tespit etse, ve bir sonraki aşamada başka bir uygulama dosyayı yaratsa bir doğru çalışır. Çünkü dosyayı overwrite edecek bayraklar ile açmadğı için hata alır ve dosyayı ezmez.
When --no-clobber is set, it checks whether the destination already exists; if it determines it doesn’t, and it should therefore proceed with the copy, it remembers that it’s supposed to copy to a new file. When the time comes to open the destination file, it opens it with flags which enforce its creation, O_CREAT and O_EXCL; the operating system then checks that the file doesn’t exist while opening it, and fails (EEXIST) if it does.
-p seçeneği
preserve anlamına gelir. dosyaların mode,ownership,timestamp gibi özellikleri muhafaza edilir. Açıklaması şöyle
-p     same as --preserve=mode,ownership,timestamps

   --preserve[=ATTR_LIST]
          preserve the specified attributes (default: mode,ownership,time‐
          stamps), if  possible  additional  attributes:  context,  links,
          xattr, all
Örnek
Şöyle yaparız.
cp --preserve oldfile newfile
Şöyle yaparız.
cp -p oldfile newfile
Örnek
Şöyle yaparız.
cp --preserve=timestamps oldfile newfile
-R/-r seçeneği
recursive anlamına gelir.
Örnek - globbing
Her şeyi kopyalamak için şöyle yaparız
cp -r ~/Documents/* ~/copies # works as expected
Ancak bazı dosyaları kopyalama çalışmıyor
cp -r ~/Documents/*.odt ~/copies # does not work as expected
glob işlemini bash'e bırakarak şöyle yaparız. Burada target ve source directory yer değiştiriyor. Ayrıca "/**/*.odt" kullanabilmek için bash ile recursive glob etkinleştiriliyor. 
shopt -s globstar
cp -nt ~/copies/ ~/Documents/**/*.odt
Eğer çok fazla dosya varsa şöyle yaparız
find ~/Documents -name '*.odt' -exec cp -nt ~/copies/ {} +
Örnek
İsmi subdir olanları hariç bırakmak için şöyle yaparız.
cp -r srcdir/!(subdir) dstdir
-t seçeneği
target directory anlamına gelir.
Örnek
Şöyle yaparız
cp -t ~/me/dir1 dir1/linkdir6/file1
--remove-destination seçeneği
Örnek
Elimizde şöyle bir sembolik link olsun. common.py dosyasını yani gerçek dosyayı değil ama semboli linkin başlangıcını değiştirmek isteyelim
sd/common.py -> actual_file
Şöyle yaparsak çalışmaz. Çünkü gidip gerçek dosyanın üzerine yazar
cp /tmp/Star_Wrangler/common.py sd/common.py
Açıklaması şöyle
This depends on what Unix you are using.

On some BSD systems (OpenBSD, FreeBSD), you will find that cp -f will unlink (remove) the symbolic link and replace it with the source.

Using GNU cp, this would not have the same effect, and you would need to use the long option --remove-destination instead.

On macOS, use cp -c to, as the manual says, "copy files using clonefile(2)".

On NetBSD, use cp -a ("archive mode", the same as cp -RpP on that system). This doesn't work on GNU, macOS, OpenBSD, or FreeBSD, even though all of these systems have the same or similar -a option for cp (on GNU systems, it's the same as -dR --preserve).

You already mention this yourself: Removing the link before copying the file will solve the issue. The rm utility removes the link rather than the file referenced by the link. This is also the most portable way to replace a symbolic link with a regular file.

Sembolik linki kaldırmak için şöyle yaparız.
rm -f sd/common.py
--sparse seçeneği
Şöyle yaparız.
$ cp --sparse=always junk junk.sparse && mv junk.sparse junk
-v seçeneği
Şöyle yaparız.
cp -v ~/Downloads/*.pdf ~/Downloads/BOOKS/
-x seçeneği - GNU Extension
Açıklaması şöyle. İç içe iki mount noktası varsa işe yarar.
The -x flag to cp is a GNU extension. When copying a single file, this option will have no effect, but when copying a whole file hierarchy, the -x option prevents the copying of files and directories that does not live on the same filesystem as the original source.

For example, on a filesystem with mount points at /usr and /usr/local, using cp -xR /usr /some-dest would not copy the hierarchy under /usr/local.

There are other utilities with an -x option with similar semantics, such as du and find (the flag is called -xdev for find), and rsync
Örnek
Şöyle yaparız.
cp -xr / blah


shopt komutu

-s seçeneği 
1. direxpand
Örnek
Şöyle yaparız.
if shopt -s direxpand 2>/dev/null; then
   # the direxpand option exists
else
   # the direxpand option does not exist
fi
2. extglob
Adı extglob ama aslında bash içinde regular expression işlevinin etkinleştirilmesi demek
bash kabuğunda KSH-style extended glob etkindir. Bu yüzden sanırım parantez içinde glob için karakter verebilmeyi sağlıyor. Bu özelliği bash içinde de etkinleştirmek için kullanılır. Açıklaması şöyle
Extended globs like +(...) aren't enabled in Bash by default, you'll need to explicitly use shopt -s extglob in the script to enable them.
Örnek - +(...)
bash satırından çalıştırınca sonuç şöyle. Arka arkaya gelen tüm space karakterlerini tek space karakteri ile değiştirir
$ s='1 2,   3   4,'
$ s0="$(echo ${s//,/ }|tr -s ' ')"
$ echo "s0: $s0"
s0: 1 2 3 4
$ d="'${s0//+([[:space:]])/"' '"}'"
$ echo "d: $d"
d: '1' '2' '3' '4'
Örnek - *(...)
Açıklaması şöyle
The expression *([0-9]).conf is a KSH-style extended glob. The feature is enabled by default for interactive bash shells, but to use it in a bash script it must be enabled explicitly using shopt -s extglob
Yani bash kabuğundan şöyle yapabiliriz
ls /etc/pve/lxc/*([0-9]).conf
Ama bash içinden bu çalışmaz.
#!/bin/bash
ls /etc/pve/lxc/*([0-9]).conf

Çıktı olarak
Result: ERROR
syntax error near unexpected token `('
Örnek - ? karakteri
Elimizde şöyle bir kod olsun. Burada te?xt örüntüsünün text uzantılı dosyaları bulmasını bekleriz.
$ touch file.txt file.text
$ ls file.*
file.text  file.txt
$ ls file.te?xt
ls: cannot access 'file.te?xt': No such file or directory
Açıklaması şöyle.
The ? matches a single character (like . in a regular expression). You have no file matching the pattern file.te?xt so the pattern remains unexpanded.

You may have expected it to work as in a regular expression, where it means "zero or one of the previous expression". Unfortunately, there is no such wildcard in bash.
Şöyle yaparız
$ shopt -s extglob
$ ls file.t?(e)xt
file.text file.txt
3.  histverify
Açıklaması şöyle
If the histverify shell option is enabled, and Readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the Readline editing buffer for further modification.