-1

or in other words, how to cat files from a long path directory

cd /media/home/user/usb/Documents/Linux/test.txt
cat test.txt | grep abc

How do I shorten this?

I tried with a script:

#!/bin/bash

var1="/media/home/user/usb/locuments/linux/test.txt"
var2="cat test.txt | grep abc"

"cat test.txt"=`var1 var2"

or

"cat test.txt"=`cat test.txt | grep abc`

LOL ... Obviously don't have even a basic knowledge about scripting

There should be some other ways of doing this

What would be the best method to use for this to work?

what do I start to search and try for?

  • Are you just looking for grep abc /media/home/user/usb/Documents/Linux/test.txt? – terdon May 30 at 8:02
  • I am trying to use cat test.txt | grep abc , and abc is a text from file test.txt, and that file is located in /media/home/user/usb/Documents/linux/ – brkroot May 30 at 8:17
  • Yes, so isn't grep abc /media/home/user/usb/Documents/Linux/test.txt what you are looking for? There is no need for cat and no need to cd into the directory. You can just run grep abc /media/home/user/usb/Documents/Linux/test.txt from wherever you are. Or do you want to do this with a single command? Do you want to be able to run foo and have that execute grep abc /media/home/user/usb/Documents/Linux/test.txt? – terdon May 30 at 8:22
  • oh yes, grep abc /media/home/user/usb/Documents/Linux/test.txt does the job. which is great as I don't have to cd to the folder. Thank you! But now how do I make it short? it is a file located in USB stick and I will have to extract info from this file plenty of times. And the command is quite long to type it every time. So is it foo I am looking for? I – brkroot May 30 at 8:27
1

If you always need to run that exact command, you can use an alias. Run nano ~/.bashrc and add this line to the file:

alias abc='grep abc /media/home/user/usb/Documents/Linux/test.txt'

Save the file, open a new terminal and you will now be able to just run abc and get your results.

If you need to be able to search for different patterns, use a function instead. Again, run nano ~/.bashrc and add these lines (but delete the alias above if you have added it):

function abc(){
    grep "$@" /media/home/user/usb/Documents/Linux/test.txt
}

Save the file, close it, open a new terminal and you can now run:

abc foo # search /media/home/user/usb/Documents/Linux/test.txt for 'foo'

To search for the string foo in the file /media/home/user/usb/Documents/Linux/test.txt. Or

abc bar # search 

To search for the string bar, etc.

  • Thank you very much Your script does exactly what I need Only little problem I had was that I didn't know I have to run source ~/.bashrc after saving the bashrc file. Felt pretty stupid because the 'abc' didn't work and I checked the line few times. I will learn more about shell scripting. Thank you for taking the time. – brkroot May 30 at 9:47
  • @brkroot that's why I explicitly wrote "open a new terminal" after both of the solutions I gave :). The ~/.bashrc is read (sourced) each time you start a new shell session and the easiest way to do that is to open a new terminal. – terdon May 30 at 9:54
  • actually yes, I didn't open a new terminal, but typed in another already open one. But now I got the idea and purpose of opening new terminal. Although I still can source the bashrc in the current terminal after I close nano :). Anyway, I am getting right into experimenting with different functions now. got so much to do – brkroot May 30 at 10:10

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.