2 Nisan 2019 Salı

tail komutu

-c seçeneği
Açıklaması şöyle
-c, --bytes = [+] NUM
output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file
Örnek
Dosyanın sonunu okumak için eksi bir sayı veririz. Şöyle yaparız.En son byte'ı oku anlamına gelir.
tail -c1 myfile.txt
Örnek
Dosyanın başından itibaren belli bir byte sayısın atlayıp gerisini okumak için artı bir sayı veririz. Şöyle yaparız. İkinci byte'tan itibaren oku anlamına gelir.
tail -c+2 myfile.txt
Eğer sayı olarak +1 verirsek tüm dosyayı okur. Çünkü 1. byte'tan itibaren oku anlamına gelir. Yani cat komutu ile aynı kapıya çıkar.
Örnek
Dosyamızda başta 26 byte'lık bir header sonda iki 2 byte'lı bir footer olsun. İlk 26 byte'ı ve en son iki byte'ı atlamak için şöyle yaparız.
tail -c +26 file | head -c -2
-f seçeneği
inotify_init ile dosya gözetlenmeye başlanır.Dosya değişince gerektiği kadarını okur. inotify çağrısı şöyle görülebilir.
fstat(3, {st_mode=S_IFREG|0644, st_size=139, ...}) = 0
fstatfs(3, {...}) = 0
inotify_init()                    = 4
inotify_add_watch(4, "/path/to/file", ...)
fstat(3, {st_mode=S_IFREG|0644, st_size=139, ...}) = 0
read(4, 0xd981c0, 26)             = -1 EINTR (Interrupted system call)
Şöyle yaparız
$ tail -f /myfile
-n seçeneği
İki kullanımı var
1. Sondan kaçıncı satırı görmek istediğimizi belirtir. 
2. Kaçıncı satırdan başlamak istediğimizi belirtir
Açıklaması şöyle.
-n, --lines=[+]NUM            output the last NUM lines, instead of the last 10;
                              or use -n +NUM to output starting with line NUM
Örnek - Son 2 Satır
Şöyle yaparız.
tail -n 2 poem | head -n 1
Örnek - İlk satırdan başla
Şöyle yaparız.
tail -n +0 -f some/file | awk ...
Örnek - İkinci satırdan başla
Açıklaması şöyle
tail -n+2 select all but the first row.

1 Nisan 2019 Pazartesi

chown komutu - Dosya Ve Dizinlerin Sahibini ve Grubunu Değiştirir

Giriş
Söz dizilimi şöyle. Bu komutlar bir dizinin sahibini veya grubunu değiştirebiliriz.
chown user:group [file|directory]
- Sadece sahibi değiştirmek için user'dan sonra :group gelmesi gerekmez. 
- Sadece grubu değiştirmek için :group şeklinde kullanırız.

Sahip ve Grup parametresi Değiştirmek
Örnek
Şöyle yaparız. Sahip ve grubu aynı yapar.
chown root: myapp
Örnek
Şöyle yaparız.
chown root:root myapp
Daha sonra ll komutu ile dosyaya bakarsak şuna benzer bir çıktı görürürz.
$ ll myapp
-rwsr-xr-x 1 root root 8712 Sep  8 18:12 myapp*
Dosyanın grubu root olduğu için Effective User ID çıktısını alsaydık 0 görmemiz gerekirdi.
#include <stdio.h> 
#include <unistd.h>
#include <sys/types.h>

void main() {
  printf("real uid: %d\n", (int) getuid());
  printf("effective uid: %d\n", (int) geteuid());
}
-h seçeneği
Açıklaması şöyle.
Change the owner of a symbolic link:

chown -h user path/to/symlink               
Dosya sembolik link (symbolic link) ise gerçek dosyayı değil sdece linki değiştirir. Şöyle yaparız.
chown -h my_user:users myapp
-R seçeneği
Açıklaması şöyle.
Recursively change the owner of a folder and its contents:

