2011-09-15

Linux: A Beginner's Guide, Basic File Editing



Writing through a haze of vapor



What to expect today:


As the title suggests, we are going to cover basic file editing in this lesson.  Here's an overview:


  • Nano: A basic text editor included in every Linux distro.  This is a command line interface (CLI) app with some very simple keybinds and is used by most beginners at first before moving on to more powerful text editors like vim.
  • echo:  This is a very simple CLI command that is often used to write simple values into text files, often for the purposes of quickly adding information in text to a new config file or even for leaving yourself notes (more on this trick later).
  • Config Files: Short for Configuration Files.  Often-times these files are suffixed with .conf, .config, or .rc. They are read by a program each time that program is run (usually).  There are more types of config files in the system, but these are the "Big Three" to be aware of.
  • Permissions: We will take a deeper look into permissions today, but not so deep as to have you crying in a corner with a blankie.  We'll go over quickly how to see the permissions needed to edit a file and how to gain those permissions in some basic and secure ways.
Note: You will find that as you use Linux and become comfortable with being an administrator on your system that what I show you here may not be the best way to do things.  That is alright!  I am purposely holding back so that you have room to explore and discover new ways of doing things.  I may show you a method that I deem "secure enough" for basic principles, I am sure that there are security professionals out there that will quickly tell you that what I am teaching you is "wrong".  That is fine, I am not a security pro and probably never will be.  Please understand this, these guides are meant to be for the most basic of Linux users, they are meant to be "noob" friendly.  As always, do your research (which is part of your homework assignment at the end of this particular guide) and explore new ways of staying secure and being productive.  It is the best way to learn!


Why shouldn't I just skip this section?


Good question.  Realistically you could "get by" without it.  Nowadays there are lots of apps out there that will configure your system without you ever needing to know how to edit a file.  True story!  You may be have been using Linux Mint, Ubuntu, PeppermintOS, and others that are what the more elitist of us Linux users refer to as "The n00b distros".  That's okay, but let me ask you this: Why did you ever pick up a Linux distro?  Was it the lack of viruses/malware/spyware?  Was it that you could tailor the system to your needs without needing to worry about software patents?  Was it the absolute control?

Here's why you should stick around: If you can read this guide and understand it, you can read any guide that explains how to customize your system, troubleshoot, or otherwise "fix" any software related problem that could ever occur.  Being able to quickly edit a text file via the CLI will help you when you ask someone for help, it's a basic skill that you will be expected to know about prior to ever asking a question on any forum, IRC channel, and Mailing List.  I'm sorry, most Linux users are pretty patient, but not all of us are willing to sit there for several hours teaching you how to perform basic functions.  You might be a pro mechanic, if I came to you asking you to help me install an engine into a a classic car, and when you got there you had to explain to me what a wrench was, and "righty tighty lefty loosy", you'd probably be outta there in about five minutes screaming at me that I need to pony up the cash for someone to do it for me.  Think of this as your first major step in being able to help yourself.  You'll appreciate it the independence, trust me!


Hello....hello....hello....hello....oo....oo....o....


"Echo" is going to be our first venture today.  At this point I want you to minimize Google+, Twitter, Tumblr, Facebook (who uses that anymore?), your email, and basically anything else that is a distraction; then I want you to crack open your terminal and get ready to make a small mess of things.  Point your terminal to a directory where you can do some work using "cd", then I want you to make a file called "stuff".

Now, type the following into your terminal:

(darthlukan@SithCouncil)$ echo I like text > stuff
(darthlukan@SithCouncil)$ cat stuff
I like text
(darthlukan@SithCouncil)$

Good stuff, right? Let me break down what we've just done:

echo             I like text               >                   stuff
 ^                      ^                       ^                    ^
Command |  string/text        |   "Linker"/Input      |      file


We gave the command (echo) some text (I like text) and used a "Linker" (>) to point to the file (stuff).  In a nutshell, that's how "echo" works.  There are official names for all of the stuff above, I'm not going to use those because right now it's more important that you understand how this works versus how to pass a test (sorry, I'm not an accredited educational institution, yet).

