USF Home
Administrators
Hardware Status





Search this site
 

Linux Tutorial: Chapter 3

3.0 Basic Commands

In this chapter we will talk about some of the basic commands to help a new user quickly get a grasp on using a Linux system. We will discuss how to view, create and modify directories and files. You will also learn how to move around the system and change file permissions.

3.1.0 Basic commands for navigating the filesystem

3.1.1 The 'ls' command

This is perhaps one of the most basic commands that there is, you will use it all the time and it is nice to know a few different ways on how to use it. What the command does is outputs to the screen a listing of all the files in your current directory. Here is an example of the ls command in action:
[bob@host ~] ls
dir1 dir2 file1 file2 file3 file4 file5
[bob@host ~]
As you can see this gives us a listing of all the files and directories that are in the current directory. This isn't all the information that can be given by the ls command though, lets say that you wanted to know more information about a file... such as the date it was created/modified, who owns the file, what permissions are set on the file, and maybe you would like to know the size of the file. To get this information we can simply pass something called an argument to the ls command. The argument that we give the command is -l, which lists in long format. So when we piece the command together with the argument we get ls -l. Let's try it out:
[bob@host ~] ls -l
total 8
drwxr-xr-x 2 bob other 4096 Apr 25 11:05 dir1
drwxr-xr-x 2 bob other 4096 Apr 25 11:05 dir2
-rw-r--r-- 1 bob other 0 Apr 25 11:11 file1
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file2
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file3
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file4
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file5
[bob@host ~]
Let's take a minute to explain what we are seeing here. The first line as you can see, tells us the size of all the files in the directory in kilobytes (1 kilobyte = 1000 bytes). The second line that we get is a directory called dir1, we know this is a directory because if we look at the first bit of information we are given, the permissions on the file, it has a 'd' for the first letter in the set. The second piece of information we are given is the number of links to the file (explained later in this chapter). Next is the user and group that the file is owned by. Then comes the size of the file, this is given in bytes. And finally the date and file of the last modification of the file.

Another argument that is nice to know with the ls command is -a. This shows all hidden files in a directory. These hidden files are the ones that begin with a period and are usually configuration files that are used mostly by installed programs. Here is what we get when we use the command:
[bob@host ~] ls -la
total 12
drwxr-xr-x 5 bob other 512 Apr 25 16:29 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 2 bob other 512 Apr 25 16:29 dir1
drwxr-xr-x 2 bob other 512 Apr 25 16:29 dir2
-rw-r--r-- 1 bob other 0 Apr 25 16:29 file1
-rw-r--r-- 1 bob other 0 Apr 25 16:29 file2
-rw-r--r-- 1 bob other 0 Apr 25 16:29 file3
-rw-r--r-- 1 bob other 0 Apr 25 16:29 file4
-rw-r--r-- 1 bob other 0 Apr 25 16:29 file5
[bob@host ~]
Now we see two more files called .ssh and .tcshrc. These are the are both used for configuration of programs. We also see . and .., these are both directories. The . stands for the current directory, and the .. stands for the previous directory.

There are other arguments that can be used with the ls command, you can type 'man ls' for more information.

3.1.2 The 'cd' command

