Shell Commands (Komut satırı dersleri, notları)

mv
mv source target : source target'e taşınır.
mv folder1 folder2 target     : folder1 ve folder2, target'e taşınır.
mv folder1 file1 target : folder1 ve file1, taget'e taşınır.
mv -option source target : source target'e taşınır.

Örnek :
mv ~/Downloads/*.mp3 ~/Music/
--------------------
compgen : to list all available commands/aliases/functions/etc..
http://stackoverflow.com/questions/948008/linux-command-to-list-all-available-commands-and-aliases
compgen -c will list all the commands you could run.
compgen -a will list all the aliases you could run.
compgen -b will list all the built-ins you could run.
compgen -k will list all the keywords you could run.
compgen -A function will list all the functions you could run.
compgen -A function -abck will list all the above in one go.
--------------------
man mkdir
man grep
man ls
man man
gibi komutları kullanarak bir komut hakkında bilgi alabiliriz.
mkdir komutu hakkında bilgi almak için man mkdir komutunu çalıştırırız.
--------------------
telnet ip_address portaddress : bir ip adresindeki bir port açık mı diye check eder.
ping yapılamayan bir ip'ye telnet yapılıyor olabilir, ping yetkilerinin kapalı
olmasından kaynaklanıyor olabilir bu.
--------------------
$PATH dersen path isimli env. var.'ın değerini göterir.

--------------------
env : environment variable'ları return eden komut.
--------------------
whoami
--------------------
cal 2016 komutuyla 2016 yılı takvimini gösteririz.
--------------------
The cd command changes the current directory of a shell process;
the Perl chdir function changes the current directory of a Perl process.
They're exactly the same thing, just spelled differently.
--------------------
ps aux | grep firefox
ps -ef | grep firefox
Bu 2 komut aynı işi yapar.
ps aux ve ps -ef çalışan process'lerin ayrıntılarını verir output olarak.
Bu output'u alan grep komutu, içerisinde firefox olan satırları return eder.

--------------------
ls -l sample.txt
Bu sorgu ile, dosyaların read/write yetkilerini ve yaratıldığı
tarih bilgilerini de görebiliriz.
--------------------
CAT command
http://www.cyberciti.biz/faq/howto-use-cat-command-in-unix-linux-shell-script/

If you specify more than one file name, cat will display those files one after the other,
concatenating their contents to standard output. So this command:
cat mytext.txt mytext2.txt
Will print the contents of those two text files as if they were a single file.

cat test1.txt >> test2.txt
will read the contents of test1.txt, and write them at the end of test2.txt.
If test2.txt does not already exist, it will be created and the contents of test1.txt will be written to the new file.

cat test1.txt test2.txt >> test3.txt
This will write the combined contents of test1.txt and test2.txt to the end of test3.txt.

echo "My Shopping List" | cat - list.txt
cat will read from standard input if you specify a hyphen ("-") as a file name.
For example, if you have a file, list.txt, which contains the following text:
apples
oranges
butter
bread
...you could use the echo command to output text, pipe that output to cat, and instruct cat to catenate it with the file's contents, like this:
The above command would output the following text:
My Shopping List
apples
oranges
butter
bread
--------------------
TAC (which is "cat" backwards) concatenates each FILE to standard output just like the cat command, but in reverse:
line-by-line, printing the last line first.
This is useful (for instance) for examining a chronological log file in which the last line of the file contains the most recent information.
If no FILE is specified, or if the FILE is specified as "-", tac reverses the contents of standard input.

tac file1.txt
Prints the lines of file1.txt in reverse, from last line to first.
--------------------
tail myfile.txt
Outputs the last 10 lines of the file myfile.txt.

tail myfile.txt -n 100
Outputs the last 100 lines of the file myfile.txt.

tail -f myfile.txt
Outputs the last 10(default value) lines of myfile.txt, and monitors myfile.txt for updates;
tail then continues to output any new lines that are added to myfile.txt.

tail -f -100 myfile.txt
Outputs the last 100 lines of myfile.txt, and monitors myfile.txt for updates;
tail then continues to output any new lines that are added to myfile.txt.


tail -f access.log | grep 24.10.160.10
This is a useful example of using tail and grep to selectively monitor a log file in real time.
log dosyasında 24.10.160.10 içeren satırlar ekrana yazdırılır.

In this command, tail monitors the file access.log. It pipes access.log's final ten lines,
and any new lines added, to the grep utility. grep reads the output from tail, and outputs
only those lines which contain the IP address 24.10.160.10
--------------------
komut satırından dosya yaratalım:
cat > sample.txt
komutunu verip girmek istediğimiz text'i gireriz Ctrl+D 'ye
basıp geri döneriz.
veya
ls > sample.txt komutuyla ls'in sonucunu sample.txt dosyasına
yazıdrmayı deneriz, eğer böyle bir dosya yoksa yaratılır.
dosya yaratılmış mı diye bakmak için, dosyanın read/write
yetkilerine yaratıldığı tarihe bakmak için ise şu komutu veririz:
ls -l sample.txt
veya
One method for creating a blank text file is to use the right arrow (>).
Example :    > sample.txt
*********
touch sample.txt
Another command that works similarly to using the right arrow (>) to
create an empty text file is the “touch” command.
Type “touch” followed by a filename, as shown in the following image.
The “touch” command creates an empty text file. Use Vi, or other text editor, to add text to the file.
Details of touch command : http://www.tecmint.com/8-pratical-examples-of-linux-touch-command/
Touch komutu genellikle dosya yaratmak için kullanılır.
--------------------
Terminal'deki current path'i nasıl buluruz?
Su an hangi path'deyim diye bakmak istiyorum diyelim.
pwd
The pwd command displays the absolute pathname of the
current working directory to the computer screen.

Type pwd command:
pwd
Output:
/home/vivek
Above command print the full filename of the current working directory i.e /home/vivek.

Display actual directory location
Use -P option to display the physical current working directory
(all symbolic links resolved).
For example,
/home/lighttpd is /var/www/root/lighttpd:

cd /home/lighttpd
pwd
Output:
/home/lighttpd

Now run with -P option
pwd -P
Output:
/var/www/root/lighttpd
--------------------
terminal'den root directory'ye gidiyorum. Sonra şu komutu veriyorum.
Bu komut root directory'de attığımız ls komutunun sonucunu Desktop/sample.txt
dosyasının içine yazar.
ls > /root/Desktop/sample.txt
--------------------
The “cat” command can also be used to view the contents of your file.
Yani bu komut bir dosyanın içeriğini return eder.

cat sample.txt

The contents of the file is printed to the screen and you are returned to the prompt.
--------------------
ping google.com -> Biz durdurana sürekli olarak paket gönderir.
ping -c 10 192.168.1.100   -> 10 adet paket gönderir, bu paket hangilerinin geldiğini
söyler.
--------------------
GREP commands
http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/

grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName

How do I use grep command to search a file?
Search /etc/passwd file for boo user, enter:
$ grep boo /etc/passwd

$ grep -i "boo" /etc/passwd
You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with the -i option:

$ grep -r "192.168.1.5" /etc/
OR
$ grep -R "192.168.1.5" /etc/
You can search recursively i.e. read all files under each directory for a string "192.168.1.5"

$ grep -w "boo" file
grep can be used to search exact words only.
When you search for boo, grep will match fooboo, boo123, barfoo35 and more.
You can force the grep command to select only those lines containing matches that form whole words i.e. match only boo word:

$ egrep -w 'word1|word2' /path/to/file
Use grep to search 2 different words
Use the egrep command as above.

$ grep -c 'word' /path/to/file
Count line when words has been matched
The grep can report the number of times that the pattern has been matched for each file using -c (count) option:

$ grep -n 'root' /etc/passwd
Pass the -n option to precede each line of output with the number of the line in the text file from which it was obtained:
Sample outputs:
1:root:x:0:0:root:/root:/bin/bash
1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh
3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh

$ grep -v bar /path/to/file
Grep invert match
You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:

# dmesg | egrep '(s|h)d[a-z]'
UNIX / Linux pipes and grep command
grep command often used with shell pipes. In this example, show the name of the hard disk devices:

Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'

# grep -i 'Model' /proc/cpuinfo
However, above command can be also used as follows without shell pipe:
S
ample outputs:
model : 30
model name : Intel(R) Core(TM) i7 CPU       Q 820  @ 1.73GHz
model : 30
model name : Intel(R) Core(TM) i7 CPU       Q 820  @ 1.73GHz

$ grep -l 'main' *.c
How do I list just the names of matching files?
Use the -l option to list file name whose contents mention main():


$ grep --color vivek /etc/passwd
Finally, you can force grep to display output in colors, enter:
--------------------
FIND commands
find . -name "*.php"
root directory'de recursive olarak arar.

find . -name "*.php"
This command searches for files by their name.

http://www.binarytides.com/linux-find-command-examples/
http://www.tecmint.com/35-practical-examples-of-linux-find-command/
--------------------
Sed is the ultimate stream editor. If that sounds strange, picture a stream flowing through a pipe.
http://www.grymoire.com/Unix/Sed.html
http://tecadmin.net/example-of-find-and-replace-string-in-files-with-sed/#
http://www.folkstalk.com/2012/01/sed-command-in-unix-examples.html

A common use of sed is to modify each line of a file or stream by replacing specified parts of the line.
For example if you have a file that contains the lines:

1, Justin Timberlake, Title 545, Price $6.30
2, Taylor Swift, Title 723, Price $7.90
3, Mick Jagger, Title 610, Price $7.90
4, Lady Gaga, Title 118, Price $6.30
5, Johnny Cash, Title 482, Price $6.50
6, Elvis Presley, Title 335, Price $6.30
7, John Lennon, Title 271, Price $7.90
If the file name is "songs.txt" and you wanted to change all occurrences of 6.30 to 7.30 you could use the command:
sed 's/6.30/7.30/' songs.txt > songs2.txt
which writes the modified file to "songs2.txt".
So the output file would contain:
1, Justin Timberlake, Title 545, Price $7.30
2, Taylor Swift, Title 723, Price $7.90
3, Mick Jagger, Title 610, Price $7.90
4, Lady Gaga, Title 118, Price $7.30
5, Johnny Cash, Title 482, Price $6.50
6, Elvis Presley, Title 335, Price $7.30
7, John Lennon, Title 271, Price $7.90
If you wanted replace all occurrences of "Cash" with "Trash" you would be use:
 sed 's/Cash/Trash/' songs.txt > songs2.txt
Cash 'leri Trash ile replace et.

which would create a file with content:
1, Justin Timberlake, Title 545, Price $7:30
2, Taylor Swift, Title 723, Price $7.90
3, Mick Jagger, Title 610, Price $7.90
4, Lady Gaga, Title 118, Price $7:30
5, Johnny Trash, Title 482, Price $6.50
6, Elvis Presley, Title 335, Price $7:30
7, John Lennon, Title 271, Price $7.90
Sed is also frequently used to filter lines in a file or stream.
For example if you only want see the lines containing "John" you could use:
sed -n '/John/p' songs.txt > johns.txt
which will write the following lines to file johns.txt:
5, Johnny Trash, Title 482, Price $6.50
7, John Lennon, Title 271, Price $7.90
reference : http://linux.about.com/od/commands/a/Example-Uses-Of-Sed-Cmdsedxa.htm
----
Windows :
How to run exe file in command line windows
Open command prompt -> Got to your .exe's location using cd command -> execute your .exe
You can add Console.ReadKey() at the end of your code so that program will wait until the user presses some key.
---------------------------------------------------------------
help : to list all available commands
---------------------------------------------------------------
set : Gets a list of all environment variables. Tüm environmen variable'ları
ve bunların değerlerini return eder. set c -> c ile başlayan env. var.'ları,
set p -> p ile başlayan env. var.'ları .... return eder.
path : environment variable path'in değerini yazdırır.
Environment variable'ların değerlerini boyle yazdiririz.
echo %PATH%
echo %OS%
echo %COMPUTERNAME%
echo %HOMEPATH %

---------------------------------------------------------------
netstat -aon | find ":8080" | find "LISTENING"
taskkill /PID 8712 /F
---------------------------------------------------------------
netstat -ano : hangi local ip'de hangi port'da hangi process çalışıyor görmek için.
---------------------------------------------------------------
http://ss64.com/nt/tasklist.html
http://superuser.com/questions/914782/how-do-you-list-all-processes-on-the-command-line-in-windows
---------------------------------------------------------------
Current directory windows'da da linux'daki gibi . 'dır.
---------------------------------------------------------------
findstr /S "ekran" *
/S Searches for matching files in the current directory and all subdirectories.
current directory'de ve bu directory'nin altındaki tum directory'lerde
"ekran" string'ini içeren satırları bulup yazdırır.
findstr, linux'daki grep komutuna benzer.

findstr /S /M "ekran" *
/M Prints only the filename if a file contains a match.

findstr /S /M /I "ekran" *
/I Specifies that the search is not to be case-sensitive.

findstr /S /I /N "ekran" *
/N Prints the line number before each line that matches.

/X Prints lines that match exactly.
---------------------------------------------------------------
ipconfig -> ip adresine bakarsın, hangisi snein ip adresin
anlamak için şuna dikkat et, hem default gateway hem subnetmask
için belirli bir değer olan yerdeki ip değeridir senin ip.
---------------------------------------------------------------
cls -> clears the terminal screen. linux'daki clear. (CLearScreen)
---------------------------------------------------------------
dir > file.txt
dir komutu linux'daki ls komutunu karşılığıdır, yukarıdaki
pipe komutu sayesinde dir komutunun sonucu dosyaya yazdırılır.
---------------------------------------------------------------
ipconfig /all > C:\Users\metis\Desktop\output_file.txt
Bu komut ile ipconfig'den daha detaylı bir bilgi elde ederiz.
It gets more in-depth information about local area connection
ifconfig çok az bilgi verir. ifconfig /all çok daha fazla bilgi verir.
---------------------------------------------------------------
ping command is a subset of ICMP. It uses echo request.
When you ping a device, you send echo request andif the device
you ping is active or online, you get an echo response.

For example, if your router is down, and you try to ping it
and you get no response, then you know that the router is
what is giving problems.
---------------------------------------------------------------
tracert google.com
---------------------------------------------------------------
nslookup google.com
--------
Bash Scripting in Linux
http://mally.stanford.edu/~sr/computing/basic-unix.html
http://linuxconfig.org/linux-commands
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
-------------------------------------------------------
Variables are declared and assigned without $ and without {}. You have to use
var=10
to assign.
(Burada variable declare ettik ve bu variable'a bir değer assign ettik. Burada variable'ı declare ederken $ veya {} işaretlerinden birini kullanmadığımıza dikkat.)
-------------------------------------------------------
In order to read from the variable (in other words, 'expand' the variable), you must use $.
$var      // use the variable
${var}    // same as above
${var}bar // expand var, and append "bar" too
$varbar   // same as ${varbar}, i.e expand a variable called varbar, if it exists.
This has confused me sometimes - in other languages we refer to the variable in the same way, regardless of whether it's on the left or right of an assignment. But shell-scripting is different, $var=10 doesn't do what you might think it does!
( Bir variable'ın değerini elde etmek için variable isminin başına $ işareti getirilir. Örneğin, $var ,   )
-------------------------------------------------------
How to run a bash script? Hello Bash!!
test100.sh isimli bir dosyaya şunları yaz :
#!/bin/bash 
echo Hello World
Sonra test100.sh bash scripting dosyası şöyle çalıştırılır:
# oiyio@ubuntu:~/Desktop$ bash test100.sh
# Hello World
# oiyio@ubuntu:~/Desktop$ ./test100.sh
# bash: ./test100.sh: Permission denied
# oiyio@ubuntu:~/Desktop$ chmod 777 test100.sh
# oiyio@ubuntu:~/Desktop$ ./test100.sh
# Hello World
# oiyio@ubuntu:~/Desktop$ 
Not : Bir bash scripting dosyasını çalıştırarak ve bu dosya içerisine yazdığımız kodları tek tek terminal'de çalıştırarak aynı output'u aynı sonucu elde ederiz. Yani bash scripting dosyasına yazdığımız kodlar, terminalde otomatik olarak tek tek çalıştırılıyor gibi düşün.
-------------------------------------------------------
test101.sh isimli bir dosyaya şunları yaz :
TEST="Unix Programming"
$echo $TEST
Unix Programming
-------------------------------------------------------
http://www.computerhope.com/unix/uenv.htm
http://unix.stackexchange.com/questions/103467/what-is-env-command-doing
env is a shell command for Linux, Unix, and Unix-like operating systems.
It can be used to print a list of the current environment variables,
or to run another program in a custom environment without modifying the current one.
-------------------------------------------------------
http://unix.stackexchange.com/questions/114300/whats-the-meaning-of-a-dot-before-a-command-in-shell
http://ss64.com/bash/source.html
http://askubuntu.com/questions/232932/in-a-bash-script-what-does-a-dot-followed-by-a-space-and-then-a-path-mean
http://stackoverflow.com/questions/8748831/bash-when-do-we-need-curly-braces-in-variables
http://linuxsig.org/files/bash_scripting.html
http://stackoverflow.com/questions/13665803/check-bindir-value

http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash
http://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-scrip

Hiç yorum yok:

Yorum Gönder