Linux-Shell-常用命令和脚本

43次阅读

共计 3513 个字符,预计需要花费 9 分钟才能阅读完成。

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