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

2011-09-04

Sabayon Linux: Awesome Edition







Those who have been following me on Google+ have seen a lot of posts centering around this upcoming release.  I have good news!  Sabayon Linux: Awesome Edition was added to the build server for daily spins today.

What does this mean?


This means that within the next few days an official release of Awesome Edition will be available on the Sabayon Linux website via bittorrent and mirror downloads.  Official support for this edition of Sabayon will also be available via IRC on FreeNode in the #sabayon channel.


What is Sabayon Linux: Awesome Edition?


Awesome Edition is an official spin of Sabayon Linux using the Awesome Window Manager.  It is aimed at power users who want a minimalistic, no frills environment to build their system on.  It comes with Sabayon's Fusion Kernel, a modified Linux kernel with experimental features enabled.  Just like all other Sabayon versions, this spin comes with the Anaconda installer and supports both text and graphical installations.



That sounds cool!


It is very cool!  For more information, head on over to http://www.sabayon.org/ and check out the download page.



2011-09-03

Linux: A Beginner's Guide, First Steps pt.2








Welcome back!  I hope you've gotten good and bored with the last bit of stuff I had you practicing.  Just as with learning a new language (I've recently decided to learn Dutch) and music, nothing can be so vital as practice.  Last time we talked about moving around the filesystem, looking into directories, and how to create directories.  Today we are going to talk about renaming, copying, moving, and deleting folders and files in the terminal.

Why do I need to learn this?


I'll answer this question with a bit of a story.  When I was nine (1993) I had decided that I was going to share the Linux world with a friend of mine.  Slackware had literally just come out to the public and I was lucky enough to get a copy of it.  My buddy (taken in by all the uber cool hacker news and not wanting to risk not being "cool") was totally happy with trying it out.  He was eleven years old.  Now, keep in mind, people were a lot different when it came to computers back then.  Personal computers were pretty limited to rich people or people who worked in the field, but were starting to make their way into mainstream society.

Needless to say, we didn't bother asking his mom if we had permission, we were too "cool" for that.  Now, my friend had no concept of what Linux was, for him, it was just like the Mac machines that we booted up in school.  There would be graphics and all of this other fun stuff to play with, right? Wrong!  Nowadays you kids have it easy.  There are installers that do it all for you (Arch and Gentoo are the only two mainstream distros that I know of that are still semi-manual).  I booted up the image and we were off.  Long story short, I got Slackware installed on his machine and hooked him up with twm, a very old but stable window manager for Linux, and even got some source packages together for him so that he could go online.

All was well in the world, right? Nope!  See, his mom worked with my mom, a cubicle wall apart.  When my mom found out what I was doing (my brothers were snitches) both moms were livid.  Mine more than his (she though open source software was stealing).  His mom came home, and was at least happy that we backed up her files and left her with a working system.  So she ran with it.  Her only real response was "You just make sure that if it breaks, you can fix it!"  We were in the clear! Until a week later...

You see, my buddy's mom wasn't the brightest person in the world, and my buddy even less so.  She managed to uninstall twm without knowing how to get it back.  No biggy, right?  Big fat wrong!  I thought that my friend had been paying attention when I showed him how to install stuff and fix things.  He wasn't.  I had neglected to teach him some very basic things, like how to use something other than a GUI.  Because of this, we were both grounded until I could get their computer back in working order.  What made matters worse was, there's a lie in there.  We were grounded until I got it back in working order, I was grounded until my mom said so.  Gotta love parents that work in Contract Law Translation.  (Not!)

So you see, while now we have GUI tools that can pretty much do all of this stuff for you, back then if something happened to the GUI and you didn't know the command line, you were pretty much screwed.

I don't want you to be screwed, you don't want you to be screwed, so let's not get screwed.  Besides, "'Tis better to have and not need than to need and not have."


Creating Files


You know how when you touch something it leaves a fingerprint?  Good.  You know how I said that everything in Linux is a file?  Great! You've been paying attention.  Well, in Linux it's really easy to create a file.  You just have to "touch" something.  In this case, that something is the filesystem.  We are going to leave a "fingerprint" on your filesystem using the "touch" command.  If you haven't done so already, please open your terminal and follow along.  Assume that after each command that I want you to press "Enter" (Carriage Return) unless otherwise stated.

(darthlukan@SithCouncil)$ touch me
(darthlukan@SithCouncil)$

Don't worry, in Linux, no news is good news (usually), so the fact that you were returned to an empty prompt is good.  What did we just do here other than allude to a sick joke?  We just used the command "touch" to create a file named "me".  This is a common thing in Linux (not touching yourself!) that you should get used to.  In Linux, you type a command (touch) and what follows is generally either an argument, the name of a file (me) or some other instruction.  Now, go ahead and type "ls" (without quotes) and hit enter.  You should see somewhere in that list a file named "me".  Pretty easy huh?

