9

I know that Linux is an operating system built on text files. What I would like to do is create a file using vi of all the various commands I have learned. That way I could find each individual command very easily using the / in vi. But I only want to do this if it doesn't change the way that my computer functions. Would this be a good idea?

  • 12
    Yes, it will have a text file more somewhere. ;-) (Joking) Really, a file is a file. Is like asking if adding a photo to a folder or creating a new word document will change your computer. Yes it will; you have one more photo/document there. Whatever, the answers you got are mostly correct. – Rmano Feb 20 '15 at 8:40
  • Why would you think it would do it? – Braiam Feb 21 '15 at 4:32
  • 2
    Not sure what you mean by "an operating system built on text files", but Linux isn't really different from, say, Windows, when it comes to text files. Windows has .ini and .bat files for example, if that's what you are thinking. – hyde Feb 21 '15 at 10:53
  • Also, do you think there is anything special about vi (as compared to, say, gedit) or did you just mean "a text editor"? – Mr Lister Feb 21 '15 at 16:12
  • 1
    @BJsgoodlife Some text files control the system. They're put in special places and generally can't be modified without sudo. – cpast Feb 22 '15 at 9:08
34

Writing a text file isn't going to "change the way that your computer functions".

In particular, in order to do something like that, you'd have to do one of

  • overwrite some important file
  • write your file to a special protected location like a *.d directory in /etc; you won't have permission to do this
  • mark the file executable and put it in your path

Since you're not going to explicitly be doing anything like this, there is nothing to be worried about. In particular, the other answer goes to unnecessary lengths by suggesting your text files need to begin with hashes on every line. Of course they don't. Just storing a text file somewhere within a normal home directory is not going to do anything.

  • 3
    I suggested putting # so that nothing unintentional gets performed by any chance. I think its always better to be on the safer side. – heemayl Feb 20 '15 at 5:28
  • 8
    I think it's not always better to be on the safer side. Safety has a cost, and there is a point at which the reduced risk is no longer worth the cost. For example, when it exceeds the value of the thing you're trying to keep safe (in contexts where this argument makes sense). Over on Information Security you can find a lot of answers discussing how proper security is a tradeoff, and the only completely secure system is also completely useless, so it would be silly to insist on perfect security in all cases. – David Z Feb 20 '15 at 8:35
  • 9
    As a somewhat separate issue, I think the most important point to bring up (which this answer does, and the other does not) is that creating an arbitrary file is not going to do anything to change the system. You can only affect the system by creating a file that some other process is looking for, or by executing the file you create, if it contains the code to affect the system - but that falls under the general advice of not running completely unknown shell commands, which is smart whether you're working with files or not. – David Z Feb 20 '15 at 8:38
  • 2
    make me a sandwich. You won't have permission to do this. sudo make me a sandwich. Aye, aye, sir! – Lie Ryan Feb 20 '15 at 12:32
  • 2
    @heemayl why not just make the first line in the file exit? – Random832 Feb 20 '15 at 15:11
15

You can save all the commands you have learned in a file but some important points to note:

  • Don't make the file executable
  • Never source the file
  • The best way would be to put a # in the beginning of all the commands to comment out all the commands so they don't get executed accidentally. This way you can actually search for the command you need and also would be risk free. You can add # at the start of each line by the following command:

    sed -i 's/^/#/' <file_name>

EDIT: I have given the third point as an extreme security measure, as "neon_overload" and "David Z" pointed out that putting hashes in all the lines is too extensive and hence not needed, i would second with them in this context. Also as pointed out by "random832", a far better alternative to my third point would be to start the file with exit, as no later commands will be executed then.

