Useful Linux commands:
Important note: Linux file names and commands are case sensitive.
ls - directory listing. Similar to DOS's
dir. Use
ls -l to get a detailed listing.
Example
Code: Select all
me@arsd:~$ ls -l /dev/cdrom
lrwxrwxrwx 1 root root 8 2005-04-22 20:53 /dev/cdrom -> /dev/hdc
This does a long listing of one file, /dev/cdrom. If the arguement was a dircetory, it would list the contents of that directory. Now, I'll explain the output.
lrwxrwxrwx : this is the file's permissions. The first letter is what kind of file it is. This file is a link, so it says
l. If it were a directory, it would say
d. The other letters are its read, write, and execute permsiions for its owner, group, and others. I will explain more on this later.
The 1 I think is the inode number, which tells where it sits on the disk. I'm not really sure about much details about it though.
The next 2 things are the owner and group of the file. More on this when I venture into permissions.
Afterwards is the file's date stamp. This tells you when the file was last accessed.
Lastly is the file name. Since this example is a symlink, it tells the file and to what it points.
-------------------------
OK, now that you can see the files, it is time for simple file management.
cd - changes the directory. Is is
cd dir .
cp - this copys a file. It is as simple as
cp src dest .
There are many more options to make this more powerful that I will cover later.
rm - removes (deletes) a file. Is is
rm file . It also has many more options to make it powerful that I will cover later. Use this with care - it does not ask for confirmation and once a file is gone its gone.
mkdir - makes a directory. It is
mkdir name .
rmdir - removes a directory. It is
rmdir name .
ln - not the natural logarithm, but rather it creates a link. Links are rather advances, so I will cover them later, but be aware of this command. You will love it once you know it.
unlink - removes a link. More on this later.
chmod - changes the file's permissions, more in the permissions section.
chown - changes the file's owner. More in the permissions section.
chgrp - changes the file's group. More in the permissions section.
-------------
And two important commands:
apropos - searches the command desctiptions for a word. Try
apropos file to see the results.
man - displays a command's manual page. Try
man ls to see what it does.
-------------------------
Shell tips
Using the Linux command line is much easier than using a DOS/Windows cmd line. You have various features that help out:
Tab completion: When typing a command or filename, you can press the tab key to have the shell automatically finish it for you. If there are multiple choices left, it will beep. If you then press tab again, it will list all the choices for you.
Command editing: You can use the left and right arrow keys to edit your command. Use the up and down arrows to bring recently used commands back up.
I/O Redirection: A very important concept if you want to get into some serious command line work. You can use these to string commands together to do amazing tasks. There are 4 operators will describe here.
> : Redirects the standard outpt of the command to a file. This truncates (ereases the contents) the file if it exists or creates the file if it does not exist. Usage:
command > file
>>: Like
>, but it does not clear the contents of an existing file. It instead adds the output to the end of the file. Usage:
command > file
|: the pipe. Redirects the standard output of command one into standard input of command two.
command 1 | command 2 . This is one you will use ALOT on a *nix system.
<: Redirects a file into the standard imput of the command.
command < file
-----------------------
Links
The command
ln I mentoned above is used to create a link. Now, I will explain a little about links.
There are two types of links: hard and soft (also called symbolic, or symlinks).
Hard links point to a file's place on the hard drive. They are like giving the file multiple names. The file remains until all its links have been deleted. I honestly don't understand them all that well yet, because I mostly use symlinks.
Symlinks point to a file. Doing anything to a symlink actually affects the target file itself. Opening the link opens the target file. Changing to a link (if it points to a directory) changes to the target directory, transparently as if the target directory was actually located in the link's directory.
----------------------------
Permissions
Each file in a *nix system has 3 important things pertaining to its permissions. Its owner, its group, and its permission flags. These are changed with chown, chgrp, and chmod, respectivly.
The permissions flags are shown to you in a long ls listing. It would generally look something like this:
-rw-r--r--
The first letter tells what kind of file it is. The next 9 are the permissions.
The first 3 of those 9 are its permissions for the files owner.
The next 3 are the permissions for other users in that file's group.
The last 3 are for everyone else.
The 3 letters each have a meaning:
Read Write eXecute.
On a regular file these mean the following:
Read: tells if the file can be opened for reading.
Write: tells if the file can be opened for writing. Note that even if it is not openable for writing that the file can be deleted: that is determined by the directory's attributes.
eXecute: tells if the file can be executed. This should be set on shell (or perl, etc) scripts or binary files, but not anything else.
On a directory, the permissions have a slightly different meaning:
Read: tells if the directory's contents can be searched. This does not make a difference as to if the files inside it can be opened, but if it is not set than someone cannot use a ls on it; hence to open any files inside he would have to know the exact filename. NOTE that this does nothing if the eXecute is not set.
Write: tells if the contents of the directory can be altered. This needs to be set if you want to create new files in the directory, rename files in the directory, or delete files in the directory. Note that this makes no difference as to if you can edit the contents of a file: it only makes a difference when working with the file's existance.
eXecute: tells if a user can open a file in the directoery. If this is not set, than the directory is locked.
I'll edit in more info later.