Binary Revolution Forums: Shell tricks - Binary Revolution Forums

Jump to content

Welcome to Binary Revolution Forums

Welcome to the Binary Revolution Forums! You are a guest and are welcome to browse our 7 public forums at the top of the list without registering. All other forums are restricted to registered users only and will give you an "unauthorized" message if you try to browse them. However, if you Register or Sign In (ABSOLUTELY FREE and PRIVATE) you will be able to access many more sections of the site that are reserved for registered members and have lots of other benefits such as bypassing those annoying ads.

  • NO ADS! All of the ads are for GUESTS ONLY!
  • The annoying guest message (this very one) at the top of every page will go away.
  • Access our private messaging system to communicate with other users.
  • Start new topics and reply to others instead of just reading.
  • Subscribe to topics and forums to get automatic updates on watched threads.
  • Add/view events to our community calendar.
  • Customize your profile and see your statistics.
  • Change your preferences such as choosing layouts and tweaking your settings.
  • Contribute your site to our database of links.
  • Access our Gallery and all of its features (instead of just viewing thumbnails) including uploading images of your own.
  • Build up your reputation using our reputation controls.
  • Once again: NO ADS! All of the ads are for GUESTS ONLY! It is all free so Register Now!
Guest Message © 2012 DevFuse
ANNOUNCEMENTS & NEWS:


ANNOUNCEMENT: BinRev is now proud to be hosted and affiliated with lunarpages! See the announcement at the top of the forums for more details.
ANNOUNCEMENT: Follow us on twitter: @binrev @stankdawg

NOTE 2011-11-11: post-dated! I will be getting married on 11/11/11 and will be unavailable for the rest of of November! Any forum issues should be directed to lattera. We apologize in advance for slow responses.

-- StankDawg

  • (5 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • You cannot start a new topic
  • You cannot reply to this topic

Shell tricks Rate Topic: ***** 3 Votes

#61 User is offline   mobilec 

  • SCRiPT KiDDie
  • Icon
  • View blog
  • Group: Members
  • Posts: 25
  • Joined: 09-September 06
  • Location:Ottawa, Canada
Reputation: 0
Neutral

Posted 17 February 2008 - 07:03 PM

View PostOhm, on Feb 11 2007, 04:31 PM, said:

Try echo -n >file instead. No newline.

touch file

0

#62 User is offline   duper 

  • Dangerous free thinker
  • Icon
  • View blog
  • Group: Members
  • Posts: 815
  • Joined: 06-May 05
  • Location:NYC
Reputation: 2
Neutral

Posted 17 February 2008 - 07:15 PM

View Postmobilec, on Feb 17 2008, 07:03 PM, said:

View PostOhm, on Feb 11 2007, 04:31 PM, said:

Try echo -n >file instead. No newline.

touch file



>file

This post has been edited by duper: 17 February 2008 - 07:16 PM

0

#63 User is offline   mirrorshades 

  • aviatorglasses
  • Icon
  • View blog
  • Group: Agents of the Revolution
  • Posts: 950
  • Joined: 07-March 06
  • Gender:Male
Reputation: 4
Neutral

Posted 17 April 2008 - 02:49 PM

Okay... this may be a trick, because it's not in the manpage and I've not really seen it listed anywhere else. :)

To quickly move back and forth between two directories, you can use a single hyphen (-) after the "cd" command. Basically, it takes you back to the directory you were in immediately before the current one:

root@nato [/]# cd /etc/ssl
root@nato [/etc/ssl]# cd /
root@nato [/]# cd -
/etc/ssl
root@nato [/etc/ssl]# cd -
/
root@nato [/]#

0

#64 User is offline   PurpleJesus 

  • Dangerous free thinker
  • Icon
  • View blog
  • View gallery
  • Group: Members
  • Posts: 1,570
  • Joined: 29-May 06
  • Gender:Male
  • Location:800
Reputation: 9
Neutral

Posted 17 April 2008 - 08:46 PM

View Postmirrorshades, on Apr 17 2008, 01:49 PM, said:

Okay... this may be a trick, because it's not in the manpage and I've not really seen it listed anywhere else. :)

To quickly move back and forth between two directories, you can use a single hyphen (-) after the "cd" command. Basically, it takes you back to the directory you were in immediately before the current one:

root@nato [/]# cd /etc/ssl
root@nato [/etc/ssl]# cd /
root@nato [/]# cd -
/etc/ssl
root@nato [/etc/ssl]# cd -
/
root@nato [/]#



