26 Mar, 2008  |  Posted by Danesh  |  in HowTo, Linux

Not many people I know use the tee command but I find it quite useful. I tend to use it quite often in my scripts actually.

So what does it do?

The tee command has the ability to take the standard input and redirected it to multiple outputs. For example, the ls command would normally just return file names on your screen but what if you also need to keep a log of those file names in a text file.

Using the tee command you could simply write something like this “ls * | tee -a output.txt“. The command will return the file names on screen and also append them to the output.txt file. Screenshot below,

tee1

A lightly more complex use of the command. I have a file with duplicate entries. Combining the tee command with the uniq and sort commands I am able print the desired output on screen and also dump it into the file output_sorted.txt. Screenshot below,

tee3

There are way more usages for the tee command but for today I stuck to the basic. I’ll try to post more about it in the future.

21 Jan, 2008  |  Posted by Danesh  |  in HowTo, Linux

I develop quite a few bash scripts to automated backups and dropbox operation in my office. One of the usual requirements is to tar (tar.gz) the files locally and later untar them on the destination server when needed. I have a few simple scripts with interactive menus which help the data center operations team with their daily backup tasks.

One of the challenges during development was the ability to untar files on the backup server over SSH. The command by default will untar to the home directory instead of the target folder output always returned success but the files were nowhere to be found. This apparently is a limitation on the tar command, it did not know where to untar the files to when being executed over SSH. Fortunately the fix was really simple.

Original command,

ssh 127.0.0.1 'tar zxvf ~/test/files.tzr.gz'

The simple fix,

ssh 127.0.0.1 'cd ~/test/ ; tar zxvf files.tar.gz'

or, (thanks Aik)

ssh 127.0.0.1 'tar zxvf files.tar.gz -C ~/test/'

Here’s a video I put together to demonstrate the above. Hopefully I got it right.

Continue Reading ->

16 Jan, 2008  |  Posted by Danesh  |  in HowTo, Linux

Need to find files older than certain time frame? This will help, “find [dir] -type f -mtime +[24hours*n] ”

Examples,

Show files older than 7 days

find /tmp/ -type f -mtime +7

Show files older than 7 days and rm them.

find /tmp/ -type f -mtime +7 -exec rm {} \;

or if you have a large number of files

find /tmp/ -type f -mtime +7 | xargs rm
10 Jan, 2008  |  Posted by Danesh  |  in Linux

My friend wanted to know how to select all files but one on the CLI or in a bash script. This is how I normally do it, do you know a better way?

From the command line

ls * | grep -v [pattern to ignore]

or

ls [!pattern to ignore]  *

in a bash script it may look like this,


for i in `ls * | grep -v [pattern to ignore]`
do
   do something here
done

Continue Reading ->