Difference between revisions of "Centos File System Commands"

From rbachwiki
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 51: Line 51:


==File Commands==
==File Commands==
'''See what drives are available'''
ls -l /dev/sd*
  locate filename
  locate filename
The locate command looks in a premade database that contains a list of all the files on the system. This database is indexed much like a textbook for fast searching, yet can become outdated as files are added and removed from the system, which happens on a regular basis. As a result, the database used for the locate command (/var/lib/mlocate/mlocate.db) is updated each day automatically and can be updated manually by running the updatedb command at a command prompt. You can configure the directories that are searched by the updatedb command by editing the /etc/updatedb.conf file.
The locate command looks in a premade database that contains a list of all the files on the system. This database is indexed much like a textbook for fast searching, yet can become outdated as files are added and removed from the system, which happens on a regular basis. As a result, the database used for the locate command (/var/lib/mlocate/mlocate.db) is updated each day automatically and can be updated manually by running the updatedb command at a command prompt. You can configure the directories that are searched by the updatedb command by editing the /etc/updatedb.conf file.
Line 71: Line 74:
'''View devices that are currently used on the filesystem and their major numbers'''
'''View devices that are currently used on the filesystem and their major numbers'''
  cat /proc/devices
  cat /proc/devices
== Mounting Devices ==
== Mounting Devices ==
''' use the mkfs (make filesystem) command and specify the filesystem type using the –t switch and the device file representing the floppy disk device. To format the floppy disk inside the first floppy disk drive in the computer with the ext2 filesystem, place a floppy disk in the floppy disk drive and type the following command:
''' use the mkfs (make filesystem) command and specify the filesystem type using the –t switch and the device file representing the floppy disk device. To format the floppy disk inside the first floppy disk drive in the computer with the ext2 filesystem, place a floppy disk in the floppy disk drive and type the following command:
Line 89: Line 93:
''' To crate an ISO image'''
''' To crate an ISO image'''
  mkisofs -RJ -o newimage.iso /data
  mkisofs -RJ -o newimage.iso /data
==Hard Drive Management==
== Working with Hard drive partitioning==
  fdisk /dev/sda
'''Check to see what drives are available on the system'''
ls -l /dev/sd*
 
  fdisk /dev/sdc
Once you start the fdisk command, the fdisk program executes. <big>'''To see a menu of all available commands, type m'''</big>, then press<Enter>.
<br>
Print the partition table to the screen
p
To create a partition Type
n
a. Because this is a newly installed drive, you will be asked if you want to create a primary
partition or an extended partition. In this case, we want a primary partition, so at the prompt, type
p
Type 1 for the partition number
1
 
*. Accept the default starting position for the first sector by pressing the <Enter> key.
*. Make the partition 200MB in size by typing +200MB. Press <Enter> to create the partition.
*. Then, confirm that the partition was created by pressing p again to print the partition table.
Notice that a new partition is on the system.
p
*. To “write” the changes to the partition table, press w
w
ls -l /dev/sd*
=== Use mkfs to create a filesystem===
mkfs -t ext4 /dev/sdc1
'''Mount the newly created filesystem'''
mkdir /new # this creates folder to mount to
mount /dev/sdc1 /new
to verify that the partition is correctly mounted
ls -l /new
 
===Mount the file system when the server boots===
nano /etc/fstab
Enter the following data - hitting the tab key between entries
/dev/sdc1    /new    ext4    defaults    1    1
 
save file<br>
'''Note: Each line in the /etc/fstab file contains six fields of data.'''
#Field exampleMeaning
#/dev/sdc1 The partition to mount
#/new The mount point
#ext4 The filesystem type
#defaults The mount options
#1 Used by the dump command for backups
#1 Used by the fsck command for filesystem checks
It is important to test your /etc/fstab entry prior to rebooting the system. To do this, first unmount
the filesystem with the following command.
umount /new
 
then mount the filesystem
mount /new
 
To print a list of the partitions currently set on /dev/sda, you could press the p key at the fdisk prompt:
To print a list of the partitions currently set on /dev/sda, you could press the p key at the fdisk prompt:
  cfdisk command is similar to fdisk
  cfdisk command is similar to fdisk
Line 96: Line 152:
   parted /dev/sda
   parted /dev/sda
   print
   print
