Find and excute

I use this command quite often to set the permissions on multiple files or directories recursively¬†inside a directory tree. I’ve prepared a couple of sample commands first one change the permissions on files only, second change permissions on directories only.

The base command is

user$ find ./ -type _x_ -execdir _command_ {} +
argument list:
./ - the current directory.
-type _x_ what to look for replace _x_ with, d for directories, f for files, l for links and many more in the man page of the find command.
-execdir _command_ the command and its argument to execute on the target, from the directory were the file is located.
{} +  find replaces this with the target file.

The first example set the permissions to rw-r–r– (644) on all files inside a directory tree.

user$ find ./ -type f -execdir chmod 644 {} +

The second is to change the permissions of directories for rwxr-xr-x (or 755) for all the directories in a directory tree

user$ find ./ -type d -execdir chmod 755 {} +

Now imagine that you want to list all the links and its associated target

user$ find ./ -type l -exec file {} +

NOTE: this time I’m using -exec and not -execdir this because I want to see the relative path from where I’m executing the command.

Posted in KB

Search and replace from shell using perl

Perform a search and replace within files using perl and regular expressions. To do this you will need perl installed windows or linux

user$ perl -pi -w -e'/seach_string/replase_string/g;' file pattern
-pi assume loop like -n but print line also, like sed
-w enable many useful warnings (RECOMMENDED)
-e one line of program (several -e's allowed, omit programfile)

Use netcat to dump incoming packets

Use linux netcat to create a server that will listen for incoming connections on a specific port and dump the contents of the network traffic.

user$ nc -l -p port
-l Is used to specify that nc should listen for an incoming connection, rather than initiate a connection to a remote host. Any hostname/IP address and port arguments restrict the source of inbound connections to only that address and source port
-p Specifies the source port nc should use, subject to privilege restrictions and availability
Posted in KB