[Linux] Linux 個人常用指令

Linux指令實在沒辦法都記下來, 放在網誌上做紀錄, 以後方便查

How to find file by giving time range?

find . -newermt "2014-09-30 12:00:00" ! -newermt "2014-09-30 15:00:00"

How to output shell variable string length?

# usage ${#variable}

words="this is a test"
echo ${#words} # result 14 length

How to transfer mass files from one server to antoher(not SCP way)

ssh $host 'cd $directory; find . -type f -name "some file name" | tar czvf - --files-from -' | tar zxvf - -C $path

How many files in a directory?

ls -1 $directory/ | grep -c ""

Generate Password

apg -n 1 -M NL -a 0 -m 10 -x 15 # Generate a random but human-readable password from 10 to 15 characters long made with only lower case letters and numbers

Check OS port usage (may work with grep)

netstat -tulnap

Problem: Argument list too long


起因: exec() 這個會太長(相當多指令會call到這個Linux function)
判斷: getconf ARG_MAX 會顯示有多少限制,  這是字串長度, 所以並不是檔案個數, 而是argument的總字串長度

(所以ls /home/xxx/yyy/* 會比 cd /home/xxx/yyy; ls * 要長很多)
**解決1:** 依照需求自己改成一筆一筆處理
for i in file.*
do
    rm “$i”
done
缺點:基本上比較有機會完成, 但是會太慢

**解決2:**

Change /usr/include/limits.h   but be careful

**其餘解(不一定會成功):**
Find
find . -name “file.*” -exec rm {} +
SCP
ssh $sourcehostname 'cd $sourceDir; find . -type f -name "*.txt" | tar czvf - --files-from -' | tar zxvf - -C $destinationDir
Remove files
find . -name "*.tmp" -delete
needs higher gnu find version (findutils >4.1.20)

Intersection

cat a b|sort|uniq -d

Check software install location (Rad Hat)

rpm -ql $soft_name | cat -n

Random sleep(within 10s)

sleep $(($(($RANDOM%10))+1))

Split filename & path

filename=$(basename $fullfile)

extension=${filename##*.}

filename=${filename%.*}

Show only dir

tree -d

Eliminate ^M

s/^M//g filename
 ^M: press ctrl+V + m

Search History

1. ctrl+r
2. Keyword

User connection Time

ac -d #per day

ac -p #every one

ac -y #per year

ac -d or -p  name #specific user time

File Status

stat filename

Check OS version

cat /etc/*release

Check CPU top 10 usage

ps -eo pcpu,pid,user,args | sort -n -k 1 -r | head -10

Check Dir. space

du -sh $directory_name

Improved Diff

diff -u file_1 file_2

Dynamic System Memory Usage

watch -n 1 -d free

Cpu Info.

cat /proc/cpuinfo

Hardware Info.

lspci

Tar copy with dir.

tar -cf - $directory | ssh $host tar -xf - -C $destination

Grep color

alias grep='grep --color'

Identify 32 or 64 bit OS?

getconf LONG_BIT

留言

這個網誌中的熱門文章

[Linux] Linux下查詢硬體記憶體資訊 Memory Information

[Other] Chrome 重新整理所有開啟頁面

[Python] Simple Socket Server