cool... it works..
0

#65 User is offline   inaequitas 

  • SUP3R 31337
  • Icon
  • View blog
  • Group: Members
  • Posts: 158
  • Joined: 25-July 07
Reputation: 0
Neutral

Posted 19 April 2008 - 10:24 AM

View Postmirrorshades, on Apr 17 2008, 11:49 AM, said:

Okay... this may be a trick, because it's not in the manpage and I've not really seen it listed anywhere else. :)

To quickly move back and forth between two directories, you can use a single hyphen (-) after the "cd" command. Basically, it takes you back to the directory you were in immediately before the current one:

root@nato [/]# cd /etc/ssl
root@nato [/etc/ssl]# cd /
root@nato [/]# cd -
/etc/ssl
root@nato [/etc/ssl]# cd -
/
root@nato [/]#


If you want to keep track of multiple folders you've been through, try pushd / popd. You basically get a 'directory stack' to push and pop from at your heart's desire.
0

#66 User is offline   .solo 

  • Gibson Hacker
  • Icon
  • View blog
  • Group: Members
  • Posts: 80
  • Joined: 16-September 07
Reputation: 4
Neutral

Posted 19 April 2008 - 04:41 PM

Often forget to use sudo and then having to up-arrow and add it is a bitch. Instead do a
sudo !!

!! - denotes last command in history
echo $?

returns the exit code of the last command, which is often 0 for successful or 1 for failure. perhaps someone could elaborate.
cd ~

~ - denotes home directory. again not a groundbreaking trick just something beginners might find useful

This post has been edited by .solo: 19 April 2008 - 04:44 PM

0

#67 User is offline   .solo 

  • Gibson Hacker
  • Icon
  • View blog
  • Group: Members
  • Posts: 80
  • Joined: 16-September 07
Reputation: 4
Neutral

Posted 19 April 2008 - 04:47 PM

why the fuck are my code boxes HUGE!?
0

#68 User is offline   Dirk Chestnut 

  • SUP3R 31337 P1MP
  • Icon
  • View blog
  • Group: Members
  • Posts: 268
  • Joined: 21-September 06
  • Location:248
Reputation: 2
Neutral

Posted 11 June 2008 - 10:14 PM

Here's a yum specific trick:

I typically work with Redhat-related (Redhat, Fedora, CentOS, etc) systems, and use yum as my desired mechanism for managing installation/dependencies of packages. I also do a lot of dev work in Perl, which means installing lots of modules from CPAN. If the distro provides one, the sanest way for me to install packages is usually through my distro's repositories. This isn't always the easiest to do, though, as packages are rarely named Some::Module in the repos, nor are they always called perl-Some-Module.

Take the following example (from Fedora 8) for a common module, LWP::Simple:

$ yum search LWP::Simple
No Matches found
$ yum search perl-LWP-Simple
No Matches found


It's not that Fedora 8 doesn't have it's own package for LWP::Simple, it's just that it's bundled with something else, and named in a way you wouldn't necessarily expect. If you instead run this command:

$ yum whatprovides 'perl(LWP::Simple)'
perl-libwww-perl.noarch : A Perl interface to the World-Wide Web


Voila! We now know that 'perl-libwww-perl' is actually the package responsible for the LWP::Simple module.
0

#69 User is offline   Baphomet 

  • Will I break 10 posts?
  • Icon
  • View blog
  • Group: Members
  • Posts: 6
  • Joined: 30-June 08
Reputation: 0
Neutral

Posted 07 July 2008 - 01:28 PM

Here's my "trick"

$? holds the return value for the last command. E.g.

$ curl urlthatdoesntexist.com
curl: (6) Couldn't resolve host 'urlthatdoesntexist.com'
$ echo $?
6

$ curl www.google.com > /dev/null
  % Total	% Received % Xferd  Average Speed   Time	Time	 Time  Current
								 Dload  Upload   Total   Spent	Left  Speed
100  6538	0  6538	0	 0  18957	  0 --:--:-- --:--:-- --:--:-- 55232
$ echo $?
0


EDIT: Oops, just realized this has already been covered, hopefully my elaboration will add something ;)

pushd stores the current direction and changes to a new one. popd returns to the last stored directory.
$ pwd
/home/baphomet
$ pushd /
/ ~
$ pwd
/
$ popd
~
$ pwd
/home/baphomet

This post has been edited by Baphomet: 07 July 2008 - 01:32 PM

0