Now, I want you to open your file manager, the GUI one that came with your system, and I want you to do the same thing.  If you right-click on an empty space in the current folder of your file manager, you should have a context menu open up (kinda like in Windows).  One of the options should be "New...".  Go ahead and create a new file named "you".  That probably took a few clicks, huh?  I want you to think back to when I showed you how to make folders (directories, mkdir command).  Remember how we were able to make a whole bunch of folders all at once?  Can you do the same in your GUI file manager? No, you can't (there are file managers that allow this, but they are beyond the scope of this document and are more advanced than you probably want to get at the moment).

Go back to your terminal and type the following:

(darthlukan@SithCouncil)$ touch him her them
(darthlukan@SithCouncil)$

Pretty cool huh?  Can you see where this is valuable in saving time?  I can!  We have just one problem now, but this problem will be expanded on (along with the answer to the problem) in the next section.


Moving Files


Isn't this a fine mess?  We now have files left rampant in our /home directory.  I don't know if you have OCD or not, but I do, and disorderly stuff mucking around in my realm of order and perfection irritate the living day lights out of me.  Let's clean up a little!  I want you to create a directory using your terminal called "pronouns".  If you have forgotten how to do this, then you didn't do your homework like I asked in the previous section.  If you did do your homework, you've already made the folder while reading, so I'll move on.

(darthlukan@SithCouncil)$ mv you pronouns
(darthlukan@SithCouncil)$ mv me pronouns && mv her pronouns
(darthlukan@SithCouncil)$ mv -t pronouns him them
(darthlukan@SithCouncil)$ cd pronouns
(darthlukan@SithCouncil)$ ls

her  him  me  them  you
(darthlukan@SithCouncil)$

This is the part where I want you to look at what we just typed and I want you to think about it.  Don't skip down further to the next section! Seriously, look at what we did.  Have you caught it? Good!  For those still wondering why I made you re-read what we just did, it's because I taught you something in a somewhat sneaky way.  Remember when you were a kid and some older relative told you "there's more than one way to skin a cat?"  They were teaching you about Linux and didn't even know it!  In Linux there are multiple ways to do the same thing.  You've got the first way, do it one at a time.  The second way, use "&&" to link way number one and repeat it (that's not actually what && does, but for now it will do).  Third, we use an argument/option to do what we want in one command.

How did I know that using "-t" and reversing the order of the folder with the files would copy them properly? I'm awesome that way.  Actually, I know this because at one time I was taught the most important lesson I think that I have ever learned (other than "Microsoft is the devil"), that was " --help and -h will save your life!"  Go ahead and try it!

(darthlukan@SithCouncil)$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

Report mv bugs to bug-coreutils@gnu.org
GNU coreutils home page:
General help using GNU software:
For complete documentation, run: info coreutils 'mv invocation'
(darthlukan@SithCouncil)$

Read that help output.  Commit at least the first portion to memory if you can.  If you can't, just remember that "--help" could save your life!  On to the next section.


Renaming Files

Just as Ron White told you the story of him being called "Tater" so that he could give you another great joke about being drunk in public, I told you about how to move files with "mv" so that I could show you how to rename them.  Go ahead and type the following into your terminal:

(darthlukan@SithCouncil)$ mv him he
(darthlukan@SithCouncil)$ ls
he  her  me  them  you
(darthlukan@SithCouncil)$

Nifty!  So what did we do?  We used the "move" command to rename a file, right?  Not exactly.  Here's the more complicated explanation.  What we did was pick up the file "him" and then placed him somewhere issuing him the new name "he".  Although he's in the same folder, he's not in the same place on the disk that he was earlier.  So we really did move him, just not out of the folder.  There really isn't much more to say here, so we can move on to the next section.  (We could go into more complicated methods of file renaming, but they require more explaining than is really appropriate for this guide).



Copying Files and Folders

Let's have some fun.  So, we all know that maintaining backups is important, right?  If you didn't know that, consider yourself educated!  Backup backup backup backup backup! Never forget that.  Well, what's a backup?  It's a copy of something.  Tonight/today you will learn the basics of backups.  Look at that! Two birds with one stone, I'm on a roll tonight. :)

Go ahead and type the following into your terminal:

(darthlukan@SithCouncil)$ cd 
(darthlukan@SithCouncil)$ mkdir grammar
(darthlukan@SithCouncil)$ cp -r pronouns grammar
(darthlukan@SithCouncil)$ ls grammar
pronouns
(darthlukan@SithCouncil)$

Whoa! That was pretty quick huh?  So what did we do?  Go ahead and type:

(darthlukan@SithCouncil)$ cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
      --copy-contents          copy contents of special files when recursive
  -d                           same as --no-dereference --preserve=links
  -f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again (redundant if
                                 the -n option is used)
  -i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -n, --no-clobber             do not overwrite an existing file (overrides
                                 a previous -i option)
  -P, --no-dereference         never follow symbolic links in SOURCE
  -p                           same as --preserve=mode,ownership,timestamps
      --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                 mode,ownership,timestamps), if possible
                                 additional attributes: context, links, xattr,
                                 all
      --no-preserve=ATTR_LIST  don't preserve the specified attributes
      --parents                use full source file name under DIRECTORY
  -R, -r, --recursive          copy directories recursively
      --reflink[=WHEN]         control clone/CoW copies. See below
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --sparse=WHEN            control creation of sparse files. See below
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -s, --symbolic-link          make symbolic links instead of copying
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 copy only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -x, --one-file-system        stay on this file system
      --help     display this help and exit
      --version  output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well.  That is the behavior
selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.

When --reflink[=always] is specified, perform a lightweight copy, where the
data blocks are copied only when modified.  If this is not possible the copy
fails, or if --reflink=auto is specified, fall back to a standard copy.

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.

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

Don't worry, there's a very good reason why I keep pasting the "--help" output, but we'll get to that in a minute.  

What we just did was copy a directory (and its contents) into another directory. If we only wanted to copy the files themselves, we would have stayed inside the "pronouns" directory and copied the files, but that would have required a lot of typing.  Instead what we did was use the "-r" option to copy the folder and its contents recursively.  That means that the copy command sees what's inside of "pronouns", notes it, and starts making copies of the folder itself and then the files, placing the whole thing inside the destination "grammar".  

On to the next section minions!



Deleting files and folders


I want to start this section with a warning.  The command that I'm about to show you comes in many different formats, some of which are used deliberately by kids, criminals, and mean people (but mostly just by kids) to make an otherwise unwitting individual delete their system!  So before I show you the right way, I want you to commit the wrong way to memory so that you do not make the mistake that many people have made in the past (and still do unfortunately).

Plain:  rm -rf /*               (as root)
Another: sudo rm -rf /*
Fork Bomb: :(){ :|:& };:

(Fork Bombs are functions that make copies of themselves, if you accidentally run one, reboot.)

Basically, if anyone gives you a command that you are leery of, enter it into Google first, if there are articles saying "Don't run this ever!", it's probably a good idea to stop talking to whoever told you to run it :)

Now for the correct way to delete, we don't use root.  Ever! Unless you know what you are doing, Don't ever run a delete command as root!


Please type the following:

(darthlukan@SithCouncil)$ cd grammar/pronouns
(darthlukan@SithCouncil)$ rm them
(darthlukan@SithCouncil)$ ls
he  her  me  you
(darthlukan@SithCouncil)$

Well, now that we have "them" out of the way, it's time to get "you" and "her" alone!  Pay attention to the following:

(darthlukan@SithCouncil)$ rm *e
(darthlukan@SithCouncil)$ ls
her you
(darthlukan@SithCouncil)$

What the heck was that all about!?!  Now you see why "rm -rf /*" is so dangerous, especially as root.  What do you think would have happened if we would have forgotten to type "e" after the "*" (asterisk)?  Exactly what you hopefully have caught onto on your own.  Everything in the folder would have been deleted!  "*" is what we call a "wildcard".  The above command basically said "delete everything(*) with an "e" at the end of the name.  That's why even though "her" has an "e", the file was not removed.  What do you think would have happened if we would have entered in "rm h*"?  Think about it.  That's right! It would have been just "me" and "you" (which is not nearly as exciting as what we actually are left with, "you" and "her").

Go ahead and step out of the folder by typing "cd ..".  You should now be inside of the "grammar" folder.  Go ahead and type the command "rm pronouns/*".  Now type "ls pronouns".  You shouldn't see anything but space.  You just deleted files without actually needing to be in the folder.  Now, I'm going to show you two ways of deleting a folder:

(darthlukan@SithCouncil)$ rm -r pronouns
(darthlukan@SithCouncil)$ rmdir pronouns

"rm -r" means, "remove recursively" and "rmdir" means, "remove directory".  I'll leave it up to you which you prefer, both do the same thing.  Just remember to be very careful when deleting files and folders!  You can very easily cause more harm than good.  When all else fails, make a backup!!!  If you aren't sure that you've made a recent backup, make another backup!



Homework


I know that it must seem demoralizing and "wrong" to assign homework for something that should be fun, but you'll get over that pretty soon.  I want you to create a folder called "practice".  In this folder I want you to practice everything that we've talked about today as well as what we talked about last time.  I want you to make more folders and files, move them, copy them, delete them.  Here's the catch, I want you to find different ways of doing all of these things.  Play with the asterisk (*) and see what other uses it has with those commands that we've learned to see if you can make messing with files easier.  See in which ways it can cause problems.  Practice, practice, practice!


What to expect next time

Next time we'll talk about reading and writing to files using terminal commands and text editors.  Get ready! This is where you'll start to realize what kind of power (and safeguards) Linux arms you with.  It's going to be pretty exciting as we are going to look at system files and some ways to make your life easier when editing them.

See you next time!


-DarthLukan