Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Saturday, June 27, 2015

How to replace substring in linux

There is an easy way to do a mass replace of substring in linux. You can use perl command and a combination of "find/xargs/perl" like this:

find -name '*.log' -type f -print0 | xargs --null perl -pi -e 's/redhat/SUSE/'

The you can tweak the first part of the command (find) to point it to the files which have to be changed. redhat will be replaced with SUSE for all log files in this case.

Wednesday, October 3, 2012

Create symlink to the most recent directory

Recently I needed to find a way to create a symlink to the most recent directory so it can be synced up to some other place. Here is the command in linux which can do that:


find /home/ -maxdepth 1 -mindepth 1 -type d -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | cut -d ' ' -f 3 | head -n 1 | awk -F" " '{print "ln -s "$1" latest-dir"}'| bash

The command above creates a symlink to the most recent directory under /home/. As usual, first try it without "| bash" at the end, so you can test what does it print out.

Monday, September 10, 2012

How to delete files not updated within X days

How to get a list of files which were not modified within last X days? Here is the trick. This command will print out information about those files which were not modified within last 30 days in the current directory:

find . -mtime +30 -type f -exec ls -la {} \; 


This command will delete those files which were not modified within last 30 days in the current directory:

find . -mtime +30 -type f -exec rm -fr {} \;