=== Display Filesystem Data ===
The df command will display filesystem information such as the size of the file system and how much space is being used
df /dirname
The du commadn will display how much space (combined size of all files) a specific directory structure contains
du /home/
==Repairing File Systems==
'''You need to unmount the filesystem before running the fsck'''
unmount /moundir
fsck -f /dev/sdc1
===Create a Swap File system===
# To create a swap filesystem, first create a regular partition using fdisk
fdisk /dev/sdc
p # to view current partitions
n # to create a new partition
p # to create a primary partition
2 # partition number
# accept default starting position
+128MB # make the partition 128 mb in size
w # save your changes
'''To convert this partition into a swap space'''
swapon /dev/sdc2
swapon -s # verify swap space
==Working with LVM==
==Working with LVM==
LVM's consist of  
LVM's consist of  
Line 147: Line 228:
Just as you can use the mke2fs command to make an ext2, ext3, or ext4 filesystem, you can use the e2fsck command to check an ext2, ext3, or ext4 filesystem. The e2fsck command accepts more options and can check a filesytem more thoroughly than fsck. For example, by using the –c option to the e2fsck command, you can check for bad blocks on the hard disk and add them to a bad block table on the filesystem so that they are not used in the future
Just as you can use the mke2fs command to make an ext2, ext3, or ext4 filesystem, you can use the e2fsck command to check an ext2, ext3, or ext4 filesystem. The e2fsck command accepts more options and can check a filesytem more thoroughly than fsck. For example, by using the –c option to the e2fsck command, you can check for bad blocks on the hard disk and add them to a bad block table on the filesystem so that they are not used in the future
  e2fsck -c /dev/vg00/data
  e2fsck -c /dev/vg00/data
=[[Centos OS]]=
[[Category:Bash]]

Latest revision as of 15:56, 28 August 2019

Displays type of file

file me.text
#output
ASCII text

Wildcard Metacharacters

* Matches 0 or more characters in a filename
? Matches 1 character in a filename
[aegh]  Matches 1 character in a filename—provided this character is either an a, e, g, or h
[a-e]  Matches 1 character in a filename—provided this character is either an a, b, c, d, or e
[!a-e] Matches 1 character in a filename—provided this character is NOT an a, b, c, d, or e

Viewing Text Files

cat -n file.txt
# will display the file with line numbers
tac file.text
#will display the file in reverse

The head command displays the first 10 lines (including blank lines) of a text file to the terminal screen but can also take a numeric option specifying a dif- ferent number of lines to display.

head -3

tail command can be used to display the end of text files. By default, the tail command displays the final 10 lines of a file, but it can also take a numeric option specifying the number of lines to dis- play on the terminal screen

tail -3

The more command gets its name from the pg command once used on UNIX systems. The pg command displayed a text file page by page on the terminal screen, starting at the beginning of the file; pressing the space bar or Enter key displays the next page, and so on.

more file.txt

the less command is named for doing more than the more command (remember that “less is more,” more or less). Like the more command, the less command can browse the contents of a text file page by page by pressing the spacebar and line by line by pressing the Enter key; however, you can also use the cursor keys on the keyboard to scroll up and down the contents of the file. Displaying the Contents of Binary Files

strings /bin/echo | more
# strings command piped to the more command

od command, which displays the contents of the file in octal format (numeric base 8 format).

od filename | head -5
# od command piped to head to display 5 lines

Grep command

Grep Command requires a minimum of two arguments, search are case sensitive

grep "find this" filename.txt
grep -i "find this" filename.txt 
# the -i option makes it case insensitive

To view lines that contain the word “toe” or “the” or “tie,” you can enter the following command:

grep "t.e" filename.txt

To view lines that start with the word “I,”

grep "^i" filename.text

Creating Hard and Soft Links

You can HARD LINK a file an unlimited number of times, but the files must reside on the same file system

ln file1 file2

Symbolic Link (Shortcut)

ln -s sourcefile1 destinationfile2
ln -s ../room/closet-a sybolinkname # this links to a folder closet-a in folder room
ls -l # view the symbolically linked filename after creation

Get information on a file

stat filename

List permission for a directory instead of listing the content of the dir

ls -ld

File Commands

See what drives are available

ls -l /dev/sd*
locate filename

The locate command looks in a premade database that contains a list of all the files on the system. This database is indexed much like a textbook for fast searching, yet can become outdated as files are added and removed from the system, which happens on a regular basis. As a result, the database used for the locate command (/var/lib/mlocate/mlocate.db) is updated each day automatically and can be updated manually by running the updatedb command at a command prompt. You can configure the directories that are searched by the updatedb command by editing the /etc/updatedb.conf file.

 find /etc -name inittab
 updatedb # will update the database

