Monday, June 14, 2010

convert dos file to linux file

We often use the commands to convert the dos file to linux, using the commands

dos2unix - DOS/MAC to UNIX text file format converter
unix2dos - UNIX to DOS text file format converter


The conversion can be done by the commands tr and sed.

$ tr -d \\r < dos-file.txt > temp.txt && mv temp.txt dos-file.txt

here dos-file.txt will be converted to unix fileformat, temp.txt is the temporary file created and then renamed to dos-file.txt

$ sed -e 's/$/\r/g' unix-file.txt > temp.txt  && mv temp.txt unix-file.txt
here unix-file.txt will be converted to dos fileformat, temp.txt is the temporary file created and then renamed to unix-file.txt.

Thursday, June 3, 2010

find: files between dates

Finding the files in the date range

check man find for more

 find - search for files in a directory hierarchy
        -mmin n
              File's data was last modified n minutes ago.
       -mtime n
              File's  data  was  last modified n*24 hours ago. 

       -type c
              File is of type c:
              b      block (buffered) special
              c      character (unbuffered) special
              d      directory
              p      named pipe (FIFO)
              f      regular file
              l      symbolic link; this is never true if the -L option or the -follow  option
                     is  in effect, unless the symbolic link is broken.  If you want to search
                     for symbolic links when -L is in effect, use -xtype.
              s      socket
              D      door (Solaris)


       -name pattern
              Base of file name (the path with the leading directories removed) matches  shell
              pattern  pattern.   The  metacharacters  (`*', `?', and `[]') match a `.' at the
              start of the base name (this is a change in findutils-4.2.2; see  section  STAN‐
              DARDS  CONFORMANCE  below).   To  ignore a directory and the files under it, use
              -prune; see an example in the description of -path.  Braces are  not  recognised
              as  being special, despite the fact that some shells including Bash imbue braces
              with a special meaning in shell patterns.  The filename  matching  is  performed
              with  the  use of the fnmatch(3) library function.   Don't forget to enclose the
              pattern in quotes in order to protect it from expansion by the shell.




Listing the files with date

$ ls -l
-rw-r----- 1 syslog adm 1555 2010-06-03 07:54 messages
-rw-r----- 1 syslog adm 487017 2010-05-30 08:02 messages.1
-rw-r----- 1 syslog adm 646 2010-05-23 07:59 messages.2.gz
-rw-r----- 1 syslog adm 415 2010-05-16 07:47 messages.3.gz
-rw-r----- 1 syslog adm 55321 2010-05-09 07:49 messages.4.gz




-mtime 4 gives the file created exactly 4 days ago (4*24)

$ find . -mtime 4
./messages.1

$ date -d "4 days ago"
Sun May 30 16:15:14 IST 2010




-mtime 11 gives the file created exactly 11 days ago (11*24)

$ find . -mtime 11
./messages.2.gz

$ date -d "11 days ago"
Sun May 23 16:16:04 IST 2010




-mtime -11 gives the files created from now till 11 days

$ find . -mtime -11
./messages
./messages.1




-mtime +11 gives files created after the 11th day

$ find . -mtime +11
./messages.3.gz
./messages.4.gz




now when we want the files created from 11th day and older or rather above ten days then we can have

$ find .  -mtime +10
./messages.2.gz
./messages.3.gz
./messages.4.gz




if we want to have list of files created 10 days before whose name begins with messages

$find . -mtime +10 -a -name "messages*" -a -type f -exec ls -l {} \;
-rw-r----- 1 syslog adm 646 2010-05-23 07:59 ./messages.2.gz
-rw-r----- 1 syslog adm 415 2010-05-16 07:47 ./messages.3.gz
-rw-r----- 1 syslog adm 55321 2010-05-09 07:49 ./messages.4.gz

I would always suggest to have the -a -type f option so you are surely working on files only, as ls -l will in sub-directories.

Wednesday, June 2, 2010

sed: for the text between matching pattern

sed -ne '/IC/,/EI/p' country-codes.txt where IC is the first pattern and IE is the last for the file having country codes

file:country-codes.txt
GY,Guyana
HA,Haiti
HO,Honduras
HU,Hungary
IC,Iceland
IN,India
ID,Indonesia
IR,Iran
IZ,Iraq
EI,Ireland
IM,Isle of Man
IS,Israel
IT,Italy

command: sed -ne '/IC/,/EI/p' country-codes.txt
IC,Iceland
IN,India
ID,Indonesia
IR,Iran
IZ,Iraq
EI,Ireland