LUnix Shell Basics often use grooming

Source: Internet
Author: User
Tags tar unzip disk usage lunix

1 Ps-ef shows the process being executed, PID and other information
UID PID PPID C stime TTY time cmdroot 1 0 0 03:45? 00:00:02 Init [5]root 2 1 0 03:45?

00:00:00 [Migration/0]root 3 1 0 03:45? 00:00:00 [Ksoftirqd/0]root 4 1 0 03:45? 00:00:00 [Events/0]root 5 1 0 03:45?

00:00:00 [Khelper]root 6 1 0 03:45? 00:00:00 [Kthread]root 9 6 0 03:45?

00:00:00 [Kblockd/0]root 10 6 0 03:45? 00:00:00 [Kacpid]

2 df-h readable display disk space M is trillion filesystem Size used Avail use% mounted on/dev/sda2 18G 5.0G 12G 31%//dev/sda1 289M 16M 258 M 6%/boottmpfs 506M 0 506M 0%/dev/shm
3 with the DF command, it is easy to see if the disk is running out of space, and the system administrator needs to know what to do when there is insufficient space.

We are able to use the du command, which displays the disk usage of a particular folder, which is a quick way to infer whether the system has large disk occupants. Du, Du-h (show M. g units), Du-c (summary), Du-s (summary all. Show only one)du [-a |-S] [-K] [-M] [-g] [-l] [-r] [-X] [-H |-l] [File ...]
8.0K./test_flume/.flumespool24k./test_flume8.0k./mnt58m./awk28k./shell18m./spark76m.
4 sort (sort) sort file.txt
Sort numeric sort by character by default Sort-n file.txtsort-n file2023567917
Sort-m File3.txt (according to month sort)
Sort-t ': ' K 3-n/etc/passwd by the specified delimiter ': ' The third field is sorted by number
Du-sh * | Sort-nr by space occupy from large to small sort contains directories and files R means descending order
5 gzip: Use to compress files
Gzip 2.shgzip file*gzip-r test1 test2 compress 2 files

TAR-CVF My.tar my.sh tar compression tar-xvf my.

Tar tar unzip
TAR-ZXVF filename.tgz
7 Print environment variable printenv
Bashtest=testingecho $test
7 Finger Llisc
/usr/sbin/groupadd GKFX
/usr/sbin/usermod-g shared Test
File permissions Table 755



chmod o+r newfile Add entry read to no one
chmod u-x newfile the entry Mountain Lake users have permission to run chmod u+x newfile
Chown change user file owner and group

9 Shell
Rpm-qa | Sort | More
echo $?






The IF expression returns 0 run successfully thenfi

if test [condition]

For Var in Li72 Alibaba Taobao Newbatilrdo echo the state $testdone

Use a transfer character backslash to conform to a transfer single argument using a double argument to define the value of a single argument

#!/bin/bashfor file in/home/bigdata/test/*/home/li75/do if [-D "$file"]then echo "$file is a directory" elif [-F " $file "]then echo" $file is a file "Fidone



#!/bin/bashfor ((i=1; i<=; i++)) do echo "The next number was $i" done

#!/bin/bash var1=10while [$var 1-gt 0]do echo $var 1 var1=$[$var 1-1]done

#!/bin/bashvar1=100until [$var 1-eq 0]do echo $var 1 var1 =$[$var 1-25]done

#!/bin/bashfor ((a =1; a<=3; a++)) do echo "Starting loop $a:" For ((b=1;b<=3;b++)) do echo "Inside loop: $a * $b" Don Edone

Breakcontince

#!/bin/bashfor ((a=1; a<4; a++)) do echo "Outer loop: $a" for ((b=1;b<100;b++)) does if [$b-gt 4]then break 2fiecho  "Inner Loop: $b" Donedone > Test.txt Redirect to a file | Sort sorts

The number of parameters in the parameter is the name of the program, "$" for the first parameter, and the second parameter is $ three for the third parameter.

#!/bin/bashfactorial=1for (number =1;number<= $1;number++) do factorial=$[$factorial * $number]doneecho the Factor Ial of $ is $factorial


Name = ' basename $ ' gets execution shell script name