Now, using what you know of "echo", I want you to do the same thing and see if you can guess what will happen.  Enter in some arbitrary text in place of "I like text" then "cat" the file so that you can see what is inside.  I'll give you a minute to do that.

Did what you expected to happen, happen?  If you expected the "one linker" to just add a second line to your file, unfortunately, you were wrong.  If you expected to end up with only one line replaced by your new text, then you guessed correctly.  Think of a single ">" as saying "Replace what's there with the text that I gave you."  Well, if one ">" will flat out replace, what do you think that ">>" will do?  Try it!

(darthlukan@SithCouncil)$ echo They like text >> stuff
(darthlukan@SithCouncil)$ cat stuff

I like text
They like text
(darthlukan@SithCouncil)$

Interesting.  One ">" writes text to the file at the cost of replacing anything else that was in that file, but ">>" will just add to or append text to a file.  Try it again with some more text using ">>" to test our theory.

(darthlukan@SithCouncil)$ echo We like text >> stuff
(darthlukan@SithCouncil)$ cat stuff
I like text
They like text
We like text
(darthlukan@SithCouncil)$

Great stuff!  See if you can come up with a "real world" example of where this would help you out, either at home, in school, or at work.  I'll give you one: note taking.  That's right, when I'm at school (I'm studying Software Development) I use "echo" to quickly jot something down into a file in my "schoolnotes" folder.  I have one file for "homework", another for "TODO", "quicknotes", and "IMPORTANT".  When I'm in class, rather than get a cramp writing down notes on paper (I write slow), I instead use "echo" to quickly jot down something that I want to save for later.  I have a cronjob (we'll discuss these in a later how to) that I use to read all of those files once an hour and display a notification tooltip so that I am reminded of what I wrote in these files and don't forget about them.  Don't worry about cronjobs and notifications just yet, we'll get to those in later articles.  For now, just be thinking of how you can use "echo" to your advantage.

"But Brian! What if I don't want to create a ton of files with notes all over the place and I want to reuse a file for notes?"  Great question!  One method that I use for this is to:

(darthlukan@SithCouncil)$ echo . > stuff
(darthlukan@SithCouncil)$ cat stuff
.
(darthlukan@SithCouncil)$

You can probably come up with something else, but I figure using a period (.) is a pretty simple thing that doesn't involve a lot of typing (we are trying to make your life easier after all).  I'm sure that you will be able to find more ways of using "echo" to accomplish tasks and quickly write to files, for now, let's move on.


"A machine so small you can only see it in a terminal."


Before we jump into using Nano, we first need to understand what Nano actually is.  Nano is a text editor, it is a program that runs in a terminal and allows the user to edit text files.  For some of you that is enough, for others I've only confused you, and still others, I'm about to blow your mind!  Remember when I said that in Linux, everything is a file? Think about it.  Try again.  Think harder.  I think you've got it!  If not, I'll explain:

In Linux everything is a file.  If you've noticed, there aren't very many "file extensions" in Linux.  If you are coming from Windows, you are used to ".TXT", ".doc(x)", ".xls(x)", and so on.  Depending on the file extension depends on which program(s) can open that file.  I want you to forget about that for now.  You are using Linux, you can open almost anything using almost anything (there are always exceptions to the rules).

You may have seen a file with the extension ".py".  That's a "Python File" (to put it in simple terms).  Well, that Python File is actually a Text File.  Why? Because it has text in it.  Ever see a ".config" ? Yep, text in that one too.  ".cpp"? Also text.  In Linux we classify a file by what it contains rather than by what program we use to open it.  This may seem a little backwards for some people, but I'll explain a little further.  All of the files that I mentioned above contain text.  Therefore, I need to use a program that can edit text in order to change things (such as with a config file).  The extension doesn't matter (for the most part, there are exceptions such as compiled files that require de-compiling, but that is well beyond the scope of this document, and you wouldn't be reading this if you were already programming).  So long as I have a program that can take a "blank sheet of paper" and add words to it, I can edit almost any file in a Linux system.

In Windows, when you wanted to open a ".txt" file, you use Notepad.  For ".rtf" you use WordPad, and for ".doc" you use MS Word.  But what would you use if you had a ".lua" or ".conf" file?  Some of you more savvy users may say Notepad++  (great piece of free software by the way), but I have a feeling the majority of you may have already gotten lost.

