Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have created a script that allows the user to select a file by copying and pasting that file. Then perform a grep on that file. I want to know is it possible to put a number in front of each files in the directory so the user will only have to type a number.

What I have tried:

  • Do ls -l then ls | wc -l then have the user select the corresponding file number, no luck.
  • Next, ls -i, tried to perform a grep using the node number for that file, no luck.
share|improve this question
    
This is an ideal use for bash arrays. –  technosaurus Aug 1 '12 at 13:46
1  
Obligatory warning about parsing ls output - know the dangers before attempting to rely on it. –  jw013 Aug 1 '12 at 14:32
add comment

3 Answers 3

In bash, ksh and zsh you can use the select keyword for that:

select file in *; do
  grep 'something' "$file"
done

To give the use instruction while the select list is displayed, set $PS3 before running select, for example:

PS3='number of file to grep in, ^C to exit: '
share|improve this answer
1  
+1, but this sends you into a loop selecting files. To do this only one time use a break before done. This also works in zsh. –  bahamat Aug 1 '12 at 16:09
    
Thanks for the zsh note, @bahamat. –  manatwork Aug 1 '12 at 16:22
1  
The loop will exit when it sees EOF, so use ^D to exit. ^C will abort your script, which may not be what you want. –  Starfish Aug 1 '12 at 18:27
    
Good point, @Starfish. –  manatwork Aug 1 '12 at 18:35
    
That is, if you want to leave it up to the user. –  bahamat Aug 1 '12 at 19:46
add comment

By the way, you can use inode number and do it with only ls:

ls -i

will output something like:

 $ ls -i
13107210 file
13238318 dir
13109301 script.sh

However the number is pretty big comparing to cat -n. But the main advantage of this way is that you always will find the exact file, while with cat -n + sed you can take another if between two command some new files were created in your dir.

share|improve this answer
    
I tried the ls -i option with no luck. Is it possible to grep the file using the inode number? What would be the command? –  user1545938 Aug 7 '12 at 10:09
    
the best way to find file with exact inode is to use find : find . -inum INODE_NUM . Is this what are you talking about? –  rush Aug 7 '12 at 10:29
    
After trying to put a number in front of the file with the ls command. I looked into the ls -i command. In my script I did the –  user1545938 Aug 8 '12 at 11:51
    
Sorry about that. I'll posted the script. I'm going to try the steps Bernhard posted. Thanks for all the good information. –  user1545938 Aug 8 '12 at 12:02
    
Btw, if you explain properly what's going wrong, I'll be able to help you. –  rush Aug 8 '12 at 12:08
show 2 more comments

For putting the line numbers in front of ls, it may be easiest to

 ls -1 | cat -n

Then to select file number N, you could try

ls -1 | sed -n Np
share|improve this answer
    
There is also ls -1 | nl. –  scai Aug 1 '12 at 12:51
3  
Also, -1 option to ls is implicit when output goes to a pipe. –  enzotib Aug 1 '12 at 13:02
1  
In case between ls|cat and ls|sed some new files appear in dir it may select wrong file. –  rush Aug 1 '12 at 15:08
    
@rush One shouldn't store the selected number but the filename belonging to this number. –  scai Aug 1 '12 at 16:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.