MY unix commands

tail a file

tail -100f FileName

-f to allow file monitoring

less command

Another way is to watch file using less +F FileName to wait for new data. use ctrl+c to exit to the normal less mode to do search/page uptown etc. once down, we can hit F again to the watch mode.

less only opens chunk of file so should be more memory friendly than vim when opening large file. And its command set is very similar to vi. The con is the syntax highlight is not that good comparing to vim.

check process

ps -f -p ThePID

-f full all columns

-p by process ID

ps -e -f

-e all processes

ps -ef | grep yourkeyword

to search for process

if the CMD is very long like running java command with long classpath and param. use

-ww

or

ww

Wide output. Use this option twice for unlimited width.

Extract/Compress file

tar -xvf YOUR.tar.gz

xf are required: The -x (for extract). The -f indicates that the next argument (i.e., piece of input data in a command) will be the name of the new archive file.

The -v (i.e., verbose) option is commonly used together with the -c/-x and -f options in order to display a list of the files that are included in the archive.

-c instructs tar to create an archive:

tar -cvf file.tar file1 file2 file3   OR  tar -cf * to compress all.

The -t option tells tar to list the contents of an uncompressed archive without performing an extraction. Thus, the following would list the contents of file.tar:

tar -tf file.tar

More reference like delete/add file to tar.

remove file/direcotry

rm to remove file. To remove non-empty directory:

rm -rf dirToBeRemoved/
  1. -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
  2. -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions

disk usage

df -h

-h: human readable display

check directory/fold size

du -sh folderName

with out ‘s’, it would print out size recursively

find difference for two files

Option 1: diff -y file1 file2

-y will display output in two columns. More options about diff

-i is also handy when we want to ignore case

To get only diff, we can add a pipe and grep lines with ‘|’ like:

diff -y file1 file2 | grep -E ".*(\||>).*"

-E in grep means: Interpret PATTERN as an extended regular expression

Option2: grep -vf file1 file2

-v Invert the sense of matching, to select non-matching lines.

-f Obtain patterns from FILE, one per line.

so the above command will output the lines in file2 but not in file1. And we can run the reverse: grep -vf file2 file1 to get the lines in file1 but not in file2.

The first param could also be just a String  like: ‘grep -v apple fruitList.txt’ will get all lines in fruitList.text that is not apple.

grep man page

find string in current directory

grep -nr yourString* .

The dot at the end searches the current directory. Meaning for each parameter:

'-n'          Show relative line number in the file
'yourString*' String for search
'-r'          Recursively search subdirectories listed
'.'           Directory for search (current directory)

find code line number

find . -type f -name "*.java" -or -name "*.xml" | xargs cat | wc -l

list who is listening a port

lsof -i :PORT_NUMBER

OR

netstate -anp | grep PORT_NUMBER

Shutdown and reboot

use ‘halt’ and ‘reboot’ to do shutdown/reboot in cmd.

 replace all in VI

:%s/search_string/replacement_string/g

verfiy http/https connectivity

HERE

For general ip/port connectivity test, use nc(netcat)

nc -zv YourIp YourPort

-z : Specifies that nc should just scan for listening daemons, without sending any data to them. It is an error to use this option in conjunction with the -l option.

-v: verbose

change permissions

HERE

create link(ln) to file/directory

HERE

about sudo/su

HERE

about make file immutable

With chattr, we can make a file immutable or append-only. We can use lsattr to view the attritbutes.

HERE

run bat from intellij

External tools

The External Tools feature allows you to:

  • add custom menu items to run external programs, scripts, .bat files, etc.
  • parametrize custom commands with a rich set of macros, such as path to the currently opened file in the editor, project classpath, project output path, etc.
  • assign custom shortcuts to the most frequently used external tools

 

It is under: IDE settings -> external tool

 

This page shows the detail of the options.

copy file through ssh

I use winscp or secure shell client.

 

sftp is (and works) similar to ftp

scp is a neat little program:

copy from a remote machine to my machine:
scp user@192.168.1.100:/home/remote_user/Desktop/file.txt /home/me/Desktop/file.txt