EDIT-2: "Rinzwind" has mentioned two important points:

  • Do not overwrite an existing file

  • Store the file in the user's home directory (/home/$USER/).

  • 4
    put a # in the beginning of all the commands to comment out all the commands so they don't get executed accidentally I am not quite sure whether that's supposed to be a serious suggestion or a joke. My sarcasm detector failed me today. – Lie Ryan Feb 20 '15 at 12:35
  • The word that you need to put importance on is 'accidentally'..the user may accidentally keep the file someplace unsafe as executable....here i am just taking no chances..hope you are clear now.. – heemayl Feb 20 '15 at 12:49
  • 4
    Couldn't he just put an "exit" or something like that at the beginning of the file, instead of commenting every single line? – o0'. Feb 20 '15 at 15:49
  • Maybe he meant #! -- that is set the interpreter to null so it doesn't run. – Joshua Feb 21 '15 at 0:27
  • 1
    @BJsgoodlife, no #! -- just exit; that way you're also protected against sh yourfile, which an invalid shebang won't help against. – Charles Duffy Feb 21 '15 at 0:54
7

neon_overload has a very good answer.

I'll expand a bit on a certain facet of your question - I would make this a comment, but I don't have enough reputation on here.

Linux, as an operating system, is very much NOT based on text-files. That said, I can kind of see where you are coming from. To an outside observer, there is a fair bit of going on with editing and launching "text" files, especially compared to Windows.

This has several reasons, some of which are:

  • There is no registry system in Linux, like is in Windows. Therefore, it is common for applications to store their settings in textfiles, and you would be instructed to edit them, rather than edit registry like in Windows.
  • Linux is very widely used as a server system, and many of those, who use it on desktops do work with server systems. Unlike Windows Server, Linux servers usually run without a graphical interface. Therefore, products that are expected to run on Linux servers, and, indeed, their users and administrators, are expected to work without a graphical interface. Products (and system features, worth saying) that are only expected to run on Windows are usually more GUI centered (like, say MS SQL)
  • Linux scripting system is somewhat more common and more transparent than Windows. You would see scripts on Windows - but they would be called something.vbs or another.bat. You wouldn't neccesarily realize they are text files - but, considering what is in my second point, it is much more common in Linux to go snooping around .sh files.
  • Lastly, many users of Linux are accustomed to working without graphical interface, and will, for many things, rather edit the files directly as they would without GUI, rather than fumbling around with graphical tools. I know that when I want to do something deeper in the system, I just start the terminal, and so do many of my colleagues.
  • "There is no registry system in Linux", but there is dconf (en.wikipedia.org/wiki/Dconf). Not all systems that run the Linux kernel will have it, and not all things a Windows system would configure in the registry will be configured there, though. – Daniel Landau Feb 24 '15 at 21:19
1

The short answer is, no, you won't change your computer by creating a text file in vi. However, if you aren't careful, creating that first text file just might change your life. Learning the Unix way can be a deeply rewarding practice.

0

vi is a text editor, therefore if all you want is just store some text snippets in a text file, and search through it, then you can do it all directly from the terminal with the built-in commands.

  1. Write a note to a text file (will create the file if it didn't exist):

    echo note with important data >> info.txt

IMPORTANT: Watch out for single and double quotes, and prefix them with slashes if you want them included.

  1. Search the file for some text (e.g. important):

    grep --color -i important info.txt

To avoid typing --color -i that should be aliased in your ~/.bashrc.

You can also add multiline text to existing or non-existing text files:

cat >> info2.txt

then type or paste the text, and when done then press CTR+Z

As for changing the system, no, it won't change the way your system works as long as you create files in your own home folder and don't execute them (you can execute a file even if it's not executable, e.g. with bash myFile). Also make sure you only use the root privileges strictly when you need them.

And finally, your interpretation of file-based system has misled you somewhat: most operating system have files that store important settings which deeply affect the way the system operates, but they are located in predefined locations and typically are protected from modification by regular users.

It is possible though to easily shoot yourself in the foot, for example, if as a regular user you have a local 'executable' folder that you added to PATH, and if in that folder you happen to have executable files that have same names as built-in common utilities or bash keywords, such as test, echo, cat, for, cut, etc.

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.