Learning Linux Part 3/15: Working at the shell
Part 3 of the series...! This one is about the commandline, files and file actions.
This time covers:
- special characters at the commandline
- common linux folders
- file types in linux
- searching text and files
- copy, move, destroy and create folders
- some other commands...
- creating text files with vi(m)
- settings in vi(m)
SPECIAL CHARACTERS AT THE COMMAND LINE
At the shell some characters have special functions. For example the asterisk (*) is used as a wildcard. Here are some more characters with their exact function:
( * ) - Character to be used for any number of characters. You can also use multiple instances of this character, then it's search results will be limited. For example looking for a file called "beta27" and "bABCDEFGtHIJKLM27" in the directory /home/user can be done using find /home/user -name "b*t*27"
( ? ) - Can be used as a wildcard for one character. ls -l b?ta will display all files starting with a "b", followed by any character and ending at "ta".
( [ ] ) - Characters between these characters can provide additional characters. For example "ls -l file[sd]" will return both "files" and "filed".
( ~ ) - This character represents the current logged in users home directory
( - ) - This represents the previous directory. This might become handy when switching between 2 directories.
Like Windows has My Documents, Program Files, etc. Linux also has it's LSB ( Linux Standards Base ) which describes which files should reside in which folder.
| /bin | Contains commands needed for the booting of the computer. This folder is accessible for users. |
| /sbin | This folder holds all system files which are used to manage the system. Some examples are shutdown, fdisk, etc. |
| /etc | Most (system)applications have their configuration files located here. |
| /lib | Shared library files can be found here. It's mostly the same as DLL files on a windows machine. |
| /dev | Folder that contains all the device files. Like /dev/hd, /dev/sda, /dev/usb, /dev/cdrom |
| /var | contains files with a variable size like log files (for example /var/log/messages). |
| /home | contains all home directories. |
| /boot | contains the files needed to boot a machine. Like the kernel, initrd, etc. |
| /opt | folder which is used in most cases to store large applications, like KDE, openoffice, etc. |
| /proc | folder which holds all processes. This folder does not contain files, but references to processes. |
| /usr | contains user related files and (sub)folders. |
| /usr/bin | Contains the most commands and applications that can be used by users |
| /usr/etc | contains configuration files for applications in /usr/bin |
| /usr/lib | contains shared library files for applications in /usr/bin |
FILES
A linux filesystem makes distinction between executable files and non-executable files. Executable files are files which have the execution permission bit set. If you'd like to know what the contents of a certain file is, you can use the command "file" followed by the file you would like to investigate.
An example:
ferdinant@overloader:~$ file /etc/passwd
/etc/passwd: ASCII text
LINKS
A link is a reference to another file or directory. Linux does know 2 type of links:
- Hard links
- Soft links
More about links in another lesson.
SOCKET AND FIFOS
Sockets and fifos are files which are created dynamically, which also are deleted dynamically, to exchange information between processes.
If you are looking for a special text phrase in a directory with files, you can use the command "grep". Grep has several options to scan through files for a given pattern.
To only view the contents of a file, you can use the commands "cat, tac, head, tail, more and less". Check their man pages for more information.
If you want to search for a certain file you can use the commands "locate" and "find".
COPY, MOVE, DESTROY AND CREATE FOLDERS
To copy a directory you can use the "cp" command. This command has several extra options, a few of them are:
-v | gives verbose output
-r | copy directories recursively ( means that it will also copy subfolders )
-a | archive ( equal to --dereference --preserve and --r(ecursive) )
-i | interactive ( prompt before overwrite )
Moving files and directories can be done using the "mv" command. The options are almost equal to "cp".
Destroying a (empty) directory can be done using "rmdir". If the directory is not empty, then it can be deleted using "rm -r" which recursively removes the directory and all it's content.
Creating folders can be done using the "mkdir" command. An easy to use option is the "-p" option, which creates all parent directories if they do not exist.
SOME OTHER COMMANDS....
Here are some other commands which might become usefull in your daily sysadmin tasks:
cal | returns a simple calendar. First argument give is a month and/or a year.
date | when no arguments given, it gives back the current set time. Also let you adjust the date/time of your computer.
free | Shows how many memory is available
CREATING TEXT FILES WITH VI(M)
The text editor vi has 2 modes available:
1. Input Mode
2. Command mode
In the Input mode, you can type text into the file. The command mode enables you to perform commands in vim like replacing text, save the file, perform an undo, etc.
Saving a file with vi can be done in the command mode using ":w". If you also want to exit vim you can combine the command by adding a "q", which makes it ":wq". If you did not modified anything, you can exit vim using ":q". And if you did edit something, but would you like to quit without saving, you can add an "!" character. ( ":q!" ).
Some other handy commands in vi are
a | append text to the place where the cursor is.
i | insert text to the place where the cursor is.
o | open a new line and add text there
x | delete the character at the cursors position
d$ | delete text from the character till the end of the line
dd | delete the whole line
dw | delete from the cursor untill the next word
Showing line numbers can be done using ":se number" ( undoing this with nonumber instead of number ).
Search, Cut and Paste. Searching for a certain text phrase can be done using a slash followed by the searchphrase.
( For example: /word )
Cutting can be done by just deleting the lines. This will put the deleted text into the buffer of Vi(m). If you would like to paste the cutted text you can simply use the "p" (paste) character on you keyboard.
If you want to copy text from a text file, you can use the the option starting with a "d" mentioned earlier. But, instead of the you can use the "y" character.
SETTINGS IN VI(M)
Some settings that might become usefull while editing in vi(m) are:
ai | autoindent
eb | error bells - plays error sounds when an invalid command is used
nu | shows line numbers
sm | showmatch, shows the corresponding brackets.
smd | show mode, shows in which mode you are
warn | shows a warning when you want to quit vi(m) without saving