Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Monday, July 7, 2014

bash - history sharing between sessions


# .bash_rc
HISTSIZE=9000
HISTFILESIZE=$HISTSIZE
HISTCONTROL=ignorespace:ignoredups

history() {
  _bash_history_sync
  builtin history "$@"
}

_bash_history_sync() {
  builtin history -a         #1
  HISTFILESIZE=$HISTSIZE     #2
  builtin history -c         #3
  builtin history -r         #4
}

PROMPT_COMMAND=_bash_history_sync


history() overrides the original function to make sure that the history is synchronised before it is printed, so the numbers match.

Source | cheat sheet (pdf) | Nice article


Wednesday, June 25, 2014

bash - customizing

In the ".bashrc" file, you can add...


To add the time/date in the history:
HISTTIMEFORMAT="%d/%m/%y %T "

Shortcut for "ls -l":
alias ll="ls -l"

Shortcut for a fast maven clean build:
alias mvnci="mvn clean install -DskipTests -Dcheckstyle.skip=true"

Modify the prompt:
export PS1="\n\u@\h \w\n$ "

Always activate the color in grep:
export GREP_OPTIONS="--color"

Add magic to the space !cmd ...
bind Space:magic-space

Result:

__________________________.bashrc __________________________
# .bashrc
HISTTIMEFORMAT="%d/%m/%y %T "
export PS1="\n\u@\h \w\n$ "
export GREP_OPTIONS="--color"
alias ll='ls -l'
alias mvnci="mvn clean install -DskipTests -Dcheckstyle.skip=true"
bind Space:magic-space
__________________________.bashrc __________________________

Extra
To show line numbers in vi, modify the .vimrc (once inside vi, you can disable it if you want to copy the line without the number with: ":set number!"):
set number

Friday, April 11, 2014

dos2unix


To convert all the files :
find . -type f -print0 | xargs -0 dos2unix

To convert all the files except those in the « .svn » directories :
find . -type f -not -iwholename '*.svn*' -print0 | xargs -0 dos2unix 

Cygwin - Install (Windows)


www.cygwin.com

Add modules:
- dos2unix (Utils \ dos2unix)
- xsltproc (Libs \ libxslt)
- wget (Web \ wget)
- curl (Net \ curl)
- nc (Net \ nc)
- git (Devel \ git)
- svn (Devel \ subversion)
- ssh (Net \ openssh)
- scp (Net \ openssh)
- openssl (Net \ openssl)

Monday, March 17, 2014

Find path

Linux/UNIX/Mac OS X:
$ which java

Windows XP
for %i in (java.exe) do @echo.   %~$PATH:i

Windows since 2003
where java

Tuesday, February 4, 2014

bash

Comment = '#'
Shell   = '$'
Output  = '>'

## Print Working Directory
$ pwd


## Change Directory: to /directory
$ cd /directory
# Examples:
>/directory
# Change Directory, from: /directory to /
$ cd ..
>/
# Change Directory: previous directory
$ cd - 
# Change Directory: go to home
$ cd
# Or
$ cd ~
>~


## Display output one screen at a time
$ less file
$ more file


## Concatenate and print (display) the content of file(s)
$ cat file1
# Or
$ cat file1 file2
# Examples:
$ cat File1.txt File2.txt > union.txt
$ sort -u File1.txt File2.txt > unique_union.txt
# Put the contents of a file into a variable
$ my_variable=`cat File3.txt`


## Create a test file
$ echo "test" > test.txt


## Filename and Directory Completion
$ cd /opt/I[TAB]
> cd /opt/IBM
$ cd /opt/IBM[TAB][TAB]
>IBM/         IBMinvscout/


## List of Variablenames
$ $[TAB][TAB]


## Variablename Completion
$B[TAB]
>$BASH


## Print variable's value
$ echo $BASH_VERSION
> 4.2.45(1)-release


## Username's list
$ cd ~[TAB][TAB]


## Hostname's list
$ ssh @[TAB][TAB]


## Run a command script in the current shell
$ ./download-logs.sh


## Search the user's $path for a program file
$ which ssh
>/usr/bin/ssh


## Copy one or more files to another location
$ cp f1 f2


## Move or rename files or directories
$ mv old_name new_name


## TAR + GZIP
$ tar -czvf archive.tgz files/


## Create a symbolic link to a file or a directory
$ ln -s [OriginalSourceFile] [NewLinkFile]


## Change access permissions
# http://ss64.com/bash/chmod.html
$ chmod access file
# Examples:
$ chmod u+x script.sh
$ chmod a-x file
$ chmod go+rw file
# Read by owner
$ chmod 400 file
# Allow everyone to read, write, and execute file
$ chmod 777 file


