Jump to content

I Didn't Know You Could Do That in Linux...


Recommended Posts

  • 2 weeks later...
  • Replies 36
  • Created
  • Last Reply

Top Posters In This Topic

Using netcat (nc) to capture the latest Windows worm for forensic analysis - this captures the Sasser worm (which spread via port 445)

 

nc -l -v -p 445>sass$$$

 

Directory listing - sort by date

ls -tl

 

sort by reverse date

ls -trl

 

sort - smallest to largest filesize

ls -al | sort +4n

 

largest to smallest

ls -al | sort +4nr

 

grep & date

grep "$(date +"%b %d")" /var/log/messages

 

so if todays date is 18th Nov, the above command would be translated into

grep "Nov 18" /var/log/messages

(useful for cron jobs e.g you want to be emailed if a certain message appears in your logs)

 

Rename file extensions recursively down through an entire directory tree

This finds all .bak files in my /websites directory and renames them to .bak.php

 

find /websites -name *.bak | xargs -i -t rename .bak .bak.php

 

Find all *.jpg files that are actually PNG files and copy them to my home dir

(requires ImageMagick package to be installed)

identify *.jpg | grep "PNG" | cut -d '[' -f 1 | xargs -i -t cp {} /home/kde-head/pngstuff/{}

 

Mail the output of something to yourself:

ls -al /var/lib | mail -s "lib dir" kde-head@somewhere.com

Link to comment
Share on other sites

For those (like me) who have zero coding / scripting ability, the single most useful little 'scripting'-type thing you can learn is this generic command:

 

for i in `ls -1`; do something $i; done

 

You can put any command in between the ` `, and you can use any command after "do". So this lets you specify a set of 'things' to do something to, and then do anything to that set of 'things'. Here's a simple example I use quite often:

 

for i in `ls -1 *.jpg`; do convert -scale 800x600 $i $i; done

 

This scales every jpg in a given directory down to 800x600. Fast, no mess. (BTW, the -1 option for ls makes each result print out on a single line, instead of printing as many columns as will fit on the screen. It's necessary to make the for loop process the name correctly.) There's all sorts of things you can do with this simple template, just add ingenuity :)

Edited by adamw
Link to comment
Share on other sites

Also you are doing an Useless Use Of 'ls', you can do the same better w/o it;

for i in *.jpg; do convert -scale 800x600 $i $i; done

 

following aRTee's advice your command will be safer in the form:

 

for i in *.jpg; do convert -scale 800x600 $i ${i%.*}.new.jpg; done

 

that will produce the output image as "imagename.new.jpg"

 

HTH

Link to comment
Share on other sites

Just another silly trick, display the current month's calendar showing today within brackets:

 

~$ cal | sed 's/ '"${today:=$(date +%e)}"' /['"$today"']/'
   November 2004
Su Mo Tu We Th Fr Sa
   1  2  3  4  5  6
7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22[23]24 25 26 27
28 29 30

 

The same but marking today in red too:

 

~$ echo -e "$(cal | sed 's/ '"${today:=$(date +%e)}"' /\\033[0;31m['"$today"']\\033[0;39m/')"

 

That is a cool thing to add to your motd, or bashrc

 

more info at man cal, man sed and man bash

Link to comment
Share on other sites

aru: yep, that's a good alternative. I generally don't like to keep the large versions, but if you do, yours is a nice way. I included the 'ls' to indicate that the first part of this template can include a command, and by implication it can be *any* command. I wasn't trying to use the most efficient method of this particular operation, I was trying to illustrate a *general* template that you can use to do lots of things. I'm sorry I couldn't come up with an example that really *needed* to use a command, but my brain was tired. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share


×
×
  • Create New...