Unix 101: File Attributes

So, we now know our way around the Unix filesystem now and we know how to read, move or delete files. We also know how to ask for help if we’re stuck. This post in the Unix 101 series will teach you about file permissions.

We’ve been talking about Unix being a multi-user, multiprocessing system, but we haven’t really though of security yet, have we ? I mean, if there are several users using a system at the same time, your personal data can’t be safe, right ?

Wrong !

The designers of Unix also faced this problem … and fortunately they were smart enough to devise a solution for this.

We learned earlier ( did we ? ), that everything in Unix is a file. Well, it is, and every file has certain attributes associated with it. These attributes define the permissions that the file has, or rather, the permissions that a particular user has to access that file. All this getting too complicated ? An example would be perfect here:


$ ls -l

total 4

drwx–x–x  13 sharninder sharninder 4096 Sep 29 02:53 ./

drwxr-xr-x   3    root           root           4096 Mar  6  2008 ../

drwxr-x—   3   sharninder mail           4096 Mar  7  2008 etc/

-rwxrw-r–  10 sharninder sharninder 4096 Apr  4 01:52 test.sh

This is the listing you get when you look at a directory with the long listing option ( -l ) to ls. This listing can be broken down into 7 sections. Each line describes one line and from left to right, the various columns give the: the permissions, the number of links, the owner, the group owner, the size in bytes, the date and time of the last modification, and the file’s name. 

The first character of the permission column tells us what kind of file this is. ‘d’ stands for directory, hyphen (-) for regular file. The next three triplets of three characters each tell us, in order, the permissions on the file that apply to the file’s owner, the file’s group and the public.

So, for example, the file test.sh is a regular file (-), the owner has the permissions, rwx, the group has the permission, rw, and the public has only the ‘r’ permission. ‘r’ stands for read-only, ‘w’ stands for writing (which implies reading) and ‘x’ stands for executable. For directories, ‘x’ means that the directory can be browsed through.

Before, we learn how to set permissions, we should know what the third and fourth columns stand for. The third column of the file listing is name of the owner of the file and the fourth column lists the group the file owner belongs to.

Now, we’ll learn how to set permissions ourselves.

To set or change the permissions on a file, we use the chmod command. For example:

$ chmod a+rw test.sh

The above command will set the read-write (+rw) permission for the public (a – all) on the file test.sh. Similarly:

$ chmod a-rw test.sh

will remove the read-write (-rw) permission for the public (a -all) from the test.sh file.

$ chmod u+x test.sh 

This will set the executable permissions on the test.sh file for the user who owns the file (u – user).

$ chmod g-rw test.sh

Will take away the read-write permissions from the test.sh file for the user who belong to the group (g -group) of the file’s owner.

With the chmod command we can only deal with the permissions imposed on the owner/group of the file. What if we need to the owner/group itself. That is taken care of by the chown command.

$ chmod sharninder.root test.sh

The above command will set the user of the test.sh file as sharninder and the group as root. Basically, the format to use is <user>.<group>

Thats all for today. With those two commands in hand, you should be able to handle almost all Unix permissions. Read the man pages for both the commands and you should be good to go.

Unix 101: Manipulating files – Copying, moving, deleting

This post is the next in a series of posts we’re doing on Unix. The aim of the series is to get our readers familiar with the basics of working on a Unix or a Linux system.

Uptil now we’ve learned a bit about how the Unix system works and how to navigate around the filesystem. We can now login to a unix system and start viewing the files and moving around between different directories. We also know a little about processes on a Unix system.

What next ?

This post will teach you about different file manipulation operations. By file manipulation, I mean, commands to copy, move or delete files. Since, on a Unix/Linux system everything is a file, the commands that we learn about today can also be used to work with directories, with some changes.

Unix commands have a reputation for being terse and short, which goes back to the old days when all input to a system was through some complex circuit manipulations or the computer had to be fed punch cards to recognize input. It made sense to use short commands for the purpose back then and modern Unixes/Linuxes have just carried on the tradition.

Take for example, the command to copy a file, cp, the syntax for which is:

# cp <sourcefile> <destinationfile>

For eg:

# cp test1 test2

The above command will just make a copy named test2 of the file named test1. Simple, isn’t it ?