If [-n "] to infer that there are no incoming parameters
If [$# <5] infers the number of incoming parameters
Gets the value of the last parameter of the params =$#
echo Parm: $params or the last Paramer ${!#}

$* the full number of references as a reference [email protected] to separate all the parameters as strings

#!/bin/bashwhile [-N "$"] do case "$ in-a" echo "Found the-a option";; -B) echo "Found the-b option";; -c) echo "Found the-c option";;  *) echo "is not a option";; Esac Shiftdone




#!/bin/bashecho-n ' Enter your name: "Read Nameecho" Hello $name, Welcome to my prorram. "

#!/bin/bashread-p "Please enter your age:" Agedays =$[$age * 365]echo "This makes you over $days days old!"
Ls-al test test22 test3 badtest &>test7
#!/bin/bashecho "This is a error" >&2echo "This is normal output"
#!/bin/bashexec 1>testoutecho "This was a test of redirting all output" echo "without have to redirect every line"
#!/bin/bashexec 2>testerror
echo "This was the start of the script" echo "now reidirecting all output to another" exec 1>testout2
echo "This putput should go to the" echo "but shis should go to Testerror file >&2


#!/bin/bashexec 0<testfile2count=1while Read Linedo echo "line # $count: $line" count=$[$count +1]done
Lsof-a-P $$-D 0,1,2
Redirect to empty file Ls-al >/dev/null
Cat/dev/null > Testfile
Record files and print to screen at the same time
Date | Tee testfile
Append Data
Date | Tee-a testfile
Kill-9 Process ID


#!/bin/bashtrap "Echo HaHa" SIGINT Sigteermecho "This was a test program" Count=1while [$count-le]do echo "Loop # $c Ount "Sleep 10count=$[$count +1]done echo" This was the end of the test program "

Execute the command in the background &
SH 37.sh &
PS AU
Sometimes it is necessary to start a session from the terminal and then let the script execute after the end, even if exiting the terminal session can sometimes be used nohup
Jobsjobs-rjobs-sjobs-l
Nice set Priority
Nice-n sh 38.sh >test4out &


At Command cron table for batch command

#!/bin/bashtime= ' Date +%t ' echo ' This script-ran at $time "echo" This is the end of the script ">&2

The Corn table uses a special format to create the time of the job.


Ten * * * command
View scheduled tasks for logged-on user Configuration
Crontab-l
Edit Task Corntab-e

#!/bin/bashfunction func1 {echo ' This was an example of a function "}count=1while [$count-le 5]do func1 count=$[$cou NT +1]done echo "This was the end of the loop" func1 echo "Now this is the end of the script"
The return value of the function is $?
#!/bin/bashfunc1 () {echo ' trying to display a non ' ls-ls 41.sh}echo ' testing the function: ' Func1echo ' The exit status is: $ ?

"
#!/bin/bashfunction db1 {read-p "Enter a value:" Value echo "doubling the value" return $[$value * 2]}db1 echo "The N EW value is $? "

function return value
#!/bin/bashfunction db1{read-p "Enter a value:" Valueecho $ [$value]}
result= ' db1 ' echo ' The new value is $result '

The supplied function file can be used to import existing library functions using either the Source command or the (dot operator). BASHRC
Source/home/li72/libraries/myfuncs. /home/li72/libraries/myfuncs

function Addem {echo $[$ +$2]}
function Multem {echo $[$ * $]}
function Divem {if [$2-ne 0] then echo $[$/$2]else Echo-1fi}

#!/bin/bash. ./myfuncs
result= ' Addem ' echo ' The result is $result '
. BASHRC each time the shell is started, it checks the file, adding its own definition function at the end of the existing file.
. /home/bigdata/test/shell/myfuncs
Text processing sed luinx edit Replace
Gawk

echo "This is a test" | Sed ' s/test/big test/'
Cat>dog.txtthe quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
Sed ' s/dog/cat/' dog.txt The quick brown fox jumps over the lazy cat. The quick brown fox jumps over the lazy cat. The quick brown fox jumps over the lazy cat. The quick brown fox jumps over the lazy cat. The quick brown fox jumps over the lazy cat.

Sed-e ' > s/brown/green/> s/fox/elephant/> s/dog/cat/' Dog.txt
The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat.
Read Editor commands from file cat>scripts/brown/green/s/fox/elephant/s/dog/cat/

Sed-f Script Dog.txt
The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat.

Cat>script2{print $ "s userid is" $
Join the user
Useradd Xuhui Change passwordusermod-p xuhui Xuhui
View Password file tail-l/etc/shadow

CD-Return to the previous Access folder
Find File command
find [path] [options] [action]



Find. -name ' test* ' to find the file name at the beginning of test

Find. -mtime-90-print find files that change time within 90 days
grep [Options] [mode] [file]


Find World Word grep-w ' world ' wodl.txt in Wodl.txt


















LUnix Shell Basics often use grooming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.