## Change file owner and group
$ chown [user] [filename]
# Examples:
# Owner => root
$ chown root file
# Group => admin
$ chown :admin file
# Owner => root AND Group => admin
$ chown root:admin file


## Change group ownership
$ chgrp [group] [filename]
# Examples:
$ chgrp oracle /usr/database


## Change file timestamps
$ touch filename
# Exemples:
$ touch -d "2 hours ago" filename
$ find DIRECTORY -exec touch -d "2 hours ago" {} +
$ find /opt/phantomjs/tomcat/apache-tomcat-6.0.37/logs/ -type f -mtime +30 -exec rm -f {} \;


## Find a specific file
$ find . -type f -name log4j*
# Examples:
# Fin a file and change the modification date
$ find DIRECTORY -exec touch -d "2 hours ago" {} +


## Reload shell configurations
$ source ~/.profile
# Or
$ source ~/.bashrc


## Exceute last command starting by 'command'
$ !command


## Remote connection with SSH
$ ssh user@hostname
>password


## Copy a remote file
# Push:
$ scp location user@othermachine.com:destination
# Pull:
$ scp user@othermachine.com:location destination
# The '-r' parameter is used to copy an entire directory, or you may use * to match on multiple files/directories.


## Find a command from the history
$ history | grep command


## Maven
$ mvn clean package
## SVN
#Checkout
$ svn co https://sasvnd1.int.videotron.com/svn/...
# Update
$ svn up
## Delete a file
$ rm file
# Delete many files starting with the letter f
$ rm f*
# Delete a directory
$ rm -r directory


## Debug Apache/IHS configurations
httpd.exe -e debug -k start
# Restart Apache/IHS with new configurations
$ httpd.exe -k restart


## head & tail
# Print the first lines of a file (default: 10 lines)
$ head -100 file.log
# Print the last lines of a file (default: 10 lines)
$ tail -100 file.log
# Extract lines 40-50 from a file, first using head to get the first 50 lines then tail to get the last 10:
$ head -50 file.log | tail -10


## Load bash as default shell
$ vi .profile
# Add:
bash


## Add an alias in the bash
$ vi ~/.bashrc
# Add
alias ALIAS_NAME='command'
# Examples:
alias ll='ls -l'
alias tailf="tail -f SystemOut.log"


## Customize the shell prompt
$ export PS1="\n\u@\h \w\n$ "
# http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html


## Execute many commands in an "atomic" way
$ command1;command2
# Examples:
$ alias logs="cd /opt/IBM/WebSphere/AppServer/profiles/AppSrv01/logs;ls"
$ rm app; ln -s app_AAAAMMDD app


## Configure a "daemon"
$ crontab crontab_file
# Print the current crontab
crontab -l 
# To debug the daemon, we need the mail because the deamon will tell us if a task had a problem
mail


## Open a file in edit mode (or create it with a ':wq' at the end)
vi file


## Create a shell script
$ echo "#!/bin/bash" > script.sh
$ vi script.sh
$ chmod u+x script.sh


## Process viewer
$ top
# Or
$ ps
# Examples:
$ ps -ef | grep tomcat

See also:
http://ss64.com/bash/

Configure crontab (UNIX/Linux)


If you are about to modify a crontab, it's better to keep a backup:
In command line:
> crontab -l > /home/usr/crontab_[Modification date : YYYY_MM_DD_hh_mm]_bk 

To modify the crontab, you first need to create a copy of the actual configuration:
crontab -l > /home/usr/crontab_current.txt

# .------------------- minute (0 - 59)
# |   .--------------- hour (0 - 23)
# |   |   .----------- day of month (1 - 31)
# |   |   |   .------- month (1 - 12) OR jan, feb, mar, apr ...
# |   |   |   |   .--- day of week (0 - 6, Sunday = 0) OR sun, mon, tue, wed, thu, fri, sat
# |   |   |   |   |
# *   *   *   *   *   command to be executed
# Operators : * = all
#             , = and
#             - = to
#
# Everyday of the week: 3:40, 7:40, 12:40, 16:40 and 22:40
40 3,7,12,16,22 * * * /application/script/run_refresh.sh param1 param2 > /dev/null


When the modification is over,
> crontab /home/usr/crontab_current.txt

If you are modifying the file in Windows, don't forget to convert the EOL:
> dos2unix /home/usr/crontab_current.txt /home/usr/crontab_current.txt


If you want to periodicaly delete log files, you can add this to the crontab:
> find /opt/logs -name "*.log" -type f -mtime +1 -delete