Showing posts with label Command Line. Show all posts
Showing posts with label Command Line. Show all posts

Monday, September 7, 2026

How to Find a List of All Open Files in Linux/UNIX

How to find list of all open files in Linux/UNIX


Its really very simple to find the list of open files on UNIX/Linux. As you know Linux/UNIX kernel has many powerful commands which helps a lot.

For checking the list of all open files on your system, you can use the command

Command :-

lsof

lsof - list open files , this has many command options.Some important options as below :

-p - for specific PID
-u - for specific user


Example :-

lsof -u manoj


Output :-

lsof      9963 manoj  mem       REG                8,1     52120 11276171 /lib/x86_64-linux-gnu/libnss_files-2.15.so
lsof      9963 manoj  mem       REG                8,1     47680 11276175 /lib/x86_64-linux-gnu/libnss_nis-2.15.so
lsof      9963 manoj  mem       REG                8,1     97248 11276165 /lib/x86_64-linux-gnu/libnsl-2.15.so
lsof      9963 manoj  mem       REG                8,1     35680 11276167 /lib/x86_64-linux-gnu/libnss_compat-2.15.so
lsof      9963 manoj  mem       REG                8,1   7224832  5511486 /usr/lib/locale/locale-archive
lsof      9963 manoj  mem       REG                8,1   1815224 11276120 /lib/x86_64-linux-gnu/libc-2.15.so
lsof      9963 manoj  mem       REG                8,1    149280 11276100 /lib/x86_64-linux-gnu/ld-2.15.so
lsof      9963 manoj  mem       REG                8,1     26258  5769171 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
lsof      9963 manoj    0u      CHR              136,0       0t0        3 /dev/pts/0
lsof      9963 manoj    1u      CHR              136,0       0t0        3 /dev/pts/0
lsof      9963 manoj    2u      CHR              136,0       0t0        3 /dev/pts/0
lsof      9963 manoj    3r      DIR                0,3         0        1 /proc
lsof      9963 manoj    4r      DIR                0,3         0    64363 /proc/9963/fd
lsof      9963 manoj    5w     FIFO                0,8       0t0    64368 pipe
lsof      9963 manoj    6r     FIFO                0,8       0t0    64369 pipe
lsof      9964 manoj  cwd       DIR                8,1      4096  3932162 /home/manoj
lsof      9964 manoj  rtd       DIR                8,1      4096        2 /
lsof      9964 manoj  txt       REG                8,1    131312  5505664 /usr/bin/lsof
lsof      9964 manoj  mem       REG                8,1     52120 11276171 /lib/x86_64-linux-gnu/libnss_files-2.15.so
lsof      9964 manoj  mem       REG                8,1     47680 11276175 /lib/x86_64-linux-gnu/libnss_nis-2.15.so
lsof      9964 manoj  mem       REG                8,1     97248 11276165 /lib/x86_64-linux-gnu/libnsl-2.15.so
lsof      9964 manoj  mem       REG                8,1     35680 11276167 /lib/x86_64-linux-gnu/libnss_compat-2.15.so
lsof      9964 manoj  mem       REG                8,1   7224832  5511486 /usr/lib/locale/locale-archive
lsof      9964 manoj  mem       REG                8,1   1815224 11276120 /lib/x86_64-linux-gnu/libc-2.15.so
lsof      9964 manoj  mem       REG                8,1    149280 11276100 /lib/x86_64-linux-gnu/ld-2.15.so


Apart from there are many other options you can use to find more powerful lsof command.

Example 2:-

manoj@manoj-HP-ProBook-6450b:~$ ps -ef | lsof -p 9987
COMMAND    PID USER   FD      TYPE DEVICE SIZE/OFF NODE NAME
kworker/u 9987 root  cwd   unknown                      /proc/9987/cwd (readlink: Permission denied)
kworker/u 9987 root  rtd   unknown                      /proc/9987/root (readlink: Permission denied)
kworker/u 9987 root  txt   unknown                      /proc/9987/exe (readlink: Permission denied)
kworker/u 9987 root NOFD                                /proc/9987/fd (opendir: Permission denied)

Linux / Unix: Find and Remove Files with One Command

This is really very easy in Linux/Unix. This is the reason why Linux/Unix is so popular in most of the industries.  


COMMAND :-

find - search for files in a directory hierarchy
 

SYNOPSIS 

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

SYNTAX

find <dir-name> <criteria> <action>

dir-name - The directory which you want to search.
criteria - This is searching criteria, what you want to search.
action(if required) - This is command action you want to execute.

There are many options or expressions you can use to find your relevant files and folders.

Example :- The below command for finding all files with extension .xml

dev@jdg-developer-desktop ~ $ find soap_projects/  -name "*.xml"
 