Why did I spend so much time on all of that?  Hopefully while reading about all of that stuff a lightbulb clicked on.  There's a hidden warning in there about root.  Remember when I talked to you about being responsible with how you use root?  This is one of those times.  A text editor is a very powerful tool to begin with.  In the hands of root, a text editor can mean certain apocalyptic doom to your system.  Unless you know what you are doing, trust the people instructing you, or are following a trustworthy wiki article for your distro, avoid editing files as root!  You've been warned, now, off to the fun part.


Do work


In your terminal, type the following:

(darthlukan@SithCouncil)$ nano stuff



Before we get rolling, take a deep breath!  There is nothing to fear on this screen.  What we have here is a very basic text file with only a few lines of text.  If you look at the header above, you'll notice that you can see what version of Nano we are running, the file currently in view, the text that we entered with "echo" and some basic commands at the bottom of the screen.

Some basic usage of Nano:


  • Arrow keys will move you around the text
  • "^" means "ctrl", the Control Key
  • Nano is always in "Edit Mode", if you type, you will insert text
  • Pressing control and a letter will perform an action
  • "M" as in "M-|" = Meta or Alt Key
  • Feedback is provided at the bottom of the screen, just above where commands are listed
  • ctrl+g will display extra help
Most people will never work with a file requiring more than the commands listed at first on the bottom of the screen.  In fact, while researching the different items that I set out to describe in this document, I actually learned quite a bit about Nano and I've been using it for several years!  That just goes to show that you don't need to know everything about something in order to be effective.

Let's edit some text!  Use your down arrow key to get to the line below "We like text".  Type something, anything.  I chose "A lot of people like text.".  Now, see if you can figure out how to save this file.  I'll give you a minute.

