linux: useful commands

start time

1.uptime

16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00

2. date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"


3. cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("系统已运行:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'

Sed

//delete the first six char in every line

sed -i 's/^......//' tokenized-documents

//delete :

sed -i 's/://' tokenized-documents

//delete all spaces

sed -e 's/ //g' tokenized-documents > tokenized-documents.trimspace

//replace [ to \t

sed -i 's/\[/\t/g' tokenized-documents.trimspace

 

 

awk

add line no to file

awk '{ print FNR "\t" $0 }' testfile

//split every line in questions file and produce new file named with $1 for every line

//which content is $2

#cat questions | awk -F '\001' '{print $2 > $1}'

//the reverse produce

#awk '{print FILENAME "\t" $0 > "qestions"}' *

http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html

Splite huge file to chunks

#split --lines=1 -d  questions q_

remote call shell script

#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done

compare files in two directories

diff --brief -r dir1/ dir2/
diff -qr dir1/ dir2/

 

find files and copy to somewhere

find . -iname '*.jar' -exec cp {} ./jars \;

 

ZIP

# zip -r archive_name.zip directory_to_compress

# unzip archive_name.zip

TAR

# tar -cvf archive_name.tar directory_to_compress

# tar -xvf archive_name.tar.gz

# tar -xvf archive_name.tar -C /tmp/extract_here/

TAR.GZ

# tar -zcvf archive_name.tar.gz directory_to_compress

# tar -zxvf archive_name.tar.gz

# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

TAR.BZ2

# tar -jcvf archive_name.tar.bz2 directory_to_compress

# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

To scrab a picture, use the following command

#gnome-screenshot -i

convert dos text format (newlines CR/LF) to linux format

#dos2unix  -n sourcefile.text  targetfile.text

http://www.cyberciti.biz/faq/howto-unix-linux-convert-dos-newlines-cr-lf-unix-text-format/

Find strings in files

find . | xargs grep 'string' -sl

find . -iname '*php' | xargs grep 'string' -sl

find . -iname '*php' -mtime -1 | xargs grep 'string' -sl

find . -iname '*php' -mtime +1 | xargs grep 'string' -sl

String replace

//replace matched string

sed -i 's/old-word/new-word/g' *.txt

//replace line

sed -i '/nameNode/c\nameNode=hdfs://192.168.122.1:2014' examples/apps/sqoop/job.properties  

//combined with grep
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

//combined with find

find examples/ -name 'job.properties' |xargs sed -i '/jobTracker/c\jobTracker=192.168.122.1:2015'

bash history

Make an entry as follows in .bash_profile to save export command permanently
export HISTTIMEFORMAT='%F %T  '
export HISTIGNORE='ls -l:pwd:date:'
export HISTCONTROL=ignoredups
HISTSIZE=0  //not keep history of commands 
#history -c   //clear history
#history | grep pwd

Search previously executed command with ‘Ctrl+r’ command
Once you’ve found the command you’re looking for, press ‘Enter‘ to execute 
the same else press ‘esc‘ to cancel it.
see:http://www.tecmint.com/history-command-examples/

start time

1.uptime

16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00

2. date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"


3. cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("系统已运行:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}'

Sed

//delete the first six char in every line

sed -i 's/^......//' tokenized-documents

//delete :

sed -i 's/://' tokenized-documents

//delete all spaces

sed -e 's/ //g' tokenized-documents > tokenized-documents.trimspace

//replace [ to \t

sed -i 's/\[/\t/g' tokenized-documents.trimspace

 

 

awk

add line no to file

awk '{ print FNR "\t" $0 }' testfile

//split every line in questions file and produce new file named with $1 for every line

//which content is $2

#cat questions | awk -F '\001' '{print $2 > $1}'

//the reverse produce

#awk '{print FILENAME "\t" $0 > "qestions"}' *

http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html

Splite huge file to chunks

#split --lines=1 -d  questions q_

remote call shell script

#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done

compare files in two directories

diff --brief -r dir1/ dir2/
diff -qr dir1/ dir2/

 

find files and copy to somewhere

find . -iname '*.jar' -exec cp {} ./jars \;

 

ZIP

# zip -r archive_name.zip directory_to_compress

# unzip archive_name.zip

TAR

# tar -cvf archive_name.tar directory_to_compress

# tar -xvf archive_name.tar.gz

# tar -xvf archive_name.tar -C /tmp/extract_here/

TAR.GZ

# tar -zcvf archive_name.tar.gz directory_to_compress

# tar -zxvf archive_name.tar.gz

# tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

TAR.BZ2

# tar -jcvf archive_name.tar.bz2 directory_to_compress

# tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

To scrab a picture, use the following command

#gnome-screenshot -i

猜你喜欢

转载自ylzhj02.iteye.com/blog/2042224