03 - Basic File Management Flashcards

1
Q

Different types of file

A

RHEL support seven types of files:
1. regular
2. directory
3. block special device
4. character special device
5. symbolic link
6. named pipe
7. socket

The first two are the most common in Linux. The two types of device files are used by the operating system to communicate with peripheral devices. There are many instances of symbolic links as well. The last two types-named pipes and sockets-are used in inter-process communication.

Linux does not require an extension to a file to identify its type. It provides two elementary commands called file and stat, in addition to ls command, to ascertain the type of data that a file may contain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Regular files

A

Regular files may contain text or binary data. These files may be shell scripts or commands in the binary form. When you list a directory, all line entries for files in the output that being with the hypen (-) character represents regular file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

file and stat command

A

file return the specific type of data that the file contains, and stat simply states that it is a regular file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Directory files

A

Directories are logical containers that hold files and subdirectories.

letter d at the beginning of each line entry identifies the file as directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Character and device special device files

A

Each piece of hardware in the system has an associated file in the /dev directory that is used by the system to communicate with that device. This type of file is called a device file.

There are two types of device files: character (or raw) and block.

run file and stat command for devices and block files.

Every hardware device such as a disk, CD/DVD, printer, terminal has an associated device driver loaded in the kernel. The kernel communicates with hardware device through their respective device drivers. Each device driver is assigned a unique number called the major number, which the kernel uses to recognize its type.

There may be more than one instance of the same device type in the system. In that case, the same driver is used to control all those instances. For ex. SATA device driver controls all SATA hard disks and CD/DVD drivers. The kernel in this situation allots a minor number to each individual device within that device driver category to identify it as unique device.

In short, major number points to the device driver, and a minor number points to a unique device or partition that the device driver controls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Symbolic links

A

A symbolic link may be considered a shortcut to another file or directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Compression and archiving

A

RHEL offers a multitude of compression tools such as gzip(zunzip) and bzip2(bunzip2)

tar and star commands have the ability to preserver general file attributes such as ownership, owning group, and timestamp as well as extended attributes such as ACLs and SELinux concepts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

gzip and gunzip

A

gzip is used to create a compressed file of each of the specified files and it adds the .gz extension to each file for identification.

gunzip is used to unzip the file.

-r can be used to compress as entire directory tree
-l option to display compression information about a gzipped file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

bzip2 and bunzip2

A

The bzip/bunzip is another compression pair for zipping and unzipping files. It adds .bz2 extension.

bzip2 has better compression ration but it s slower.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

tar

A

The tar (tape archive) command is used to create, append, update, list, and extract files or an entire directory tree to and from a single file, which is called a tarball or tarfile.

-c = creates a tarball
-f = specifies a tarball name
-p = preserve file permissions. default for the root user. specify this option if you create an archive as a normal user
-r = appends files to the end of an extant uncompressed tarball
-t = lists contents of a tarball
-u = appends files to the end of an extant uncompressed tarball provided the specified files being added are newer
-v = verbose
-x = extracts or restores from a tarball

Ex: tar -cvf /tmp/home.tar /hometar

tar also supports options to directly compress the target file while being archived using gzip or bzip2 command.

-z = compresses a tarball with gzip
-j = compresses a tarball with bzip2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

vim - modes of operation

A

vim has four modes of operation:
1. command mode
2. input mode
3. last line mode
4. visual mode (not discussed)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is command mode in vim?

A

command mode: is the default mode. The vim editor places you into this mode when you start it. While in the command mode, you can carry out tasks such as copy, cut, paste, move, remove, replace, change, and search on text, in addition to performing navigational operations. This mode is also known as the escape mode because the Esc key is used to end the mode.

While in command mode, you may carry out advanced editing tasks on text by pressing the colon character (:), which places the cursor at the beginning of the last line of the screen, and hence it is referred to as the last line or extended mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is input mode in vim?

A

In input mode, anything that is typed on the keyboard is entered into the file as text. Command cannot run in this mode. The input mode is also called the edit mode or the insert mode. You need to press the Esc key to return to the command mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

vim saving and quitting vim

A

:w = writes changes into the file without quitting vim
:w file2 = write changes into a new file called file2 without quitting
:w! =write changes to the file even if the file owner does not have write permissions on the file
:wq = write changes to the file and quites vim
:wq! = write changes to the file and quits vim even if the file owner does not have write permission on the file
:q = quits vim if no modification were made
:q! = Quits vim if modification were made, but we don’t wish to save them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

changing text in vim

A

cl = changes the letter at the cursor location
cw = changes the word at the cursor location to the end of the word
cc = changes the entire line
C = change text at the cursor position to the end of the line
r = replaces the character at the cursor location with the character entered following this command
R = overwrites or replaces the text on the current line
J = joins the next line with the current line
xp = switches the position of the character at the cursor position with the character to the right of it
~ = changed the letter case at the cursor location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

navigating within vim

A
17
Q
A
18
Q

del

A
19
Q

deleting text

A
20
Q

undoing and repeating

A
21
Q

searching for text

A
22
Q

replacing text

A
23
Q

copying, moving, and pasting text

A
24
Q

creating files and directories

A

touch
-d and -t = sets date and time on the file
-a and -m = enables you to change only the access or the modification time on a file to the current system time
-r = sets the modification time on a file to that of a reference file’s

removing:
rm
-i = confirmation

The rm command can also be used to erase a file that has a wildcard character, such as an asterick (*) or a question mark (?), embedded in its name. These characters have special meaning to the shell, and filenames containing them must be prepended with the backslash character (). to instruct the shell to treat them as regular characters.

mkdir
-p = parent option to create nested directories
-v = verbose

rmdir
-i = confirmation
-r or -R = recursive will remove a directory and all of its contents
-v = verbose

25
Q

cat command

A

cat > catfile1

ctrl+d to save the file

Displays file content

26
Q

File linking - file attributes and inode

A

Each file within a file system has a multitude of attributes assigned to it at the time of its creation. These attributes are collectively referred to as the file’s metadata, and they change when the file is accessed or modified. A file’s metadata includes several pieces of information, such as the file type, size, permissions, owner’s name, owning group name, last access/modified times, link count, number of allocated blocks, and points to the data storage location. This metadata takes 128 bytes of space for each file. This tiny storage space is referred to as the file’s inode (index node)

An inode is assigned a unique numeric identifier that is used by the kernel for accessing, tracking, and managing the file. In order to access the inode and the data it points to, a filename is assigned to recognize it and access it. This mapping between an inode and a filename is referred to as a link. It is important to note that the inode does not store the filename in its metadata; the filename and corresponding inode number mapping is maintained in the directory’s metadata where the file resides.

27
Q

TAC

A

tac displays the contents of a text file in reverse.

28
Q

less and more

A

Both less and more are text filters that are used for viewing long text files one page at a time, starting at the beginning.

The less command is more capable than the more command. less does not need to read the entire file before it starts to display its contents, this making it faster.

The more command is limited to forward text searching only, whereas less is able to perform both forward and backward searches.

spacebar / f = scrolls forward one screen
enter = scrolls forward one line
b = scrolls backward one screen
d = scrolls forward half a screen
h = display help
q = quits and returns to the command prompt
/string = searches forward for a string
?string = searches backward for a string (less)
n = finds the next occurrence of a string
N = finds the previous occurrences of a string (less)

29
Q

head and tail

A

head displays the starting few lines of the specified text file. By default, it returns the first ten lines.

tail displays the ending few lines of the specified text file. By default, it returns the last ten lines.

tail helps watching a log file. The -f option enables this function.

30
Q

Counting words, lines, and characters in text files

A

wc
-l = print a count of lines
-w = print a count of words
-c = print a count of bytes
-m = print a count of characters

31
Q

copying files and directories

A

cp file1 file2

cp file1 dir1

-i = interactive, option to instruct cp to prompt for confirmation before overwriting

cp -r dir1 dir 2
-r = recursive, copies an entire directory tree to another location
-i = interactive, option to instruct cp to prompt for confirmation before overwriting

ls -l dir2 -R => view the entire dir2 hierarchy

-p = option that provide the ability to preserve the attributes (timestamp, permissions, ownership, etc.) of a file or directory being copied.

cp -p flie1/tmp

32
Q

Moving and renaming files and directors

A

mv command is used to move or rename files

-i = used for confirmation

33
Q

Ways to create file and directory links

A

There are two ways to create file and directory links, they are referred to as hard links and soft links.

Links are created between files or between directories, but not between files and directories.

34
Q

hard link

A

A hard link is a mapping between one or more filenames and an inode number, making all hard linked files indistinguishable from one another. This implies that all hard-linked files will have identical metadata. Changes to the file metadata and content can be made by accessing any of the filenames.

A hard link cannot cross a file system boundary, and it cannot be used to link directories because of the restrictions placed within linux designed to avoid potential issues with some commands.

ln file10 file20

35
Q

soft link

A

A soft link (aka symblic link or symlink) makes it possible to associate one file with another. The concept is analogous to that of a shortcut in MS windows where the actual file is resident somewhere in the directory structure, but there can be one or more shotcuts with different names pointing to it. With a soft link you can access the file directly via the actual filename as well as any of the shortcuts. Each soft link has a unique inode number that stores the pathname to the file it is linked with. The a symlink, the link count does not increase or decrease, rather each symlinked file receives a new inode number. The pathname can be absolute or relative depending on what was specified at the time of its creation. The size of the soft link is the number of characters in the pathname of the target.

A soft link can cross a file system boundary and it can be used to link directories, as it simply used the pathname of the destination object.

ln -s file10 file20

Soft link for creating the directories is same as for files.