Linux: How to find size of the current directory

In linux, the directory is just a pointer to the location. What we really want if the size of all the files that are contained in that directory. To see the individual sizes of all the directories inside the current directory:

du -sh *

the ‘s’ option provides a summary, the ‘h’ option provides the output in a human readable format. There is a ‘c’ option which can combine the sizes and give a total.

du -h --max-depth=1 | sort -hr

provides the size of all sub-folders at the first level. Sort command sorts the output, with the ‘r’ option meaning ‘reverse’, i.e. the largest one first.

 

 

Linux: vi search and replace command

Format

The format of the command is given below:

%s/<string_being_replaced>/<replacement_string>/

Examples

In order to replace the word “hate” with “love”.

  • Open the file in vi
  • Hit [Esc] (Esc in vi lets you type commands instead of typing content)
  • Type : (colon) followed by %s/hate/love/ and hit [Enter] key.
:%s/hate/love/

Above command will replace first occurrence of word hate with love on all lines. The % represents all lines.

To replace all occurrences of word hate with love on all lines, use the g option (g stands for global, indicating all occurrences).

:%s/hate/love/g

You can replace g with a number 1,2,…N to change only the n’th occurrence on each line.
Use find and replace on line ranges (match by line numbers)

You can also make changes on range of lines i.e. replace first occurrence of hate with love on lines 5 through 20 only, enter:

:5,20s/hate/love/

Following command will replace first occurrence of hate with love starting at the current line for the next 100 lines:

:.,+100s/hate/love/

Word Match

You can match by words i.e. find the next line containing a word “test”, then replace all occurrences of hate with love:

:/test/s/hate/love/g

To specify ranges:

:/test/,/guest/s/hate/love/g

Linux: Can’t open display for xterm

When using xterm, if you are not able to see the display show up on your screen, or you get an error, try setting your display.

Find the ip of your local windows desktop using the following command:

C:\>ipconfig


Windows IP Configuration


Ethernet adapter Local Area Connection 6:

   Connection-specific DNS Suffix  . : dev.company.gov
   IPv4 Address. . . . . . . . . . . : 1xx.xx.93.63
   Subnet Mask . . . . . . . . . . . : 255.255.254.0
   Default Gateway . . . . . . . . . : 1xx.xx.92.1

C:\>

The information given in the IPv4 Address is your ip. Now you can set the display on the linux box using this command:

export DISPLAY=1xx.xx.93.63:0.0

Some common reasons for xterm not starting:

  • When connecting to the server using putty, check if”Enable X11 Forwarding” checkbox is checked under Connection –> SSH –> X11

PuttyX11Setting

  • Make sure Exceed is running
  • If that still doesn’t work, check  your bash_rc. One time we had the bash_rc setting the DISPLAY to 0, which was causing the xterm to not work. Once we commented out that line and started again, xterm worked.

 

 

JBoss: Command Line Interface [CLI]

JBoss 7 has a new Command Line Interface (CLI) which can be used to deploy or undeploy applications.

Go to the bin folder of your jboss installation

Type the following command to start the CLI:

./jboss-cli.sh

You will see the following response:

You are disconnected at the moment. Type ‘connect’ to connect to the server or ‘help’ for the list of supported commands.

Type connect

[disconnected /] connect

If you do not connect then press CTRL-C. It will say the following:

The controller is not available at localhost:9999

That means that the jboss installation is not using the standard port 9999. You can find the port in the server.xml file of the server.

Once you connect, just typing undeploy will show you all the deployments.

Typing undeploy with the name of the ear file will undeploy the application

undeploy my_app.ear

 

 

Linux: scp command basics and to copy a directory

The scp command in linux is used to securely copy (scp – secure copy) files and folders from one linux server to another. The basic structure of the scp command is as follows:

scp source  target
scp username@source_servername_or_ip:/path/to/file_or_directory location/to/copy/file_or_folder/to

When you run the above command from the target directory, you will be prompted to type the password for the username on the source server. The username should have at least read privileges to the file_or_folder you are trying to copy.

To copy to the current directory location, you can just specify a ‘.’ as below:

scp username@source_servername_or_ip:/path/to/file_or_directory .

To copy an entire directory, use the following command:

scp -r username@source_servername_or_ip:/path/to/directory .

Linux: How to switch user

To switch user when you are in the sudoers list:

sudo su - username

Linux will then ask you to type your password (not the user you are switching to, but the user you are logged in as). You can always do whoami to check who you are running the command as

whoami