Linux Nano-FAQ

This document aims to help people who are new to the UNIX world, and who may have some previous DOS or Windows background. It is a list of answers to questions that seem really silly at first, but which inevitably come up when using a UNIX for the first time. This document is not about graphical interfaces—it only explains the text console interface to Linux. If you are interested in more complicated topics, I recommend the UNIX FAQ.

Note: This FAQ is not related to the Nano text editor. The word nano simply refers to the list of very “small” (or simple) questions.

This FAQ is written primarily for Linux, but many answers are also true under other UNIX operating systems. (In fact, the terms Linux and UNIX have the same meaning in this document.) This FAQ is a work in progress, and I plan to add more questions and answers later. If you notice any errors, or if you have comments or suggestions, please feel free to contact me. My e-mail address can be found on my home page.

Current revision of this file: October 3rd, 2004

The latest version of this document can be found at: http://home.kianga.eu/doc/2004/10/3/linux-faq

Contents

  1. General Questions
    1. What does the command … do?
  2. File and Directory Management
    1. How do I list the contents of a directory?
    2. What do the file and directory permissions mean?
    3. How do I change the current directory?
    4. How do I copy a file?
    5. How do I move a file?
    6. How do I rename a file?
    7. How do I delete a file?
    8. How do I use wildcards to handle multiple files?
    9. Why can’t I rename all files with one extension to another?
    10. How do I edit a file?
    11. How do I display the contents of a file?
    12. How do I create a directory?
    13. How do I delete a directory?
    14. How do I change the access permissions of a file?
    15. What is a symbolic link and how do I make one?
    16. What is a hard link and how do I make one?
    17. How do I combine multiple commands in one line?
    18. How do I handle archives and compressed files?
  3. Handling and Manipulating Text Files
    1. How do I search for a word in one or more files?
    2. What is piping?
    3. What are stdin and stdout?
    4. How do I replace all occurences of a word in a file with a different one?

General Questions

What does the command … do?

The most important thing to know under Linux/UNIX is that almost every command has a so-called manpage (short for: manual page). To see the manpage for a command, simply type: man command

To see a list of commands that might be related to a certain topic, you can type: apropos topic

File and Directory Management

How do I list the contents of a directory?

The command for listing a directory’s contents is ls.

By default, this lists only the file and directory names, but nothing more. If you want to know more, you can type ls -l to get a long listing. This will show you the permissions, the number of hard links to the file, the owner, group, size, last modification date, and the name of the file.

What do the file and directory permissions mean?

When you type ls -l, you see output like the following:

-rw-r--r--   1 root     root          578 Apr 15 12:58 passwd
lrwxrwxrwx   1 root     root           13 Oct 22  1999 utmp -> /var/log/utmp
drwxr-xr-x   2 root     root         4096 Oct 21  1999 vga/
-r-xr-xr-x   1 root     bin        102724 Oct  9  1999 telnet

The first group of ten letters are the permissions for this file. The first letter tells you what kind of file you are looking at:

Special files are usually found in the /dev directory. These are devices which either let you access some kind of hardware or perform other special functions. For example, a simple way to print a document is to send it to the device /dev/lp0 like this: cat filename > /dev/lp0

Sockets and pipes are more advanced concepts and beyond the scope of this FAQ.

The following nine letters are the actual permissions. They are grouped in three blocks, each three letters long: The first group defines the permissions for the owner of the file, the second group for the group of the file, and the last group for everyone else. Here are their meanings:

Again, take a look at the example:

-rw-r--r--   1 root     root          578 Apr 15 12:58 passwd

This tells us that the file passwd is a regular file (-); it is readable and writable (but not executable) for the user root, readable for the group root, and also readable for everyone else.

Depending on the type of file, these permissions sometimes have special meanings:

For directories, read permission means that you can list the contents of the directory. Write permissions means that you can create and delete files in this directory, and execute permission means that you can change into it. Note that read and execute permissions for directories are quite different: When a directory is readable, then you can only see the names of the files in it. You cannot read them, you cannot even see what permissions or modification dates they have, and you cannot type cd to change into this directory.

