Sunday, January 18, 2009

Command-line

บรรทัดคำสั่งพื้นฐาน

- สำหรับเราเอง เขียนไว้กันเหนียวเผื่อลืม
- อย่าเชื่อมาก ต้องลอง

What is command line ?
- One interface with linux os for linux user.

What is shell ?



Why should know command shell ?
- Can work on every linux distribution.
- Every nix base it's same command (little bit diff).
- Windows have a plan to change dos command to compatible with linux.
- Easy to work with.
- Any way, Command-line interface is faster (Comfirm by jui ^^' so really)

Terminal structure (teletypewriter)
- tty1 to tty7 (Desktop)
- By press Ctrl + Alt + F1 to F7

Linux file system
- in linux don't have drive every drive is folder
- / call "root" is top of directory in linux file system
- /bin : essential binaries
- /boot : static files of boot loader
- /etc : host specific system config
- /usr : shareable and read-only data
- /var : variable data files
- /sbin : system binary
- /tmp : temporary files delete on bootup
- /dev : location of special or device files
- /home : user home directory
- /lib : library and kernel modules
- /media : mount files
- /opt : add-on application software
- /root : home of root

Command prompt
- username@hostname:~$
- "username" is user in linux system
- "hostname" is computer name
- "~" working directory
- "$" is permission (and # for root)

Command-line pattern
- [command]
- [command] [option]
- [command] [option] [file or path]
- [command] [file or path] [option]
- [command] [option] [file or path] [file or path2]

Special path
- "." is current directory
- ".." is parent directory
- "~" is home directory

File of linux system
- File
- Hidden file
- Directory
- Link

Help for linux by
- "man"
- "--help"
### File command
$ ls -la             => directory listing with hidden files
$ cd dir             => change directory to dir
$ cd|cd ~            => change to your home
$ pwd                => print work directory for show current directory
$ mkdir dir          => create directory dir
$ rm file            => remove file
$ rm -r dir          => remove directory dir
$ rm -f file         => force remove file
$ rm -rf dir         => force remove dir
$ cp file1 file      => copy file1 to file2
$ cp -r dir1 dir     => copy dir1 to dir2, create dir2 if it doesn't exist
$ mv file1 file2     => rename or move file1 to file2 if file2 is an existing directory, moves file1 into directory file2
$ ln -s file link    => create symbolic link link to file
$ touch file         => create or update file time stamp
$ cat file           => output the content of file
$ cat > file         => places standard input into file instead text editor ^^' and stop by ^c (^ is ctrl)
$ cat file1 > file2  => copy content in file1 replace to file2 if files is an existing
$ cat file1 >> file2 => copy content in file1 append to file2 if files is an existing
$ more file          => output the content of file each page
$ less file          => output the content of file and can go to next or previous page
$ head file          => output the first 10 lines of file
$ tail file          => output the last 10 lines of file
$ div file1 file2    => output difference content between file1 and file2

### Searching
$ grep pattern files     => search for pattern in files
$ grep -r pattern dir    => search recursively for pattern in dir
$ grep ^pattern files    => search begin by pattern
$ command | grep pattern => search for pattern in the output of command
$ locate file            => file all instances of file

### Process management
$ ps               => display your currently active processes
$ ps -aux          => display any your currently processes
$ top              => display all running processes and change display every 1 sec
$ htop             => third party program same 'top'
$ kill pid         => kill process id pid
$ killall program  => kill all processes named proc (use with extreme caution)
$ bg               => lists stopped or background jobs; resume a stopped job in the background
$ fg               => brings the most recent job to foreground
$ fg n             => brings job n to the foreground
$ /etc/init.d/serv start|stop|restart|reload => for start stop service

### Shortcuts
$ Ctrl+C            => halts the current command
$ Ctrl+Z            => stops the current command, resume with fg in the foreground or bg in the background
$ Ctrl+D            => log out of current session, similar to exit
$ Ctrl+W            => erases one word in the current line
$ Ctrl+U            => erases the whole line
$ Ctrl+R            => type to bring up a recent command

### System info
$ clear|reset       => clear screen
$ echo pattern      => display pattern
$ time command      => use how long work with command
$ date              => show the current date and time
$ cal               => show this month's calendar
$ uptime            => show current uptime
$ w                 => display who is online
$ whoami            => who you are logged in as
$ finger user       => display information about user
$ uname -a|r        => show kernel information
$ cat /proc/cpuinfo => cpu information
$ cat /proc/meminfo => member information
$ man command       => show the manual for command
$ df                => show disk usage
$ du                => show directory space usage
$ free              => show memory and swap usage
$ whereis app       => show possible locations of app
$ which app         => show which app will be run by default

### Compression
$ tar cf file.tar files        => create a tar named file.tar containing files
$ tar xf file.tar              => extract the files from file.tar
$ tar czf file.tar.gz files    => create a tar with Gzip compression
$ tar xzf file.tar.gz          => extract a tar using Gzip
$ tar cjf file.tar.bz2         => create a tar with Bzip2 compression
$ tar xjf file.tar.bz2         => extract a tar using Bzip2
$ tar tf|tvf file.tar|.gz|.bz2 => display content in zip file
$ gzip file                    => compresses file and rename it to file.gz
$ gzip -d file.gz              => decompresses file.gz back to file

### Network
$ ping host            => ping host and output results
$ whois domain         => get whois information for domain
$ dig domain           => get DNS information for domain
$ dig -x host          => reverse lookup host
$ wget file            => download file
$ wget -c file         => continue a stopped download
$ ssh|telnet user@host => remote host
$ ftp|sftp host|user@host => remote ftp host
$ scp file user@host:path => copy file to remote host dont' forget (:) after remote host
=> scp file user@host ,incorrect ,it only copy file to file name user@host
$ scp user@host:file file => copy file from remote host

Example:
$ scp file1 user@somehost:/opt    => copy file1 to /opt of remote host
$ scp user@somehost:/opt/file1 ./ => copy file1 from remote host to current dir of local host

### Installation
$ Install from source:
./configure
make
make install

$ dpkg -i pkg.deb               => install a package (Debian)
$ dpkg -l                       => display installed package in system
$ dpkg -l program               => display program info

$ apt-cache search program      => for search program
$ apt-cache show program        => for show detail of prog and dependent library
$ sudo apt-get install program  => install program
$ sudo apt-get update           => update package repository
$ sudo apt-get upgrade          => upgrade program in system
$ sudo apt-get remove program   => remove program
$ sudo apt-get build-dep prog   => only install dependent package of program

$ rpm -Uvh pkg.rpm              => install a package (RPM)
$ rpm -ivh pkg.rpm              => same install package
$ rpm -evh pkg                  => erase package pkg

* option 'vh' for display percent while processing of command

### File permissions
$ chmod octal file  
=> change the permissions of file to octal
,which can be found separately for user,group
, and world by adding;

4 - read (r)
2 - write (w)
1 - execute (x)

Examples:
$ chmod 777              => read, write, execute for all
$ chmod 755              => rwx for owner, rx for group and world
$ chmod ugo+|-rwx        => u = owner, g = group , o = other
$ chown user.group file  => change to another user and group

For more options, see man chmod

### Forward output and something special
- ";" => run command finished can run command2 automatic by use ';'

Example:
$ chmod o+w file1 ; chmod o-rx file1

- "`" => is call 'backtrix' sinagure
Example:
$ passwd `whoami` => for change password current user logined
$ apt-get install linux-headers-lbm-`uname -r`-xen

$ !!    => repeates the last command
- "&&"  => same ";"
- "&"   => run program finished and can do any thing with that shell

Example:
$ gclctool &  => run calculator and can use that shell to any thing

- ">"   => forward output

Example:
$ ls -l > file1 => output of ls -l overwirte into file1 if exist file1

- ">>"  => forward output append

Example:
$ ls -l >> file1 => output of ls -l append into file1 if exist file1

- "\"   => use in long command

Example:
$ chmod o+w  file1 \
;chmod o-rx file1 \
;chmod o+rx file1

- "<"   => don't explain ^^' it reverse with ">"

Example:
$ mysql user < file1 =""> for check md5sum

- 0|1|2 => get type of output 1 is valid output, 2 is error output , 0 all output

Example:
$ ./configure 2> file => for get only error output into file

### Application and Utility
$ lsusb|lspci    => display device usb or pci type
$ fdisk          => display disk device
$ mount|unmount  => mount partition

Example:
$ mount -a       => mount all device in /etc/fstab

$ script file    => use record keylogger and press ^D for stop
$ ttyrec file    => third party program same 'script' it for record keylogger
$ ttyplay file   => display file so create by ttyrec
$ alias 'alias=command'  => for create alias instead long command

Example:
$ alias 'll=ls -l'

$ unalias alias  => cancel alias name

Example:
$ unalias ll

$ chroot => program for change eviroment for work

Sample chroot: when u use live cd if u must to use enviroment real system
Useful chroot: use chroot in live cd to correct real enviroment system but u must know path for config file

$ history  => for display all old command

### Administration tools
$ sudo|gksudo  => became to root and do it
note: edit permission sudo config file /etc/sudoers
$ su    => switch to root
$ sudo visudo  => can use this command only debian base

$ shutdown -h now|poweroff  => shutdown machine can can define time for shutdown by number minute instead "now"
$ shutdown -r now|reboot    => for reboot machine can define time for rebott

- setting ip /etc/network/interface
- setting dns /etc/network/resolve.conf
- cron , crontab

Example:
$ crontab -l => list task in cron of current user
$ crontab -e => for edit cron can see detail in wiki enter * 5 char , * is every day)
$ sudo crontab -e => crontab for superuser mode
note: if for some command need permission root , solve by crontab -e by sudo sample 'sudo crontab -e'

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
*  *  *  *  *  command to be executed

Example:
$ crontab -e
* * * * * touch ~/test-cron.txt

$ sudo adduser|useradd user => add user i recommened 'adduser' it will be create home for user
$ sudo deluser user1        => remove user
note: u can check exist user by "ls /home"

### Text editor
- pico , nano
- vi stantdard editor in unix system
- searching      => default mode type '/word', press n|shift+n for find next and previous
- delete line    => default mode type 'dd'
- save file      => default mode ':w file'
- exit file      => default mode ':q!'
- save and exit  => default mode ':wq file'
- go to line     => default mode n+Shift+G => n is number of line to go
Note: '=>' = mean

Related Post:
References:

1 comment:

  1. ชอบบทความของ คุณ JuUieR มากครับ..
    ผมในฐานะที่เพิ่งศึกษาลินุกซ์ (เริ่ม อูบุนตู 8.10) ก็อยากศึกษาเพิ่มเติมพร้อมกับเขียนบล๊อกตัวเองกันลืม ได้มีโอกาสมาเจอบล๊อกนี้เข้า เป็นแนวทางที่ผมคาดหวังไว้เลยครับ... เลยขออนุญาตินำบทความนี้ ไปแปะไว้ในบล๊อกผมนะครับ (ให้เครดิตไว้แล้วครับผม) ขอบคุณล่วงหน้าครับ

    ReplyDelete

Popular Posts