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)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic

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

#21 User is offline   lmnk 

  • elite
  • Icon
  • View blog
  • Group: Members
  • Posts: 120
  • Joined: 23-May 04
Reputation: 0
Neutral

Posted 31 January 2007 - 10:51 AM

View Postchaostic, on Jan 31 2007, 03:26 AM, said:

Now if there was an easy way to switch thru both modes...
You could write a script to do it for you, and then make an alias that runs the script.

edit: Also, there were a few posts about people not using chmod properly. If you know about permissions, to way to find the numeric version:
4 = read
2 = write
1 = execute
and that the three digits (unsurprisingly) represent user, group and others respectively.
So rwxr-xr-- would be (rwx) (r-x) (r--) = (4+2+1)(4+1)(4) = 754

This post has been edited by lmnk: 31 January 2007 - 11:57 AM

0

#22 User is offline   Octal 

  • Dangerous free thinker
  • Icon
  • View blog
  • Group: Members
  • Posts: 886
  • Joined: 19-February 06
Reputation: 0
Neutral

Posted 31 January 2007 - 04:41 PM

Some shell info:
http://news.softpedia.com/news/How-to-Cust...mpt-40033.shtml
that is for customizing the shell.
0

#23 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 31 January 2007 - 05:07 PM

View Postlmnk, on Jan 31 2007, 10:51 AM, said:

View Postchaostic, on Jan 31 2007, 03:26 AM, said:

Now if there was an easy way to switch thru both modes...
You could write a script to do it for you, and then make an alias that runs the script.

edit: Also, there were a few posts about people not using chmod properly. If you know about permissions, to way to find the numeric version:
4 = read
2 = write
1 = execute
and that the three digits (unsurprisingly) represent user, group and others respectively.
So rwxr-xr-- would be (rwx) (r-x) (r--) = (4+2+1)(4+1)(4) = 754


Knowing about it or not, that's still really error-prone. You say sure, I can do it that way. But when you're dragged out of bed at 3am to fix a problem, what are the chances you're going to make a mistake using the octal mode? Symbolic mode saves you from making stupid mistakes, use it when it makes sense. I only use octal mode when I want to set every bit to exactly what I want.
0

#24 User is offline   tehbizz 

  • Progenitor of noob slaying
  • Icon
  • View blog
  • Group: Members
  • Posts: 2,039
  • Joined: 02-April 05
  • Gender:Male
Reputation: 0
Neutral

Posted 01 February 2007 - 12:51 AM

View PostBeave, on Jan 31 2007, 01:56 AM, said:

Here's a easy, but useful one.. Say you have a large file (/var/log/messages)... you want to "zero" it out, but keep the file.. Type:

:> /var/log/messages

Done...


echo>/var/log/messages can do the same! Another tip!

Although, I don't consider 90% of the stuff in this topic a trick or a tip but basic things known about the shell(s) that you can find in the manpage alone.
0

#25 User is offline   Beave 

  • SUPR3M3 31337 Mack Daddy P1MP
  • Icon
  • View blog
  • Group: Agents of the Revolution
  • Posts: 344
  • Joined: 26-July 05
Reputation: 4
Neutral

Posted 01 February 2007 - 04:27 AM

View Posttehbizz, on Feb 1 2007, 12:51 AM, said:

View PostBeave, on Jan 31 2007, 01:56 AM, said:

Here's a easy, but useful one.. Say you have a large file (/var/log/messages)... you want to "zero" it out, but keep the file.. Type:

:> /var/log/messages

Done...


echo>/var/log/messages can do the same! Another tip!

Although, I don't consider 90% of the stuff in this topic a trick or a tip but basic things known about the shell(s) that you can find in the manpage alone.


That's correct., it'll do the same thing, but it's not the same thing. I just threw it out there as a faster means of zero'ing a file (of course, not all that much faster..). It's not something you can "man" for, hence the "trick". While 90% of it might not be relivant to you learning anything, it might be useful to others. For example, someone said using cat for creating a ISO... I pointed out dd, a small rant about hard drives, and a nice recovery tool.