On the other hand, if your directory is just executable, but not readable, then you can change into it, and you can access any file in it according to its own permissions, but you cannot see the list of files in that directory. In other words, you must know the exact name of the file you are accessing. ls -l will not work here.

The sticky or setuid bit has a special meaning: If it is active, then it appears instead of the execute bit. An example:

-rwsr-xr-x   1 kianga   users           0 Apr 30 14:40 setuidexec
-rwSr--r--   1 kianga   users           0 Apr 30 14:39 setuidfile

The first file named setuidexec is readable and executable for everyone, and writable only for the user kianga. However, instead of an x there is a s in the owner permissions (highlighted in bold). If it is a lower case s, this means that the execute bit is also set. If it is a capital S, then the execute bit “below” is not set. The same can appear in the permissions for the group of the file or for “everyone else”.

So, what does this magic bit mean? For an executable file, it means that when the program is executed, it will be executed not by the user who started it, but by the user (or group) who is listed as the owner of the file. Thus, a file with permissions -rwsr-xr-x is called a setuid file (because it sets its own user-id), and a file with -rwxr-sr-x is called setgid (because it sets its own group-id). For the “other” group of permissions, it does not make any sense at all and has no effect.

There is another meaning for this bit when it is applied to a directory: When it is in the user permissions, it should mean that every file created in that directory will have the same user-id as the owner of the directory. For security reasons, this does not work, since it would enable you to create files that belong to a different owner than you. It does work on the group permissions though, setting the group of every file to the group of the directory. This can be useful if you want all files created in a directory to belong to the same group.

Finally, the so-called sticky bit has the following meaning: Imagine a directory where everyone has write permissions, so everyone can create files in it. This is often the case for the directory /tmp, which is a place to store temporary files. However, since everyone has write permissions, it also means that everyone can delete any file in it, even files owned by other users. The sticky bit prevents this: if it is set, you can only delete files in this directory if the files belong to you, even if you have write permissions for the directory.

How do I change the current directory?

This is the same command that you probably know from DOS: cd.

…except that you use slashes where you used backslashes. For example: cd /home/foobar brings you to the user foobar’s home directory. To change to your own home directory you can simply type cd and hit return. When you navigate through the filesystem, you can often use the shell’s filename completion feature; to change to the directory /i-hate-extremly-long-directory-names/and-dont-want-to-type-them-all you could type /i-hate<tab>and<tab><return>.

You can also type cd ~foobar to change to the home directory of the user foobar.

How do I copy a file?

With the command cp.

cp file1 file2 copies file1 over file2. cp file1 /dir2 places a copy of file1 in the directory /dir2. You can also copy multiple files: cp file1 file2 file3 file4 /dir copies all the files to the directory /dir. Be careful with this command since all files are copied into the last one. If the last parameter is a file, all given files might be copied into the last one overwriting it. Thankfully, most UNIX utilities are smart enough to detect this and will abort with an error instead, but if you value your files you should not count on it. Always keep backups!

To copy a whole directory with all contents, you can type cp -R directory /new/directory. This copies all files, and makes you the owner of the copies. To preserve all owners and permissions (only works as user root), use cp -ax. However, this trick will only work with the GNU version of the cp command.

How do I move a file?

The command for this is mv.

Moving files works similar to copying them (see the section above). A little difference is that you can just type mv dir1 dir2 to move a directory with all its contents. Moving files is usually a lot faster because the operating system can just modify the directory entry of the file to reflect a new location, instead of having to copy all of its data.

How do I rename a file?

The same way you move a file: with mv.

When you think about it, renaming a file isn't much different than moving it. The designers of UNIX thought the same, so you can simply use the mv command like this: mv oldname newname

How do I delete a file?

Deleting files works with rm.

The command will remove all files you pass as parameters. rm file1 file2 file3 will remove the three files. If you do not want to hear any questions whether you really want to delete a file, type: rm -f and remember to think at least twice before you press RETURN.

How do I use wildcards to handle multiple files?

Using wildcards under Linux is almost the same as under DOS.

To delete all JPEG files in a directory, you can simply type: rm *.jpg. One difference is that you do not have to type *.* when you mean all files, a simple * is sufficient. Under UNIX, file extensions do not get any special treatment, they are simply part of the file name.

