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

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. 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

OR

netstat -anp | grep PORT_NUMBER

OR

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

:%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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s