[phpBB Debug] PHP Notice: in file /includes/db/dbal.php on line 110: Undefined array key "cached"
[phpBB Debug] PHP Notice: in file /includes/db/dbal.php on line 111: Undefined array key "normal"
[phpBB Debug] PHP Notice: in file /includes/db/dbal.php on line 112: Undefined array key "total"
[phpBB Debug] PHP Notice: in file /includes/session.php on line 885: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 885: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 885: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3391: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3393: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3394: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3395: Cannot modify header information - headers already sent by (output started at /includes/functions.php:2922)
Cabrillo College Linux Classes • View topic - File names with trick or treat

File names with trick or treat

Introduction to UNIX/Linux

File names with trick or treat

Postby alexa bolas on Wed Nov 05, 2008 12:56 pm

Ok, since I missed last wednesday I missed how to do this.
However, how do I find the files that contain trick or treat?
I know we learned this but it did not stick in my brain.
Thanks for any help. Alexa
alexa bolas
 
Posts: 30
Joined: Wed Sep 03, 2008 7:28 pm

Re: File names with trick or treat

Postby Michael Wicherski on Wed Nov 05, 2008 2:01 pm

well we were only searching for them in our home directory and there were a number of different ways that people came up for doing it

the way i did it was to find all the files in my home directory and pipe that into grep and look for trick and then treat

find ~ 2>/dev/null | grep tr[ie][ca][kt]

(or as two separate commands find ~ 2>/dev/null | grep trick and find ~ 2>/dev/null | grep treat)

another way which i thought of after rich mistakenly told me i missed some was

cd; ls -lRa | grep trick

which recursively lists all the files in your home directory then pipes that into grep
Michael Wicherski
 
Posts: 140
Joined: Mon Sep 01, 2008 5:23 am

Re: File names with trick or treat

Postby sergio ybarra on Wed Nov 05, 2008 2:30 pm

I missed last class to. And that was a neat trick, I did it the hard way and the results I think cost me some points.
sergio ybarra
 
Posts: 21
Joined: Wed Sep 03, 2008 7:26 pm

Re: File names with trick or treat

Postby alexa bolas on Thu Nov 06, 2008 2:28 pm

Micheal, I really appreciate your response but I am super dense here. The command does not make sense to me. I have no idea what is being redirected; why the grep is used; what is in the meta characters and how are they being used. Help!!
alexa bolas
 
Posts: 30
Joined: Wed Sep 03, 2008 7:28 pm

Re: File names with trick or treat

Postby collin seaton on Thu Nov 06, 2008 7:38 pm

[quote="alexa bolas"]Micheal, I really appreciate your response but I am super dense here. The command does not make sense to me. I have no idea what is being redirected; why the grep is used; what is in the meta characters and how are they being used. Help!![/quote]

Hi Alexa-
Let's step through Michael's answer one step at a time:
[code]find ~ 2>/dev/null | grep tr[ie][ca][kt][/code]

So find searches for filenames. The ~ tells it to start at your home directory (try cd-ing into a directory other than your home and typing cd ~ you'll find yourself back in your home directory). At this point we're using find to search starting from your home directory.

The next part, 2>/dev/null redirects stderr (identified with 2) into /dev/null (also known as the bit bucket). This results in all errors dumping into /dev/null as opposed to showing on the screen.

The pipe ( | ) redirects stdout from one command as stdin of the next, so in this case find is going to list all the files in your home directory or deeper in the tree, and list those out. By piping the find command's results into grep, you can now give grep arguments defining what you're searching for.

grep is essentially a regular expression tool- it searches for patterns. Let's break the metacharacters down one by one now.

tr - tells grep to look for files starting with the characters 'tr'
[] tells grep to match only one of the characters inside to the next available character so...
[ie] - tells grep that the next character after 'tr' should be either an 'i' or an 'e'
[ca] - tells grep that the next character after tr[i (or) e] should be either a 'c' or an 'a'
[kt] - tells grep that the next characters after tr[i (or) e][c (or) a] should be either a 'k' or a 't'

grep steps through everything from find's stdout and tries to match it to the pattern specified. In the example here, it ultimately matches to files named 'trick' or 'treat'. I don't think it's necessarily a perfect solution, however, as grep would also match a files named 'trict', 'triak', 'triat', and on and on.

I hope that helps!

-collin
collin seaton
 
Posts: 24
Joined: Wed Sep 03, 2008 7:26 pm

Re: File names with trick or treat

Postby alexa bolas on Thu Nov 06, 2008 9:04 pm

Collin,
That explanation did help get through my dense head. Not sure why that was/is hard for me to understand.
Alexa
alexa bolas
 
Posts: 30
Joined: Wed Sep 03, 2008 7:28 pm

Re: File names with trick or treat

Postby Michael Wicherski on Fri Nov 07, 2008 4:57 am

thanks collin, and sorry i didn't get back to you sooner alexa


@ collin; it isn't perfect, true because it would list (possible) extra files - but it gets the job done quickly ^_^; you could always run the same command twice using "trick" and "treat" as the patterns to search for with grep to be more accurate
Michael Wicherski
 
Posts: 140
Joined: Mon Sep 01, 2008 5:23 am


Return to CIS 90 - Fall 2008

Who is online

Users browsing this forum: No registered users and 0 guests

cron