Linux file types and file-related commands

Source: Internet
Author: User
Tags character classes exit in lowercase parent directory printable characters readable uppercase letter file permissions

File type

llYou can see the file details later:

-: General file (internal type is what, with file command)

d: Directory, catalog file

b: BLOBK device, block devices file, support random access in "block" units

    • Major number: The main device ID, which is used to indicate the device type, and then determine which driver to load

    • Minor number: The secondary device numbers that represent different devices in the same type.

such as 1,3 ,1,5

c: Character device, character equipment file, support for linear access in "character" units

l: Symbolic link, symbolic link file (soft connection file)

p: Pipe, Named pipes

s: Socket, Socket file

Path

Absolute Path : The path from which to start, starting with a slash / .

    • /etc/syscofnig/network-scrpits
      The absolute path is divided into two segments, dirname and basename:
1234 [Email protected] app]# Dirname/etc/sysconfig/network-scripts/ifcfg-ens192/etc/sysconfig/network-scripts[[email Protected] app]# basename/etc/sysconfig/network-scripts/ifcfg-ens192ifcfg-ens192

relative path : A path that does not start with a slash.

    • xxx/yyy: The yyy subdirectory of the XXX subdirectory under the current directory
    • .: Current directory
    • ..: Parent Directory
    • ~: User's home directory
    • -: Toggles the last entered directory, and uses it with the command

Tips: The - principle, the system remembers the previous working directory, stored in the environment variable OLDPWD , can be used to echo $OLDPWD see.

Common commands: 1. pwd: Show working Directory

Printing working directory

2. cd: Switch Directories

Change Directory

    • cd: Switch Home Directory

    • cd ~: Switch Home Directory

    • cd ~USERNAME: Switch to user username's home directory

    • cd -: Switch back and forth between the last directory and the current directory (PWD to OLDPWD)

    • cd ..: Switch to Parent directory

    • cd /path/to/directorySwitch to an absolute directory

    • cd path/to/directory: Switch to a relative directory

3. ls: List Content

List (use the/var directory for example)

    • -a, --all : List all files containing hidden files

    • -A, --almost-all : List . .. all files except the Frog

    • -F: The-f parameter adds a forward slash (/) after the directory name to make it easier for the user to distinguish them in the output. Similarly, it adds an asterisk to the executable file (such as the My_script file above) so that the user can find the files that are available to run on the system.

    • l, --long : Long format information, list the detailed properties of the file, the command can be abbreviated to ll ,alias ll=‘ls -l --color=auto‘

    • -h, --human-readable : Size is expressed in human-readable format

    • -d, --directory : View the directory itself rather than the internal file details

    • -r, --reverse : Reverse sort (descending)

    • -R, --recursive recursive display (basic without this, recursive display with tree command more intuitive)
    • -t: Sort by modified time
4. cat: Text file output to Terminal

concatenateStitching

You can separate cat:cat /etc/issue

Multiple files can be connected to displaycat /etc/issue /etc/redhat-release

123456789 [Email protected] app]# Cat/etc/issuecentos release 6.9 (Final) Kernel \ r on an \m [[email protected] app]# Cat/etc/issu E/etc/redhat-releasecentos Release 6.9 (final) Kernel \ R on the \m CentOS release 6.9 (final)

Parameters:

    • cat -s file: Delete extra blank lines (multiple lines of blank lines, delete to only one line left blank)
12345678910111213141516171819 $ cat Multi_blanks.txt Line 1 line2 line3 line4 $ cat-s multi_blanks.txt #压缩相邻的空白行 Line 1 line2 line3 line4
    • cat -T file: Displays tabs as^I

To look at the tabs in the script, we usually write scripts to avoid tabs and use multiple spaces. So this command is often used to query where tabs are used.

    • cat -n file: Show Line numbers
123456789 $ cat Lines.txt Line line line $ cat-n lines.txt 1 line 2 Line 3 line
5. tac: With catInstead, go from the back row to the front row output statView file status

You can see,, atime(Access) mtime(Modify) , and Ctime(Change) file permissions, file owners and owning groups, and file size-related content.

