1

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

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: '
5
  • 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
2

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
4
  • There is also ls -1 | nl.
    – scai
    Aug 1 '12 at 12:51
  • 4
    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
0

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.

7
  • 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? 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 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. 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
0

Also have a look at percol, fzy, smenu and friends. They'll happily take from stdin, present a user-friendly select menu with interactive filter, then pipe selected lines out again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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