(My) Unix useful CLI commands

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Video

Remove the rotating bit in video metadata with ffmpeg

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Video

Remove rotation metadata from video with FFMPEG

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=0 output.mp4

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Mud, Sweat and Tears by Bear Grylls

Just finished reading “Mud, Sweat and Tears” by Bear Grylls. Everything comes with a cost, and he payed with lots of mud and sweat. I believe most of the tears were dropped by happiness. I liked it.

It starts really slow and dull, about his roots, from the ancestors (great grandfathers and so) to parents and sister. All the people that were influential through childhood to manhood… i understand that he wants to give a good background about himself, but it’s rather too much of it, as one actions are much more important that one background.

Anyway down the line it starts to pay off, from the SAS training (the failed first attempt to the successful second go), the parachute accident, his faith and recovery, and of course it climaxes with the climb to the summit of Everest.

It’s an inspirational book, lots and lots of details (the summit ascend alone is around 200 pages) and funny stories. It has a simple life pattern: do what you want/love, work hard, give that extra to the ordinary, take risks, and with a bit of luck it all comes together.

Here’s to capitalism

For sure it has a zillion faults, but in other hand it can supply all kinds of material goods, both essential and non essential ones. It has been glorified or vilified by people all over the world.

But i wonder, what other system could provide me with a full spec, technology loaded, big 50″ TV screen for just a tiny fraction more than the country minimal wage (delivery included) ?

JS setTimeout variable passing, accessing this and scope

I’m really not a JS ninja, but as the defacto language of the Web, it’s rather a unique day if i don’t read or write some javascript code and this a pattern that is well worth to know.

First example, a simple var:

function myFunction() {
    var myVar = 'test';
    setTimeout('alert(myVar)', 1000);
};

myFunction();

fails with myVar is not defined error, because setTimout works on the window object (the top level object for browsers), and there is no myVar registered as a global var (window.myVar is undefined).

You can easily work this out with a closure:

function myFunction() {
    var myVar = 'test';
    setTimeout(function() { alert(myVar) }, 1000);
};

myFunction();

simply a closure is a function declared inside other function. We have an anonymous function as the first argument of setTimeout inside myFunction, so it’s a closure. The magic of closures is that they keep state (the local variables remain accessible) after the container function has exited.

The curious case of this keyword… take the code:

function myClass () {
    this.myVar = 'test';

    this.myFunction = function() {
        setTimeout(function() {alert(this.myVar)}, 1000);
    }
}

myobj = new myClass();
myobj.myFunction();

and boum! undefined pops-up, WTF? after messing around with call, apply and even with i could only make it work assigning this to a local variable:

function myClass () {
    var self   = this;
    this.myVar = 'test';

    this.myFunction = function() {
        setTimeout(function() {alert(self.myVar)}, 1000);
    }
}

myobj = new myClass();
myobj.myFunction();

this of course is a non elegant solution, so please be my guest of posting a better solution to this issue.