This command is used to change to a different directory. From the previous example we have two directories that we can move into called dir1 and dir2. If we wanted to change into dir1 we would type:
[bob@host ~] cd dir1
[bob@host ~/dir1]
See how we are now in the dir1 directory? Let's now use the command 'ls -la ' to see what files are in this directory.
[bob@host ~/dir1] ls -la
total 6
drwxr-xr-x 2 bob other 512 Apr 25 16:52 .
drwxr-xr-x 5 bob other 512 Apr 25 16:29 ..
-rw-r--r-- 1 bob other 13 Apr 25 16:52 linux
[bob@host ~/dir1]
You may be wondering how you get back to the previous directory. This is when the .. directory comes into play.
[bob@host ~/dir1] cd ..
[bob@host ~]
As you see, we told cd to change to .., which stands for the previous directory, and it places us back in the home directory for bob. (Note: A user's home directory is often denoted by the tilde, ~). Had we told cd to change to . we would remain in the current directory.

3.1.3 The 'pwd' command

Sometimes it is needed to know the full path of the directory that you are in and your command prompt isn't set up to give you that information. You can use the command pwd to help you with this problem. Here is an example:
[bob@host ~/dir1] ls
total 6
drwxr-xr-x 2 bob other 512 Apr 25 16:52 .
drwxr-xr-x 5 bob other 512 Apr 25 16:29 ..
-rw-r--r-- 1 bob other 13 Apr 25 16:52 linux
[bob@host ~/dir1] pwd
/home/bob/dir1
[bob@zedd ~/dir1]

Here the command prints to the screen for us the full path of the current working directory.

3.2.0 Basic commands for managing files

Linux has some basic commands that you should learn to manage your files. You can copy, delete, move/delete files and also create directories. We will show you how to use each of these and will also talk about what links are and how to use them.

3.2.1 The 'cp' command

This command is used to copy files. If you have ever used the copy command in MS-DOS you will find this somewhat familiar. Let's explain the basic uses for the cp command.

To copy a file to another location you would use cp in it's most basic form: cp source target.
For example,
[bob@host ~] ls
total 10
drwxr-xr-x 4 bob other 512 Apr 26 14:57 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 2 bob other 512 Apr 25 16:52 dir1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file1
[bob@host ~] cp file1 file2
[bob@host ~] ls
total 10
drwxr-xr-x 4 bob other 512 Apr 26 14:57 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 2 bob other 512 Apr 25 16:52 dir1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file2
[bob@host ~]
Like the ls command you can have several switches that change the way the program operates. Let's take a look at a few common ones...
Usage: cp -i
-i tells cp to use interactive mode. Which will prompt you if it needs to overwrite a file.

Usage: cp -r source target
-r will copy a directory and all its contents.

Usage: cp -R source target
-r will copy a directory and all its contents, but pipes are only replicated not read from.

Usage: cp -p
-p will preserve the owner group id, permissions, etc. The user copying the files must be of equal or higher access to do this.

3.2.2 The 'rm' command

This command is used to delete files and is similar to the MS-DOS del command.

The basic usage of the command is 'rm filename', but there are other arguments that can be used with the command. For instance, you must use a switch (argument) to specifically delete directories.
Usage: rm -i
-i tells rm to use interactive mode. Which will prompt you to delete each file.

Usage: rm -r,-R
-r and -R tells rm recursively removes a directory and all its subdirectories. This switch must be used to delete a directory wheter it is empty or not.

3.2.3 The 'mv' command

The mv command moves a file or directory to another location, or renames the file if in the current directory. The basic usage of the command is:
mv source target

Usage: mv -i
-i tells mv to use interactive mode. Which will prompt if there is already a file of name target.

3.2.4 The 'mkdir' command

This command simply creates a new directory. To use it simply type:
mkdir dirname
You can create a subdirectory tree using the -p switch,
mkdir -p dir/sub1/sub2
will create a directory named dir in the current directory and then a subdirectory called sub1 in dir and then another subdirectory will be created in sub1 called sub2.

3.2.5 The 'ln' command

Linux can have special files called links which point to a real file. This creates a shortcut to the file which can be used for easier access to the file. Instead of having multiple copies of one file, which would take up more space than necessary, the link just points back to the path of the real file that we want.

There are two kinds of links and you must understand the difference before you can be totally safe in using links. The first type of link is called a hard link. When you create a hard link to a file, the new link will take on all the permissions that are set on the original file, the link should look the same as a regular file. The main thing that you need to know here is that when you create a hard link all the changes made to the linked file will change the main file. If you delete the main file the link will keep the state of the original file. To totally get rid of the file you need to also delete the link. The next type of link is called a symbolic link. A symbolic link simply points to the original file, the link itself does not have the same properties of the original file, doing ls -l "linkname" will show that. The important thing here is that if you delete the original file the link will still be trying to point to the file, even though it doesn't exist! So remember that a hard link becomes the original file when the original file is deleted and a symbolic link does not! Deleting either a hard or symbolic link will not remove the original file.

Now let's move on to using the command:
ln source target
This will create a hard link where source is the file you wish to link and target is the desired name for the link. Note that you can not make hard links of directories.

Usage: ln -s source target
-s creates a symbolic link where source is the file or directory you wish to link to and target is the link name.

Let's see links in action:
[bob@host ~] ls -la
total 14
drwxr-xr-x 4 bob other 512 Apr 29 16:10 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
[bob@host ~] ln file1 link1
[bob@host ~] ls -la
total 16
drwxr-xr-x 4 bob other 512 Apr 29 16:11 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 2 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 2 bob other 5 Apr 29 15:37 link1
[bob@host ~]
Here we created a hard link called link1 from file1. Notice that properties from file1 still remain the same. If we deleted file1, link1 would still remain and contain the information that was last in file1. Let's have an example of a symbolic link:
[bob@host ~] ls -la
total 14
drwxr-xr-x 4 bob other 512 Apr 29 16:16 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
[bob@host ~] ln -s file1 symlink1
[bob@host ~] ls -la
total 16
drwxr-xr-x 4 bob other 512 Apr 29 16:17 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
lrwxrwxrwx 1 bob other 5 Apr 29 16:17 symlink1 -> file1
[bob@host ~]
Now you should be able to see the difference here. See how we created the symbolic link called symlink1 and how it tells you it points to file1? Also see the lrwxrwxrwx instead of -rw-r--r-- that we got in the previous example? If we remove the file file1 the symlink link will be of no use anymore.

If you want more information on this command you can type 'man ln'.

3.3.0 Basic commands for managing file permissions

3.3.1 The 'chown' command

This command will change the owner of a file, either by the user or the group. To use the command you must be either the current owner of the file or have higher access, such as super-user.
Changing the owning user:

[bob@host ~] ls -la
total 16
drwxr-xr-x 4 bob other 512 Apr 29 16:17 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
lrwxrwxrwx 1 bob other 5 Apr 29 16:17 symlink1 -> file1
[bob@host ~]


Lets change the owner of the file file1 to the user janet.

[bob@host ~] chown janet file1
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 Apr 29 16:17 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 115 Apr 24 16:12 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 janet other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
lrwxrwxrwx 1 bob other 5 Apr 29 16:17 symlink1 -> file1
[bob@host ~]


From this example you can see that the file is now owned by the user janet. Please take note that this will only work if the user exists, and the user must be a member of the group.

Changing the group:

If you need to specify a different group we can change it by using the chown command, specifying the group. So if janet was in the group www we can set her as the owner of the file file1 by using:

[bob@host ~] chown janet:www file1

The colon ':' separates the user and the group.
For more information on chown type 'man chown'.

3.3.2 The 'chmod' command

This command is used to change the permissions that are set on each file. The specify which user(s) can have read/write/execute access for a file or directory. There are three different ways of setting read/write/execute access: user, group, and other.
User: The settings for the specific user owning the file.
Group: The settings for the group owning the file.
Other: The settings for everyone else that isn't a user/group owner of a file.
You can see the indvidual settings for a file by using ls -l filename:
[bob@host ~] ls -l file2
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
We are interested in the first bit of information here, -rw-r--r--. The first space tells what type of file we have here, common characters for the first space are d for directory, l for a symbolic link, or just - for a regular file -- as we have in the example. The next three spaces give us the read/write/execute access for the user owning the file. Here the user bob has read and write access to the file; r = read, w = write, x = execute, and - = empty. The next three spaces are the settings for the owning group and the very last three spaces are the settings for a user/group that does not own the file.

Lets break down the example so you may see it more clearly.


This should make things a little more clear. The next thing you will need to know is how to set the r,w,x or - to change the files permissions. This is done by using numbers to represent the r,w,x or -. Here is an example of the command chmod 755.

This should give you a pretty good idea of how we get the individual numbers for the permissions. The 7 sets the modes rwx for the user owning the file, and the two 5's set the modes r-x for the group owning the file and everyone else.

For more information about this command type 'man chmod'.

Take Chapter Quiz


 



Copyright © 2008, Research Computing, USF, 4202 E. Fowler Avenue, Tampa, FL 33620