Centos File System Commands

From rbachwiki
Jump to navigation Jump to search

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 file1 file2
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

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

Hard Drive Management

fdisk /dev/sda

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