Tag Archives: linux

Locating files on linux using find

A very powerful tool on linux systems is the find command. It is used to locate file in the system across directories.


amit@texens:~$ find

It takes in the location to be searched, and the criteria. It can search using criteria such as file name, owner, group, file permission, last edit time etc. It is recursive in nature, that is it looks into the folders recursively.


amit@texens:~$ find . -name foobar

will list all the files with the name foobar.

See manpage for find for more information.

Note: grep is different from find in the way that grep looks for content inside the files wheras find locates files itself and not the content inside them.

Exclude svn files while copying in linux

SVN recursively populates .svn folders in all the children folders of a project under version control. There are a number of techniques to copy the parent folder without copying the .svn folders and files, but I find the following to be easiest and quickest.

amit@texens:~$ rsync -aC /path/to/srcdir/ /path/to/destdir

The trailing “/” on srcdir is to make sure that the contents of srcdir are copied to destdir. Without the trailing “/”, the destdir will contain the srcdir and everything will be one more level down.

One other option is to copy the directory without paying any heed to the .svn directories and then find and delete all the .svn folders using the following command.

amit@texens:~$ cp -r /path/to/srcdir/folder /path/to/destdir
amit@texens:~$ find /path/to/destdir -name '.svn' -exec rm -r {} \;

Auto mount partition in ubuntu

I have a dual boot system consisting of 3 partitions – one for windows, one for ubuntu and a bulky MEDIA drive that I share with both of them as it contains all my media files like Music and Videos.

The easiest way to mount a partition automatically to a destination is by editing the fstab.

Open the terminal and determine the device to be mounted.
You need to be root in order to see the disk list

amit@texens:~$ sudo su
[sudo] password for amit:

Use fdisk -l to determine the disk

root@texens:/home/amit# fdisk -l

You’ll get something like this

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x54e308d9

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 7 HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 6528 52326400 7 HPFS/NTFS
/dev/sda3 6529 33523 216837337+ 5 Extended
/dev/sda4 33524 38913 43295175 83 Linux
/dev/sda5 6529 33274 214837213+ 7 HPFS/NTFS
/dev/sda6 33275 33523 2000061 82 Linux swap / Solaris

Note, the line

/dev/sda5 6529 33274 214837213+ 7 HPFS/NTFS

/dev/sda5 is the partition that we want to mount. Make a note of it.

Now we’ll edit the fstab file located at /etc/fstab.
Again you need to be root in order to edit the fstab file.
Make sure you are the root and use an editor of your choice to edit the file. I’ll be using vi editor for the same.

root@texens:/home/amit# vi /etc/fstab

The fstab file looks something like this.

# /etc/fstab: static file system information.
# Use 'vol_id --uuid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
proc /proc proc defaults 0 0
# / was on /dev/sda4 during installation
UUID=1e6770f3-7cd7-4be9-b6f0-3c30f96f852f / ext3 relatime,errors=remount-ro 0 1
# swap was on /dev/sda6 during installation
UUID=232230d0-236c-4e62-9c05-99b61d48b23e none swap sw 0 0

Now, all we need to do is to add the
I want to mount the partition to the location /media/bulk
Create the folder /media/bulk

root@texens:/home/amit# mkdir /media/bulk

Now, add the following line to the end of the file.

/dev/sda5 /media/bulk ntfs-3g defaults,locale=en_US.utf8 0 0

And save this file.
Now, hit the mount command to mount it just this time.

root@texens:/home/amit# mount -a

Next time when you boot into your computer, it will be automatically mounted at its designated position.

Adding a startup script that always runs at boot time

You have a script that you need to run everytime your system boots? say, for example you need to dial up to connect to internet. Its very simple and straight forward:-

Write a script and add it to your /etc/init.d/ directory, say your script’s name is wvdial.sh

$ mv wvdial.sh /etc/init.d/
$ update-rc.d wvdial.sh defaults

that’s all… now boot your system and tada… no more typing in commands on the terminal after boot to do those daily bootup tasks!

Backup your code using a shell script

There are a number of version control system such as CVS and svn that help you do a lot of stuff one of which is to have a backup of your code so that you can revert back just in case you screw up. But it’s not really worth it if you’re working alone on that code and its not too many a files. For example you’re doing your coding assignment that won’t last for more than 15 days. And yes, as per the lab instructions you have to do the coding all by yourself. In such cases, its good to have a script handy that can create a backup of your code and name it appropriately (say today’s date) and store it in a backup directory.

I created such a script for my day to day assignments and am sharing with you all.

Filename: backup.sh


FILENAME=`date +%y_%m_%d.tar`;

tar -cvvf $FILENAME code/;
mv $FILENAME backup/;


This script creates a tar archive of the code directory, names it today’s date and stores it in the backup directory.

A sample run of the code gives the following output and a tar archive by the name 09_10_02.tar is added to the backup directory.


amit@texens:~/btp/src$ sh backup.sh
drwxr-xr-x amit/amit 0 2009-10-02 01:39 code/
-rwx—— amit/amit 10077 2009-10-02 01:39 code/main.c
-rw-r–r– amit/amit 93 2009-10-02 00:04 code/Makefile
-rwxr-xr-x amit/amit 18344 2009-10-02 01:38 code/main

amit@texens:~/btp/src$ ls backup


NOTE: one may change the date format to reflect the current time in addition to the date if several backups are made in a single day.

Redirecting Output from Terminal to a file

Today, I was trying to get a scheduler simulator up and running. The source code had a Makefile which was doing quite some job. I tried to do a make only to end up with a lotta errors. The list of errors was so long that the terminal didn’t show up the entire error by scrolling up. I tried to redirect it to a text file using

$ make | tee filename.txt

But, instead of getting the errors, what I got was the entire make command after getting proccessed. I was interested in the errors rather than what the make command was.
I used this useful feature called script. All you gotta do is run the script command before you start.

$ script

You’ll get the prompt

$ Script started, file is typescript

This is the system’s way of telling that the system is recording everything going on the terminal
Once you’re done recording, just type the exit command

$ exit

And tada… its done, the terminal tells you

$ Script done, file is typescript

Everything goes into the file ‘typescripit’ which is created into the current directory.
For details checkout the manpages for script

$ man script