Tuesday, November 27, 2012

Useful commands and howtos at your finger tips

Useful commands and howtos at your finger tips

linux|unix|dos|windows|mac
jdk|regex|...

1. How to find files and operate some command on that file list

Example (a) Find all files updated within the last 11 minutes, and copy them to another directory:
find . -mmin 11 -exec cp '{}' /home/temp/ \;



2. How to read a file line by line through a unix/linux shell script

http://www.bashguru.com/2010/05/how-to-read-file-line-by-line-in-shell.html

3. How to extract only the file names after you have figured out which files from the ls -ltr listing

Assume filelist.txt contains the ls- ltr output redirected
cat filelist.lst
-rw-r-----  1 oraxxxx emsxxxx     2119 Dec  5 02:50 sid_ora_5565.trc
-rw-r-----  1 oraxxxx emsxxxx    11591 Dec  5 02:50 sid_ora_24464_TEST.trm
-rw-r-----  1 oraxxxx emsxxxx   469796 Dec  5 02:50 sid_ora_5565_TEST.trc
-rw-r-----  1 oraxxxx emsxxxx   698558 Dec  5 02:51 sid_lgwr_6551.trc
-rw-r-----  1 oraxxxx emsxxxx     5855 Dec  5 02:52 sid_ora_31733_TEST.trm
-rw-r-----  1 oraxxxx emsxxxx    12321 Dec  5 02:52 sid_ora_29474_TEST.trm

cat filelist.txt | cut -d ":" -f 2 | cut -d  " " -f 2 > fileslist.txt

Now fileslist.txt will have only the filenames

cat fileslist.tst
sid_ora_5565.trc
sid_ora_24464_TEST.trm
sid_ora_5565_TEST.trc
sid_lgwr_6551.trc
sid_ora_31733_TEST.trm
sid_ora_29474_TEST.trm


4. How to convert from postscript to pdf format


Ensure you have ghostscript installed
Then use the ps2pdf command

ps2pdf -dEmbedAllFonts=true -dOptimize=true -dUseFlateCompression=true myfile.ps

This will create a pdf file with the same name as the .ps file, in the current working directory.

[This page gives a detailed overview of the ps2pdf command.]


5. How to generate a keystore


JDK ships with a keytool program, that can be used to generate one keystore:

keytool -genkey -v -keystore filename.keystore -alias keyaliasname -keyalg RSA -keysize 2048 -validity 10000

6. How to list all files within a given directory in DOS/Windows

Use the following DOS command in the command window:
dir /b /s /a:-D

[Source: http://superuser.com/questions/653860/list-files-recursively-showing-only-full-path-and-file-size-from-windows-command]



7. Howzzat RegEx ?

7.1. How to get the file name only, from a list of full path names ?


For example, we have a file that has list of full path names, like as shown below:

D:\lib\SCEMDT-lib\xmlparserV2\xml.jar
D:\lib\SCEMDT-lib\xmlparserV2\xmlparserv2.jar
D:\lib\SCEMDT-lib\jdev-rt.jar
D:\lib\SCEMDT-lib\ant-launcher.jar

How do we get only the filenames then?

Answer: 
Use regex (.*?)(\\(?=[^\\]*$))(.*?$) as the matching pattern.
and then replace with \3

Explanation:
The second part of the regex i.e., (\\(?=[^\\]*$)) says to match a literal backslash \ and then asserts ((?=) is positive lookahead) that no other character up to the end of the string is a backslash \. 
The first part and the third part of the regex is plain and simple: the first part (.*?) points to the string before the last backslash \; and the second part (.*?$) points to the string after the last backslash \ till the end of the line.

The replacement pattern is \3 , which means the third part (i.e., the string after the last backslash \ till the end of the line)




8. Closing a session after running a blocking command line script

After you have started a blocking command line script (such as the startWebLogic.sh), you can not close the session from which you started the script. Because, if you close the session, then the script too closes.

One option is to use nohup & . But this can work when the script does not ask for any console input, such as credentials, etc.

For scripts like startWebLogic.sh that asks for console inputs, we still have a way:

Follow the following sequence of steps to detach the running startWeblogic.sh process after the weblogic has been started.
Pause: ^z
Send to background: bg
Check background jobs: jobs
Detach from session: disown
Verify if really detached: jobs



9. mactips - Tips & Howtos for your first mac

(esp. if this is the first time, you have moved to a Mac from a Windows or a Linux)

9.1. Setting environment variables that are available to all applications

Put the setenv statements in the file /etc/launchd.conf
It requires superuser permissions.

sudo vi /etc/launchd.conf

setenv JAVA_VERSION 1.6
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
setenv GROOVY_HOME /Applications/Dev/groovy
Save the file, and reboot the mac. Or, run the following command, and relaunch the required applications:
grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl

Courtesy: Setting environment variables in OS X?

Later on, even after restarting, the env variable settings sometimes vanish. I added the above command into the ~/.bash_profile file.

9.2 Setting PATH on mac

By any of the following methods:
  1. $HOME/.bash_profile file using export syntax.
  2. /etc/paths.d directory.

9.3 Copy Path of a folder in mac

  • Open Automator.
  • Select the Service template.
  • Set options at the top to Service receives selected Files or Folders in Finder.
  • Drag over “Copy to Clipboard.”
  • Save the service with a name like “Copy Path as text
Now, in the right-click context menu for any folder in finder, you will see a new menu item "Copy Path as text"

The service can also be assigned a keyboard shortcut, from the System Preferences -> Keyboard -> Shortcuts

Courtesy: http://stationinthemetro.com/2014/06/04/copy-file-path-as-text-in-mac-os-x


...to be continued...