#70 User is offline   Spyril 

  • Hakker addict
  • Icon
  • View blog
  • Group: Members
  • Posts: 588
  • Joined: 06-May 07
  • Location:North Dakota
Reputation: 14
Neutral

Posted 10 July 2008 - 10:00 PM

View most-used commands:
history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c | sort -n | tail | sort -nr

0

#71 User is offline   Ohm 

  • I could have written a book with all of these posts
  • Icon
  • View blog
  • View gallery
  • Group: Members
  • Posts: 3,209
  • Joined: 04-October 06
  • Gender:Male
  • Location:Maine, USA
Reputation: 27
Neutral

Posted 10 July 2008 - 10:55 PM

View PostSpyril, on Jul 10 2008, 11:00 PM, said:

View most-used commands:
history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c | sort -n | tail | sort -nr


I think you took a few extra steps in there. The second awk doesn't do anything, only the first word of the command will get through the first awk, and there aren't bound to be any pipe characters in that so the second awk is essentially a no-op. You also do an extra sort.

history | awk '{print $2}' | sort | uniq -c | sort -nr | head

0

#72 User is offline   Alk3 

  • "I Hack, therefore, I am"
  • Icon
  • View blog
  • View gallery
  • Group: Binrev Financier
  • Posts: 1,001
  • Joined: 06-May 06
  • Gender:Not Telling
  • Location:912 Georgia
Reputation: 0
Neutral

Posted 24 July 2008 - 09:11 PM

