Monday, January 27, 2014

Linux - Commands

JAR file extraction

- cd /where/you/want/to/extract/it
- jar xvf /path/to/jarfile.jar

text search

grep -r "search" .

file search

find /path/to/search -name 'fileName'

directory search

find httpdocs -type d

Find and kill a process that is using a particular port

- sudo netstat -lpn | grep :9083

tcp        0      0 0.0.0.0:9083            0.0.0.0:*               LISTEN      8950/java

- sudo kill 8950

delete all files which were modified 3 days ago

find /path/to/destination -mtime +3 -exec rm -f {} \;

download file by using curl

curl -O http://www.example.com/example.json

change default java version at CentOS

- alternatives --install /usr/bin/java java /usr/java/latest/bin/java 1
- alternatives --config java

set default editor

export VISUAL="nano"  / export EDITOR="nano"

the longest line of a file

awk '{ if (length($0) > max) {max = length($0); maxline = $0} } END { print maxline }' YOURFILE

length of the longest line of a file

awk '{ if ( length > L ) { L=length} }END{ print L}' YOURFILE

count number of matches per line

grep -o -n 'pattern' <filename> | cut -d : -f 1 | uniq -c

counting files in the current directory

ls -l | grep -v ^l | wc -l
ls *.log | wc -l

display a specific line from a file

- sed -n '20,+0p' file_name
- awk 'FNR==20' file_name

display specific lines from a file

- sed -n '20,+20p' file_name
- awk 'FNR>=20 && FNR<=40' file_name

Calculate size of files in shell

find . -name "*pattern*" -ls | awk '{total += $7} END {print total}'

add a string after each line in a file?

sed -e 's/$/string after each line/' -i filename

add a string to the beginning of each line in a file?

sed -e 's/^/prefix/' file

Turning multiple lines into one line with comma separated

paste -d, -s filename

check permissions

stat -c "%a %n" file/dir

find object which permission is not 755

find /usr/bin /sbin /bin -type f ! -perm 755

use '-pl' to build specific module

- mvn package -pl module1,module2
- mvn clean install -pl module1,module2

skip test (maven)

mvn -Dmaven.test.skip install

print first word

awk '{print $1}' filename

delete lines below current line in vim

dG

delete lines above current line in vim

dgg

remove ^M

sed 's/\r$//g' old_file > new_file

split a large file into small parts

split --bytes=100M big-file new-file

split a large file into small parts by lines

split -l 200000 filename

check the number of CPU cores

nproc

check the total size of the memory in GB

free -g

check the version of the CentOS

cat /etc/redhat-release

check the file system type

df -T

compare the contents of two files

xxd file1 > file1.hex
xxd file2 > file2.hex
diff file1.hex file2.hex

or

cat file1 | od -c > file1.od
cat file2 | od -c > file2.od
diff file1.od file2.od

lines between two line numbers

sed -n '3,6p' file-name

convert the charset of a text file

iconv -f latin1 -t UTF-8 file-name

show the first n charaters of each line

cut -c-50 file-name

how to remove first N characters in each line

:%s/^.\{0,N\}//

Check files/folders size including hidden ones

du -sch .[!.]* * |sort -h

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.