soap_projects/folk-enabler-no-sec--soapui-project.xml
soap_projects/aim-localhost-tomcat-soapui-project.xml
soap_projects/roamenable-service-soapui-project.xml
soap_projects/search-enabler-soapui-project.xml

Find all the files , which file name contains "data" and extensions with .txt

dev@jdg-developer-desktop ~/workspace_test $ find .  -name "*data*.txt"  

./test_data_file.txt


Example for finding and delete all all files with "find" command.


find . -name "*data*.txt" -exec rm -rf {} \;


Example for finding and delete all .xml files.


find / -name "*.xml" -exec rm -f {} \;

Example for finding and delete all .xml files.

find /searchdirectory -name "*.xml" -exec rm -f {} \;



Delete command for all file with find command result :-

-exec rm -rf {} \;



How to Set Java Version in Linux

How to set java version in Linux or Unix.


Here I have used Linux Mint, but this setup can work for other Linux distributions. 

Checking the existing java version on your linux machine.
dev@developer-desktop ~ $ java -version
java version "1.6.0_36"
OpenJDK Runtime Environment (IcedTea6 1.13.8) (6b36-1.13.8-0ubuntu1~12.04)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

 

The above command shows the existing java version installed on this machine. Now I can check the alternative version to set. These alternative versions are available for installation. The below command will prompt with sudo password and selection number. Try with below command :


dev@developer-desktop ~ $ sudo update-alternatives --config java
[sudo] password for dev: ***********
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      auto mode
  1            /usr/bin/gij-4.6                                 1046      manual mode
  2            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
  3            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode.

In the above command, I have opted the Selection 3 , because I want java 1.7 version. You can choose as per your requirements. 


Now you can check whether version 7 is installed or not ? Try the check version command again as below.

dev@developer-desktop ~ $ java -version
java version "1.7.0_79" -----> This is newly installed.
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.12.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)




Hope this will help you.

Friday, July 1, 2016

How to Test a Web Service Using cURL from the Command Line

How to test web service using command line curl.


How to test web service using command line curl, is a very often requirement.

This is always very easy to call the web service via a java client,  but sometimes we need to call the service using curl.

One thing you know ? its really very tactical to  call the web service from terminal/command line using curl command.


I have  steps with sample example :-


You need to know the below things before calling the service via curl.

Service Endpoint - The web service endpoint you want to call.

Example - http://myserverip.mycorpnet.com.au:16500/NaaSAutomation/NaaSManageService

Operation Name - The operation name , you want to execute.

Example - readProviderRequest

SoapAction - The soap action defined in WSDL.

Example - SOAPAction:/NaaSAutomation/Resources/WSDLs/ESB/Service/NaaSManageService-service0.serviceagent/NaaSManageServicePortEndpoint0/ManageProviderRead

Create sample valid request xml for sending to the service.

Request.xml
------------------

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dto="http://dto.service.myservice.networkservice.test.service.mycorp.com.au/">
   <soapenv:Header/>
   <soapenv:Body>
      <dto:readProviderRequest>
         <callerIdentity>myIdentity</callerIdentity>
         <dto:ProviderName>techbyteslearn</dto:ProviderName>
      </dto:readProviderRequest>
   </soapenv:Body>
</soapenv:Envelope>

Curl Command :-

curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:/NaaSAutomation/Resources/WSDLs/ESB/Service/NaaSManageService-service0.serviceagent/NaaSManageServicePortEndpoint0/ManageProviderRead" --data @Request.xml http://myserverip.mycorpnet.com.au:16500/NaaSAutomation/NaaSManageService

Response got from Curl :-

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <ns0:ManageProviderCreateRes xmlns:ns0="http://www.mycorpnet.com.au/NaaSAutomation/Resources/Schemas/CreateRequest.xsd">
         <ns0:response>
            <ns0:providerName>techbyteslearn</ns0:providerName>
            <ns0:createStatus>SUCCESS</ns0:createStatus>
            <ns0:rollbackInitiated>TRUE</ns0:rollbackInitiated>
         </ns0:response>
      </ns0:ManageProviderCreateRes>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Thursday, May 28, 2015

How to Convert Epoch Time to Human-Readable Format in Unix

How to convert epoch to human readable format in UNIX ?


In this example the epoch time is 1432783870. Use the below command to convert the epoch time to human readable time.Technically epoch time known as UNIX time , also known as POSIX time. 

Example :-

dev@javadevelopersguide-developer-desktop ~/Epoch/ $ date -d @1432783870
Thu May 28 13:31:10 EST 2015
dev@javadevelopersguide-developer-desktop ~/Epoch/ $ date -d @1377360000
Sun Aug 25 02:00:00 EST 2013

Find more here.