<<

1. Does the rename a file or move a file? It actually does both, depending on the context. If the source and target are both files, it does rename. If the source is a file and the target is a , it does move. However, once we understand the internal working of the mv command with respect to inodes, we will realize both are same.

2. Does moving a file to a directory using mv command create a new file and copy contents to it OR just move the file itself? It depends on whether the target location is in the same filesystem or a different . If within the same file system, it is simply a move, else the mv command creates a new file and copies the contents to it from the old file and deletes the old file.

3. Why does create and copy when the target directory belongs to a different file system? In case, if the file is simply moved to the target directory as is, it may so happen that the target file system alread has a file with the same inode number as that of our source file. And hence create a new file and copy contents.

4. What is wrong if a file already exists in the target FS with the same inode number? No 2 files can have the same inode number within a file system(except hard linked files).

5. When I move a file to a different directory, does the inum still remain the same? and No. As long as the directory being moved to is within the same file system, the inode number remains the same. Inode number changes on moving the file to a different file system. a) Moved to a directory in same filesystem: $ -li file 9962605 -rw-r--r-- 1 guru users 98 Jul 6 15:11 file $ mv file bin/file0 $ ls -li bin/file0 9962605 -rw-r--r-- 1 guru users 98 Jul 6 15:11 bin/file0 b) Moved to a directory in a different filesystem: $ ls -li file1 9962607 -rw-r--r-- 1 guru users 19 Jul 5 17:37 file1 $ mv file1 /tmp $ ls -li /tmp/file1 6161903 -rw-r--r-- 1 guru users 19 Jul 5 17:37 /tmp/file1

6. In case of renaming a file from say f1 to f2, does the inode number change? No. The inode number does not change when a file is simply renamed. $ ls -li file 9962606 -rw-r--r-- 1 guru users 98 Jul 6 15:11 file $ mv file file0 $ ls -li file0 9962606 -rw-r--r-- 1 guru users 98 Jul 6 15:11 file0

7. Say, within a directory, if a file is renamed, what exactly changes in the inode information of the source file? Nothing changes in the inode information of the source file. This is because only the file name changes as part of the rename, and the file name is not part of the inode information all. The mapping of the inode number with the file names happen at the parent directory level. In this mapping, the file name is updated against the inode number.

8. Is the directory also is associated with an inode number? Yes, the directory is also a kind of file, a special file(Everything is a file in Unix, they say). One of the attributes of inode is the of file tells whether it is a file, directory, socket, , etc. The directory also has an inode number, and all the inode related information such as the permissions, size, owner, the group, etc.. are stored in the inode of the directory.

9. Is there any difference between the inode info contained for a file and a directory? One of the data presented in the inode is the pointer to the data blocks which is the pointer to the location containing the data belonging to the file(stat command). In case of a directory, the data in the data blocks will contain the mapping of the file names to its inode numbers, which is typically the info present in a directory. Let us consider a directory "xyz". The inode number of "xyz" is 113 $ ls -lid xyz 113 drwxr-xr-x 4 root root 1024 Jun 27 12:15 xyz/ A typical directory mapping structure of "xyz" will look like this:

Inode 113 . 113 .. 13 bin 610723 temp 610789

As seen, the directory information contains the first 2 entries : one for the current directory(.), and one for the parent directory(..). Followed by these 2 will be an entry for each and every file present under the "xyz" directory. Note: The directory mapping information does not contain its own name though.

10. Where does the directory inode number gets mapped with the directory name? At the parent directory. As shown above, every directory maintains a table which contains the list of files and directories present under the directory. And hence in the parent directory of the current directory, the name of the current directory will be found just like how we can the name of the sub-directory of "xyz" from the above table. Hence, Unix will read the inode number of the parent directory from its directory , and go to the directory mapping of the mapping directory, and compares the inode numbers one by one. One of them will match, and it can get the name of its directory from there.

The mapping of the parent directory of "xyz" directory:

Inode 13 . 13 .. 610623 xyz 113 home 610791 - See at: http://www.theunixschool.com/2012/08/move-files-and-directories-inodes-part- 3.html#sthash.EusFp2kd.dpuf

Method one

Since a leading minus in filename introduces ambiguity in recognizing the filename, you just need to eliminate such ambiguity by prepending a pathname to the filename. For example, if you have a file named “-my.txt” in current directory, you can access it as follows.

$ 600 ./-my.txt $ ./-my.txt

Method two

The second method to deal with a leading hyphen character is to take advantage of a special argument “--” which is interpreted by getopt() as the “end of option.” standard command line utilities use getopt() to process command line arguments. When getopt() encounters “--”, it stops option-scanning process. Therefore, just insert “--” in front of the filename to it explicit that the hyphenated filename is not part of command line arguments.

$ chmod -- 600 -my.txt $ rm -- -my.txt

-k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1)

is one of the most commonly used editors in UNIX. When we open a file using vi, we do some settings depending on our needs say, to set line number, to set indentation, to set the tab space etc in the file. However, these settings or customizations last only till the file is open. Some of these settings any user would always like to retain on opening any file. These settings can be grouped in a file called .exrc file, and hence the settings become permanent.

Whenever a file is opened using vi, vi looks for the .exrc file in the home directory of the user. If present, it reads the file and applies the settings in the file being opened. And hence any customization we would like to have in the file should be put in this. In fact, all the commands which we execute in the escape mode or the last line execution mode can be put in the .exrc file.

Settings commonly maintained in the .exrc file are:

1. Set commands :- Set commands used in vi like set list, set number, etc. 2. Abbreviations:- Frequently used words can be abbreviated. For example,say, a user uses the word 'include' lot many times. The user can create an abbreviation 'inc' for the word include. 3. Mapping keys:- Some key combinations can be mapped or made into a hot-key combination. We saw in in one of our earlier articles, how to map function keys.

A typical $HOME/.exrc file will look like as shown below: - See more at: http://www.theunixschool.com/2010/06/what-is-exrc-file- for.html#sthash.i9qEkl0u.dpuf

et number

set autoindent

set nowrapscan

ab inc include

map Q :q!

1. The first three entries are part of the set commands, which includes setting line number, auto-indentation and search wrapping. With these settings, whenever a file is opened, automatically the line numbers will be set, and so is the indentation and search wrap.

2. The abbreviation 'ab inc include':- whenever you type 'inc' followed by a space in a file, it automatically gets converted to 'include'. This is a very helpful feature at times.

3. map Q :q! - This command maps the Q key with the quit operation. So, whenever the user wants to quit the file, instead of typing ':q!', the user can simply type 'Q' from the escape mode.

The .exrc file should be present in the home directory itself and there is no limitation on the number of customizations being done in it. - See more at: http://www.theunixschool.com/2010/06/what-is-exrc-file- for.html#sthash.i9qEkl0u.dpuf

Temporary Buffer

Deleted or copied text goes into a temporary unnamed buffer. The contents of the temporary buffer may be retrieved by using the p or P commands.

Lettered Buffers

There are 26 lettered buffers (a-z). Contents of a lettered buffer are saved until you copy or delete more characters into it, or until you quit your current vi session. eg. From Command Mode

"ayy Copy (yank) a line into buffer letter "a" "ap Put contents of lettered buffer a below the current line

When a process calls one of the exec functions that process is completely replaced by the new program. The new program stats execution from main function. The processed does not change across an exec because a new process is not created.But this function replaces the current process with new program from disk.

List the files in current directory sorted by size ? - ls -l | ^- | -nr find / -type f -atime -30 > December.files

Delete blank lines in a file ? - sample.txt | grep -v ‘^$’ > new_sample.txt