0

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?

Zanna
  • 72,312

3 Answers3

1

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"
0

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.

0

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

  1. Copy the following on an empty document.

    #!/bin/bash
    
    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.

M. Becerra
  • 3,538
  • 5
  • 24
  • 40