Introduction to UNIX
What do we mean by Directory and File?
Before you read on it is important to have a clear understanding of the concept of Directories and Files as you need to know the difference to get around in the system.
When you want to keep something you usually put it in some kind of storage. You can compare a computer with an office. At the office you would store stuff in a filing cabinet, on a computer you would put it on a hard drive. In the filing cabinet you can put documents like papers, drawings, videotapes or CDs. On the hard drive you can store documents digitally, like text, images, music and programs. In the filing cabinet you have many different directories that can contain both documents and more directories. Also the new directories can contain new documents and even more directories.
In a computer the hard drive will be the filing cabinet, directories and subdirectories represent the directories and files represent the different documents. A file can be a text file, a picture, a recording of a song and programs, while directories are used to sort the different files. A file cannot contain a directory and a subdirectory is a directory than is inside another directory.
Often the word element is used to represent both files and directories.
The name- and structure-system of files and directories
As in a filing cabinet the purpose of directories is to create a hierarchical storage system which is sorted and easy to navigate through. To describe where a directory or file is you use a system of paths. All paths is made in relation to the top of the hierarchy, which is "/". If there is a directory below the top called home the path to it will be /home. If the directory /home then has a subdirectory called homee the path to it is /home/homee. Below homee is another subdirectory je which has yet another subdirectory called jensj, the full path to je then will be /home/home/je/jensj. If an address starts with an "/" it is called an absolute path because it only can be interpreted in one way. More in depth explanation will be given further down in the article when it's needed.
Print Working Directory(pwd)
To see where in the file system you are you can use the command pwd. Directly after you log in, pwd will always give you the path you your home directory, this is because the home directory is the default directory you are placed in when you log in.
login:~$ pwd /home/homee/je/jensj
On the beginning of the first line in the example above you will see the name of the machine you're connected to, a colon, what directory you are in and a dollar sign on the end. After the dollar sign the command you are typing in is displayed. On the line below the result or output of the command is written.
The "~" sign represents the path to your default home directory, in the example /home/home/je/jensj, The reason for using "~" instead of the full path is to make it simpler to refer to the home directory of users on the system. If you are executing a command with files on another users home directory it is much faster to just type "~username" than the full path. Here "~", "~jensj" and /home/homee/je/jensj all refers to the same area. Another user, Arne, has the path /home/homek/ar/arne which then can be written as "~arne".
List the content of a directory (ls)
To list the content of a directory you can use the command ls. For jensj ls will give the output:
login:~$ ls Mail/ lettermom.txt mail@ public_html/
We see that Jens' home directory contains 4 elements, Mail and public_html are directories because they have an "/" at the end, mail is a symbolic link @ and lettermom.txt is a regular file.
Unix is case sensitive. That means Mail and mail is not the same element. A symbolic link just means that the element is referring to another element. If the element the symlink is referring to is removed the symlink will point to nothing and be useless. Symlinks are explained further in its own chapter.
ls options
The option -l
This option sorts the list alphabetical in long format
login:~$ ls -l total 4 drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 3 jensj ntnu 43 Jul 16 13:49 public_html/
Permissions L User Group Size mm dd hh:mm Name
- Permissions. Explained in its own chapter below.
- L. How many links there are to the element. (Ignore this!!)
- User. Who owns the element.
- Group. Explained in its own chapter below.
- Size. How much space the element uses on the hard drive. Measured in bytes.
- mm dd hh:mm. Gives the date of the element's last change, or if it has never been changed, when it was created.
-
Name. The name of the element and what kind of element it is.
If you look at the list that ls displayed we see that mail/ refers to Mail/. So in particular case they are the same directory even though their names are different. But if Mail/ is removed mail/ is useless.
The option -a
This option lists hidden files and directories. They always begin with the period symbol. This is mostly config files that lists commands that shall be run when you log in or start a program.
storm:~$ ls -a ./ .Xdefaults .bash_profile Mail/ public_html ../ .bash_history .bashrc lettermom.txt .Xauthority .bash_logout .netscape/ mail@
Here we see that there is a directory, .netscape/, and six hidden files. All of the .bash files and .Xdefaults iare used when logging in. .Xauthority is a file containing some security settings for the user. .netscape/ is settings and bookmarks for Netscape.
The option -la
This option combines both -l and -a. All the elements, both hidden and normal are listed in long format.
storm:~$ ls -la total 104 drwx--x--x 5 jensj ntnu 4096 Jul 20 18:44 ./ drwxr-xr-x 1531 root root 65536 Jul 20 06:43 ../ -rw------- 1 jensj ntnu 49 Jul 20 14:26 .Xauthority -rw------- 1 jensj ntnu 196 Jul 16 13:37 .Xdefaults -rw------- 1 jensj ntnu 1089 Jul 19 08:32 .bash_history -rw------- 1 jensj ntnu 232 Jul 16 13:37 .bash_logout -rw------- 1 jensj ntnu 236 Jul 16 13:37 .bash_profile -rw------- 1 jensj ntnu 212 Jul 16 13:37 .bashrc drwx------ 3 jensj ntnu 4096 Jul 20 18:44 .netscape/ drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 3 jensj ntnu 43 Jul 20 14:55 public_html/
The option -ld
This option only shows the directory you are in, not the content of it. This can be useful if you just want information about the directory and not all the files.
storm:~/public_html$ ls -ld drwxr-xr-x 5 jensj ntnu 93 Jul 22 17:44 ./
If you want to list the content of a directory, you do not need to go to the actual directory, just type in the path to the directory after the command. login:~$ ls -la public_html/bilder
Here the content of the directory bilder/ is listed.
It is a subdirectory of the directory public_html which is in the home directory.
When you long list a directory you will always have two lines in the top which
is the directories "." and "..". These these dots represent the current directory
and the higher directory that the current directory is in. You mostly use them
when you copy files and directories.
The option -ltr
Instead of sorting alphabetically you can sort after the time the element was last saved. This is the option -t. You can also reverse the sort with -r. Together with -l, ls will now display a list where the last file is the one that was changed last.
storm:~$ ls -ltr public_html/bilder total 164 -rw-r--r-- 1 jensj ntnu 1920 Jul 16 13:46 diskette.gif -rw-r--r-- 1 jensj ntnu 160335 Jul 22 17:40 100acre.gif
Here we can see that the file 100acre.gif is the one that has been changed last. This is practical if you cannot remember the name of the file e you last worked with in the directory.
Change directory(cd)
To move around in the file system you use the command cd. The command need an argument which says where to move. If you want to move to the folder public_html you write
storm:~$ cd public_html storm:~/public_html$
Here we can see that the command prompt changes and we are in the subdirectory public_html.
As said earlier ".." means the directory above the current, so to move up again we type:
storm:~/public_html$ cd .. storm:~$
So, to move two directories upwards we type:
storm:~$ cd ../.. storm:/home$
If the argument to cd starts with "/" the path is absolute. If not, the command is interpreted relative to where you are in the file system. If you are in the home directory you then do not have to type the whole path, just what comes after the current directory's name. The command is also interpreted absolute when the "~" symbol is included because "~" refers to /home/homee/je/jense which starts with a "/". Also, if no argument is added you will be returned to your home directory
storm:/home$ cd /local/www/html/studorg storm:/local/www/html/studorg$ cd ~/public_html storm:~/public_html$ cd storm:~$
Make a directory (mkdir)
To make new directories you use the command mkdir. This command need at least one argument which is the name of the new directory. To make the directory musikk/ you type:
storm:~$ mkdir musikk storm:~$ ls -l total 4 drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwx------ 2 jensj ntnu 9 Jul 21 02:10 musikk/ drwxr-xr-x 3 jensj ntnu 43 Jul 20 14:55 public_html/
We can see that the directory musikk/ has been made. The directory is made in the current directory. If you want the directory to be made in another directory you need to either go to the directory with cd or include the path to the new directory. If you want to make the directory sound/ in the directory public_html/ you type:
storm:~$ mkdir public_html/sound storm:~$ cd public_html/ storm:~/public_html$ ls -l total 8 -rw-r--r-- 1 jensj ntnu 21 Jul 22 17:44 IMAGEMAP drwxr-xr-x 2 jensj ntnu 9 Jul 22 17:44 USERBIN/ drwxr-xr-x 2 jensj ntnu 49 Jul 22 17:40 sound/ drwxr-xr-x 2 jensj ntnu 29 Jul 22 17:42 imagemap/ -rw-r--r-- 1 jensj ntnu 806 Jul 18 13:17 index.html drwx------ 2 jensj ntnu 9 Jul 27 17:04 musikk/
sound/ was now created as a sub directory of public_html/.
Remove directory (rmdir)
To remove directories you use the command rmdir. This command needs at least one argument which is the path to the directories that you are deleting.
storm:~$ rmdir musikk public_html/sound storm:~$ ls -l . public_html .: total 4 drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 3 jensj ntnu 43 Jul 20 14:55 public_html/ public_html: total 8 -rw-r--r-- 1 jensj ntnu 21 Jul 22 17:44 IMAGEMAP drwxr-xr-x 2 jensj ntnu 9 Jul 22 17:44 USERBIN/ drwxr-xr-x 2 jensj ntnu 29 Jul 22 17:42 imagemap/ -rw-r--r-- 1 jensj ntnu 806 Jul 18 13:17 index.html
The directories musikk and public_html/sound is now deleted.
To use this command the directory you're removing needs to be empty.
Copy files (cp)
To copy files the command cp is used. This command need at least two arguments, which file to copy and where to put the new copy. If there are more than two arguments the last one will have to be a directory. Examples are:
cp fromfile tofile cp fromfile todirectory cp fromfile1 fromfile2 ... fromfile9 ... todirectory
Jens has already written a letter to his mom. To save time he will send almost the same letter to his uncle. He copies the file lettermom.txt to letteruncle.txt so he can edit the letter to his uncle without changing the letter to his mom.
storm:~$ cp lettermom.txt letteruncle.txt storm:~$ ls -l total 8 drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt -rw------- 1 jensj ntnu 135 Jul 21 13:17 letteruncle.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 3 jensj ntnu 43 Jul 20 14:55 public_html/
If you want to copy more then one file at a time it can be done with giving more arguments to the command. Note that the last argument must be a directory.
storm:~$ cp lettermom.txt letteruncle.txt public_html storm:~$ ls -l public_html total 12 drwxr-xr-x 2 jensj ntnu 29 Jul 16 13:47 bilder/ -rw------- 1 jensj ntnu 135 Jul 21 13:18 lettermom.txt -rw------- 1 jensj ntnu 135 Jul 21 13:18 letteruncle.txt -rw-r--r-- 1 jensj ntnu 806 Jul 18 13:17 index.html
Both letters are now copied into the directory public_html.
In the next example Jens has looked at the user Jorun and seen that she has a file he needs. This file is in her home directory and is called .emacs. Jens wants to have a copy of of the file in his home directory.
storm:~$ cp ~jorunj/.emacs . storm:~$ ls -la total 100 drwx--x--x 7 jensj ntnu 4096 Jul 25 11:44 ./ drwxr-xr-x 1531 root root 65536 Jul 25 11:32 ../ -rw------- 1 jensj ntnu 49 Jul 20 14:26 .Xauthority -rw------- 1 jensj ntnu 196 Jul 16 13:37 .Xdefaults -rw------- 1 jensj ntnu 2941 Jul 24 20:20 .bash_history -rw------- 1 jensj ntnu 232 Jul 16 13:37 .bash_logout -rw------- 1 jensj ntnu 236 Jul 16 13:37 .bash_profile -rw------- 1 jensj ntnu 212 Jul 16 13:37 .bashrc -rw------- 1 jensj ntnu 147 Jul 25 11:44 .emacs drwx------ 2 jensj ntnu 126 Jul 21 05:50 .netscape/ drwx------ 2 jensj ntnu 51 Jul 24 19:45 Brev/ drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ drwxrwx--- 2 jensj ineo 9 Jul 21 15:57 boksider/ lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 5 jensj ntnu 93 Jul 22 17:44 public_html/
On the command prompt you can see that Jens only wrote "." not the full path to his home directory. He could do that because "." represents the current directory he is in. To copy elements form other users you need to have permission to read them. This will be explained further down.
Delete files (rm)
To delete files you use the command rm which is an abbreviation of remove. This command needs at least one argument which is what file to delete. You can also delete more than one file at the time.
storm:~/public_html$ rm letteruncle.txt lettermom.txt storm:~/public_html$ ls -l total 4 drwxr-xr-x 2 jensj ntnu 29 Jul 16 13:47 bilder/ -rw-r--r-- 1 jensj ntnu 806 Jul 18 13:17 index.html
The letters to mom and uncle have now been deleted. You should note that just the copies in the directory public_html have been deleted. The original files that were in Jens' home directory are still there.
storm:~$ ls -l total 8 drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ drwxrwx--- 2 jensj ineo 9 Jul 21 15:57 boksider/ -rw------- 1 jensj ntnu 135 Jul 21 13:17 letteruncle.txt -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 5 jensj ntnu 93 Jul 22 17:44 public_html/
To delete all the files in a folder you need to use the option -r which means recursive. To delete all the files in a directory you then have to type rm -r directory. Note that this command gives no option to undo the deletion, when the files are deleted this way you cannot recover them.
Move files (mv)
To move files you use the command mv. This command needs at least two arguments to work, which file(s) to move and where to put them. If you have more than two arguments the last one needs to be a directory. Examples of usage:
mv fromfile tofile mv fromfile todirectory mv fromfile1 fromfile2 ... fromfile9 ... todirectory
The listing below shows that Jens has created a directory named Brev/:
storm:~$ ls -l total 8 drwx------ 2 jensj ntnu 9 Jul 24 19:33 Brev/ drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ drwxrwx--- 2 jensj ineo 9 Jul 21 15:57 boksider/ -rw------- 1 jensj ntnu 135 Jul 21 13:17 letteruncle.txt -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 5 jensj ntnu 93 Jul 22 17:44 public_html/
Now Jens wants to move the letters to mom and uncle to the directory Brev/:
storm:~$ mv letteruncle.txt lettermom.txt Brev storm:~$ ls -l total 0 drwx------ 2 jensj ntnu 49 Jul 24 19:34 Brev/ drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ drwxrwx--- 2 jensj ineo 9 Jul 21 15:57 boksider/ lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 5 jensj ntnu 93 Jul 22 17:44 public_html/
As we can see, the letters are moved from the home directory and are located in the Brev/ directory:
storm:~$ cd Brev storm:~/Brev$ ls -l total 8 -rw------- 1 jensj ntnu 135 Jul 21 13:17 letteruncle.txt -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt
If you move a file inside the same folder it works like renaming.
storm:~/Brev$ mv letteruncle.txt letterjorun.txt storm:~/Brev$ ls -l total 8 -rw------- 1 jensj ntnu 135 Jul 21 13:17 letterjorun.txt -rw------- 1 jensj ntnu 135 Jul 16 19:00 lettermom.txt
Now uncle has no letter, but Jorun has.
Permissions and groups
With the ls -l command we can see a lot of information. The fields we have not covered are permissions and groups.
Permissions L User Group Size mm dd hh mm Name storm:~$ ls -la total 96 drwx--x--x 7 jensj ntnu 4096 Jul 24 19:34 ./ drwxr-xr-x 1530 root root 65536 Jul 24 06:28 ../ -rw------- 1 jensj ntnu 49 Jul 20 14:26 .Xauthority -rw------- 1 jensj ntnu 196 Jul 16 13:37 .Xdefaults -rw------- 1 jensj ntnu 2644 Jul 23 11:27 .bash_history -rw------- 1 jensj ntnu 232 Jul 16 13:37 .bash_logout -rw------- 1 jensj ntnu 236 Jul 16 13:37 .bash_profile -rw------- 1 jensj ntnu 212 Jul 16 13:37 .bashrc drwx------ 2 jensj ntnu 126 Jul 21 05:50 .netscape/ drwx------ 2 jensj ntnu 51 Jul 24 19:45 Brev/ drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/ drwxrwx--- 2 jensj ineo 9 Jul 21 15:57 boksider/ lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/ drwxr-xr-x 5 jensj ntnu 93 Jul 22 17:44 public_html/
The field for permissions is always made up of 10 symbols. The first one shows what type the element is
- = File d = directory l = Symbolic link
The last nine symbols are split into three groups of three symbols. The first three represent the user, the middle three represent the group and the last three represent everyone else.
It is now group permissions gets important. You can give members of a group permission to both read and write to a file, while everyone else is limited to read only.
In each of the three groups there is a set pattern for which letters it can contain and what they mean. The meaning is different for directories and for files. The following is for files, numbered from the left.
1 r Permission to read the content of the file 1 - No permission to read the content of the file 2 w Permission to edit the content of the file 2 - No permission to edit the content of the file 3 x The file is executable (a program) 3 - The file is not executable (a program)
The symbols for directories
1 r Permission to read what files are in the directory 1 - No permission to read what files are in the directory 2 w Permission to edit files in the directory 2 - No permission to edit files in the directory 3 x Permission to access the directory 3 - No permission to access the directory 3 s Same as x, but files that are created in the folder get the same group settings as the folder. Normally new files get the same group as the user.
With Jens we can read the following information from selected lines in the listing above:
drwx--x--x 6 jensj ntnu 4096 Jul 21 15:57 ./
Everyone has permission to enter his home directory, but no one but him can read the content of the directory or write to it.
drwxr-xr-x 3 jensj ntnu 43 Jul 21 14:34 public_html/
Everyone has permission to enter and read the content of the directory public_html, but only Jens can write to it.
drwxrws--- 2 jensj ineo 9 Jul 21 15:57 boksider/
Only members of the group ineo have permission to enter the directory boksider/. They can also write to files in it and all new files are automatically under the group ineo.
drwx------ 2 jensj ntnu 9 Jul 16 13:40 Mail/
The directory Mail/ is only accessible for Jens, as it should be, no one but the user should have access to his e-mail.
lrwx------ 1 jensj ntnu 4 Jul 20 14:27 mail -> Mail/
The permissions to the symbolic link mail is not important because it inherits the permissions to the directory it links to that is active.
Checking group membership
To see what groups you are a member of you can use the command groups:
storm:~$ groups ntnu ineo
Jens is a member of the groups ntnu and ineo. When you get a user you also get group membership. Usually you are a member of your faculty's student group.
You can also ask for and create personal groups in kundesenteret. These groups rights and membership is controlled by the users themselves.
Change group (chgrp)
To change what groups that have access to different files and directories you use the command chgrp. The first argument tells which group that shall access and all arguments after that give which files and folders it applies to:
storm:~$ ls -ld boksider drwxrwx--- 2 jensj ineo 9 Jul 21 15:57 boksider/ storm:~$ chgrp ntnu boksider storm:~$ ls -ld boksider drwxrwx--- 2 jensj ntnu 9 Jul 21 15:57 boksider/
The group access to boksider/ has been changed from ineo to ntnu.
Change permissions (chmod)
This command is used to change permissions of a file or directory. This command needs at least two arguments, the first one is what kind of change shall be done and the following argument is what element shall be modified
For groups we use use the symbols:
u user g group o other a all
The symbols for actions that shall be executed
+ Add permission - Remove permission = Set permission to
From earlier we have the symbols for the different permissions
Files:
1 r Read permission 2 w Write permission 3 x Executable program
Directories:
1 r Permission to read the content of the directory 2 w Permission to change content in the directory 3 x Permission to access directory and go to subdirectories 3 s Same as x, but when file as created they get the same permissions as the directory
To change the permissions for the file 100acre.map so that your group and everyone else can read it:
storm:~/public_html/imagemap$ ls -l total 0 -rw------- 1 jensj ntnu 0 Jul 22 17:42 100acre.map storm:~/public_html/imagemap$ chmod go+r 100acre.map storm:~/public_html/imagemap$ ls -l total 0 -rw-r--r-- 1 jensj ntnu 0 Jul 22 17:42 100acre.map
Here we can see that the permissions have been changed.
In this particular example you could also have used chmod go=r 100acre.map. The permissions for groups and all would have been set to "r--" no matter what they were earlier. By using + he only changes the read permission, not all the permissions.
Symbolic links (ln -s)
With this command you can create symbolic links between elements. This command needs at least two arguments, what the link shall point to and what the name of the link shall be.
Jorun has a program in her home directory which Jens wants access to. Jens could just copy the program, but he makes a symbolic link instead. This is because it has two advantages first, he saves hard drive space, as a symbolic link takes virtually no space. The second advantage is that Jens knows that Jorun keeps the program updated with the newest version. When Jens runs the program by the symbolic link he will run the same version as Jorun has.
The program file is called maplay and Jens wants to put the link in the directory bin/. As long as Jens only says what directory it shall be put in, not the name of the link, it will get the same name as the file referred to.
storm:~$ ln -s ~jorunj/bin/maplay bin storm:~$ cd bin storm:~/bin$ ls -l total 0 lrwx------ 1 jensj ntnu 28 Jul 27 17:55 maplay -> ~jorunj/bin/maplay*
If Jens shall be able to run the program he needs to have the permission to access Jorun's home directory and bin/ directory. In addition to have read permissions for the directory he also needs read and execute permissions for the program file, maplay. This Jorun has to grant him.
View content of files (cat)
To view content of files the command cat is used (cat = concatenate files and print). This command needs to have at least one argument which gives information about what files that shall be shown.
storm:~/Brev$ cat lettermom.txtHei Mor! Gratulerer med dagen som var for 5 dager siden. Som vanlig husket jeg det ikke i år heller. Unnskyld... Jeg håper du har det bra og at du hadde en fin bursdag. Jeg har det bare bra her i Trondheim. Har dere fint vær hjemme? Her i Trondheim er det ganske mye regn, men det gjør jo ingenting når jeg sitter inne og leser. Jeg kommer hjem til jul. Hilsen Jensemannen.
The text in Norwegian above is the content of the file being shown
Show content of files scrollable (less)
If you use cat on files longer than one terminal screen the text will just roll above the window and you can only read the end. If you use the command less you can scroll through the content instead. This command need one argument, the name of what file to show.
To move around in the file you use the following keys:
space one page down enter one line down b one page up j one line down k: one line up g Beginning of file G End of file /word search after word q quit
In addition to those there are many other functions that you can list out with the man command which is explained further down. On some very few machines you will not find less, but the command more which works the same way.
Show the start of a file(head)
This command shows the first 10 lines of a file. This command needs at least one argument, the name of what file to show. Jens has saved a song in the directory Brev/. He had named the file sang.txt, but he cannot remember what song it contained. An easy way to see that is to list the beginning of the file.
storm:~/Brev$ head sang.txt Neste Sommer - DeLillos [ NB! B = "norsk" H ] (D Bm Em F#) x 2 D Bm Em F# Det var en gang en sommer i nittenhundre og nittitre D Bm Em F# Hvor alt var saa behagelig og verden var discret
Based on this he can see that it contains some songs by the band Delillos.
If you want more or less that 10 lines you add the option:head -number_of_lines filename
Show the end of a file (tail)
This command shows the last 10 lines of a file. Like head it needs to know what file you want to open.
Jens wonders if "neste Sommer" is the only song in the file.
storm:~/Brev$ tail sang.txt Det var en gang en sommer i nittenhundre og nittitre hvor verden laa ufarlig langs Norges kyst et sted paa radioen var det et program om sommermat vi ruslet ned og tok et bad [refrenget to ganger til...]
This is the end of the same song we saw with tail, so the file only contains one song.
If you want more or less than 10 linestail -number of lines filename
Clear the window(clear)
This command removes all the text in the window, and displays the prompt like it is when you have just logged in.
Show how much drivespace you got left (diskkvote)
This command shows how much space you have been granted and how much you use. If you use too much it also shows what files that will be removed the next time the periodic deletion is run.
login:~$ diskkvote
--Disk quota for user jensj_
Your quota...............: 61440 KB
Checking directories....: /home/homee/je/jensj
: /local/www.stud/users/jensj
Diskusage................:32902 KB
You are below your quota of 61440 KB
If you have recently added many files this might be a good command to run to check your diskusage.
Personal use of drive space (diskusage).
This command shows how much space subdirectories and files in them use.
login:~$ diskusage 292 ./.netscape 172 ./public_html 164 ./public_html/bilder 12 ./Brev 0 ./Mail 0 ./bin 0 ./boksider 696 .
In total Jens uses 696 KB on the harddrive. The space each directory and file uses is displayed in the first row. That means the directory /public_html/bilder contains 164KB of data, and the whole of /public_html is 172KB. On the bottom of the row you see the total usage.
Manual pages (man)
With most commands manual pages are supplied. These you can access by typing man command, as in for the command cat you type man cat
storm:~$ man cat
CAT(1) FSF (GNU Text Utilities) CAT(1)
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [-benstuvAET] [--number] [--number-nonblank]
[--squeeze-blank] [--show-nonprinting] [--show-ends]
[--show-tabs] [--show-all] [--help] [--version] [file...]
DESCRIPTION
This manual page documents the GNU version of cat. cat
writes the contents of each given file, or the standard
input if none are given or when a file named `-' is given,
to the standard output.
OPTIONS
-b, --number-nonblank
Number all nonblank output lines, starting with 1.
Man pages are shown with the less command, so you move around like with the less command. More info about man pages can be found by typing man man
Man pages can be a bit overwhelming at first, but they are supposed to show all important info so they can be huge. The important parts of a man page are:
Synopsis What options the command accepts. The options are optional if they are marked with two []. Description What the command does. Options What the options is, there are usually a lot of them
Search for man pages (apropos)
If you know what to do, but not what command that has that function you can search for it.
storm:~$ apropos mail MIME (1) - Multipurpose Internet Mail Extensions Rnmail (1) - a program for replying via mail audiosend (1) - Send an audio email message cmail (6) - an email chess helper elm (1) - an interactive mail system filter (1) - filter incoming messages before adding to mailbox formail (1) - mail (re)formatter frm,nfrm (1) - list from and subject of selected messages hypermail (1) - convert mail archives in UNIX box format to HTML pages mailcap (4) - metamail capabilities file mailto (1) - Simple mutlimedia mail sending program mailto-hebrew (1) - Run the mailto program to send Hebrew/English mail messages (1) - quick count of messages in mailbox or directory metamail (1) - mailcap-based multimedia mail handling metasend (1) - Crude interface for sending non-text mail mimencode (1) - Translate to and from mail-oriented encoding formats pine (1) - a Program for Internet News and Email printmail (1) - format mail in a readable fashion for printing procmail (1) - autonomous mail processor ... osv 50+ lines more ...
You can then choose what command that might suit you.
Reserved symbols and their meanings.
In UNIX there are some symbols that have special meanings, these should not be used in names of directories and files. Important examples
? Every symbol, or no symbol * Any combinations of any symbols, like a vast number of ?. ; Separates between commands Space Separates commands, options and arguments. Tab Not allowed as a normal symbol (og teng) Sends the process to the background, se the chapter about processes. ( Groups commands ) Groups commands | Sends output of commands to the next command > Sends output to a file Takes input from a file
Compressing files (gzip)
This command can compress files so they take less space on the hard drive. In general terms you can say the files are coded to unreadable text which uses less space than the original file. To read the files again or work with them you need to uncompress them again. To save space there it is a good rule to compress files you want to keep but seldom use.
The command gzip is short for gnu zip, where gnu is name for a collection of free opensource programs. The command need one argument, what file to compress.
storm:~/Brev$ ls -l total 12 -rw------- 1 jensj ntnu 135 Jul 21 13:17 letterjorun.txt -rw------- 1 jensj ntnu 379 Jul 26 14:06 lettermom.txt -rw------- 1 jensj ntnu 1040 Jul 26 15:13 sang.txt storm:~/Brev$ gzip *.txt storm:~/Brev$ ls -l total 12 -rw------- 1 jensj ntnu 145 Jul 21 13:17 letterjorun.txt.gz -rw------- 1 jensj ntnu 268 Jul 26 14:06 lettermom.txt.gz -rw------- 1 jensj ntnu 457 Jul 26 15:13 sang.txt.gz storm:~/Brev$
We remember from earlier that * replaces any possible letters and the command will therefore be run on all files that end with .txt. the files that have been compressed now have the file ending .gz.
We see that lettermom and sang now uses less space on the hard drive, while letterjorun actually takes up more space. This is because the compression algorithm uses repetitions and it gets more effective the bigger the file is. On very small files like this one it serves no purpose.
Decompressing files (gunzip)
This command decompresses files that had been compressed with the command gzip. The command needs one argument, what compressed file to decompress.
storm:~/Brev$ gunzip *.gz storm:~/Brev$ ls -l total 12 -rw------- 1 jensj ntnu 135 Jul 21 13:17 letterjorun.txt -rw------- 1 jensj ntnu 379 Jul 26 14:06 lettermom.txt -rw------- 1 jensj ntnu 1040 Jul 26 15:13 sang.txt storm:~/Brev$
Now the files can be read and edited again, but takes up more harddrive space.
Archive files and directories to one file (tar)
This command enables you to archive several files and directories to one file. This can be very useful especially when you want to compress many files at a time. The program was originally used to copy date over to backup tapes and the name is a short for tape achiever. In normal use you always use the option f. Another quirk is the lack of "-" in front of options. More options are listed below.
Options
c Add files to archive x Extract files from archive t Show the content of an archive v Vocal mode, prints all steps it does f Archive to file not tape
Generally to archive more than one file to one archive you type: tar cvf filename.tar file1 file2 ... file9 ...
The option v is not needed, it just displays the things the tar program does.
storm:~$ tar cvf backup.tar Brev Brev/ Brev/lettermom.txt Brev/letterjorun.txt Brev/sang.txt storm:~$ ls -l Brev backup.tar -rw------- 1 jensj ntnu 10240 Jul 27 21:31 backup.tar Brev: total 12 -rw------- 1 jensj ntnu 135 Jul 21 13:17 letterjorun.txt -rw------- 1 jensj ntnu 379 Jul 26 14:06 lettermom.txt -rw------- 1 jensj ntnu 1040 Jul 26 15:13 sang.txt
It is easy to add more files to the archive
storm:~$ tar cf backup.tar public_html/bilder public_html/index.html
After you have made the archive it reasonable to test the content.
storm:~$ tar tvf backup.tar drwx------ jensj/ntnu 0 Jul 27 17:27 1996 Brev/ -rw------- jensj/ntnu 379 Jul 26 14:06 1996 Brev/lettermom.txt -rw------- jensj/ntnu 135 Jul 21 13:17 1996 Brev/letterjorun.txt -rw------- jensj/ntnu 1040 Jul 26 15:13 1996 Brev/sang.txt drwxr-xr-x jensj/ntnu 0 Jul 27 17:07 1996 public_html/ drwxr-xr-x jensj/ntnu 0 Jul 22 17:40 1996 public_html/bilder/ -rw------- jensj/ntnu 1920 Jul 16 13:46 1996 public_html/bilder/diskette.gif -rw------- jensj/ntnu 160335 Jul 22 17:40 1996 public_html/bilder/100acre.gif -rw------- jensj/ntnu 806 Jul 18 13:17 1996 public_html/index.html
To get the archive to take less space it would be reasonable to compress it.
storm:~$ ls -l backup.tar -rw------- 1 jensj ntnu 163840 Sep 7 21:45 backup.tar storm:~$ gzip -9 backup.tar storm:~$ ls -l backup.tar.gz -rw------- 1 jensj ntnu 158479 Sep 7 21:45 backup.tar.gz
In this occasion we saved almost nothing by compressing it. That is because the biggest part of the archive is picture files which is compressed already depending of the file type.
| Languages: | Comments: | 0 | Aid: | 310 | User rating: |
|
|
| Last updated: | 2007-07-02 | Author: | thomasan | Read: | 4267 times |