DISCLAIMER : Please note that blog owner takes no responsibility of any kind for any type of data loss or damage by trying any of the command/method mentioned in this blog. You may use the commands/method/scripts on your own responsibility.If you find something useful, a comment would be appreciated to let other viewers also know that the solution/method work(ed) for you.


UNIX one liners - Very helpful in daily activities

In our day to day activities, we will be doing most of the routine tasks which needs many commands to run for completeing the task.
Below are few of the one-liner commands that can achieve our tasks.


These one liners may be of help to identify files to be acted upon by xargs.

1. To identify files in dir /tmp dated March with  psx2 anywhere in the name :and remove them

    find /tmp -type f -name *psx2* -ls | awk '$8=="Mar"' | awk -F" " ' { print  $11 }' | xargs rm -rf


2. To identify multiple name formats dated Apr 10 and gzip them ...

    cd /logs/of/surpriseme

    find `pwd` -type f \( -name user1_log..txt -o -name system3_log.txt \) -ls | grep "Apr 10" | awk -F" " ' { print  $11 }' | xargs gzip



3. To identify files dated March 20-31 and remove them.

    find `pwd` -type f  -ls |  awk '$6=="Mar" && $7>19 && $7<32' | awk -F" " ' { print  $11 }' | xargs rm -rf
       
        find `pwd` -type f  -ls |  awk '$8=="Aug" && $9>19 && $9<22'


4. To find the total filespace used by User willy on a system.

    find / -user willy -type f -ls 2> /dev/null | awk '{ sum += $7 } END { printf " %7.3f MB \n", (sum / (1024 * 1024)) }'

5. To find 20 biggest files on the /var filesystem showing their owner, group,  size in MB and path:

    find  /var -type f   -ls | sort -rn +6 | head -20 | awk -F " " '{ printf " %s \t %s \t %7.3f MB \t %s \n" , $5, $6,  ( $2/1024), $11}'

6. To find  files older than 12-Jan-2007  and move them to the /tmp dir. First identify a file with that date stamp in the file system. Then

    find `pwd` -type f  !-newer /absolute_path_to/file_with_cutoffpoint/datestamp - ls | awk -F " " '{ print $11 }' | xargs -I { } mv { } /tmp
7. To rename files names with extension .txt to .log

    find $(pwd) -type f -name *.txt | xargs -i mv {} {}.log


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.