Tuesday, 2 February 2016

Soft Link/Hard Link

We are using the 'pwd' and 'ls' command in our everyday life. Here I will discuss some interesting options we rarely use with them. If ur in doubt at any point please refer the man pages.
pwd command:
-------------------------------------
Basically it prints the present working dir.
Now lets use pwd with some useful options.
[root@localhost tmp]# ln -s /tmp/test/ temp
[root@localhost tmp]# ls -l temp
lrwxrwxrwx. 1 root root 10 Jul 19 04:40 temp -> /tmp/test/
Here I created a symnolic lik named 'temp' for the folder '/tmp/test'.
For more info on symlinks and hardlinks, please refer
--
http://www.geekride.com/hard-link-vs-soft-link/
--
[root@localhost tmp]# cd temp/
-L (logical) Use PWD from environment, even if it contains symbolic links
[root@localhost temp]# /bin/pwd -L
/tmp/temp
-P (physical) Avoid all symbolic links
[root@localhost temp]# /bin/pwd -P
/tmp/test

that with -L option it print working directory from environment even if it contains symlinks. So it prints the current dir i.e /tmp/temp. However, -P print actual physical current working directory by resolving all symbolic links i.e /tmp/test.
ls
===
Basically it list the files in the dir.
When using -F an asterisk (*) means the file is executable, at sign (@) indicates a symbolic link, an equal sign (=) is a socket, and a pipe or vertical bar (|) is a FIFO. You can refer the following link to know more about 'FIFO'
[root@localhost ~]# ls -F /var/run/cups/cups.sock
/var/run/cups/cups.sock=
[root@localhost ~]# touch test
[root@localhost ~]# chmod +x test
[root@localhost ~]# ls -F test
test*
[root@localhost ~]# ln -s testing test
[root@localhost ~]# ls -F test
test@
[root@localhost ~]# mkfifo a=rw MYFIFO
[root@localhost ~]# ls -F MYFIFO
MYFIFO|
-S option is used for sorting file by size.
[root@localhost ~]# ls -lS
total 68
-rw-r--r--. 1 root root 41433 Jun 17 17:49 install.log
-rw-r--r--. 1 root root 9154 Jun 17 17:47 install.log.syslog
drwxr-xr-x. 2 root root 4096 Jul 19 04:32 dir
-rw-------. 1 root root 3332 Jun 17 17:49 anaconda-ks.cfg
lrwxrwxrwx. 1 root root 7 Jul 19 04:21 test -> testing
prw-r--r--. 1 root root 0 Jul 19 04:24 a=rw
prw-r--r--. 1 root root 0 Jul 19 04:24 MYFIFO
ls -a shows hidden files. However, 'ls -a .*' doesn't work as indended.
Note
-----
* matches any string, including the null string, ? matches any single character. Characters enclosed in [ ] specify a list or range of characters, any of which will match.
[root@localhost ~]# mkdir example
[root@localhost ~]# cd example/
[root@localhost example]# touch .foo ..foo ...foo
[root@localhost ~]# cd example/
[root@localhost example]# ls .*
.foo ..foo ...foo
.:
..:
anaconda-ks.cfg dir install.log MYFIFO
a=rw example install.log.syslog test
Now here is the problem.
The problem is that .* is expanded to include . and .., which are then both displayed. Instead of getting just the dot files in the current directory, you get those files, plus all the files and directories in the current directory (.), all the files and directories in the parent directory (..), and the names and contents of any subdirectories in the current directory that start with a dot. This is very confusing. Here it prints the contents in 'example' dir as well as its parent dir.
let try with the option 'd' with 'ls'
[root@localhost example]# ls -d .*
. .. .foo ..foo ...foo
Now we will match file only starting with '.'
[root@localhost example]# ls -d .[!.]*
.foo
You can verify the same the echo command
[root@localhost example]# echo .[!.]*
.foo

[root@localhost example]#
.[!.]*
is a filename expansion pattern where [] denotes a list of characters to match, but the leading ! negates the list. So we are looking for a dot, followed by any character that is not a dot, followed by any number of any characters
Similarly
[root@localhost example]# ls -d .??f*
...foo
[root@localhost example]# ls -d .?f*
..foo
[root@localhost example]# ls -d ..?f*
...foo
[root@localhost example]#
You can use echo * as an emergency substitute for ls if the com-
mand is corrupt or not available for some reason. This works because is expanded by the shell to everything in the current directory, which results in a list similar to what you’d get with ls.
I have only included a few options. If ur curious enough, u can always refer the man page for these commands.
For any suggestions,questions,problems regarding this post, please feel free to share.

No comments:

Post a Comment