Another wildcard is ?, which matches just a single letter. To delete the files cat and car, you can type: rm ca?

Be careful with wildcards though: read the next question first!

Why can’t I rename all files with one extension to another?

Because wildcards under Linux work differently than under DOS.

You probably tried something like cp *.txt *.bak. This will not work as expected. To understand wildcards under Linux, you must know that the whole magic for wildcards is done by the shell (the program that waits for commands), not by the actual command (like cp) that you execute. This is different from DOS, where every command must handle wildcards itself.

When you type a command under Linux, your shell first scans it for any wildcards. It then looks at your directory and replaces these wildcards with the actual file names, and only then the command is passed to the actual program. So, suppose you have the files a.txt, b.txt, and c.txt and then type the command: cp *.txt *.bak

The following happens:

  1. The shell scans your command for wildcards.
  2. It searches for files matching *.txt and finds a.txt, b.txt, and c.txt. It replaces *.txt in the command with the text: a.txt b.txt c.txt.
  3. Next, it searches for files matching *.bak and finds none, so the shell just keeps the wildcard unchanged.
  4. The resulting command is: cp a.txt b.txt c.txt *.bak.

As you can see, this command actually copies all files matching *.txt to a file named *.bak.

One way to accomplish the above task would be:

for file in *.txt; do cp $file $file.bak; done

Note that this will probably only work under the bash shell. A much simpler and more compatible way would be to create a tar archive of the desired files with: tar -cf backups.tar *.txt. To extract the files from the archive again, you would type: tar -xf backups.tar. By adding the z option to tar (i.e. -czf or -xzf), you can get compressed archives as well. The secret to using a UNIX effectively is to use the right tool for each job.

How do I edit a file?

Quick answer: Try pico filename.

There are many editors under Linux. Here is a short list of the most popular ones:

My suggestion for unexperienced users is to use either pico or nano.

How do I display the contents of a file?

Type: cat filename

If you just want to see the first few lines, use head filename. For the last few lines, there is the command tail.

If the file is too large and does not fit on your screen, you can use a pager program. Type less filename, or if that does not work, try more filename.

How do I create a directory?

Just like under DOS: mkdir directory

How do I delete a directory?

You can either use rm or rmdir.

To delete an empty directory you can use rmdir dir. The command will not work if the directory contains any files. The command rm -r will delete recursively, meaning rm -r dir will delete the directory and all of its contents. If there are write protected files in the directory you will be prompted whether you want to delete them. If you want to delete a directory which contains write protected files and you are prompted a couple of hundred times, you can use rm -rf dir which will forcefully delete the directory, no questions asked.

Be careful with these commands, especially when you are root: rm -rf / will completely eradicate your beloved Linux installation, without even a single confirmation.

How do I change the access permissions of a file?

The command for this is: chmod

chmod is rather intuitive. Here are some examples:

To change the permissions for a directory and all of its contents, use: chmod -R

There is another little trick that can come in handy: Imagine you have some files that are readable only by you. Some of them are also executable. Now you want to make them readable by all, and if they are executable, also executable for all. If you write chmod a+rx *, then every file will be executable. However, if you write chmod a+rX (note the capital letter X) then only those files that are already executable for you also become executable for everyone.

What is a symbolic link and how do I make one?

A symlink is a concept similar to a shortcut under Windows. It is like a sign that points to another file. To create a symbolic link to a file type: ln -s originalfile linkname

Some examples. Take a look at this directory listing produced by ls -l:

-rw-------   1 kianga   users       72534 Apr 30 16:40 myfile
lrwxrwxrwx   1 kianga   users           6 Apr 30 16:41 hello -> myfile
lrwxrwxrwx   1 kianga   users          11 Apr 30 16:41 passwd -> /etc/passwd

We have one file here and two symlinks (in the lines starting with an l for link). As you can see, the link hello points to the file myfile. Indeed, if you typed cat hello, you would get the contents of the file myfile. The link passwd points to the file /etc/passwd in a different directory.

Note that the permissions for a link are always rwxrwxrwx. This does not mean that you can write to the file to which the link points; a link itself simply does not have any permissions. Only the permissions of the actual file matter.