Continuing the tradition of terse commands is the command to move files, mv.

# mv <sourcefile> <destinationfile>

For eg:

# mv test1 testdirectory/test2

The above command will copy the file named test1 to the directory named testdirectory and will change the name of the file to test2. If you don’t give the name of the destination file (test2 in the example), the file will be moved with the same name as the source.

The last command that we’ll be talking about today is the remove command, or rm. rm is the command to use when you want to remove a file from the system.

# rm <filename>

For example,

# rm test1

The above command will delete the file named test1. Use the command with care, though, since unlike Windows there is no concept of a recycle bin in Unix/Linux and any file that you delete using rm will be gone forever and you cannot change your mind later.

The above commands also work on directories, since a directory is also a file in Unix, a special file.

That was a basic overview of the cp, mv and rm commands. As always, I’d urge you to RTFM (read the fine manual – man pages in this case) and try out these commands for yourself. The only way to understand Unix is to use it.

I really hope this series of posts is benefitting atleast someone out there. I would love to hear some feedback from you guys on how I can improve my next posts and also what would you like me to cover.

Unix 101: Unix processes

I’m sure you’ve heard people tell you that Unix (and Linux) is a multiuser/multitasking operating system. But, what does that mean ?

Well, for one, you can create multiple users on linux and let all of them use the machine at the same time, by letting them login remotely (We’ll cover this later). And thus, Linux is a multiuser operating system.

Multitasking is a little more complicated.

You see, a Unix system at any time is always running more than one programs simultaneously. Ah, but I am only running my browser, you ask ? Well, yes and no. The browser is the only program that you are running but there is more to the operating system, than just the browser, right ? For example, the graphical interface that you’re running. And what if there are more people working on the same system (the multiuser part of it), then all of them would be running their own programs, right ? So, to cut things short, any Linux or Unix system at any time is always running multiple programs/tasks and thus, is a multitasking system. How does it do that ?

Continue reading “Unix 101: Unix processes”

Unix 101 – Help aka 'man' pages

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.”

– Dennis Ritchie

Dennis Ritchie is one of the creators of Unix and the above quote summarizes the philosophy behind Unix. The Unix command line is terse by design and it can take a new user a long time to understand (and remember) the nuances of a particular command line.

Fret not, cause man is at hand.

man is the unix/linux command line utility designed to display documentation about a given command. For example, suppose you’ve forgotten the option used to display the long list of files in a directory, you can use the following command to view the help manual for the ‘ls’ command:

$ man ls

That’s it.

This will display a page with a description of the ‘ls’ command and all the options that it supports. If you’re lucky, the man page writer would have also included some examples describing the usage of the command.

Now, try viewing the man page for all the other commands that we’ve discussed till now.

Unix 101: Listing and reading files and moving between directories

In the last post of this series, we learned about logging in to a Linux box, creating a new non superuser account and start using that.

In this post we’ll learn how to start working with files. Creating, deleting and managing files is the reason you use a computer, right ?

Unix has a concept of directories, which is the same as a folder on Windows.

To list all the files in the current directory, use the following command:

$ ls

This will give you a list of directories in what is called the wide format.

Continue reading “Unix 101: Listing and reading files and moving between directories”

Unix 101 – Basic Unix skills

I have a confession to make. I’m a Unix guy, have always been and will always be. I’ve been using Unix for the last 10 years or so and will continue using it for as long as I can. There is something about the simplicity of the unix command line that none of the flashier operating systems can match.

I’ve also been interviewing people with basic Unix skills for various positions over the last couple of years. For some reason, I’ve found that Indian college curriculum doesn’t put too much emphasis on Unix and so, most people who come out of these colleges don’t know much beyond the flashy GUIs that most Unix/Linux distros have. Infact, a lot of people who come for interviews tend to thing that Redhat/Fedora IS Linux. And when asked, tell me that the only Linux they have experience on is Linux version 9 !

Anyway, I’ve decided to start series on basic Unix skills on this blog in the hope that this’ll be a useful reference for someone starting to learn Unix/Linux. I’ll be mostly concentrating on Linux as that is what I’m most comfortable with and currently work with, but if I do get requests for similar posts on other Unix systems, I’ll be happy to oblige.

Continue reading “Unix 101 – Basic Unix skills”