Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Monday, July 21, 2014

List file names with modified time stamp customized

Commands used to list files from a directory yields results of more details and time stamp with GMT values. To customize the output to display the file names only with time stamp in custom format with sorted order, use:
$ find . -mindepth 1 -maxdepth 1 -type f -printf "%TY-%Tm-%Td %TH:%TM:%.2TS %f\n" | sort -n 
Here the mindepth and maxdepth parameters were used to use the current directory for the search. That can be modified as per need. And the output will be,
2014-04-22 19:34:36 .bash_logout
2014-04-22 19:34:36 examples.desktop
2014-04-22 19:34:36 .profile
2014-04-22 21:26:24 dropcaches.sh
2014-04-22 22:09:20 .bashrc
2014-05-04 08:41:42 .dmrc

Tuesday, July 8, 2014

Bulk rename using bash rename command

Rename command with RegEx pattern can be used to do the bulk rename operation. For example, to append the ip address of the machine in between the log files supplied, the below code is used.
#!/bin/bash
ip=$(hostname -I | sed 's/[^0-9.]*//g')
file="_"$ip"."
rename -v "s/(\w+).(\w+)+/\$1$file\$2/" *.log*
The output will like:
shell.log renamed as shell_192.168.1.2.log
shell.log.1 renamed as shell_192.168.1.2.log.1
shell.log.2 renamed as shell_192.168.1.2.log.2
shell.log.zip renamed as shell_192.168.1.2.log.zip

Monday, March 17, 2014

Split PDF into multiple with n number of pages each

The following Linux shell script splits the source PDF file into multiple files with n pages of each. Totall no of pages in given input file calculated automatically and user needs to give the pages wants in each output file. This can be configured using the variable step. Default it splits the files with 5 pages.

#!/bin/bash
file="src/test.pdf";
#count total no of pages in the file
total_pages=$(pdfinfo $file | grep Pages | awk '{print $2}')
from=1
#step defines how many pages should be in output files
step=5;
while [ $from -lt $total_pages ]
do
        to=$((from+step-1));
        if [ $to -gt $total_pages ]; then
                to=$total_pages;
        fi
        echo "Generating pdf file : pages-$from-$to.pdf...";
        pdftk $file cat $from-$to output "test-$from.pdf";
        from=$((to+1));
        echo "Done.";
done

Sunday, January 26, 2014

Drop virtual memory cache from Linux box to free RAM space

In performance perspective mode, Linux box is designed to keep the frequently used application's data in memory to boost up system performance. System with low RAM space will affected by opposite manner that the cache occupies RAM space mostly and system got slow even got hanged also. Apart from theory, we need to resolve that to run the machine without hang.

To free such cache from RAM, run the below command often. This will erase the cache with simple value what we give.
$ echo 3 > /proc/sys/vm/drop_caches

Tuesday, January 21, 2014

Remove line numbers from source code using sed

While getting sample code from internet, it may comes with line number. To simply remove those lines from the code file use the below ways.

Using sed command with RegEx pattern in Linux, that can be achieved.

To remove it from console:
$ sed -i "s/^ *[0-9.]* *//g" code.txt
To remove it from vim interface:
:%s/^ *[0-9.]* *//g
And the same RegEx pattern can be used in anywhere to remove the line numbers from the source code file.

 

Monday, October 14, 2013

Refresh system settings from sysctl.conf without restart Linux

The custom settings entered in sysctl.conf can be easily revoked without restarting the Linux box. Using sysctl command with -p argument refreshes the machine with new settings.

$ sysctl -p



Tuesday, September 24, 2013

Turn USB write protect flag off

Error:

[sdb] No Caching mode page present
[sdb] Assuming drive cache: write through
[sdb] Attached SCSI removable disk

Reason:

Most of the virus programs try to turn on write protect flag of USB disks after inserting their malicious code programs/applications in to that. So that none of the programs can able to delete that even format the disk too.

Solution:

Take a Linux console and give the below command to reset the write protect flag. Then the drive comes to normal read/write mode.

Assuming that your USB device is /dev/sdb.
         hdparm -r0 /dev/sdb

Friday, May 24, 2013

Clear sudo password remember in linux

Normally Linux will keep the session of sudo command in terminal for a while and it will not ask for password again when try to use the sudo before the expiry time. You can resolve this by clearing the session immediately by giving the below command.

sudo -k

Tuesday, April 2, 2013

Compile shell scripts to encrypted binaries using SHC

Shell scripts are the plain code files and it could be modified easily when in the case of distributing. To avoid that the scripts can be converted to some other format like binary. Using SHC library the Unix shell scritps can be converted to encrypted binaries so that reverse engineering of that is not that much easy.

The Debian or Tar source of SHC are availble in Open Source repositories. Install the tool and give the below command to make the object file.
$ shc -v -r -T -f test.sh
$ mv test.sh.x test
$ ./test 
This will output one object file and one c file. Just rename output file with the suffix ".x" (ie.test.sh.x) to some name and use it anyware.

Tuesday, February 12, 2013