What is a hard link and how do I make one?

The short answer: You will probably never need to create a hard link.

However, taking a look at hard links is interesting because it offers some insights into the inner workings of the filesystem.

Every file on a Linux partition has a unique number, called the inode number. To see which inode number a file has, you can type ls -li:

 358274 -rw-------   1 kianga   users     7397380 May  2 10:19 TLK3-CTV.mpg
 358226 -rw-------   1 kianga   users       17645 May  2 21:52 linux-howto.html
 650266 drwx------   2 kianga   users        4096 Apr 29 21:30 lojban/

As you can see, there is an additional number before the permissions of each file. This is, as you will have guessed, the inode number. You will also notice that even the directory (called lojban) has an inode number, which shows us that directories seem not to be much different from ordinary files under Linux.

In fact, this is not so far from the truth. You can actually think of a directory as list of inode numbers that make up all files in it. In the example above, the current directory consists of the files with the numbers 358274, 358226, and 650266. (Actually, there are always two more entries with the names . and .. that contain the inode number of the directory itself and the number of the parent directory, respectively. You can see them if you type ls -lia.)

So, if every file has its own inode number, and a directory is just a listing of these numbers, wouldn’t it be possible to have entries for the same inode number in two different directories? In fact it is. This is what is called a "hard link", even though the name is misleading. Both directories would list the same inode number for the file, but there is no way to determine to which directory it really belongs. Thus, you could say that every file that is displayed in some directory is actually a hard link, and there is no such thing as the "real location" of a file. A file is just an inode number. However, you can’t access a file by its inode number, you have to go through a directory entry.

To demonstrate our hypothetical experiment, let's try something. To make a hard link, we use the same command as for symbolic links, but this time we omit the -s option. Thus, to create another entry for the file called linux-howto.html in the example above, we would type: ln linux-howto.html test.html. Let's see what ls -li says now:

 358274 -rw-------   1 kianga   users     7397380 May  2 10:19 TLK3-CTV.mpg
 358226 -rw-------   2 kianga   users       17645 May  2 21:52 linux-howto.html
 650266 drwx------   2 kianga   users        4096 Apr 29 21:30 lojban/
 358226 -rw-------   2 kianga   users       17645 May  2 21:52 test.html

Look at the inode numbers of the two HTML files: they are identical. Both entries in this directory refer to the same file. Maybe you have noticed that something else has changed too: The number next to the file permissions has changed from 1 to 2, compared to the previous listing. This number is the reference count of the inode. It means that there are now two directory entries that point to this inode. Linux must do some important bookkeeping to make sure that the reference count for every inode is correct. Why?

Let us see what happens when we delete the original file. If Linux deleted the actual file (inode), then we should get some kind of error if we try to access the other entry for it we called test.html. Let’s try it: rm linux-howto.html

 358274 -rw-------   1 kianga   users     7397380 May  2 10:19 TLK3-CTV.mpg
 650266 drwx------   2 kianga   users        4096 Apr 29 21:30 lojban/
 358226 -rw-------   1 kianga   users       17645 May  2 21:52 test.html

The other file is still there. We can even display or edit its contents. But maybe you noticed that the reference count has changed back to 1. Linux keeps the actual data of the file (the inode) as long as its reference count is at least one. If it becomes zero—that is: if the last directory entry for it gets deleted—then it well be deleted as well.

As you can see, hard links are a very low-level filesystem feature that can be rather confusing. You should resist the temptation to create your own hard links (except for testing purposes maybe). In practically every situation, a symbolic link will do the job. If you should ever need to create a hard link, then you probably won’t need this FAQ anymore. But nevertheless, it doesn’t hurt if you know what a hard link is.

By the way, you can think of a symbolic link as a text file that simply contains the name of its target. You will even notice that the size of a symlink is exactly the same as the length of its target name. Linux is a fascinating thing, isn’t it?

How do I combine multiple commands in one line?

Use a semicolon to seperate them.

This may not be true for every shell, but at least under bash there are some ways to change the way the shell executes your command:

If you just want two or more commands to execute sequentially (after each other), you type: command1; command2

If you want the second command to be executed only if the first command succeeded, type: command1 && command2 (read: command1 AND command2)