A slower yet more versatile method for locating files on the filesystem is to use the find command. The find command does not use a premade index of files; instead, it searches the directory tree recursively, starting from a certain directory for files that meet a certain criterion.
To use wild card characters with the find command, enclose with "host*"

find /etc -name "host*"

To find all files starting from the /var directory that have a size greater than 4096K (kilobytes), you can use the following command:

find /var -size +4096k
Find all the directories only underneath the /boot directory, you can type the following command:
find /boot -type d

Which Command searches directories that are listed in a special variable called the PATH variable in the BASH shell

which ls
/bin/ls
which grep
/bin/grep

View devices that are currently used on the filesystem and their major numbers

cat /proc/devices

Mounting Devices

use the mkfs (make filesystem) command and specify the filesystem type using the –t switch and the device file representing the floppy disk device. To format the floppy disk inside the first floppy disk drive in the computer with the ext2 filesystem, place a floppy disk in the floppy disk drive and type the following command:

mkfs -t ext2 /dev/fd0

A list of currently mounted filesystems can be obtained by using the mount command with no options or arguments

mount

To check whether the /media/floppy directory is being used by any users, you can use the fuser command with the –u option,

fuser -u /media/floppy

To mount the first floppy device formatted with the ext2 filesystem to the /media/ floppy directory, simply type the following command:

mount -t ext2 /dev/fd0 /media/floppy
# or
mount /dev/fd0

Unmount floppy

umount /media/floppy

Mount ISO images

mount -o loop -r -t iso9660 lala.iso /mnt

To crate an ISO image

mkisofs -RJ -o newimage.iso /data

Working with Hard drive partitioning

Check to see what drives are available on the system

ls -l /dev/sd* 
fdisk /dev/sdc

Once you start the fdisk command, the fdisk program executes. To see a menu of all available commands, type m, then press<Enter>.
Print the partition table to the screen

p

To create a partition Type

n

a. Because this is a newly installed drive, you will be asked if you want to create a primary partition or an extended partition. In this case, we want a primary partition, so at the prompt, type

p

Type 1 for the partition number

1
  • . Accept the default starting position for the first sector by pressing the <Enter> key.
  • . Make the partition 200MB in size by typing +200MB. Press <Enter> to create the partition.
  • . Then, confirm that the partition was created by pressing p again to print the partition table.

Notice that a new partition is on the system.

p
  • . To “write” the changes to the partition table, press w
w
ls -l /dev/sd* 

Use mkfs to create a filesystem

mkfs -t ext4 /dev/sdc1

Mount the newly created filesystem

mkdir /new # this creates folder to mount to
mount /dev/sdc1 /new

to verify that the partition is correctly mounted

ls -l /new

Mount the file system when the server boots

nano /etc/fstab

Enter the following data - hitting the tab key between entries

/dev/sdc1     /new    ext4    defaults    1    1

save file
Note: Each line in the /etc/fstab file contains six fields of data.

  1. Field exampleMeaning
  2. /dev/sdc1 The partition to mount
  3. /new The mount point
  4. ext4 The filesystem type
  5. defaults The mount options
  6. 1 Used by the dump command for backups
  7. 1 Used by the fsck command for filesystem checks

It is important to test your /etc/fstab entry prior to rebooting the system. To do this, first unmount the filesystem with the following command.

umount /new

then mount the filesystem

mount /new

To print a list of the partitions currently set on /dev/sda, you could press the p key at the fdisk prompt:

cfdisk command is similar to fdisk

To print a list of partitions using parted

 parted /dev/sda
 print

Display Filesystem Data

The df command will display filesystem information such as the size of the file system and how much space is being used

df /dirname

The du commadn will display how much space (combined size of all files) a specific directory structure contains

du /home/

Repairing File Systems

You need to unmount the filesystem before running the fsck

unmount /moundir
fsck -f /dev/sdc1

Create a Swap File system

  1. To create a swap filesystem, first create a regular partition using fdisk
fdisk /dev/sdc
p # to view current partitions
n # to create a new partition
p # to create a primary partition
2 # partition number
# accept default starting position
+128MB # make the partition 128 mb in size
w # save your changes

To convert this partition into a swap space

swapon /dev/sdc2
swapon -s # verify swap space

Working with LVM

