Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Thursday, 6 August 2015

How to get file sizes recursively in a directory and list them in ascending order

When cleaning up unused files from your linux computer, I find it useful to know which of my files use most of the space so that I can easily determine which ones to remove.

The most basic command to find file size of a given file is:
du -sm myfile.txt

If you are in a folder with multiple files and want to get file sizes of all the files, the command is:
du -sm *

The above command does not get file sizes recursively. However, if you are happy with it and want to sort the file sizes in ascending order:
du -sm * | sort -n -k 1

Now, this is little useful as it does not do recursive file size listing. To do that:
find . -type f -exec du -sm {} \; -print | sort -n -k 1

If you are looking for a particular type of file (eg. jar files):
find . -name "*.jar" -type f -exec du -sm {} \; -print | sort -n -k 1

Enjoy!

Monday, 20 July 2015

Unix/Linux command to check if Apache Tomcat is running

So I've come across this problem quite a few times. Normal way to do this is:

ps -ef | grep tomcat

This works most of the times. If tomcat is running, it gives between 1 and 2 lines back but if not, it gives anywhere between 0 and 1 lines back. A much cleaner use of the above command would be with wc -l:

ps -ef | grep tomcat | wc -l

However, this doesn't solve the actual problem as along with the tomcat process, it also gives you the process of command "grep tomcat".

Here's the command to solve this problem. You can use either of the two below commands:

ps -ef | grep tomcat | grep -v "grep tomcat" | wc -l

ps -ef | grep tomca[t] | wc -l

The first command explicly says that once you get a list of all processes containing the word tomcat, ignore lines containing words "grep tomcat". And then the usual, pipe it to word count and output the number of lines.

The second one, however, tricks the grep into using a regular expression and ignoring itself. This is because the actual output containing "grep tomca[t]" will have the square brackets which obviously won't match the actual regular expression.


Friday, 27 September 2013

Changing keyboard layout in Ubuntu Server (Linux, How to)

So I recently installed Ubuntu Server in a virtual machine and I had problems with my keyboard layout. I use UK layout while the default that comes with Ubuntu Server is US layout. As you can imagine, this caused problems and I had to go hunt for a solution. Finally, I found one and here it is:

You need to reconfigure keyboard configuration. Type the following:
sudo dpkg-reconfigure keyboard-configuration

If that doesn't work, you might need to install console-data package. Install it by typing:
sudo apt-get install console-data

and then try first step again.

If everything was fine, you should see this:
Using this you can select brand of your keyboard. I am running this virtual machine on my Acer laptop and as you can see it is listed there. You can use up/down/page up/page down keys to navigate through the list. When you have picked one, press enter to go to next screen.

You should now see this:
This is the language selection screen. Navigate through the list, select your language and as you did in previous one, press enter to move on to next screen.

This is the screen where you select your keyboard type:
Assuming you're using a qwerty keyboard for PC, you should just leave the first one selected and press enter. However, if you are running this on a mac or your keyboard is of different kind, you are free to choose whatever applies to you from the list and continue by pressing enter.

The next screen allows you to map a key on your keyboard to alternate grammar key (Alt Gr).
I have never used this key in my life for alternate grammar, however, hoping that I may use it at some point, I would map it to right-alt key (which actually is Alt Gr key). As usual, select one from the list and press enter.

Next one is compose key. Same drill. Pick one and press enter:
Compose key, if I remember correctly, allows you to type ASCII for certain characters when you press and hold it. This key on windows is left alt key however, here you can choose whatever you want for ubuntu server.

When you press enter, the wizard will quit. So that was the keyboard configuration screen which allows you to configure your keyboard pretty neatly. Have fun!

Saturday, 14 September 2013

Assigning static IP address to your Raspberry Pi (WiFi)

When I first bought my Raspberry Pi, I had this problem. My router and TV are in different rooms and I don't have a ethernet cable. This restricts networking ability of my Pi which is quite annoying. I searched for articles on assigning static IP addresses to Raspberry Pi but most of them were talking about assigning it for eth0. I, on the other hand wanted wlan0 static IP.

Now, I could have just got a long ethernet cable and solve this whole problem but I was just annoyed and was constantly asking myself "Why isn't this working over wifi". So finally, after significant digging around, I found the solution. Here it goes:

First of all, take a backup of /etc/network/interfaces file:
pi@raspberrypi ~ $ sudo su -
root@raspberrypi:~# cp /etc/network/interfaces .


Now, in order to make this work, we need some information first. We need to find out the new static ip address that you want, gateway, mask, network and broadcast address. Assuming that you have a working wifi connection on your Raspberry Pi, type:
root@raspberrypi:~# ifconfig