Bulk Convert Office Documents into PDF using unoconv

There are many methods availabel to convert bulk amount of document files into PDF files. Using Using Open Source libarary/tool we can able to do it programatically. (ie. Open Office headless service, unoconv and etc.,).

The drawback of using Open Office headless service in the case of files are in sub directories too is it will output the files in current working directory instead of actual source file path. In that case we can use unoconv tool. It supports all the formats that Open Office supports.

Unoconv

       Unoconv is a command line utility that can convert any file format that LibreOffice can import, to any file format that LibreOffice is capable of exporting. To know what are the types it is supporting,
$ unoconv --show

Example

       To convert the list odt files from path and its sub directories use the below code. This can be modified to different input and output formats as required.

#!/bin/bash
#To avoid errors due to spaces in file names
IFS="$(printf '\n\t')"
#Finds odt files in current directory and its sub directories and process one by one
for file in $(find . -name "*.odt" -type f); do
  echo "Processing File : $file ...";
  unoconv -d document -f pdf "$file";
Done

Saturday, February 2, 2013

Internal field separator (IFS) for shell scripts

Internal field separator (IFS)

By default, Unix/Linux shell scripts takes space as field separator to process input text. This may work in some scenarios but not all case. By setting IFS variable to some character(s) will take care of input parsing using that.

