norman's blog

Notes of an amnesiac.
Never stop thinking.
Find an aesthetic description.

Monday, June 1, 2009

Hard Link and Symbolic Link

Symbolic link

# ln –s existing-file-name link-name
Also called a soft link or symlink — resembles a Windows shortcut. A symlink is a little file that contains the pathname of another object on the filesystem: a file, a directory, a socket, and so on — possibly even the pathname of another link. This pathname can be absolute or relative.
We can still edit the original file by opening the symbolic link, and changes we make doing that will "stick." But if we delete the symbolic link, it has no impact on the original file at all. If we move or rename the original file, the symbolic link is "broken," it continues to exist but it points at nothing.
Symlinks can point to files that are on different filesystems and can also point to directories.



Hard link

# ln existing-file-name link-name
It isn’t itself a file. Instead, it’s a directory entry. It points to another file using the file’s inode number. Means both have same inode number. Any changes to the original file will get reflected in the link file also as both are same.
Hard links should be on the same filesystem, and can not be pointed to directories.



To better understand the difference lets see how Linux manage files Each file is composed by two parts:

  • Filename:inode
  • Data
The inode contains some info like permissions and other info and also the address where the Data is.
Ok two different file names may point to the same inode, therefore to the same Data, this is know as hardlink. With the symbolic link, the filename points to a different inode and to a different data, but that data is actually a path to the referenced file, the OS recognize that this is a special file so instead of using the data of this file, it follows to the referenced file and acts on that file.
As you can see symbolic links needs an extra step to reach the pointed file.


NOTE:
1. Link can be created absolutely or relatively, depending on the target file path referenced.
2. Without broken link error, an absolute link can be moved solely, while a relative link can be moved together with target.
3. When creating a relative link, be ware that target-path(existing-file-name) should be regard standing at the path of the link, not the current path. To avoid error, just cd the path where you want to put the link.

Labels: ,

Read more!

Wednesday, May 13, 2009

BASH COMMANDS

ls--list files and folders in a directory (hierarchy)
-R: list subdirectories recursively
-a: do not ignore entries starting with.
-t: sort by time
-v: Interpret the digits in names such as file.6 and file.6.1 as versions, and order filenames by version.
find--search files in a directory hierarchy
search for files in a directory hierarchy, and relative paths of all found files are listed.
The syntax looks like this:
find where-to-look criteria what-to-do
Examples:
#find files named with suffix "cpp"                           
#in the current directory(max depth = 2) and delete them.
find . -maxdepth 2 -name "*.cpp" -delete 

# mv files older then 1 day to dir TMP
find . -atime +1 -type f -exec mv {} TMP \; 
see reference
grep--search pattern in the content of input file(s)
grep searches pattern in the named input FILEs (or standard input if no files are named). By default, grep prints the matching lines.
xargs
Build and execute command lines from standard input.
Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly. xargs passes these arguments in several bundles to command, allowing command to process more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe.
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or new‐lines are incorrectly processed by xargs. In these situations it is better to use the ‘-0’ option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the ‘-print0’ option does this for you.
#grep for pattern in the content of all files on the system:
find / | xargs grep pattern

#grep for pattern in the name list of all files on the system:
find / | grep pattern
Sed
stream editor for filtering and transforming text
ifconfig
dhclient
configure network interfaces using the DHCP protocol


2>/dev/null is not related to find tool as such. 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears. So 2>/dev/null in this case means that while finding for the files, in case any error messages pop up simply send them to /dev/null i.e. simply discard all error messages.

Labels:

Read more!

BASH TIPS

File Discreptor:

File input and output are accomplished by integer handles that track all open files for a given process. These numeric values are known as file descriptors. The best known file descriptors are stdin, stdout and stderr, with file descriptor numbers 0, 1 and 2, respectively. These numbers and respective devices are reserved.

Redirection:

<: redirect input 
>: redirect output
Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell.

Pipelines(|):

The output of each command in the pipeline is connected via a pipe to the input of the next command.

String Manipulation

see reference

Bath Math

See reference

Use "\" to make a command lay cross several lines in script

Labels:

Read more!

Wednesday, March 11, 2009

Execute Commands After You Exit From a Shell Prompt (by LinuxTitli)

nohup
Most of the time you login into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. However, if you logout the system, the job will be stopped. What do you do? nohup command
Answer is simple, use nohup utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell:
If you have created a background process and you log off, your process will be killed since it is associated with your shell. The nohup (no hangup) command allows you to keep processes running after you have logged off. The nohup command, is usefull if you have a process that needs to run all night or a few weeks.
nohup Syntax
# nohup command-name &
Where
  • command-name : is name of shell script or command name. You can pass argument to command or a shell script.
  • & : nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.
nohup command examples
  1. Login to remote server
  2. # ssh user@remote.server.com
    
  3. Execute script called pullftp.sh
  4. # nohup pullftp.sh &
    
  5. Type exit or press CTRL + D exit from remote server.
  6. # exit
    
  7. find&kill a process when you login again
  8. see reference
    # ps -ef | grep sleep
        scot  8419  4944  3 20:17:42 ttyp2    0:00 grep sleep
        scot  8416  4944  0 20:17:32 ttyp2    0:00 sleep 400
    # kill 8416
    [2]    Terminated             sleep 400
    #
    
run MATLAB in the Background on a Linux Server
see reference
# nohup matlab -nodisplay < YOUR_MATLAB_FILE.m > output.txt &

Labels:

Read more!