From the output, the bit that we are interested in is:
wlan0     Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:192.168.1.99  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19153 errors:0 dropped:24512 overruns:0 frame:0
          TX packets:25553 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3724822 (3.5 MiB)  TX bytes:31437802 (29.9 MiB)


The highlighted line will give you information about your current IP, broadcast and mask address. Take a note of the last two. So, in my case:
address: (this is the IP address you wish to reserve as static eg. 192.168.1.69)
mask: 255.255.255.0
broadcast: 192.168.1.255

To find out information about the last two bits, type:
root@raspberrypi:~# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0


In my case:
gateway: 192.168.1.1
network (a.k.a destination): 192.168.1.0

Now that we have all the information that we need, edit the /etc/network/interfaces file using nano. Putting all the bits and pieces together, it should look like:

Press Ctrl + O to save.
Now, since I was doing this whole process over SSH, I had to write a script to restart wifi because if I didn't, as soon as I turn it off, it would also take away my SSH access rendering me powerless. You can do the following in either case. Create a new file called restartwifi.sh using nano in root home directory:
root@raspberrypi:~# nano restartwifi.sh

Now, I am aware that this script can be improved but this is what I had before and it has worked without any problems. Press Ctrl + O to save the script. Then press Ctrl + X to exit.

Make sure you edit permissions to make the script executable. Run:
root@raspberrypi:~# chmod +x restartwifi.sh

If you're not root, you'll have to prefix above command with "sudo". 

Now comes the moment of truth. Run the script to reset your wifi by typing following command:
root@raspberrypi:~# nohup ./restartwifi.sh &

If you're in putty session, you'll immediately go offline. If you've done everything correctly, you should be able to ssh into your Pi, after couple of minutes, on your chosen IP address.

Thursday, 18 July 2013

Using find command to search for a particular directory in unix

I was looking for a particular directory today on my computer and I didn't know how to do it. After spending some time searching for it, I found the following command:

find [parent directory] -type d -name [directory name]

So in my case:

find /home/vader -type d -name wrapper_scripts

Optionally you can also use:

find /home/vader -type d | grep wrapper

Thursday, 20 June 2013

Copying symbolic links (UNIX/Linux)

At some point, you might come across a situation where you have some files and a bunch of symlinks (symbolic links) in a folder and you wish to copy the data that is pointed at by the symlinks and not just blank links. If you are using Redhat Enterprise Linux, this should automatically happen when you call the cp command. However, on UNIX platforms or Linux distributions, this is not the case by default. In those cases, you will have to issue the following command:

cp -L {file(s)} {destination folder}

The -L option in cp command forces it to follow (dereference) symlinks and copy the data that is pointed at by the symlink.

In case you do not want to follow symlinks, you can issue the following command instead:

cp -P {file(s)} {destination folder} 

Wednesday, 19 June 2013

Mounting ISO images on linux

So I learnt this today and decided to share it with you guys. If you have an iso image and want to mount it on Linux, simply execute the following command in terminal:

sudo mount -o loop mydisk.iso myfoldername

So in my case, I had iso file called 2012photos.iso on my desktop and I wanted to mount it on folder called 2012pics which was on Desktop as well. I executed:

sudo mount -o loop /home/manthan/Desktop/2012photos.iso /home/manthan/Desktop/2012pics

Saturday, 11 May 2013

Managing password expiry for a user account (Unix/Linux)

I find this very useful on several occasions so here we go. Login as root and execute the following:
passwd -x -1

So if you want to remove password expiry from bob's account, type:
passwd -x -1 bob

Notice that -1 parameter after -x represents number of days before the password expires. Since we do not want the password to expire, we have set it to -1. However, you can set it to any value you want. So, if you want your password to expire after 90 days (i.e. 3 months) for bob's account, type:
passwd -x 90 bob

Tuesday, 7 May 2013

Checking memory (RAM) size on Solaris

If you want to check how much RAM your Solaris machine has, here's how:
/usr/sbin/prtdiag -v | grep -i memory

You can also try:
/usr/sbin/prtconf | grep -i memory

Thursday, 2 May 2013

Removing password expiry from your unix machine (Linux and Solaris)

Here's a quick one. If you have a server and you do not want the password for a user to expire (as it can screw some things up while its active), you need to execute the following commands as root:

passwd -x -1

where is the username whose password expiry you wish to remove. For instance, in my case, if username is dm014, I executed:

passwd -x -1 dm014

I have tested this and it works flawlessly on linux and solaris.