Technical (Development, Security, etc.) > General Programming

Random Shellscript Snippits

(1/2) > >>

iago:
This has probably been done before, but whatever. People sometimes ask me for snippits of shellscript to do something handy. I figured I'd start posting them somewhere for the benefit of others. Feel free to contribute, but please stay on topic. If you want to discuss one, please start a new thread. (discussion here)

Find all files in the directory with the same contents as a specified file
$ md5sum * | grep `md5sum filename | cut -b1-32`

(bash) Print all duplicate files in a folder
for i in `md5sum * | cut -d\   -f1 | sort | uniq -d`; do md5sum * | grep $i; done


<edit> (on both my and tmp's posts) -- changed title to shellscript instead of shellcode. Oops! :)

mynameistmp:
perl can be a powerful stream editor. Most of you are probably familiar with perl command line execution a la the  -e flag.  Combine it with -p and -i and you get a worthy sed alternative! The -p flag tells perl to iterate the command given by -e over every command line arg (in this case, a filename, hopefully). If there are no command line args, it'll read from STDIN. The -i flag writes the output of the -p iteration back to the argument (writing the file).

Examples:

tmp@tmp:~$ perl -e 'print "test"x10;'

-Print 'test' to STDOUT 10 times. For those of you who are behind on the times ;P

tmp@tmp:~$ perl -pe 's/^(\s+)?(telnet|shell|login|exec)/# $2/' /etc/inetd.conf

-Comment out any uncommented (running) instances of telnet, shell, login, exec in inetd.conf. Note: this will only print to STDOUT, it will not edit the file.

tmp@tmp:~$ perl -pi -e 's/bgcolor=#ffffff/bgcolor=#000000/i' *.html

-Change the background colour on any html page in the directory from black to white. The i after the substitution regex is needed to negate case sensitivity. WARNING: The previous command does WRITE to all of the files specified as command line args.

Most of you can probably infer the empowering implications of this little morsel of scripting prestidigitation.

Ender:
Woops, I should have posted this here originally.


--- Quote ---andrew@roflbirds:~$ perl -pi -e 'tr/a-mn-zA-MN-Z/n-za-mN-ZA-M/;'
newbys mom
arjolf zbz
has got it goin on
unf tbg vg tbva ba

--- End quote ---

nslay:

--- Quote from: mynameistmp on February 02, 2008, 02:01:19 am ---perl can be a powerful stream editor. Most of you are probably familiar with perl command line execution a la the  -e flag.  Combine it with -p and -i and you get a worthy sed alternative! The -p flag tells perl to iterate the command given by -e over every command line arg (in this case, a filename, hopefully). If there are no command line args, it'll read from STDIN. The -i flag writes the output of the -p iteration back to the argument (writing the file).

Examples:

tmp@tmp:~$ perl -e 'print "test"x10;'

-Print 'test' to STDOUT 10 times. For those of you who are behind on the times ;P

tmp@tmp:~$ perl -pe 's/^(\s+)?(telnet|shell|login|exec)/# $2/' /etc/inetd.conf

-Comment out any uncommented (running) instances of telnet, shell, login, exec in inetd.conf. Note: this will only print to STDOUT, it will not edit the file.

tmp@tmp:~$ perl -pi -e 's/bgcolor=#ffffff/bgcolor=#000000/i' *.html

-Change the background colour on any html page in the directory from black to white. The i after the substitution regex is needed to negate case sensitivity. WARNING: The previous command does WRITE to all of the files specified as command line args.

Most of you can probably infer the empowering implications of this little morsel of scripting prestidigitation.



--- End quote ---

Why are you using perl this way? You yourself know about sed and that's universally available on every Unix-like system.

nslay:

--- Quote from: Ender on February 02, 2008, 04:45:15 pm ---Woops, I should have posted this here originally.


--- Quote ---andrew@roflbirds:~$ perl -pi -e 'tr/a-mn-zA-MN-Z/n-za-mN-ZA-M/;'
newbys mom
arjolf zbz
has got it goin on
unf tbg vg tbva ba

--- End quote ---

--- End quote ---

tr? Why perl? You're totally killing bunnies with rocket launchers!

Navigation

[0] Message Index

[#] Next page

Go to full version