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




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

to see all the directory and sort by size reversely:

du -sh * | sort -nr

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 -nir yourString* .

The dot at the end searches the current directory. If we want to search in specific file pattern or extensions, we can use the --include option, If i need to search all 8081 port from certain files in my react native project: i can do grep -nir --include=\*native\*.{java,js,m,pbxproj} '8081' . I escape * here just because fish shell treats it specially. If you use bash, you do not need to.

Meaning for each parameter:

'-n'          Show relative line number in the file
'yourString*' String for search
'-r'          Recursively search subdirectories listed
'-i'          ignore case
'.' 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


netstat -anp | grep PORT_NUMBER


fuser PORT_NUMBER/tcp

and to kill these processes, just add -k: fuser -k 10000/tcp

Shutdown and reboot

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

 replace all in VI


verfiy http/https connectivity


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


create link(ln) to file/directory


about sudo/su


about make file immutable

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


zip multiple files/directories

zip -r ./dir1 ./dir2 file1 file2


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@ /home/me/Desktop/file.txt

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

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

copy a directory from a remote machien to my machine:
scp -r user@ /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]
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
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
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
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

Preserves the modification and access times, as well as the permissions of the source-file in the destination-file
Do not display the progress bar
Recursive, so it copies the contents of the source-file (directory in this case) recursively
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


  • 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 



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