Jump to content


Photo
* * * * * 3 votes

Shell tricks


  • Please log in to reply
83 replies to this topic

#21 lmnk

lmnk

    elite

  • Members
  • 120 posts

Posted 31 January 2007 - 10:51 AM

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

Edited by lmnk, 31 January 2007 - 11:57 AM.


#22 Octal

Octal

    Dangerous free thinker

  • Members
  • 886 posts

Posted 31 January 2007 - 04:41 PM

Some shell info:
http://news.softpedi...mpt-40033.shtml
that is for customizing the shell.

#23 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 31 January 2007 - 05:07 PM

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.

#24 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

Posted 01 February 2007 - 12:51 AM

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.

#25 Beave

Beave

    SUPR3M3 31337 Mack Daddy P1MP

  • Agents of the Revolution
  • 350 posts

Posted 01 February 2007 - 04:27 AM

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 :)

Edited by Beave, 01 February 2007 - 04:29 AM.


#26 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

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.

#27 Octal

Octal

    Dangerous free thinker

  • Members
  • 886 posts

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.

#28 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

Posted 01 February 2007 - 11:38 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.


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.

#29 Alk3

Alk3

    "I Hack, therefore, I am"

  • Binrev Financier
  • 1,003 posts
  • Gender:Not Telling
  • Location:312 Chi-town

Posted 02 February 2007 - 12:52 AM

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.

#30 Beave

Beave

    SUPR3M3 31337 Mack Daddy P1MP

  • Agents of the Revolution
  • 350 posts

Posted 02 February 2007 - 06:02 AM

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...

#31 nullkraft

nullkraft

    SUP3R 31337 P1MP

  • Binrev Financier
  • 284 posts
  • Location:Think Heisenberg uncertainty principle.

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.

Edited by nullkraft, 02 February 2007 - 06:41 PM.


#32 Octal

Octal

    Dangerous free thinker

  • Members
  • 886 posts

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.

#33 b8zs

b8zs

    The phorce is with me!

  • Members
  • 76 posts
  • Location:64 Kbps x24

Posted 02 February 2007 - 10:07 PM

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


Spoiler


#34 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

Posted 03 February 2007 - 01:12 AM

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.

#35 b8zs

b8zs

    The phorce is with me!

  • Members
  • 76 posts
  • Location:64 Kbps x24

Posted 03 February 2007 - 01:26 AM

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)

#36 tehbizz

tehbizz

    Progenitor of noob slaying

  • Members
  • 2,039 posts
  • Gender:Male

Posted 03 February 2007 - 01:59 AM

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:

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:

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:

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.

#37 b8zs

b8zs

    The phorce is with me!

  • Members
  • 76 posts
  • Location:64 Kbps x24

Posted 03 February 2007 - 02:28 AM

The OP makes clear the exact purpose of this thread:

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

#38 chaostic

chaostic

    rekcah-rebÜ

  • Members
  • 724 posts

Posted 03 February 2007 - 06:37 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.


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

#39 Octal

Octal

    Dangerous free thinker

  • Members
  • 886 posts

Posted 03 February 2007 - 08:25 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.


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

#40 iceni

iceni

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 400 posts

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.

Posted Image




BinRev is hosted by the great people at Lunarpages!