How can I view file contents without going to specific path where file is? Say a file named List resides in /home/test/temp and I want to view List without going to the /home/test/temp directory. Is there any script which can make files directly accessible without changing directories?

closed as unclear what you're asking by Terrance, user68186, Ravexina, Sergiy Kolodyazhnyy, Zanna Apr 26 '17 at 6:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • less /home/test/temp won't change your current directory and will display the content of the file, but Im not sure if that's what you actually want. – M. Becerra Apr 25 '17 at 18:50
  • Thanks for your answer but I actually want is to view content of file without going to respective path and then viewing the file by cat filename command – Nisha Priya Apr 25 '17 at 19:04
  • Is it just one particular file or you want it to work with any file? – M. Becerra Apr 25 '17 at 19:08
  • To view any file now i'm doing is first going to specific path by "cd home/test/temp "and then by using "LS" command I'm getting list of file and then viewing file by "cat filename" command but I want a script so that I can directly view file – Nisha Priya Apr 25 '17 at 19:11
  • No for this particular file only – Nisha Priya Apr 25 '17 at 19:11

You seem to be confusing "going to directory" and using the full path of file, as I see from comment. You also seem to be confusing script with just one command. What M. Becerra shows in his answer is a script. Joseph Manley's answer just uses single command.

Here's the thing: you have to specify full path to a file when passing it as argument if these are random files which you want to access at will.

If you have just one file that you know will remain in same location, there's couple other approaches:

  1. Make a symlink to file in your current directory. For example, ln -s /opt/myfile.txt symlink2file.txt
  2. Set path to file as variable and use the variable to pass it as argument to a command. myvar="/opt/somefile.txt ; less "$myvar"

You should be able to just use cat /home/test/temp/list in the command line to view the file. You don't need to change to the directory to use cat if you are providing the full path of the file.

  • Thank you for the answer i'll definitely try this script – Nisha Priya Apr 25 '17 at 19:57

To display the content of a particular text file is as simple as:

  1. Copy the following on an empty document.

    cat /home/test/temp
  2. Save the document with any name, for the example I'll use FILE_NAME.

  3. Open a terminal and cd to the directory where you saved the previous file.
  4. Run chmod 777 FILE_NAME to allow the file to be executed.
  5. Add the previous script to any of the directories given from echo $PATH, I suggest /usr/local/bin:

    sudo mv /path/to/script /usr/local/bin 
  6. Now simply open a terminal and type the name of the script to see it's content.

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