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?
- 72,312
3 Answers
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:
- Make a symlink to file in your current directory. For example,
ln -s /opt/myfile.txt symlink2file.txt - Set path to file as variable and use the variable to pass it as argument to a command.
myvar="/opt/somefile.txt ; less "$myvar"
- 107,582
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.
To display the content of a particular text file is as simple as:
Copy the following on an empty document.
#!/bin/bash cat /home/test/tempSave the document with any name, for the example I'll use
FILE_NAME.- Open a terminal and
cdto the directory where you saved the previous file. - Run
chmod 777 FILE_NAMEto allow the file to be executed. 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/binNow simply open a terminal and type the name of the script to see it's content.
- 3,538
- 5
- 24
- 40