copy from my machine to a remote machine:
scp /home/me/Desktop/file.txt user@192.168.1.100:/home/remote_user/Desktop/file.txt

copy all file*.txt from a remote machine to my machine (file01.txt, file02.txt, etc.; note the quotation marks:
scp “user@192.168.1.100:/home/remote_user/Desktop/file*.txt” /home/me/Desktop/file.txt

copy a directory from a remote machien to my machine:
scp -r user@192.168.1.100:/home/remote_user/Desktop/files /home/me/Desktop/.

 

 

SCP Introduction

scp stands for secure cp (copy), which means you can copy files across ssh connection. That connection will be securely encrypted, it is a very secure way to copy files between computers

You can use scp to copy files from or to a remote server. You can also copy files from one remote server to another remote server, without passing traffic through your PC.

You can use scp on Linux, Mac and Windows (using WinSCP).

SCP Usage

scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]
from-host
Is the name or IP of the host where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command
user
Is the user which have the right to access the file and directory, that is supposed to be copied in the case of the from-host, and the user who has the rights to write in the to-host
source-file
Is the file or files that are going to be copied to the destination host, it can be a directory but in that case you need to specify the -r option to copy the contents of the directory
destination-file
Is the name that the copied file is going to take in the to-host, if none is given all copied files are going to keep its names

SCP Options

-p
Preserves the modification and access times, as well as the permissions of the source-file in the destination-file
-q
Do not display the progress bar
-r
Recursive, so it copies the contents of the source-file (directory in this case) recursively
-v
Displays debugging messages

netstat command show listening ports

Netstat is a useful tool for checking network and Internet connections. Some useful applications for the average PC user are considered, including checking for malware connections.

 

Syntax and switches

The command syntax isnetstat [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-t] [-v] [interval]A brief description of the switches is given in Table I below. Some switches are only in certain Windows versions, as noted in the table..Note that switches for Netstat use the dash symbol “-” rather than the slash “/”.

Table I. Switches for Netstat command
Switch Description
-a Displays all connections and listening ports
-b Displays the executable involved in creating each connection or listening port. (Added in XP SP2.)
-e Displays Ethernet statistics
-f Displays Fully Qualified Domain Names for foreign addresses. (In Windows Vista/7 only)
-n Displays addresses and port numbers in numerical form
-o Displays the owning process ID associated with each connection
-p proto Shows connections for the protocol specified by proto; proto may be any of: TCP, UDP, TCPv6, or UDPv6.
-r Displays the routing table
-s Displays per-protocol statistics
-t Displays the current connection offload state, (Windows Vista/7)
-v When used in conjunction with -b, will display sequence of components involved in creating the connection or listening port for all executables. (Windows XP SP2, SP3)
[interval] An integer used to display results multiple times with specified number of seconds between displays. Continues until stopped by command ctrl+c. Default setting is to display once,

 

 

Checking TCP/IP connections

TCP and UDP connections and their IP and port addresses can be seen by entering a command combining two switches:  netstat -an

JAVA_HOME in windows 7

Windows:

  • The SetX command would do so:

    setx /M JAVA_HOME “C:\Program Files (x86)\Java\jdk1.6.0_17”

    /m means machine level.

  • To check the settings, use “set” command for all the variables.

    Use “echo %JAVA_HOME%” to check it specifically.

  • To append to the PATH:

    setx path “%PATH%;c:\home” /m

Here is a good site for windows commands 

 

Unix

In Unix, go to “/etc/profile” and add “export PATH=$PATH:/usr/java/jdk1.5.0_07/bin”.


			

open MySQL in CentOs and auto start

* To start mysql server:

/etc/init.d/mysqld start

* To stop mysql server:

/etc/init.d/mysqld stop

* To restart mysql server

 /etc/init.d/mysqld restart

Tip: Redhat Linux also supports service command, which can be use to start, restart, stop any service:

# service mysqld start
# service mysqld stop
# service mysqld restart

To make it auto start:
Use: sudo /sbin/chkconfig mysqld on

To Check: chkconfig --list

To stop a service from auto starting on boot


  • sudo /sbin/chkconfig mysqld off