lunixshell基础经常使用整理

it2025-07-10  9

1   ps  -ef    显示正在执行的进程,pid 等信息  UID PID PPID C STIME TTY TIME CMD root 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    可读显示磁盘空间   M是兆 Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 5.0G 12G 31% / /dev/sda1 289M 16M 258M 6% /boot tmpfs 506M 0 506M 0% /dev/shm 3  借助df命令,能够方便地了解磁盘是否已空间不足,系统管理员须要了解空间不足时该怎么办。

 我们能够使用du命令 ,du命令显示特定文件夹的磁盘使用情况,这是推断系统是否存在磁盘 占用大户的快捷方法。 du    ,   du  -h(显示M。G单位)  ,du  -c (汇总),du  -s(汇总全部。仅仅显示一个) du [ -a | -s ] [ -k ] [ -m ] [ -g ][ -l ] [ -r ] [ -x ] [ -H | -L ][ File ... ]  8.0K ./test_flume/.flumespool 24K ./test_flume 8.0K ./mnt 58M ./awk 28K ./shell 18M ./spark 76M . 4   sort  (排序)  sort  file.txt 默认按字符排序 数字排序   sort -n  file.txt sort -n file2 0 2 3 5 6 7 9 17 sort  -M  file3.txt  (依照月份排序) sort -t ':' -k 3 -n /etc/passwd  按指定分隔符':" 第三个字段进行排序  按数字排序的 du -sh * | sort -nr  按空间占用从大到小 排序  包含目录和文件    r是指降序排列 5   gzip :用于压缩文件 gzip  2.sh gzip  file* gzip -r  test1  test2  压缩2个文件 tar  -cvf  my.tar  my.sh    tar压缩 tar -xvf  my。

tar      tar解压

tar  -zxvf filename.tgz 7 打印环境变量   printenv  bash test=testing echo $test 7  finger  llisc /usr/sbin/groupadd shared /usr/sbin/usermod -G shared test 文件权限表    755  chmod o+r  newfile  将条目读取加入到不论什么人 chmod u-x newfile  将条目山下湖用户拥有的运行权限 chmod u+x newfile  chown  改动用户文件拥有者 和所在组 9 shell   rpm  -qa | sort  | more  echo $?

if   表达式  返回0  运行成功 then fi if   test [ condition ] for  var  in Li72 Alibaba taobao Newbatilr do   echo the state $test done 使用转移字符反斜杠符合来转移单引號 使用双引號来定义单引號的值 #!/bin/bash for 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 " fi done #!/bin/bash for(( i=1; i<= 10; i++)) do  echo " The next number is $i" done #!/bin/bash  var1=10 while [ $var1 -gt 0 ] do   echo $var1   var1=$[ $var1 - 1 ] done #!/bin/bash var1=100 until [ $var1 -eq 0 ] do  echo $var1   var1 =$[ $var1 -25 ] done #!/bin/bash for((a =1; a<=3; a++)) do   echo "starting loop $a:" for((b=1;b<=3;b++))   do   echo " inside loop: $a*$b " done done break contince #!/bin/bash for(( a=1; a<4 ;a++)) do   echo "Outer loop :$a"  for((b=1;b<100;b++)) do  if [ $b -gt 4 ] then   break 2 fi echo "Inner loop :$b" done done > test.txt   重定向到一个文件         |  sort   排序 參数  位置參数 $0 为为程序名称  $1 为第一个參数 $2 为第二个參数 $3 为第三个參数 #!/bin/bash factorial=1 for((number =1;number<= $1;number++)) do   factorial=$[ $factorial * $number ] done echo  The factorial of $1 is  $factorial name =` basename $0` 获取执行shell 脚本名称 if   [ -n "$1" ] 推断有没有传入參数 if  [ $# <5 ] 推断传入參数个数  获取最后一个參数的值  params =$# echo  parm : $params 或者 The  last   paramer ${!#}      $* 把全部的參数当作一个參数   $@ 把全部參数当作字符串  分开处理 #!/bin/bash while [ -n "$1" ]  do     case  "$1" in -a) echo "Found the -a  option" ;; -b) echo "Found the -b option" ;; -c) echo "Found the -c option";; *) echo "$1 is not an option";;   esac  shift done #!/bin/bash echo -n 'Enter your name :" read name echo "Hello $name,  welcome to my prorram ." #!/bin/bash read -p "Please enter your age :" age days =$[ $age * 365 ] echo " That makes you over $days days old !" ls -al test test22 test3 badtest &>test7 #!/bin/bash echo "This is an error " >&2 echo "This is normal output " #!/bin/bash exec 1>testout echo "This is a test of redirting all output" echo "without having to redirect every line " #!/bin/bash exec 2>testerror echo "This is 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/bash exec 0<testfile2 count=1 while read line do    echo "Line #$count :$line"   count=$[ $count +1 ] done lsof -a -p $$ -d 0,1,2 重定向到空文件   ls -al >/dev/null cat /dev/null > testfile  同一时候记录文件和打印到屏幕 date | tee testfile 追加数据 date | tee -a testfile kill -9   进程id #!/bin/bash trap "echo HaHa " SIGINT SIGTEERM echo "THis is a test program " count=1 while [ $count -le 10 ] do   echo "Loop #$count" sleep 10 count=$[ $count +1  ] done  echo "This is the end of test program " 把命令在后台执行用  & sh 37.sh & ps  au 有时须要从终端启动会话,然后让脚本在结束之前以后台模式执行,即使退出终端会话有时如此 能够用nohup jobs jobs -r jobs -s jobs -l nice  设置优先级 nice -n 10 sh 38.sh >test4out & at  命令 batch  命令 cron  表格 #!/bin/bash time=`date +%T` echo "This script ran at $time " echo "This is the end of the script">&2 corn   表格使用特殊格式制定作业的时间。