Did you figure it out?  If you did, excellent!  If not, don't worry, I'm gonna give you the answer regardless.  If you press "ctrl+o" (that's the letter "o") and then "Enter" (Carriage Return) you will "Write Out" the text to the file.  That is basically the same function as "Save".  Let's say that this is a config file and we are done adding our line of text.  You've already written out the changes to the file, now it's time to exit.  Press "ctrl+x".  You should now be back to your terminal, similar to this:


Go ahead and use "cat" to display the contents of "stuff" in your terminal:

(darthlukan@SithCouncil)$ cat stuff
I like text
They like text
We like text
A lot of people like text.
(darthlukan@SithCouncil)$

Your final line of text will look different unless you chose to use what I wrote above.  You now know enough about using Nano to be dangerous.  Assuming that you use the methods that I described above, you can edit any config file on your system and be able to follow any instructions that you may be given in the future with regards to adding or deleting text from a file.


Mommy may I?

Now comes the "boring" part of our discussion.  We are going to talk more about permissions, why they are important to know about, how to know which permissions you need to edit a file, and how to get those permissions in relatively secure ways.  Keep in mind, there are many ways to do many things in Linux.  What I'm about to show you is very, very, very basic.  That means that your buddy who's been using Linux for several years as a server admin is going to bash you a little for doing this because he knows of many other secure ways of doing what I'm about to show you.  That's perfectly okay, this is just enough to get you by without breaking something, not so that you can go out and guard mainframes against super secret squirrel hacker operations against small governments.

Go ahead and "cd" to your home directory.  Then type:

(darthlukan@SithCouncil)$ ls -al


-rw-r--r--  1 darthlukan darthlukan     551 Feb  3  2011 .bashrc


(darthlukan@SithCouncil)$

What I just had you type was "look at all of the files in this directory (even hidden ones) and show me more info about each file in a detailed list".  I want you to focus on the .bashrc file in that list of files and folders. Now I'll explain what we are looking at:


  • -rw-r--r-- = Permissions.  "rw" means Read/Write, "r" means Read.  
  • darthlukan darthlukan = The user that owns the file (first) and the group that they are in (second).
  • Feb 3 2011 = The date the file was last modified.
The other numbers are not important to us right now, so I won't confuse you further.  Suffice it to say that they are added information that comes into play when dealing with space on your hard disk drive (HDD).

Let's talk a bit more about permissions.  Permissions, in basic terms, are what you are allowed to do with a file, folder, and/or device.  Focus on files and folders.  Right now, you are inside of your /home directory.  Within this directory you are God.  Within this little realm you have all the power that you could ever want.  You can create, modify, and delete files and folders to your heart's content.  Anything that you create is immediately owned by you.  No need for root.

Outside of /home/, with little exception, you're just a regular, nothing special, user.  Do not pass "Go", do not collect $200.  This is because you don't have permission to do anything.  You need root.  Flash back to your /home directory and stay there.  Permissions break down like this:

  • r = Read: You can Read a file (open, "cat", and basically look at a file)
  • rw = Read/Write: You can do all of the Read stuff, as well as Edit (Write) the file.
  • x = Execute: You can "run" the file as a program (more on this later).


There are more permissions and much more deep explanations, but this is what you need to know to understand the basics.  Moving on.

Now, the positions of these symbols is important.  See if you can guess what the following permissions mean:


  1. -rw-r--r--
  2. -rw-rw-r--
  3. -rwx-rx-r--


Think you got it?  Let's find out.


  1. Owner can read and write, group can read, others can read.
  2. Owner can read and write, group can read and write, others can read.
  3. Owner can read, write, and execute, group can read and execute, others can read.


"But Brian! I saw some stuff show up with "drw-r--r--", what does that mean?"  Good question! The "d" specifies that the file is a directory or folder.  Let's break the permissions into smaller chunks so that you can see it:

Example permissions: -rw-r--r--
-                                 rw-           r--         r--
^                                 ^               ^            ^
File classification    |  Owner   |  Group  |  Other  |


Don't worry about "Other" for now, we'll cover that in a later article.  Instead, I want you to really grasp those first two chunks.

Who's is it?


Now that you know a little about permissions, we're gonna talk about ownership.  If you own the file, you can do what you want with the file.  To change the owner of a file Linux has this great command called chown.  Quite simply, this stands for "Change Owner".  Go ahead and enter the following into your terminal and read the output:

(darthlukan@SithCouncil)$ chown --help

Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
  or:  chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.

  -c, --changes          like verbose but report only when a change is made
      --dereference      affect the referent of each symbolic link (this is
                         the default), rather than the symbolic link itself
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         ownership of a symlink)
      --from=CURRENT_OWNER:CURRENT_GROUP
                         change the owner and/or group of each file only if
                         its current owner and/or group match those specified
                         here.  Either may be omitted, in which case a match
                         is not required for the omitted attribute
      --no-preserve-root  do not treat `/' specially (the default)
      --preserve-root    fail to operate recursively on `/'
  -f, --silent, --quiet  suppress most error messages
      --reference=RFILE  use RFILE's owner and group rather than
                         specifying OWNER:GROUP values
  -R, --recursive        operate on files and directories recursively
  -v, --verbose          output a diagnostic for every file processed

The following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.

  -H                     if a command line argument is a symbolic link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help     display this help and exit
      --version  output version information and exit

