Just A Small Guideline

Feel free to browse the site. Don't be afraid to leave comments, they're supportive.
Please follow me on Twitter @ehldmnt and retweet my posts.

Monday, April 1, 2013

The Linux Command Line Interface -- File and Directory Manipulation Tutorial

Manipulating Files & File Permissions

Now before we even get started, this is going to be a bit of a large article, so you can read maybe the first three commands and study them or you can try and learn everything here as you go along.

First off, I'll start by explaining files and file permissions in layman's terms so that everyone can easily grasp the concept.


ls -l & ls -h



ls -l is a very useful command that is good to know for a few reasons.

Regular ls:


ls -l:


Let's take a closer look at the output of one of these commands. ls -l prints a "longer version" of the ls command. Here are two perfect examples of the ls -l output.


Now, there are two things. The user name "vadim" and the group "users". They sit beside the 10 characters on the far left. After that is the file size in kilobytes, the last time it was modified, and finally, the name. Now that I've clarified that, I'll explain those characters a bit more.

"d" in the first strip represents a "directory", in the second strip, it is a "-" which represents a file. You'd see as "l" if it was a link, which is similar to a "shortcut" in windows [We'll go over creating links in a later guide]. The r stands for "read" which means that you can see, and look into the file. W meaning "write" means that you can edit or "write" into the file, and "x" means you can "execute" that file, like for example if it was a program of some kind then you could run it by typing ./programname and hitting enter in the terminal.
There are nine of these characters, the first one being for the owner of the file which is the user name that is listed above. The second one being the group that is "users". Anyone in the "users" group on the computer can edit the file. The last, being "global" or "everyone". Anyone can read and execute, or go into the (d)irectory.
In the file (as expressed by the "-"), the owner can read, but not execute the file and every one else can only read it, and see that it's there.

ls -lh


The only thing that ls -lh changes is the way that the file size is read. It actually tells you that it is 4.0K(ilobytes) instead of expecting you to figure it out.

For more, see man ls.

cp


The cp command is actually a very simple command. It copies a file from one place in the file system to another. Say we had a file on the Desktop called "some_text.txt". We wanted to copy that file to our "Documents" directory.

To copy that file:


As you can see, I've both copied the file and then shown you that it is now ALSO in the Documents folder. "But what if we want to move folders? It just tells me that it 'omitted the directory'." - That's why we use the -a option for "archive".




For more, see man cp.

mv

Let's say that we didn't want our text file to be "some_text.txt" anymore, and we wanted to name it "a_text_file.txt". That's where the move command comes in.

But it doesn't just "rename" files, it also allows us to move them too! That's why it's called "move"!


You do not need an option to move a folder.


For more, see man mv.
mkdir



Being able to edit directories is fine, but what about making them? mkdir does this.


rm



What about getting rid of, or r(e)m(oving) the old file that's on the desktop? No problem.





Once again, an option is needed to remove directories. That option is -r.



It is extremely important that you do not remove things you think you might want to recover later with the "rm" command. There is no way to recover those files! To be extra careful, add the -i option to prompt you before every removal!


For more, see man rm.

less


The less command just allows you to read your text files.


For more, see man less.

chmod



Remember what we were talking about with permissions? This command allows us to change the permissions of a file. "u" represents the user, "g" represents the group, and "a" represents "all" or everyone.


But what if we wanted to be selfish and make it so that no one but ourselves could see, change, or edit the file?



For more, see man chmod.

chown



But let's say that we wanted to change who owned the file?


For more, see man chown.

If there are any questions or criticisms, feel free to comment.

No comments: