Linux File System
Q. What command is used to list the contents of directory?
Ans:
ls
ls -l
Q.How to list a directory containing millions of
files?
Ans:When you list all files using "ls ,find os.listdir
" all command hung since the folder contains too many files.
The “ls” command, by default, will sort its output.
To do that, it must first slurp the name of every file into memory. Confronted
with a very large directory, it will sit there, reading in file names, and
taking up more and more memory until eventually listing the files all at once,
in alphanumerical order.
ls and practically every other method of listing a directory (including python os.listdir, find .) rely on libc readdir(). However readdir() only reads 32K of directory entries at a time, which means that if you have a lot of files in the same directory .it is going to take an insanely long time to read all the directory entries, especially on a slow disk.
The reason it was taking forever to list the directory was because ls was reading the directory entries file 32K at a time, and the file was 513M.
So it would take around 16416 system calls of getdents() to list the directory. That is a lot of calls, especially on a slow virtualized disk. (see for more info http://www.olark.com/spw/2011/08/you-can-list-a-directory-with-8-million-files-but-not-with-ls/ )
ls and practically every other method of listing a directory (including python os.listdir, find .) rely on libc readdir(). However readdir() only reads 32K of directory entries at a time, which means that if you have a lot of files in the same directory .it is going to take an insanely long time to read all the directory entries, especially on a slow disk.
The reason it was taking forever to list the directory was because ls was reading the directory entries file 32K at a time, and the file was 513M.
So it would take around 16416 system calls of getdents() to list the directory. That is a lot of calls, especially on a slow virtualized disk. (see for more info http://www.olark.com/spw/2011/08/you-can-list-a-directory-with-8-million-files-but-not-with-ls/ )
On the other hand, ls -1 -f does not perform any
sorting. It just reads the directory and displays files
immediately.
Below table list contains the File system and its
capacity.
FAT32:
FAT32:
Maximum number of files: 268,435,437
Maximum file size: 4GB
maximum number of files per directory: up to 65535, or less depending on file names
Maximum file size: 4GB
maximum number of files per directory: up to 65535, or less depending on file names
NTFS:
Maximum number of files: 4,294,967,295
Maximum file size: 16TB currently (16EB theoretically)
Maximum file size: 16TB currently (16EB theoretically)
Ext2:
Maximum number of files: 10¹⁸
Maximum file size: 2TB
theoretical file per directory limit: 1.3 × 10²⁰ files
Maximum file size: 2TB
theoretical file per directory limit: 1.3 × 10²⁰ files
Ext3:
Maximum number of files: number of bytes in
volume/2¹³.
Maximum file size: 16GB (1KB block) to 2TB (4KB block)
Maximum file size: 16GB (1KB block) to 2TB (4KB block)
Q.Will Hard link share same Inode.?What will happen
if i delete the hard link ?
Ans:Yes.Hard Link shares same inode. If you delete the hard link other copy of file will be exist so that you can retrieve your data.
Ans:Yes.Hard Link shares same inode. If you delete the hard link other copy of file will be exist so that you can retrieve your data.
Q.What command is used to list the top 10 files / directories size wise?
Ans: for X in $(du -s * | sort -nr | cut -f 2); do du -hs $X ; done
Q.What command is used to display a list of currently running processes?
Ans:
ps
top
pstree
pgrep
/proc file system
Q.What command is used to check a file system for errors?
Ans:
fsck
fsck.ext3
fsck.nfs
fsck.ext2
fsck.vfat
fsck.reiserfs
fsck.msdos
Q. Explain UNIX file types ?
Ans:
Directory
Pipes
Fifo
Symbolic link
Named pipe
Socket
Device file
Door
Regular file
Q.What is a login shell?
Ans: A program gets executed when a user logs into UNIX box. E.g. bash, sh, ksh, csh
Q.What is UID?
Ans: User identification number which is assigned to each UNIX / Linux user; it may or may not be unique (unique number is recommended to avoid security related issues). UID and user relationship defined in /etc/passswd file.
Q.What GID? What SUID?
Ans: Definition: GID: Group identification number for the process. Valid group numbers are given in /etc/group, and in the GID field of /etc/passwd file. When a process is started, its GID is set to the GID of its parent process.
Q: What is EUID?
Ans: Definition: EUID: Expands to the effective user ID of the current user or process, initialized at shell startup.
Q.Explain Linux files permissions?
Ans: Every file on your Linux system, including directories, is owned by a specific user and group. Therefore, file permissions are defined separately for users, groups, and others.
User: The username of the person who owns the file. By default, the user who creates the file will become its owner.
Group: The usergroup that owns the file. All users who belong into the group that owns the file will have the same access permissions to the file. This is useful if, for example, you have a project that requires a bunch of different users to be able to access certain files, while others can't. In that case, you'll add all the users into the same group, make sure the required files are owned by that group, and set the file's group permissions accordingly.
Other: A user who isn't the owner of the file and doesn't belong in the same group the file does. In other words, if you set a permission for the "other" category, it will affect everyone else by default. For this reason, people often talk about setting the "world" permission bit when they mean setting the permissions for "other."
There are three types of access permissions on Linux: read, write, and execute. These permissions are defined separately for the file's owner, group and all other users.
Read permission. On a regular file, the read permission bit means the file can be opened and read. On a directory, the read permission means you can list the contents of the directory.
Write permission. On a regular file, this means you can modify the file, aka write new data to the file. In the case of a directory, the write permission means you can add, remove, and rename files in the directory. This means that if a file has the write permission bit, you are allowed to modify the file's contents, but you're allowed to rename or delete the file only if the permissions of the file's directory allow you to do so.
Execute permission. In the case of a regular file, this means you can execute the file as a program or a shell script. On a directory, the execute permission (also called the "search bit") allows you to access files in the directory and enter it, with the cd command, for example. However, note that although the execute bit lets you enter the directory, you're not allowed to list its contents, unless you also have the read permissions to that directory.
What does the output of ls -l mean? The very first column, the one that looks like a bunch of mumbo jumbo, shows the file type and permissions. The second column shows the number of links (directory entries that refer to the file), the third one shows the owner of the file, and the fourth one shows the group the file belongs to. The other columns show the file's size in bytes, date and time of last modification, and the filename.
The first column, the one that shows the file's permissions and looks like mumbo jumbo, is organized into four separate groups, although it certainly doesn't look very organized.
The first group consists of only one character, and it shows the file's type. For example, d means a directory and - means a normal file, so if you take a look at our example output, you'll notice dir is a directory, while file and other file are regular files.
The first character can be any of these:
d = directory
- = regular file
l = symbolic link
s = Unix domain socket
p = named pipe
c = character device file
b = block device file
The next nine characters show the file's permissions, divided into three groups, each consisting of three characters. The first group of three characters shows the read, write, and execute permissions for user, the owner of the file. The next group shows the read, write, and execute permissions for the group of the file. Similarly, the last group of three characters shows the permissions for other, everyone else. In each group, the first character means the read permission, the second one write permission, and the third one execute permission.
The characters are pretty easy to remember.
r = read permission
w = write permission
x = execute permission
- = no permission
You can set file permissions with the chmod command. Both the root user and the file's owner can set file permissions. chmod has two modes, symbolic and numeric.
Wipe out all the permissions but add read permission for everybody:
$ chmod a=r testfile
After the command, the file's permissions would be -r--r--r--
Which user?
u user/owner
g group
o other
a all
What to do?
+ add this permission
- remove this permission
= set exactly this permission
Which permissions?
r read
w write
x execute
The other mode in which chmod can be used is the numeric mode. In the numeric mode, the file permissions aren't represented by characters. Instead, they are represented by a three-digit octal number.
4 = read (r)
2 = write (w)
1 = execute (x)
0 = no permission (-)
To get the permission bits you want, you add up the numbers accordingly. For example, the rwx permissions would be 4+2+1=7, rx would be 4+1=5, and rw would be 4+2=6. Because you set separate permissions for the owner, group, and others, you'll need a three-digit number representing the permissions of all these groups.
Let's have an example.
$ chmod 755 testfile
The numeric mode may not be as straightforward as the symbolic mode, but with the numeric mode, you can more quickly and efficiently set the file permissions. This quick reference for setting file permissions in numeric mode might help:
Which number?
0 ---
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx
Q. What is SSH?
Ans: Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices.RFC 4252 The two major versions of the protocol are referred to as SSH1 or SSH-1 and SSH2 or SSH-
Q.Explain /etc/passwd file format ?
Ans: Passwd is a text file that contains a list of the system’s accounts giving for each account some useful information like user ID, group ID, home directory, shell, etc. Often, it also contains the encrypted passwords for each account.
Q.Explain /etc/ shadow file
Ans: pwconv command is used for giving shadow passwords. Shadow passwords are given for better system security. /etc/shadow file stores actual password in encrypted format for user’s account with additional properties related to user password i.e. it stores secure user account information. All fields are separated by a colon (J symbol. It contains one entry per line for each user listed in /etc/passwd file Generally, shadow file entry looks as follows (click to enlarge image):
Hi Prashanth,
ReplyDeleteYour writing shines! There is no room for gibberish here clearly you have explained about Linux File System. Keep writing!
Unix identifies each process with a unique integer called ProcessID. The process that executes the request for creation of a process is called the ‘parent process’ whose PID is ‘Parent Process ID’. Every process is associated with a particular user called the ‘owner’ who has privileges over the process.
I read V7 Unix introduced the first version of the modern Standard I/O library studio as part of the system library. The next implementations increased the number of libraries significantly. Doesn't it add overhead of loading unused libraries?
I am so grateful for your blog. Really looking forward to read more.
Best Regards,
Kevin
Hi There,
ReplyDeleteYour writing shines! There is no room for gibberish here clearly you have explained about Linux File System. Keep writing!
I have recently completed some web applications that a client wants to sell from their website. They have a Linux server running CentOS 6 in which I want to deploy the web apps to (5 phone apps and 1 iPad app) the server from which the customer can perform an Over The Air install to their device after payment. The way I would like to carry out the procedure is something like this:
Very useful post !everyone should learn and use it during their learning path.
Obrigado,
Preethi.