linux check in命令,Linux Shell 常用命令和脚本

Replace contents by using sed

# Replace all

sed -i "" "s|pattern|replace|g" "file.ext" # osx

sed -i"" "s|pattern|replace|g" "file.ext" # linux

# Replace the first line string

sed -i "" "1s|pattern|replace|g" "file.ext" # osx

sed -i"" "1s|pattern|replace|g" "file.ext" # linux

# Replace with \t

sed -i "" $'1s|pattern|\t|g' "file.ext" # osx

sed -i"" $'1s|pattern|\t|g' "file.ext" # linux

Delete files or folders except the specified one

rm -rf !("Except 1"|"Except 2"|"Except N")

'=' and '==' in if statement

#!/bin/bash

if [ "foo" = "foo" ]; then

echo expression evaluated as true

else

echo expression evaluated as false

fi

# or

#!/bin/bash

if [[ "foo" == "foo" ]]; then

echo expression evaluated as true

else

echo expression evaluated as false

fi

Get the modified time of file

# osx

stat -f "%Sm" -t "%Y%m%dT%H%M%S" filename # datetime

stat -f "%Sm" -t "%s" filename # timestamp in seconds

# linux

stat -c %y filename # datetime

stat -c %Y filename # timestamp in seconds

Datetime and timestamp

# Datetime

echo `date '+%Y-%m-%d %H:%M:%S.%N'`

# Timestamp in seconds

echo `date '+%s'`

# Elapsed time

STARTs=`date '+%s'`

STARTn=`date '+%N' | sed 's/^0*//'`

echo "Do something"

ENDs=`date '+%s'`

ENDn=`date '+%N' | sed 's/^0*//'`

SECS=$(($ENDs - $STARTs))

NANO=$(($ENDn - $STARTn))

if (( $NANO < 0 )); then

NANO=$((1000000000 + $NANO))

SECS=$(($SECS - 1))

fi

echo "Cost $SECS.$NANO seconds."

Check os type

if [[ "$OSTYPE" == "linux-gnu" ]]; then

# ...

elif [[ "$OSTYPE" == "darwin"* ]]; then

# Mac OSX

elif [[ "$OSTYPE" == "cygwin" ]]; then

# POSIX compatibility layer and Linux environment emulation for Windows

elif [[ "$OSTYPE" == "msys" ]]; then

# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)

elif [[ "$OSTYPE" == "win32" ]]; then

# I'm not sure this can happen.

elif [[ "$OSTYPE" == "freebsd"* ]]; then

# ...

else

# Unknown.

fi

Read file from arguments or stdin

content=""

while read line

do

content="${content}\n${line}"

done < "${1:-/dev/stdin}"

echo $content

#or

file=${1--}

content=$(cat $file)

echo $content

The substitution ${1:-...} takes $1 if defined,

otherwise the file name of the standard input of the own process is used.

Function return value

# Define a function

myfunc() {

return 1

}

# Call the function

myfunc

# Get the result from the last command,

# here is the result of `myfunc`

result=$?

Remove duplicate string(no comma contains) from list

for string in $list; do unique[$string]=1; done

echo ${!unique[@]}

Read file

# entire file

content=`cat filename.txt`

echo $content

# read line by line

while IFS='' read -r line || [[ -n "$line" ]]; do

echo "Text read from file: $line"

done < filename.txt

Batch rename as numeric

n=1

for i in $(ls | sort -n);

do

newfile=$(printf "%03d%s" $n .${i##*.})

mv "${i}" "${newfile}"

echo "${i} -> ${newfile}"

((++n))

done

Format number

printf "%05d\n" 52

Remove string chars

original="Hello World"

world="${original:6}"

hell="${original::4}"

hello="${original::-6}"

lo_wo="${original:3:-3}"

For loop

# For loop

for (( i=0; i<10; i++ )); do echo "$i"; done

# For loop range

# {START..END..INCREMENT}

for i in {1..10..2}; do echo "$i"; done

# For loop through files with spaces

for i in *; do echo "$i"; done;

for i in *.ext; do echo "$i"; done;

# For loop through ls results only show extension

for i in $(ls); do echo "${i##*.}"; done;

# For loop through ls results only show filename

for i in $(ls); do echo "${i%.*}"; done;

# For loop through ls results sort as numeric

for i in $(ls | sort -n); do echo "$i"; done;

List files as numeric sort

ls | sort -n

Batch rename

for i in *.ext;

do mv "$i" "${i%.ext}.newext";

done

Modify permission

sudo chown -R root:wheel [file|dir]

Lock & Unlock (macOS)

# lock

chflags -R uchg *

# unlock

chflags -R nouchg *

Delete unversioned files under svn

svn status --no-ignore | grep '^\?' | sed 's/^\? //'

svn status --no-ignore | grep '^\?' | sed 's/^\? //' | xargs -Ixx rm -rf xx

Find

# Find and delete empty folders

find . -type d -empty -delete

# Find over 100MB files and sort

find / -type f -size +100M -exec ls -lh {} \; | sort -n -r

# Find files between the specified dates and copy them to the specified folder

find . -type f -newermt '2018-05-25' ! -newermt '2018-05-26' | xargs -I {} cp {} "folder/"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值