chown -R user path/to/folder           
Recursive çalışır.
Örnek
Şöyle yaparız. /var/log dizini ve altındaki tüm dosyalar spring-boot-user kullanıcısına ve root grubuna ait olur.
chown -R spring-boot-user:root /var/log
Örnek
Bir dizini ve altındaki tüm dosyaların sahipliğini almak istersek şöyle yaparız.
chown -R $(id -u):$(id -g) somefolder
Örnek
Bir dizini ve altındaki tüm dosyaların sahipliğini almak istersek şöyle yaparız.
sudo chown -R $(whoami) brew/*
--reference seçeneği
Açıklaması şöyle.
Change the owner of a file/folder to match a reference file:

chown --reference=path/to/reference_file path/to/file      



26 Mart 2019 Salı

id komutu

Giriş
Belirtilen kullanıcı isminin uid değerini döner. User ID root için 0'dır.

Kullanıcı Başlangıç Numarası
Her işletim sistemi için farklıdır. Açıklaması şöyle
Many Unix systems start handing out UIDs to users at some particular number. Solaris will give the first general purpose user UID 100, on OpenBSD it's 1000, and on macOS it appears it's UID 501 that will be the UID for the first created interactive user...
Effective User ID
Şöyle yaparız.
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi
Örnek
Şöyle yaparız.
# Root user only
if [[ "$EUID" != 0 ]]; then
  ...
fi

echo "do my ops"
Kullanım
Örnek
Şöyle yaparız.
$ id
uid=1000(attie) gid=1000(attie) groups=1000(attie),27(sudo),117(docker)
Örnek
Şöyle yaparız.
id username
Çıktı olarak tüm bilgileri alırız.
[root@my01 ~]# id sylvain
uid=1003(sylvain) gid=1005(sylvain) groups=1005(sylvain)
-n seçeneği
Açıklaması şöyle. Numara yerine isim gösterir.
That will query the account database (whether it's stored in /etc/passwd, LDAP, NIS+, a RDBMS...) for the first user name with that uid.
Örnek
Şöyle yaparız.
id -nu "$uid"
Örnek
Şöyle yaparız.
id -un -- myusername
-u seçeneği
Sadece uid bilgisini gösterir.
Örnek
Her kullanıcı kendi numarasını id komutu
id -u <username>
veya $UID ile görebilir.
echo $UID
Örneğin id komutu ile benim kullanıcı numaram 1000 olarak görünüyor.


24 Mart 2019 Pazar

FileSystem - Dosya Sistemi

Kernel Module
Açıklaması şöyle.
Basically, the initialization function of your filesystem driver module needs just to call a register_filesystem() function, and give it as a parameter a structure that includes a function pointer that identifies the function in your filesystem driver that will be used as the first step in identifying your filesystem type and mounting it. Nothing more happens at that stage.

When a filesystem is being mounted, and either the filesystem type is specified to match your driver, or filesystem type auto-detection is being performed, the kernel's Virtual FileSystem (VFS for short) layer will call that function. It basically says "Here's a pointer to a kernel-level representation of a standard Linux block device. Take a look at it, see if it's something you can handle, and then tell me what you can do with it."

At that point, your driver is supposed to read whatever it needs to verify it's the right driver for the filesystem, and then return a structure that includes pointers to further functions your driver can do with that particular filesystem. Or if the filesystem driver does not recognize the data on the disk, it is supposed to return an appropriate error result, and then VFS will either report a failure to userspace or - if filesystem type auto-detection is being performed - will ask another filesystem driver to try.

The other drivers in the kernel will provide the standard block device interface, so the filesystem driver won't have to implement hardware support. Basically, the filesystem driver can read and write disk blocks using standard kernel-level functions with the device pointer given to it.

The VFS layer expects the filesystem driver to make a number of standard functions available to the VFS layer; a few of these are mandatory in order for the VFS layer to do anything meaningful with the filesystem, others are optional and you can just return a NULL in place of a pointer to such an optional function.

19 Mart 2019 Salı

ubuntu-distro-info komutu

Giriş
Ubuntu desteğinin ne zaman biteceğini gösterir.

Örnek
Şöyle yaparız.
$ ubuntu-distro-info --all -yeol -f 
Ubuntu 14.04 LTS "Trusty Tahr" 29

pkill komutu

Giriş
kill "process id" ile çalışır. pkill ise uygulama ismi ile çalışır. Kullanması daha kolaydır. Eğer sadece process id'lerini bulmak istiyorsak pgrep komutu kullanılabilir.

pkill Alternatifi
Eğer sistemde pkill yoksa şöyle yaparız
kill $(pidof gnome-disks gnome-system-monitor)
Kullanım
Örnek

Şöyle yaparız.
pkill -9 mysql
Örnek
Şöyle yaparız. Böylece tek seferde birden fazla uygulamak öldürülebilir.
pkill 'process_name1|other_process|something_else'
Döndürülen Değer
Örnek
Açıklaması şöyle. pkill bir uygulamayı öldürürse true döner.
- If pkill compton succeeds, it means that there was one or several compton processes that have now been killed, or at least signalled, and exec compton -b will not be executed.

- If pkill compton fails (no process matched the name, or there was some internal error in pkill), the body of the if statement would call your exec compton -b, which would replace the shell process with that of compton -b.
Şöyle yaparız.
#!/bin/sh

if ! pkill compton; then
    exec compton -b
fi
-f seçeneği
Bir script'e gönderir

Örnek
Şöyle yaparız
pkill -USR1 -f my_fancy_script
-P seçeneği
-P ile belirtilen uygulama tarafından başlatılan (process group) tüm uygulamaları sonlandırılır.

Örnek
Şöyle yaparız. 1500 numaralı uygulamanın başlattığı ve ismi sleep olan uygulamalar sonlandırılıyor.
pkill -P 1500 sleep

17 Mart 2019 Pazar

fg komutu

Giriş
Arka plandaki (background) bir işi ön plana (foreground) getirir.  
- İşi arka plana atmak için Ctrl +Z kullanılır
- Arka plandaki işleri görmek için jobs komutu kullanılır. 
- Arka plandaki işi indeks numarası ile öldürmek için kill komutu kullanılabilir.

Örnek
Bir soru ve cevap şöyle. Burada uygulama terminalde başlatılıyor. Penceresi açıldıktan sonra Ctrl + Z ile arka plana gönderiliyor. Bu durumda artık çarpıya basarak kapatılamıyor. Kapatabilmek için önce fg komutuyla tekrar ön plana getirilmesi lazım
Q: I opened imagemagick through the terminal with $ display filename.jpg

Once it opened, the terminal was in use (?) as in no more prompt, just blank space where I could type anything like when you write to a file with 'cat'. I used CTRL + Z to get back to the prompt and the image stayed open in imagemagick. Now I can't close it - clicking the red X on the window doesn't do anything. How do I close this window/program?

A : type fg in the terminal to bring the stopped job back to the foreground. Then the red X should work again.

fg İle Kullanılacak Sayıyı Bulmak
Açıklaması şöyle.
When you send a process to the background, whether by using ctrl-z or by & at the end of the command, you get an output in the following format: [index] process-id. If you send multiple processes to the background, the index will keep incrementing every time.
Şöyle yaparız
$ sleep 100 &
[1] 41608
$ sleep 101 &
[2] 41609
$ sleep 102 &
[3] 41610
$ sleep 103 &
[4] 41611
$ sleep 104 &
[5] 41612
$ sleep 105 &
[6] 41613
$ sleep 106 &
[7] 41614
jobs Komutu
Örnek
Şöyle yaparız.
$ sleep 123 
[1] 26103
$ jobs
[1]+  Running                 sleep 123 &
$ fg 1
sleep 123
kill Komutu İle İşi Öldürmek
Açıklaması şöyle.
If it is your one and only background job you can kill it with kill %1. If not sure you can list all your background jobs with jobs and use kill %<n> where you replace n by the number of your ping job.
Diğer Örnekler
Örnek
Şöyle yaparız.
fg %2
Örnek
Ctrl + Z ile iş durdurulur. Daha sonra şöyle yaparız.
fg ; echo "Finished successfully!"