Score:0

How do I create a .txt file at a location in terminal without changing the current directory?

lt flag

I need to create a .txt file at a specific location without changing the current directory with the cd command.

For example, I need to navigate to /username/Desktop with cd, and I need to create a .txt file in /username/Documents/folder

Score:1
ng flag

The touch command will create a blank new file at the path you specify.

If you want to create example.txt in your user's Documents folder, you would use the following command:

touch ~/Documents/example.txt

-or-

touch /home/username/Documents/example.txt

Please be aware that you must specify a valid path that already exists. The example you used in your question is not a valid path, so it won't work.

ar flag
Other than a valid path, the user must have "write permission" to the target directory.
Score:0
de flag

As @Nmath emphasized, you could give the full path to the touch command and create a new file on the existing directory. You could use TAB after typing several letters of the path to see all the possibilities in the directory branch you are on. Another alternative is to use a relative path. E.g:

Check your current working directory:

$ pwd
/home/user/folder1/folder2

Then, create file using relative path:

$ touch ../../Documents/new.txt

To check if the file is created use ls and grep commands:

$ ls ../../Documents | grep new.txt
new.txt

if you get the result below the ls command you are ready to go.

Zanna avatar
kr flag
It is inadvisable to parse the output of `ls` as the results may be unreliable. In this case the `grep` seems exceptionally unnecessary as one can simply `ls /path/to/file`. Also, an example using a relative path may be useful here, but why use a relative path in your scenario when the absolute path `~/Documents/new.txt` is shorter?
xerxes avatar
de flag
I agree that the relative path might be longer sometimes. However, my point on using a relative path is to be able to navigate without prior knowledge of the file system tree. That being said, parsing the `ls` output also serves a purpose here. It's easier to find the exact file if the target directory contains a lot of folders and files. And direct `path/to/file` works as well. Just grep serves my choice of taste here.
Score:0
cn flag

You can do this with a one-line command

cd ~/Desktop ; gedit ~/Documents/Personal/afile.txt

and you'll see a visual WYSIWYG text editor. A Save button on the upper right corner will help you retain whatever you typed.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.