LVM's consist of

  • PVs (physical volumes) - are unused partitions on hard disks that the LVM can use to store information.
  • Volume Groups (VG's) contain one or more PVs. They represent the pools of hard disk storage space that are available to the LVM for creating logical volumes. Additional PVs can easily be added to a VG after creation
  • Logical Volumes (LV's) ) are the usable volumes that are created by the LVM from the available storage space within a VG. LVs contain a filesystem and are mounted to a directory in the Linux filesystem hierarchy. In addition, LVs can be resized easily by the LVM to use more or less storage space.

To configure the LVM, you must first create one or more PVs that reference an unused parti- tion on a hard disk in your computer. Say, for example, that you recently created a new par- tition called /dev/sda4. Rather than placing a filesystem on /dev/sda4, you could instead allow the LVM to use the /dev/sda4 partition using the pvcreate command,

pvcreate /dev/sda4
pvdisplay # display detailed information obout each pv
lvm

Once you have created PVs, you can create a VG that uses the space in the PVs using the vgcreate command. For example, to create a VG called vg00 that uses the /dev/sda4 PV, you could use the following vgcreate command:

vgcreate vg00 /dev/sda4

To create a VG that uses multiple PVs, simply add multiple device arguments to the vgcreate command. For example, the vgcreate vg00 /dev/sda5 /dev/sdb1 /dev/ sdc3 command would create a VG called vg00 that uses three PVs (/dev/sda5, /dev/sdb1, and /dev/sdc3). When creating a VG, it is important to choose the block size for saving data since it cannot be safely changed later. This is called the physical extent (PE) size of the VG. A large PE size results in larger write operations and a larger maximum filesystem size for LVs. For example, a PE size of 32MB will allow for a maximum LV size of 2TB.
Next, you can create LVs from the available space in your VG using the lvcreate command and view your results using the lvdisplay command. The following commands
create an LV called “data” that uses 15GB of space from vg00 as well as an LV called “extras” that uses 16GB of space from vg00, and it displays the result

lvcreate -L 15GB -n data vg00
lvcreate -L 16GB -n extras vg00
lvdisplay

Notice from the preceding output that the new VGs can be accessed using the device files /dev/vg00/data and /dev/vg00/extras. You can also refer to your new VGs using the device files /dev/mapper/vg00-data and /dev/mapper/vg00-extras, which are typically used by the system when accessing the filesystems on your VGs.
You can work with these files as you would normally work with any other hard disk parti- tion device file. For example, to create an ext4 filesystem on these devices and mount them to the appropriate directories on the filesystem, you could use the following commands:

mke2fs -t ext4 /dev/vg00/data
mkdir /data
mkdir /extras
mount /dev/vg00/data /data
mount /dev/vg00/extras /extras

Next, you can edit the /etc/fstab file to ensure that your new logical volumes are automati- cally mounted at system startup,

cat /etc/fstab
/dev/sda1         /         ext4 .   defaults .   11
/dev/sda2         swap .    swap .   defaults .   00
/dev/vg00/data    /data .   ext4 .   defaults .   00
/dev/vg00/extras ./extras . ext4 .   defaults     00

Three other useful commands that can display information about the PVs, VGs, and LVs that are configured on your system are the pvscan command, the vgscan command, and lvscan command, respectively. Also, you can add additional storage space to your LVs after configuration. More specifically, you can add another PV using the pvcreate com- mand, add this PV to your existing VG using the vgextend command, and then increase the size of your LVs to use the additional space using the lvextend command. Before using these commands, always consult the manual pages to ensure that you are using the command options and arguments that will perform the desired actions.

Monitoring Filesystems

disk free space command

df

To view the size of a directory and its contents in kilobytes,

du -hs /usr |more

To view a summary

du -s /usr

To view the total number of inodes and free inodes for an ext2, ext3, or ext4 filesystem, you can use the dumpe2fs command with the –h switch, as shown in the following output:

dumpe2fs -h /dev/sda1

Checking Filesystems for Errors

To check a filesystem for errors, you can use the fsck (filesystem check) command, which can check filesystems of many different types. The fsck command takes an option specifying the filesystem type and an argument specifying the device to check; if the filesystem type is not specified, the filesystem is automatically detected. It is also important to note that the file- system being checked must be unmounted beforehand for the fsck command to work properly.
unmount /dev/vg00/data
fsck /dev/vg00/data
-f option fsck -f performs a full check
Dfoptions.png

Just as you can use the mke2fs command to make an ext2, ext3, or ext4 filesystem, you can use the e2fsck command to check an ext2, ext3, or ext4 filesystem. The e2fsck command accepts more options and can check a filesytem more thoroughly than fsck. For example, by using the –c option to the e2fsck command, you can check for bad blocks on the hard disk and add them to a bad block table on the filesystem so that they are not used in the future

e2fsck -c /dev/vg00/data

Centos OS