03 - Basic File Management Flashcards
Different types of file
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.
Regular files
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.
file and stat command
file return the specific type of data that the file contains, and stat simply states that it is a regular file
Directory files
Directories are logical containers that hold files and subdirectories.
letter d at the beginning of each line entry identifies the file as directory.
Character and device special device files
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.
Symbolic links
A symbolic link may be considered a shortcut to another file or directory.
Compression and archiving
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.
gzip and gunzip
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.
bzip2 and bunzip2
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.
tar
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
vim - modes of operation
vim has four modes of operation:
1. command mode
2. input mode
3. last line mode
4. visual mode (not discussed)
What is command mode in vim?
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.
What is input mode in vim?
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.
vim saving and quitting vim
: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
changing text in vim
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