You're correct, a lot of it is basic 101 unix crap... Doesn't really matter.. How bout posting a "trick" (that's worthy), then maybe other will fall :)

This post has been edited by Beave: 01 February 2007 - 04:29 AM

0

#26 User is offline   tehbizz 

  • Progenitor of noob slaying
  • Icon
  • View blog
  • Group: Members
  • Posts: 2,039
  • Joined: 02-April 05
  • Gender:Male
Reputation: 0
Neutral

Posted 01 February 2007 - 07:00 PM

OK here's a real trick.

How to read directories with spaces in them (eg. /home/tehbizz/my mp3s) into an array or anything else. I had to do this for a script I was working on for some recursive copying (NOT cp -r, that's for copying entire directories from A to B, not copying file A to directories A-Z). This is how I had to do it:

`set IFS=$'\n'`

This sets $IFS (internal field separator) to simply \n or newlines. Normally IFS is set to space horizontal tab newline (in that order) so whenever a space is encountered, IFS breaks and proceeds to the next item. As you can imagine, this is quite a problem when you are encountered folders with spaces in them and setting $IFS to only break on newlines fixes it immediately. Actually finding this solution took a good 8 or so days of trial and error and a ton of reading since not a lot of people ever bother with changing $IFS because its typical state of " \t\n" works for just about everything in the first place.
0

#27 User is offline   Octal 

  • Dangerous free thinker
  • Icon
  • View blog
  • Group: Members
  • Posts: 886
  • Joined: 19-February 06
Reputation: 0
Neutral

Posted 01 February 2007 - 07:42 PM

tehbiz, I have always used this:
cd /home/tehbizz/my\ mp3s
The '\ ' identifys a space in the file, just so bash won't think it is another day.

This took a good 5 seconds, and found out with a hit of tab because I am too lazy to type out a full name.
0

#28 User is offline   tehbizz 

  • Progenitor of noob slaying
  • Icon
  • View blog
  • Group: Members
  • Posts: 2,039
  • Joined: 02-April 05
  • Gender:Male
Reputation: 0
Neutral

Posted 01 February 2007 - 11:38 PM

View PostOctal, on Feb 1 2007, 07:42 PM, said:

tehbiz, I have always used this:
cd /home/tehbizz/my\ mp3s
The '\ ' identifys a space in the file, just so bash won't think it is another day.

This took a good 5 seconds, and found out with a hit of tab because I am too lazy to type out a full name.


I know that a \ will escape a space, putting quotes around it will do the same. Single quotes can serve the same purpose. You can even use sed or cut to chop out the spaces. However, I know this and this was /not/ what I was looking to do. Re-read my post and you'll realize that this would never work in an array as $IFS would break immediately on your space.

Try making a few directory names with spaces in them and then try to read each one into an array, you won't get the result that you expect without changing $IFS. In fact, here's a script to do it for you and echo the results:

#!/bin/bash -

mkdir "test 1"
mkdir "test 2"
mkdir "test 3"
mkdir "test 4"

for dir in $(ls $PWD)
		do
				echo $dir
		done

exit 0


Now, see if it echoes this:

test 1
test 2
test 3
test 4


And you'll realize what my trick is exactly talking about.
0

#29 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 02 February 2007 - 12:52 AM

View Posttehbizz, on Feb 1 2007, 06:00 PM, said:

OK here's a real trick.

How to read directories with spaces in them (eg. /home/tehbizz/my mp3s) into an array or anything else. I had to do this for a script I was working on for some recursive copying (NOT cp -r, that's for copying entire directories from A to B, not copying file A to directories A-Z). This is how I had to do it:

`set IFS=$'\n'`

This sets $IFS (internal field separator) to simply \n or newlines. Normally IFS is set to space horizontal tab newline (in that order) so whenever a space is encountered, IFS breaks and proceeds to the next item. As you can imagine, this is quite a problem when you are encountered folders with spaces in them and setting $IFS to only break on newlines fixes it immediately. Actually finding this solution took a good 8 or so days of trial and error and a ton of reading since not a lot of people ever bother with changing $IFS because its typical state of " \t\n" works for just about everything in the first place.

I understand what you mean and it is a great solution.
0

#30 User is offline   Beave 

  • SUPR3M3 31337 Mack Daddy P1MP
  • Icon
  • View blog
  • Group: Agents of the Revolution
  • Posts: 344
  • Joined: 26-July 05
Reputation: 4
Neutral

Posted 02 February 2007 - 06:02 AM

View Posttehbizz, on Feb 1 2007, 07:00 PM, said:

OK here's a real trick.

How to read directories with spaces in them (eg. /home/tehbizz/my mp3s) into an array or anything else. I had to do this for a script I was working on for some recursive copying (NOT cp -r, that's for copying entire directories from A to B, not copying file A to directories A-Z). This is how I had to do it:

`set IFS=$'\n'`

This sets $IFS (internal field separator) to simply \n or newlines. Normally IFS is set to space horizontal tab newline (in that order) so whenever a space is encountered, IFS breaks and proceeds to the next item. As you can imagine, this is quite a problem when you are encountered folders with spaces in them and setting $IFS to only break on newlines fixes it immediately. Actually finding this solution took a good 8 or so days of trial and error and a ton of reading since not a lot of people ever bother with changing $IFS because its typical state of " \t\n" works for just about everything in the first place.


Rock on.. From bitching (no offense) to leading the way.. :)

You've just made the thread more interesting...
0

#31 User is offline   nullkraft 

  • SUP3R 31337 P1MP
  • Icon
  • View blog
  • View gallery
  • Group: Binrev Financier
  • Posts: 284
  • Joined: 06-May 06
  • Location:Think Heisenberg uncertainty principle.
Reputation: 0
Neutral

Posted 02 February 2007 - 06:40 PM

How about another one...

If you are desperate to find a file but you only know a word or two then you can use this to dig really deep into your hard drive. FYI: This could talke a fairly long time on a large drive but if you gotta you gotta.

find * -exec strings -a -f -n 11 {} \; | grep "death eater"

Notice that the number 11 matches the number of letters and spaces in the word "deatheater". Make sure you change the number to match your search term.

This post has been edited by nullkraft: 02 February 2007 - 06:41 PM

0

#32 User is offline   Octal 

  • Dangerous free thinker
  • Icon
  • View blog
  • Group: Members
  • Posts: 886
  • Joined: 19-February 06
Reputation: 0
Neutral

Posted 02 February 2007 - 09:40 PM

This hasn't been mentioned yet:
A wildcard: *
You could put it like this:
ls *.c

would show all files in that directory that end with '.c' you could also use it like this:
ls hello*.c

would display all files in that directory that end with .'c' and start with 'hello'.

That can be helpful when you are dealing with many files in one directory.
0

#33 User is offline   b8zs 

  • The phorce is with me!
  • Icon
  • View blog
  • Group: Members
  • Posts: 76
  • Joined: 22-August 05
  • Location:64 Kbps x24
Reputation: 0
Neutral

Posted 02 February 2007 - 10:07 PM

GNU bash, version 3.1.17(1)-release (i686-pc-linux-gnu):


Spoiler

0

#34 User is offline   tehbizz 

  • Progenitor of noob slaying
  • Icon
  • View blog
  • Group: Members
  • Posts: 2,039
  • Joined: 02-April 05
  • Gender:Male
Reputation: 0
Neutral

Posted 03 February 2007 - 01:12 AM

View PostOctal, on Feb 2 2007, 09:40 PM, said:

This hasn't been mentioned yet:
A wildcard: *
You could put it like this:
ls *.c

would show all files in that directory that end with '.c' you could also use it like this:
ls hello*.c

would display all files in that directory that end with .'c' and start with 'hello'.

That can be helpful when you are dealing with many files in one directory.


Still not a trick. This is explained in the manpage.
0

#35 User is offline   b8zs 

  • The phorce is with me!
  • Icon
  • View blog
  • Group: Members
  • Posts: 76
  • Joined: 22-August 05
  • Location:64 Kbps x24
Reputation: 0
Neutral

Posted 03 February 2007 - 01:26 AM

View Posttehbizz, on Feb 2 2007, 11:12 PM, said:

Still not a trick. This is explained in the manpage.


I thought the idea was just to post shortcuts we use on a daily bases to make our lives easier when working with shells (be it explained in a manual page or not).

Your "trick" is explained in a man page as well: try looking in SH(1)
0

#36 User is offline   tehbizz 

  • Progenitor of noob slaying
  • Icon
  • View blog
  • Group: Members
  • Posts: 2,039
  • Joined: 02-April 05
  • Gender:Male
Reputation: 0
Neutral

Posted 03 February 2007 - 01:59 AM

View Postb8zs, on Feb 3 2007, 01:26 AM, said:

View Posttehbizz, on Feb 2 2007, 11:12 PM, said:

Still not a trick. This is explained in the manpage.


I thought the idea was just to post shortcuts we use on a daily bases to make our lives easier when working with shells (be it explained in a manual page or not).

Your "trick" is explained in a man page as well: try looking in SH(1)


Actually what I posted is not what IFS is but how to fully read spaced items correctly. Here's what man 1 sh has to say:

Quote

IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is ‘‘<space><tab><newline>’’
Now, it does go on to explain that:

Quote

Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with pathname expansion. If subscript is @ or *, the word expands to all members of
name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the
first character of the IFS special variable, and ${name[@]} expands each element of name to a separate word.


Then this:

Quote

If the value of IFS is null, no word splitting occurs.


However, I didn't set it to null, I set it to newlines only. I tried null and it worked but not how I wanted it to, it globbed all array elements into a single element. And since IFS is supposed to honor shell quoting, I found this too to not work so only setting it to newlines was possible for exactly what I needed. Not only did I pour over my book on shell scripting and the Advanced Shell Scripting Guide, I read tens of posts on the web about how to do this and not one had the correct answer. Then not only did I actually consult people who've used UNIX longer than Linux has been around, I could never get a straight answer on exactly what to do because so few people ever think about what IFS is and what it does. It's surprising how many people don't even know it exists. Most references ever made to IFS are buried in text or appendices simply because it's a forgotten element of the shell.

Using * as a delimiter in a search is the village secret everyone knows about but how to use IFS is something so few know. That's what makes it a real trick. So far, a number of the posts in this thread are merely shortcuts, not actual tricks. Perhaps "shell shortcuts" would have been a more appropriate title.
0

#37 User is offline   b8zs 

  • The phorce is with me!
  • Icon
  • View blog
  • Group: Members
  • Posts: 76
  • Joined: 22-August 05
  • Location:64 Kbps x24
Reputation: 0
Neutral

Posted 03 February 2007 - 02:28 AM

The OP makes clear the exact purpose of this thread:

View PostOctal, on Jan 28 2007, 06:34 PM, said:

I made this thread as a basic resource for people, like newbie hq. But this is based on intresting/time saving/etc ways to use a shell in unix.


never is "novel" brought up

thanks to everyone who is contributing though, keep the ``tricks" coming
0

#38 User is offline   chaostic 

  • rekcah-rebÜ
  • Icon
  • View blog
  • Group: Members
  • Posts: 724
  • Joined: 04-September 05
Reputation: 7
Neutral

Posted 03 February 2007 - 06:37 PM

View PostOctal, on Feb 2 2007, 09:40 PM, said:

This hasn't been mentioned yet:
A wildcard: *
You could put it like this:
ls *.c

would show all files in that directory that end with '.c' you could also use it like this:
ls hello*.c

would display all files in that directory that end with .'c' and start with 'hello'.

That can be helpful when you are dealing with many files in one directory.


Along with *, which allows any number of characters, ? is the single character wildcard. Perfect when you have a folder with files named Pic1.jpg to Pic100.jpg but only want to change Pic20-29.jpg. So instead of Pic*.jpg which will affect all 100, use Pic2?.jpg
0

#39 User is offline   Octal 

  • Dangerous free thinker
  • Icon
  • View blog
  • Group: Members
  • Posts: 886
  • Joined: 19-February 06
Reputation: 0
Neutral

Posted 03 February 2007 - 08:25 PM

View Posttehbizz, on Feb 3 2007, 01:12 AM, said:

View PostOctal, on Feb 2 2007, 09:40 PM, said:

This hasn't been mentioned yet:
A wildcard: *
You could put it like this:
ls *.c

would show all files in that directory that end with '.c' you could also use it like this:
ls hello*.c

would display all files in that directory that end with .'c' and start with 'hello'.

That can be helpful when you are dealing with many files in one directory.


Still not a trick. This is explained in the manpage.

If it was only 'tricks', there would only be you posting. Because obviously you decide what is a trick and what isn't. :P
0

#40 User is offline   iceni 

  • SUPR3M3 31337 Mack Daddy P1MP
  • Icon
  • View blog
  • Group: Members
  • Posts: 400
  • Joined: 06-January 06
Reputation: 0
Neutral

Posted 04 February 2007 - 09:30 PM

i use ctrl-r most i think: ctrl-r then start typing, it automatically greps your bash history. it's not much of a trick though :(

you can do this to use term as a calculator
echo 2+2|bc

you just have to remember the 'echo' and '|bc' bits

i've got a script i use called whatsmyip. it tells me my ip address lol it uses lynx. i've got another that's a proper calculator. i got them both from this link -
http://www.bashscrip...Scripts/crouse/

it's the script called calc.sh, not bashcalc.sh.

http://img178.imageshack.us/img178/6154/calchr0.th.png
0

Share this topic:


  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic


  • Time Now: Feb 10 2012 07:28 PM