Author Topic: Basic command for unix มีประโยชน์ ครับผม  (Read 27350 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Basic command for unix มีประโยชน์ ครับผม
« on: มกราคม 24, 2011, 01:35:29 PM »
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

Example:
$ scp file1 user@somehost:/opt    => copy file1 to /opt of remote 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)

### 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)
note: if for some command need permission root , solve by crontab -e by sudo sample 'sudo crontab -e'

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

$ sudo adduser|useradd user => add 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

References:

    * Tranning 13 of ubuntuclub.com
    * Clil command picture by Teddy in ubuntuclub.com
    * and juuier a bit
    * http://www.oreillynet.com/linux/cmd/
    * http://linuxcommand.org/

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: Basic command for unix มีประโยชน์ ครับผม
« Reply #1 on: มีนาคม 02, 2015, 03:08:42 PM »
#linux centos  , redhat

force copy and replace file
#yes | cp -rf  test1  /home/test/.

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: Basic command for unix มีประโยชน์ ครับผม
« Reply #2 on: มีนาคม 21, 2018, 05:56:13 PM »
ปกติเราสามารถเทสการ download จากต้นทางเพื่อดู ปริมาณในการ download ได้ผ่าน command iperf3 ครับ ซึ่งติดตั้งที่ client ที่เป็น window หรือ linux หรือ unix ได้
#iperf3.exe -c ping.online.net -R -p 5205

Connecting to host ping.online.net, port 5205
Reverse mode, remote host ping.online.net is sending
[  4] local 192.168.10.200 port 50851 connected to 62.210.18.40 port 5205
[ ID] Interval           Transfer     Bandwidth
[  4]   0.00-1.00   sec  94.1 KBytes   771 Kbits/sec
[  4]   1.00-2.00   sec   151 KBytes  1.24 Mbits/sec
[  4]   2.00-3.00   sec   251 KBytes  2.06 Mbits/sec
[  4]   3.00-4.00   sec   188 KBytes  1.54 Mbits/sec
[  4]   4.00-5.00   sec   251 KBytes  2.05 Mbits/sec
[  4]   5.00-6.00   sec   188 KBytes  1.54 Mbits/sec
[  4]   6.00-7.00   sec   251 KBytes  2.06 Mbits/sec
[  4]   7.00-8.00   sec   188 KBytes  1.54 Mbits/sec
[  4]   8.00-9.00   sec   188 KBytes  1.54 Mbits/sec
[  4]   9.00-10.00  sec   251 KBytes  2.06 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-10.00  sec  2.17 MBytes  1.82 Mbits/sec    0             sender
[  4]   0.00-10.00  sec  2.08 MBytes  1.74 Mbits/sec                  receiver

iperf Done.