For example, while looping through the directory file names in Linux, spaces in file name will divert the actual outcome what you are expecting. In that case use the below variable to set new line and tab characters to be a field separator and done it.
   IFS="$(printf '\n\t')"
   for fileItem in ./*
   do
        # do your stuff ...
   done

Sunday, November 11, 2012

Find string from list of ODT files in Linux

In Linux, a string pattern can be searched using grep command from one or more files from a directory (including its sub directories). Normally the search will be easy when in the case of text files including program source code files like c, PHP, Java and etc. At the same time a search from list of ODT files from a directory is not that easy.

This can be also possible with the use of utility command such as find, unzip and grep. The below code will done the job perfectly. The parameter can be changed upon your wish. This program will output file name(s) which contains the search string.

#content of searchodt.sh

#!/bin/bash

if [ $# -ne 1 ]; then
    echo "Usage: sh searchodt.sh <searchterm>"
    exit 1
fi

for file in $(find . -name "*.odt" -type f); do
    unzip -ca "$file" content.xml | grep -qil "$1"
    if [ $? -eq 0 ]; then
        echo "$file"
    fi
Done

Tuesday, May 22, 2012

Repeatable file changes made easy with SED command in Linux

Using sed command we can add desired text at desired line in a file. That can be a comment, description or anything.

The below code inserts the comment text at the line 2 at each PHP  file inside the current directory.

    #!/bin/bash

    for fileItem in ./*.php
    do
        echo "Updating file : $fileItem"
        sed '2i\//      Author: Senthilkumar C' $fileItem > tmp.php
        cp tmp.php "$fileItem"
    done
    #remove temp file
    rm tmp.php

Thursday, December 1, 2011

Download cached flash videos of Firefox and Chrome in Linux(Ubuntu)

In recent days, you could feel that the played Flash videos is no longer available in /tmp directory like some time before. This was the easiest way to get the video that was played just before completely on any browser from such Flash playing sites like Youtube. Now we can't use the same olden ways.
Here is an alternate way to get the video by doing some workaround.

By default, while executing any commands or application, Linux maintains a copy of the files that process using currently untill the process get closed or killed manually. These files are kept under a  unique directory with the name of that process id under /proc folder. Now you may got the idea i think. Yes, we need do the following thinks to find the exact path by finding the process id of the browser we are using. You may use any browser, but the concept is same.

To find the process id ( -i means case insensitive on search) ,

$ lsof | grep -i flash
This will yields the result like,

              chromium- 3111   csenthil   25u      REG       8,23     26852199      66502 /tmp/FlashXX7qfvPA (deleted) 

From the result, we can conclude that the process id of the browser (here 3111). Next step, we need to list the all Flash files under the process by using the following command.
$ ls -al /proc/3111/fd | grep deleted 
This will yields the result like (lists more files when you running more than one player instance),

              lrwx------ 1 csenthil csenthil 64 2011-12-01 20:57 25 -> /tmp/FlashXX7qfvPA (deleted)

From that, we can can conclude that the expected file resides there in the name of 25. Then why are you waiting for... Just copy the file where ever you want like,  
$ /proc/3111/fd/25 /home/csenthil/Desktop/saved.mp4


Note: This is tested under Ubuntu 11.04


Monday, November 28, 2011

Enable only manual paper feed for printers in Ubuntu

The following method is used to enable manual paper feeding option of HP printers in Ubuntu Linux.

  1. Install required printer driver software while configuring new printers (Not all printers having drivers for Ubuntu. But HP providing that)
  2. Open the HPLIB Toolbox program from menu
  3. Under General section, select the value Tray1 (Manual) for the option called 'Paper Source' (Ref the image given below)
  4. Save it and close.


From now on, the printer will wait for you to feed the paper even if you gave the print command. This may be useful when you need one side printouts for particular usage.

Note : This is tested under Ubuntu 11.10 with HP Laser set Printers.

Tuesday, November 22, 2011

Err: Unlock Login Keyring in Ubuntu 11.10 problem

Error:
Unlock login keyring password.

Reason:
Most of the Ubuntu users could faced this problem. This is occurred due to the following reasons.

  1. While using of autologin facility
  2. Frequent password change
Keyrnig password is the authentication key to execute what ever thing is done with mouse in Gnome screen. Each thing what we have done in GUI is converted into system commands and then shell only executes that and returns the result to Gnome.

Du to any one of the mentioned above reasons, the keyring password got collapsed and it asks us at every boot for the password that is just previous to the current logged in password we have changed (ie. last changed password.).

Solution:
This can be solved by following two ways. 
  1. Passing last changed password
    By providing last changed password for once. Then it will be automatically changed to current password we are using.
  2. By setting empty password to the keyring.

    This can be done by changing the password to empty using 'Passwords and Encryption Keys' tool that exist in Ubuntu by default. 
    1. To get this window in Ubuntu 11.10, click on 'Activities' and then type 'password'. The result window shows that application on that and click on that. 
    2. Right click the 'Passwords : login' and select 'Change password'.
    3. Give the current or last changed password in the column 'old password' and leave the new and confirm password as empty.
    4. Save it.

Note: This is tested under Ubuntu 11.10

Tuesday, November 15, 2011

Ubuntu login screen background change in 11.10

To change the login screen background image of Ubuntu 11.10,


Edit the file from the path called "/etc/lightdm/unity-greeter.conf" and set new image path for the property called "background". You may either set new path or copy the required image to that default path and change the name itself.


Here you can also change the default settings of,

  • Font
  • Theme
  • Icon theme
and other some more display elements.



Tuesday, November 1, 2011

Save entire console session actions into a file in Linux

Task:
          While installing or debugging a task in Linux, previous session history will helps a lot to review the work what we have done when if any error occurs. For this, we need to store the entire session action and its output into a file or some where.

Solution:
           This can be achieved by more commands depends of the user requirement.



  1. Command wise
    1. Using output redirect
          This is the simplest command used to save our console outputs into a file.
          $ ls -al > out.txt
          $ ls -al >> out.txt

      Here, '>' used to redirect the output to a file and '>>' is used to append the new output with existing one. Main drawback of this command is, the console will not display anything. All the outputs will goes to file instead of console.
    2. Using 'tee' command
          This command overrides the drawback of previous method. This will send the copy of the output only to the file. So that the normal output is displayed in console as it is.

      $ ls -al |tee out.txt

      This will erase the previous contents while writing new one. To avoid this use the following syntax to append

      $ ls -al |tee -a out.txt

      Main drawbacks of previous two commands is, we need to give the tailing commands (either > or tee) with each and every commands we are executing in that console.

  2. Session wise

    In this method, the entire session from the start to end will be save given file. This contents will be written into the file once we close the terminal.
    1. Using 'script' command
         Run the following command before starting any execution in console to start recording.

      $ script -a today.txt

      Then do your usual work and once complete the tasks then quits the terminal and see the saved file contents like,

      $ more today.txt


Note: This is tested under Ubuntu 11.10




Monday, October 3, 2011

Thunderbird: import or using of saved or backup folder in new instance in Linux

To back-up the thunderbird email client files with all email accounts
    Copy the entire .thunderbird folder from the filesystem (normally it is located at /home/<username>/.thunderbird) into some safe place (ie. other than the Operating System partition).

To restore the saved files in new Operating System or newly installed thunderbird client or in some other systems.
  1. Install the thunderbird client (ie. apt-get install thunderbird)
  2. Run the client from menu or from console once. (ie. let the programe to create neccessary folders on the path) and close it by not making any changes like new account configuration)
  3. Move the .thunderbird folder to somewhere or rename it like

    $ mv /home/<username>/.thunderbird /home/<username>/.thunderbird.org

  4. Let /Data/.thunderbird was the saved folder of thunderbird, then

    $ ln -s /Data/.thunderbird /home/<username>/.thunderbird
  5. Run the thunderbird to load the old mails and further use.

Note : This is tested under Ubuntu 10.x and 11.x


 

Thursday, September 22, 2011

Timeout error while signup new user in Drupal 6

Error:
           No response from the server side and time out error comes after 3 to 5 min after submitting the signup form in Drupal 6. But the user were created correctly without the response.


Reason:  
           I have configured Postfix and Stunnel4 in my system (Redhat) for mailing purpose. But the host name was not configured correctly. So that the Drupal script waits long time for the response from the Postfix server. This was caused the timeout error.


Solution:
          Check and make sure the '/etc/hosts' and Postfix server configuration file (normally in /etc/postfix) for valid host name or IP of your server.  Default configuration of Postfix doesn't make any problem. If you have changed anything in '/etc/hosts' file or or Postfix config file will make errors like this.



Popular Posts