Owner is unchanged if missing.  Group is unchanged if missing, but changed
to login group if implied by a `:' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.

Examples:
  chown root /u        Change the owner of /u to "root".
  chown root:staff /u  Likewise, but also change its group to "staff".
  chown -hR root /u    Change the owner of /u and subfiles to "root".

Report chown bugs to bug-coreutils@gnu.org
GNU coreutils home page:
General help using GNU software:
For complete documentation, run: info coreutils 'chown invocation'

Keep in mind, this is not the best way to get permissions to a file, it also does not work on all files.  However, it is safer to copy a file to your /home and then chown your copy than it is to leave the file in place and then edit it as root (in general, no copying your /etc directory to /home and trying to chown everything, you'll cause yourself a headache).

Go ahead and "cd" to /etc/skel.  If your distro does not have /etc/skel, don't worry, I'll post all of the output regardless.  Now, I want you to "ls -al" and and copy the .bash_profile file to your home directory as bash_profile_play (notice I dropped the '.' from the file name, this is so that it is not hidden).  If you don't remember how to copy files via the terminal, please refer to my previous article on manipulating files here.

Now that you have the file in your home directory, type:


(darthlukan@SithCouncil)$ ls -al
-rw-r--r--  1 darthlukan darthlukan     193 Sep 14 14:06 bash_profile_play
(darthlukan@SithCouncil)$

On my system, if I copy a file into my home directory, it immediately becomes mine.  Though this is the default for many distros, yours may still reflect root as the owner and the group.  If that is the case (and even if it isn't), chown the file like this:

(darthlukan@SithCouncil)$ chown username:username bash_profile_play
(darthlukan@SithCouncil)$
(darthlukan@SithCouncil)$ ls -al
-rw-r--r--  1 darthlukan darthlukan     193 Sep 14 14:06 bash_profile_play
(darthlukan@SithCouncil)$

The file is now, most definitely, yours.  "But Brian! This doesn't seem like a one size fits all solution! What if I want to edit a system config file?"  Good question!  Let's try that.

(darthlukan@SithCouncil)$ nano /etc/X11/xorg.conf



Notice the bottom bar? [ Read 70 lines (Warning: No write permission) ].  Go ahead and exit the file by hitting ctrl+x.  Next, type the following:

(darthlukan@SithCouncil)$ su -c "nano /etc/X11/xorg.conf"
Password:

If you're using a Debian based system (*Ubuntu, Mint, PeppermintOS) and have not explicitly set a root password, that command will not work for you! You will have to do this instead:

(darthlukan@SithCouncil)$ sudo nano /etc/X11/xorg.conf
Password:         <=== Depending on your system, no password may be necessary  

And this is what you should see:



WARNING!: Do not edit anything in this file! You run the risk of causing a lot of harm to your system (and your sanity) if you mess up something in this file.  The next time you reboot you could end up in a TTY console with not GUI available until the file is fixed.  If that sounds like fun to you, welcome to the club :)

Go ahead and close the file with ctrl+x.  You should now be back at your terminal as a user.  "But Brian! What did I just do?"  In the case of those of you using a system that isn't centered around "sudo", you used the su -c command to elevate yourself to root for only that one command.  If you used sudo, you gained "sorta" root permissions to be able to edit the file.  Let's break it down:

su -c = Root for one command
su = Root for as long as you want
sudo = Sort of root for only one command (I say "sort of" because there are some fineries there that are still in debate amongst many a system admin and the topic goes beyond the scope of this article, we'll fight about this later).

Now, those are the major ways of gaining root access to files and directories that you want to edit.  In case you were wondering, if you are on a system that is not Debian based (Gentoo, Arch, Slack, Fedora, etc), you can use "su" by itself, press enter, type the root password, and be root.  If you want to go back to user, just type the word "exit" and press enter.

If you are on a Debian based system (*Ubuntu, Mint, PeppermintOS, Liquid Lemur, etc) you will most likely need to "create" root (this isn't the proper term, but it fits for our purposes).  For those distros, you would type:

(darthlukan@SithCouncil)$ sudo su
Password:
(darthlukan@SithCouncil)$ su
Password:
(root@SithCouncil)#

I highly recommend that if you are on a Debian based system and you are creating a root password so that you can "su" (switch user), that you choose a very difficult password that is completely different from your regular user's password.  Security through obscurity and all that jazz.

Recap


We've done a lot today and you should be proud of yourself for sticking with it!  We covered how to write to files using echo, how to edit files with Nano, how to check a file's permissions, and how to elevate our permissions to root.  Your homework now is to find every excuse to not use root to edit a file, especially a file in your /home directory (~/, get used to that symbol set as I'll stop using /home in the next article).  Your other homework assignments are to play around with some files in the terminal and get used to editing text that way.  Read some wiki articles for your distro that talk about some simple modifications that you can make to your graphical system and that require the editing of files, then use what you've learned here to edit those files.  In other words: Practice!

Next steps


Next time we get together we'll talk about some things that we can use to make your life a lot easier, like aliases and other fun stuff.  We'll also get some more use out of editing files to customize our system in small ways and give you an introduction to hidden files and folders in Linux.

I hope that this article was educational for you, if it was, share it! If you have specific questions, comments, or suggestions, I can be found in IRC pretty easily as well as on Google+.  Click here for a link to my Google+ profile.

-DarthLukan

Post a Comment