12345678910 [[email protected] ~]# stat/etc/issue File: '/etc/issue ' size:23 blocks:8 IO block:4096 Regular filedevice:802h/2050d inode:134320235 links:1access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root) Context:system_u:object_r:etc_t:s0Acces S:2017-05-22 09:07:34.832999490 +0800modify:2016-11-30 02:12:59.000000000 +0800change:2017-05-17 16:53:20.670996867 +0800 Birth:-

6. File wildcard character (globbing)
    • *: matches 0 or more characters

    • ?: Matches any single character

    • ~: Current User Home Directory

    • ~longdream: User Longdream's home directory

    • ~+: Current Working Directory

    • ~-: Previous working directory

    • [0-9]: Matches a range of numbers, matches a

    • [a-z]或[A-Z]: letter, Case insensitive

      Tips: Because case insensitive, [a-d] represents AaBbCcDd a match

    • [a-z0-9]: Letters or numbers

    • [wang]: Matches any one of the characters in the list, matches or OR or w a ng

    • [^wang]Matches characters other than all characters in the list except,,,, w a ng

Show hidden files only:

Pre-defined character classes:man 7 glob

[:digit:]: Any number, equivalent to 0-9

[:lower:]: Any lowercase letter

[:upper:]: Any uppercase letter

[:alpha:]: Any case letter

[:alnum:]: any number or letter

[:blank:]: Horizontal white space character

[:space:]: horizontal or vertical whitespace characters

[:punct:]: Punctuation

[:print:]: Printable characters

[:cntrl:]: Control (non-printing) characters

[:graph:]: Graphic characters

[:xdigit:]: Hexadecimal characters

Practice
Exercise 1: Show /var/log All files or directories in the directory that l begin with a lowercase letter ending with an arbitrary character in the middle.

Exercise 2: Display a /etc file or directory that starts with any digit and ends in a non-numeric directory.

Exercise 3: Display /etc a file or directory that starts with a non-letter, followed by a letter and any character of any length, in the directory.

Exercise 4: Copy /etc directories, all files or directories that start with M and end in a non-numeric directory /tmp/test .

Exercise 5: /usr/share/man under Copy directory, all files or directories that begin with man, followed by a number, to the /tmp/test/man directory.

Exercise 6: Copy all the files or directories in the/etc directory to the .conf end, beginning with M,n,r,p, to the/tmp/conf.d/directory.

Answer:

123456789101112131415 Ls-d/var/l? [[: Lower:]] ls-d/etc/[0-9]*[^0-9] ls-d/etc/[^a-z][a-z]* mkdir/tmp/testcp-r/etc/m*[^0-9]/tmp/test/mkdir/tmp/test/ Mancp-r/usr/share/man/man[0-9]/tmp/test/man mkdir/tmp/conf.dcp-r/etc/[mnrp]*.conf/tmp/conf.d/
7. Touch

touch [OPTION]... FILE...

-a: Change only Atime and CTime

-m: Change only Mtime and CTime

-t [[CC]YY]MMDDhhmm[.ss]: Specify timestamps for Atime and Mtime

-c: If the file does not exist, it is not created

9. CP

cp: Copy

cp /etc/fstab /app: Copy a single file to the directory below.
cp /etc/{fstab,issue} /app/dir/, cp /etc/fstab /etc/bashrc /app/dir/ : Copy multiple files to a directory.
cp /etc/fstab /app/1.txt: Copy and overwrite the destination file (not created).

Parameters

    • -F  , --force : Force overwrite of target file.
    • -R , -R , '--recursive : Recursive copy directory (-R  ==  -R ). cp-r/var/log/app/log
    • -D : Copies the symbolic link itself, not the file that he points to. cp-d/etc/system-release/app
    • -a , --archive : archive, equivalent to -dr
    • -P : Copies the original properties of the file, equivalent to the following --preserv=[mode,ownership,timestamp]
    • --preserv[=attr_list]
      • mode : Permissions
      • ownership: Genus Principal Group
      • timestamp : timestamp
      • links : Copy the linked source file
      • xattr
      • Context
      • all
    • -v --verbose : See more information