15 10 * * * command 查看登陆用户配置的定时任务 crontab -l  编辑任务 corntab -e  #!/bin/bash function func1 {   echo 'This is an example of a function"   } count=1 while [ $count -le 5 ] do  func1  count=$[ $count +1 ] done  echo "This is the end of the loop" func1  echo "Now  this is the end of the script " 函数的返回值 $? #!/bin/bash func1(){ echo "trying to display a non" ls -ls 41.sh } echo "testing the function:" func1 echo "The exit status is :$?

"

#!/bin/bash function db1 { read -p "Enter a value :" value  echo "doubling the value "  return $[ $value * 2 ] } db1  echo "The new value is $?" 函数返回值 #!/bin/bash function db1{ read -p "Enter a value :" value echo $ [ $value *2 ] } result='db1' echo 'The new value is $result" 提供函数文件    能够用source  命令或称(点操作符)将现有的库函数引进.bashrc中 source   /home/li72/libraries/myfuncs .    /home/li72/libraries/myfuncs function addem   { echo $[ $1 +$2 ] } function multem {   echo $[ $1 * $2 ] } function divem {   if [ $2 -ne 0 ]   then     echo $[ $1 /$2 ] else     echo -1 fi } #!/bin/bash . ./myfuncs result=`addem 10 16` echo "The result is $result" .bashrc  每次启动shell 都会检查该文件  ,在已有文件的末尾加入自己定义函数 .  /home/bigdata/test/shell/myfuncs 文本处理 sed    luinx 编辑替换 gawk echo "This is a test " | sed 's/test/big test/' cat>dog.txt 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 . 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 . 从文件读取编辑器命令  cat>script s/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 $5 "'s userid is " $1 } 加入用户 useradd xuhui 改动password usermod  -p xuhui xuhui 查看password文件 tail -l /etc/shadow cd -  返回上一个訪问的文件夹 查找文件命令 find  [路径]  [选项]  [操作] find . -name 'test*'    查找 文件名为 test开头的 find . -mtime -90 -print  查找更改时间在90天内的文件 grep  [选项]  [模式]  [文件] 在wodl.txt  找World单词 grep -w  'World' wodl.txt

转载于:https://www.cnblogs.com/bhlsheji/p/5080382.html

最新回复(0)