At work I had to delete email for about 50 users on a server running courier imap as the inbound email service. Somehow they had over 100GB of email, so I was told to delete it all, but avoid destroying the Maildir directories. It uses system commands, so it has the inability to crash unless there is a big problem with the OS, but runs slower than a script in perl/python/ruby.

		   localhost:~$ cat read.sh 
			  #!/bin/bash
			  
			  workingdir='/tmp/test'
			  cd $workingdir
			  
			  array=(`ls`)
			  len=${#array[*]}
			  i=0
			  
			  while [ $i -lt $len ]; do
					  echo "${array[$i]}" >> $HOME/output.txt
					  let i++
			  done
			  
			  cat $HOME/output.txt | while read line; do
					  ls $workingdir/"${line}"/cur/* >> $HOME/result.txt  #change this to ls $workingdir/"${line}"/cur/* | xargs rm -rf
			  done


This will work for any scenario that you want to delete large quantities of files with a specific directory structure. I thought I would post this because it contains quite a few shell tricks.

You can play with this if you make a directory structure in /tmp like this:

Quote

localhost:~$ ls /tmp/test/*/*
/tmp/test/1/cur:
file

/tmp/test/2/cur:
file

/tmp/test/3/cur:
file

/tmp/test/4/cur:
file

0

#73 User is offline   Ohm 

  • I could have written a book with all of these posts
  • Icon
  • View blog
  • View gallery
  • Group: Members
  • Posts: 3,209
  • Joined: 04-October 06
  • Gender:Male
  • Location:Maine, USA
Reputation: 27
Neutral

Posted 25 July 2008 - 12:36 AM

You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?
0

#74 User is offline   Alk3 

  • "I Hack, therefore, I am"
  • Icon
  • View blog
  • View gallery
  • Group: Binrev Financier
  • Posts: 1,001
  • Joined: 06-May 06
  • Gender:Not Telling
  • Location:912 Georgia
Reputation: 0
Neutral

Posted 25 July 2008 - 05:55 PM

View PostOhm, on Jul 25 2008, 12:36 AM, said:

You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?


Because I was working with a directory structure that has about 500 email accounts. There are quite a few things left out of this script to avoid posting SQL scripts for a propriatery software company on a public forum.

The directory structure is like this: /var/mail/<customerid>/Maildir/cur

Where the customer id is pulled from a SQL database and inputted into my bash script that deletes the contents of the person(s) inbox. What decided the customer id in my case was inactive customers that had email disabled due to negligence of some type: spam, porn, large attachments, you name it.
0

#75 User is offline   Ohm 

  • I could have written a book with all of these posts
  • Icon
  • View blog
  • View gallery
  • Group: Members
  • Posts: 3,209
  • Joined: 04-October 06
  • Gender:Male
  • Location:Maine, USA
Reputation: 27
Neutral

Posted 25 July 2008 - 10:34 PM

View PostAlk3, on Jul 25 2008, 06:55 PM, said:

View PostOhm, on Jul 25 2008, 12:36 AM, said:

You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?


Because I was working with a directory structure that has about 500 email accounts. There are quite a few things left out of this script to avoid posting SQL scripts for a propriatery software company on a public forum.

The directory structure is like this: /var/mail/<customerid>/Maildir/cur

Where the customer id is pulled from a SQL database and inputted into my bash script that deletes the contents of the person(s) inbox. What decided the customer id in my case was inactive customers that had email disabled due to negligence of some type: spam, porn, large attachments, you name it.


Well with the interesting parts gutted, this script does essentially nothing. I'm just wondering why you posted it.
0

#76 User is offline   L33T_j0sH 

  • SUP3R 31337
  • Icon
  • View blog
  • View gallery
  • Group: Members
  • Posts: 185
  • Joined: 07-December 07
  • Location:ALABAMA
Reputation: 0
Neutral

Posted 12 October 2008 - 09:50 PM

Wow... I didn't know about the Alias command. I have already used it multiple times since I read this thread today. Very useful command.
0

#77 User is offline   duper 

  • Dangerous free thinker
  • Icon
  • View blog
  • Group: Members
  • Posts: 815
  • Joined: 06-May 05
  • Location:NYC
Reputation: 2
Neutral

Posted 13 October 2008 - 01:51 PM

This generates a list of resolved Linux virtual hosts on the fly:

#!/bin/sh
for i in `/sbin/ifconfig -a|grep 'inet addr'|awk -F":" '{print $2}'|awk '{print $1}'`;do host $i;done 2>/dev/null


Can anybody come up with anything that uses a different approach or is more concise?
0

#78 User is offline   MrFluffy 

  • HACK THE PLANET!
  • Icon
  • View blog
  • Group: Validating
  • Posts: 68
  • Joined: 12-December 07
  • Country:
  • Gender:Male
  • Location:somewhere
Reputation: 1
Neutral

Posted 23 April 2010 - 06:50 AM

View PostOhm, on 25 July 2008 - 10:34 PM, said:

View PostAlk3, on Jul 25 2008, 06:55 PM, said:

View PostOhm, on Jul 25 2008, 12:36 AM, said:

You just want to rm -Rf all files under $workingdir/"${line}"/cur/? That's a one liner, why is this a 20 liner?


Because I was working with a directory structure that has about 500 email accounts. There are quite a few things left out of this script to avoid posting SQL scripts for a propriatery software company on a public forum.

The directory structure is like this: /var/mail/<customerid>/Maildir/cur

Where the customer id is pulled from a SQL database and inputted into my bash script that deletes the contents of the person(s) inbox. What decided the customer id in my case was inactive customers that had email disabled due to negligence of some type: spam, porn, large attachments, you name it.

Well with the interesting parts gutted, this script does essentially nothing. I'm just wondering why you posted it.



Actually, although Im replying to prehistory, rm -rf , or rather the linux kernel has a hard limit to how many files it can remove. It spews back "/bin/rm: Argument list too long." when it exhausts the between process's execve buffer's in the kernel (its only 128k).

However, a simple solution is as below.

find . -name '*' -print0 | xargs -0 rm


Original info provided by John simpson, I just googled it when I hit the above limit one day.

http://leap-cf.org/o...May/038802.html
0

#79 User is offline   Mikhail 

  • SCRiPT KiDDie
  • Icon
  • View blog
  • View gallery
  • Group: Members
  • Posts: 21
  • Joined: 16-December 07
  • Location:Long Beach, Ca
Reputation: -3
Neutral

Posted 06 November 2010 - 03:49 PM

I/O redirection is what using < / > is called.
0

#80 User is offline   n3xg3n 

  • "I Hack, therefore, I am"
  • Icon
  • View blog
  • Group: Members
  • Posts: 960
  • Joined: 14-July 06
  • Country:
  • Gender:Male
  • Location:(703)
Reputation: 34
Neutral

Posted 21 November 2010 - 08:37 PM

If you make a mistake typing a command you can change something specific and then re-execute using the following bash trick:

[n3xg3n@enigma ~]$ wgt http://www.whatismyip.org
bash: wgt: command not found
[n3xg3n@enigma ~]$ ^wgt^wget
wget http://www.whatismyip.org
--2010-11-21 20:35:48--  http://www.whatismyip.org/
Resolving www.whatismyip.org...


basically, ^old^new will replace 'old' with 'new' in the previous command and re-execute.
0

Share this topic:


  • (5 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • You cannot start a new topic
  • You cannot reply to this topic


  • Time Now: Feb 10 2012 07:25 PM