Practice:

1) define the alias command baketc , each day to the /etc/ directory under all

Files, back up to a /testdir separate subdirectory, and requires the subdirectory format to be backupYYYY-mm-dd visible in the backup process

2) Create a /testdir/rootdir directory and copy /root all the files to that directory, and ask to retain the original permissions

Answer:

1)

1 Alias baketc=cp-av/etc/*/testdir/' Date-u '

2)

1 cp-ap/root/*/testdir/rootdir

Tips: Tips for using wildcard characters to copy:
cp -a /etc/passwd{,.bak}
Equivalentcp -a /etc/passwd /etc/passwd.bak

Ten. MV

mv: Move

    • -fForce move or overwrite

Move the file (not in the same directory), rename the file (in the same directory)
Move a directory or rename a directory

Rename.

Rename suffix

rename [options] expression replacement file...

Options

1234567891011 EXAMPLES Given The files foo1, ..., foo9, Foo10, ..., foo278, the commands rename foo foo0 foo?  Rename foo foo0 foo?? Would turn them into foo001, ..., foo009, foo010, ..., foo278. and rename. htm. html *.htm would fix the extension of your HTML files.
1234 mkdir testcd Texttouch {1,2,3,4,5}.txtrename-v. txt. Txt.bak *.txt
rm

rm: Remove

deleting files
-i: Interactive, interactive
-rRecursive deletion
-fForce Delete

rm -rf /(5 is the root can be deleted, 6 and 7 are not deleted)

We can leave a habit:
Unused files, do not delete directly, you can use the mv move to a special directory (such as all moved to/TMP)

Tree

Parameters:

    • -d: Show Only Directories

    • -L level: Specify the number of levels to display

    • -P pattern: Displays only the path that is matched by the specified pattern

mkdir, RmDir

mkdir: Make Directory

    • -pRecursive creation
    • -v: verbose, creating directory details
    • -mMode, permissions

rmdir: Remove empty directory (only empty directories can be deleted)

Exercise 1: Create,, test1/x/y1 test1/x/y2 test1/x/y1/a ,test1/x/y1/b

Practice the 2:test2 directory created below,,, a_c a_d b_cb_d

Exercise 3: Create the following directory structure under the TEST3 directory:

1234567891011121314151617181920 test3├──bin├──etc│└──sysconfig│└──network-scripts├──sbin├──usr│├──bin│├──lib│├──lib64│├── loca L││├──bin││├──etc││├──lib││└──sbin│└──sbin└──var├──cache├── log └──run

Exercise 3: Create,,, /testdir/dir1/x /testdir/dir1/y, /testdir/dir1/x/a /testdir/dir1/x/b /testdir/dir1/y/a ,/testdir/dir1/y/b

Exercise 4: Create,, /testdir/dir2/x /testdir/dir2/y /testdir/dir2/x/a ,/testdir/dir2/x/b

Exercise 5: Create/,,,, testdir/dir3 /testdir/dir4 /testdir/dir5 /testdir/dir5/dir6/testdir/dir5/dir7

Answer:

123456789101112 MKDIR-PV Test1/x/{y1/{a,b},y2} mkdir-pv test2/{a,b}{c,d} MKDIR-PV test3/{bin,sbin,etc/ Sysconfig/network-scripts,usr/{bin,sbin,Local/{bin,sbin,etc,lib},lib,lib64},var/{cache,Log,run}} mkdir-pv/testdir/dir1/{x,y}/{a,b} mkdir-pv/testdir/dir2/{x/{a,b},y} mkdir-pv/testdir/dir{3,4,5/ dir{6,7}}
14. Extension

Ways to delete large files (files that are not released and deleted, Space is still occupied):

Experiment:
Under one session, dd if=/dev/zero of=bigfile bs=1M count=2048 vim bigfile do not exit in vim mode.

Open another session window, delete the file, the results found that the file is deleted, but the space is not released, because Vim occupies the file.

At this point we can use it > to fix:

12 > BIGFILE3RM-RF bigfile3

>and >> open a single chapter speaking, in the back of the IO and redirect section.

Linux file types and file-related commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.