Bogotobogo
contact@bogotobogo.com
Type ps, then the command window shows you are running two commands: bash and ps.
We can delete a word we entered by typing cntr-w.
We can use apropos with a keyword to search for the name of a command. This searches for the keyword, and displays the line that contains the match from all man pages. If we use man -k, it provides the same output as apropos.
We can obtain root privileges with a command su (substitute user). The example below shows how to use su command:
The command uses su with -c (command) option. We used single quote (') to make sure the shell interprets the commands properly. After the execution of the command, the user does not have root privileges any more. We can see it by using su with no arguments, and what it does is exactly the same as below:
sudo also can be used to obtain root privileges. It requires us to enter our password, not the root password:
It did not succeed because the user is not allowed to do sudo. In this case, we can add the user to a group which can do sudo. For example, we need to add the existing user khong to a group wheel using:
usermod -g wheel khongThen, we can do as in the picture below.
bzip2 can be efficiently used when a file contains repeated information. As an example, I made a 1000 line file with a letter 'k'.
To display the compressed file, we can use bzcat, and it displays uncompressed data:
As we see, the file has shrinked to 49 bytes from 73,000 bytes.
We can put it back using bunzip2:
Standard output is a place a program can send information to. The program does not know where the information it sends to standard output is heading. It can go to a file, printer, or the screen. In this section, we will see, by default, the shell directs standard output from a command to the screen and explain how we can cause the shell to redirect this output to a file. Standard input is a place a program can get information from.
A device file is one of the types of file, and it resides in the /dev directory. It represents a peripheral device, such as keyboard, printer, disk drive, or screen.
The device name that the who displays after username is the filename of the screen:
[khong ~]$ who root tty1 2010-08-06 10:59 khong pts/0 2011-07-19 21:31 root pts/1 2011-07-19 19:06 khong pts/3 2011-07-19 22:21
So, in the above example, the device name is pts/3, and the pathname of the screen is /dev/pts/3.
[khong ~]$ tty /dev/pts/3 [khong ~]$
When we first log in, the shell directs standard output of our commands to the device file that represents the screen. The cat command copies a file to standard output. It's because the shell directs the standard output to the screen. Though we usually use cat with an argument, if we do not give cat an argument, i.e., hit RETURN, cat takes its input from standard input. So, it copies standard input to standard output, one line at a time.
The cat keeps copying text until we enter CONTROL-D. It sends an EOF signal to cat to indicate that it has reached the end of standard input and there is no more text to be copied. Then, it finishes execution and returns control to the shell so that the shell can display a prompt.
We can make the shell to redirect standard input/output to/from other than the device file representing the keyboard/screen. The redirect output symbol (>) tells the shell to redirect the out to the specified file rather than to the screen:
$cat > redirected_output_file_name
We can also redirect input using redirect input symbol (<):
$cat < input_file_name
The shell provides noclobber that prevents overwriting a file when we use redirection. In bash, we can enable this by set -o noclobber:
[khong ~]$ set -o noclobber [khong ~]$ echo "add more to myfile" > myfile -bash: myfile: cannot overwrite existing file [khong ~]$ set +o noclobber [khong ~]$ echo "add more to myfile" > myfile
There is also an append output symbol (>>) tells the shell to add to the end of a file.
/dev/null device is called data sink or bit bucket. We can redirect output that we do not want to /dev/null:
$echo "Go to bit bucket" > /dev/null
We can use a pipe to connect standard output of a command to a standard input of another command. The pipe has the same effect as redirecting standard output of one command to a file and then using that file as standard input to another command:
commandA args | commandB argsActually, what it does is:
commandA args > tmp commandB args < tmp rm tmp
The tee copies its standard input to both a file and to standard output. As the name suggests, it takes a single stream of input and sends the output in two directions. The tee saves a copy of standard input into a vmstat.out, and vmstat.out also sends a copy to standard output. Standard output from tee goes through a pipe to standard input of grep, which displays only those lines containing the string id
Let' briefly what the vmstat is downing.
The first row shows your server averages. The si (swap in) and so (swap out) columns show if you have been swapping (i.e. needing to dip into 'virtual' memory) in order to run your server's applications. The si/so numbers should be 0 (or close to it). Numbers in the hundreds or thousands indicate your server is swapping heavily. This consumes a lot of CPU and other server resources and you would get a significant benefit from adding more memory to your server.
The r (runnable) b (blocked) and w (waiting) columns help see your server load. Waiting processes are swapped out. Blocked processes are typically waiting on I/O. The runnable column is the number of processes trying to something. These numbers combine to form the 'load' value on your server. Typically you want the load value to be one or less per CPU in your server. The bi (bytes in) and bo (bytes out) column show disk I/O (including swapping memory to/from disk) on your server. The us (user), sy (system) and id (idle) show the amount of CPU your server is using. The higher the idle value, the better.
A link is a pointer to a file. Whenever we create a file, we're putting a pointer in a directory. There are two kinds of links: hard links and symbolic (soft) links. Hard links are becoming outdated.
A hard link to a file appears as another file. If the file appears in the same directory as the file link-to file, the links should have different name.
We use ln to create a hard link to an existing file using the following:
ln existing_file link_name
In the example above, there are two directories, folderA and folderB. The folderA has a file called myfileA and we made a link to that file in folderB with a link name link_to_myfileA.
A hard link is a pointer to a file and it's the directory entry points to the inode. But a symbolic link is an indirect pointer to a file, and the directory entry contains the pathname of the pointed-to file - a pointer to the hard link to the file.
Symbolic link can be used to create a link to a directory while hard link can not. Because Linux filesystem keeps separate control information (separate inode tables of filesystem structure) for the files it holds, it is impossible to create hard links between files in different filesystems. A symbolic link can point to any file but a hard link to a file must be in the same filesystem as the other hard link to the file.
A major advantage of a symbolic link is that it can point to a file that's not existing. This is useful if we need a link to a file that is periodically removed and recreated. A hard link keeps pointing to a removed file, which the link keeps alive even after a new file is created. On the other hand, a symbolic link always points to the newly created file and does not interfere with deleted old file. As an example, a symbolic link could point to a file that gets checked in and out under a version control system, a .o file that is re-created by the compiler each time we run make.
But symbolic link has some disadvantages. While all hard links to a file have equal status, symbolic links do not have the same status as hard links. Having several hard links is the same as having several legal names while soft links are link aliases.
To create a symbolic link, we use ln with --symbolic or (-s) option.
Note that the size and times of the last modifications of the two files are different. A symbolic link to a file does not have the same status information as the file itself. We can also use ln to create a symbolic link to a directory.
I used rsync to copy about 3TB to NAS (Network Attached Storage). Source side has /data partition and target side has /data2 share. The /data2 has been mounted on old storage server using the following commands:
mkdir /data2 mount -t nfs 192.168.100.54:/data2 /data2To prevent sending HUP signal during the copying process, I used it with nohup, and ran it background. Here is the rsync syntax for copying files from /data/mydir to /data2/mydir:
nohup rsync -a mydir /data2 &
It worked perfectly, and the output is stored in the default nohup.out. When I need to update /data2/mydir, I just re-issued the same rsync command, and it copied only the new files.