For Latest Tips, Tricks, Links and many more......to your Mobile Inbox .

Just Type From Your Mobile -


JOIN Internet_Hacker ~send to~ 567678

& also

ON Tricks_and_Tips ~send to~ 9870807070

Don't Miss It.... Join With us Today.......

(For Indian Mobiles)
Innovative PPC platform.

Greatest Linux Command Ever!

This is the greatest Linux command ever! Definitely my favorite.
find ./ -name \*.html -printf '%CD\t%p\n' | grep "03/10/08" | awk '{print $2}' | xargs -t -i mv {} temp/

linux-penguin-logo.jpgWhat it does is look (find) for files that end in .html uses the printf option to format the 'find' output, then passes it to grep for searching for a certain date, then awk for printing a certain field, and finally xargs for executing a certain command.

Let's break it down...

The printf part within the find command has the format '%CD\t%p\n'.

%Cx = File's last status change time in the format specified by x. x=D. D=date in the format mm/dd/yy
\t = Horizontal tab
%p = file's name
\n = newline

So basically it outputs the file's last status change followed by a horizontal tab, then the filename, and then a new line. But before it outputs it, it sends it to 'grep' which searches the output and only outputs lines with "03/10/09".

Example so far: (minus the awk, xargs and mv commands)
find ./ -name \*.html -printf '%CD\t%p\n' | grep "03/10/09"
Outputs this: (notice the tab to separate the 2 fields)
03/10/09 ./2005/05/index.html
03/10/09 ./2005/03/index.html
03/10/09 ./2005/04/index.html
03/10/09 ./linked-in.html
03/10/09 ./consumer-electronics/samsung-bribery-news.html
03/10/09 ./technology/iptv/index.html

Now send this output into the awk command (awk '{print $2}') which parses it and pulls out the 2nd column/field (hence the tab character), which is the filename, including the path.

Here's the output you now have after adding awk '{print $2}' in:
./2005/05/index.html
./2005/03/index.html
./2005/04/index.html
./linked-in.html
./consumer-electronics/samsung-bribery-news.html
./technology/iptv/index.html

Next, send this output of "exact path + filename" to xargs for execution in the Linux shell.

The "xargs -t -i mv {} temp/" part basically takes the input from the previous commands (files named .html modified on 3/10/09) and moves (mv) them to the temp/ folder.

The xargs command can do anything. So instead of moving the files, I could delete them, run chmod on them, or something else.

It took me awhile to write this command. I've used various methods of finding files on Linux servers over the years, but this one is one of the most powerful.

Definitely a command you should have in your Linux arsenal!

p.s. Here's another tip. If you want to search ALL files (not just .html) then use the following command. Notice the \* and not * for the search. That part got me since I didn't think the * (wildcard) had to be backslashed. Usually when you backslash a character that means you want the 'literal' character specified after the \ (backslash) character. I didn't want filenames with a '*' in it. I wanted the wildcard. That threw me for a minute before I figured it out. Anyway, here's the command:
find ./ -name \* -printf '%CD\t%p\n' | grep "03/10/08" | awk '{print $2}' | xargs -t -i mv {} temp/

No comments:

Pay Per Click Solutions - Increase your earnings

The Amazing World of Entertainment & Fun Stuff