If you want the second command to be executed only if the first command failed, type: command1 || command2 (read: command1 OR command2)

Finally, if you want to execute a command in the background, allowing you to continue working, type: command1 & If you want to get the command back in the foreground, you can type: fg.

How do I handle archives and compressed files?

To extract a plain tar archive, you type tar -xf filename.tar. To extract a gzip (.gz) compressed tar archive, tar -xzf filename.tar.gz.

Under Windows, you usually use ZIP (PKZip, WinZip) for making archives of files. Under Linux, there are two programs for this task. One program, called tar, is responsible for packing multiple files together into one file. It doesn’t do anything else, not even compression. For this you use a second program, usually gzip or bzip2. This doesn’t make much difference in the end, but it is a good thing to know.

To create plain and uncompressed archive, you type: tar -cf filename.tar [filenames]. If you would like tar to compress the resulting file afterwards, you can add either a z (for gzip) or a y (for bzip2). For example, to create a gzip-compressed archive, type: tar -czf filename.tar [filenames]. Also note that the difference between gzip and bzip2 can be really noticable: While gzip compressed files are usually bigger, it takes a lot more time to compress a file with bzip2.

Handling and Manipulating Text Files

How do I search for a word in one or more files?

Use the grep command. Type: grep word filename(s)

What is piping?

Piping is another way to pass data to a program.

Usually you execute a program and give it a file on which it should perform some action. Now suppose you want a file to be processed by multiple programs in order to get some result. You could manually start each program in turn, or you can use pipes: Like water in a pipe, you can let the data “flow” directly between two or even more programs without any intermediate files.

One example: Imagine you have a large file consisting only of addresses, each of them packed into a single line. A line in this file could look like this:

John Doe, 123 Somewhere Street, Nowhere City

Now you want a list of everyone who lives in Nowhere City. This is easy; you can use the grep command like this: grep "Nowhere City" addressfile

Unfortunately, the list of people in Nowhere City is long. Probably several pages. It would be nice if we were able to scroll through it like we can scroll through a text file with the command less. So why don’t we use less? But less wants a file name. We would need to get the list of names in Nowhere City, write it to a file and then display it with less.

This is where pipes come in handy. You can feed the output from your grep command directly to less. Here is how you do it: grep "Nowhere City" addressfile | less

The output from grep is now “piped through” less. You can do this as often as you want. For example, you can sort the list with the command sort. So, in order to get a sorted list, we type: grep "Nowhere City" addressfile | sort | less

This means: Search for the text "Nowhere City" in the file addressfile. Then take the output and sort it. After that, display it in the pager. Easy, isn’t it?

What are stdin and stdout?

These are the channels through which programs accept or output their data when they are not reading from a file. stdin stands for Standard Input, and stdout, as you will guess, stands for Standard Output.

If you start a program like cat, grep, or less (also most other UNIX programs) without a file name to process, they will read their data from stdin. By default, this means that they read their data directly from your keyboard. You can try this with the grep command. Type grep keyword, and the program will wait for user input. If you type a line that contains the keyword, the line will be printed again. This is because the program reads from stdin (your keyboard) and writes to stdout (which means your screen).

The good thing about this is that you can redirect these channels. For example, you can tell the program that it should take its standard input from a file and put its output into a different file. To output all lines of a file containing a certain word into another file, you can write: grep keyword < myfile > myoutput. This will produce a file called myoutput that contains only those lines of myfile that contain the keyword.

Note that piping is just another way of redirecting the standard input and output channels. A similar command to the above is: cat myfile | grep keyword > myoutput

This uses the cat command to print the contents of the file to stdout, which is redirected to the grep command, which in turn outputs its data into the file myoutput. You can construct very powerful commands this way—most utilities under UNIX can be used in pipes.

How do I replace all occurences of a word in a file with a different word?

Type: cat filename | sed s/oldword/newword/ > newfilename

sed stands for Stream Editor. It is a very powerful command that is much too complicated to be described here. If you would like more details, look at the manpage for sed.


This is Kianga’s Spot – a private, non-commercial home page. Feel free to contact me with any questions. See the Impressum for important disclaimers and contact information required by German law. German flag